| Conditions | 34 |
| Total Lines | 158 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like zipline.gens.AlgorithmSimulator._process_snapshot() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # |
||
| 160 | perf_tracker.position_tracker.handle_splits(splits) |
||
| 161 | |||
| 162 | def handle_benchmark(date): |
||
| 163 | algo.perf_tracker.all_benchmark_returns[date] = \ |
||
| 164 | self.benchmark_source.get_value(date) |
||
| 165 | |||
| 166 | with self.processor, ZiplineAPI(self.algo): |
||
| 167 | for dt, action in self.clock: |
||
| 168 | if action == DATA_AVAILABLE: |
||
| 169 | inner_loop(dt) |
||
| 170 | elif action == ONCE_A_DAY: |
||
| 171 | once_a_day(dt) |
||
| 172 | elif action == CALC_DAILY_PERFORMANCE: |
||
| 173 | handle_benchmark(dt) |
||
| 174 | yield self._get_daily_message(dt, algo, algo.perf_tracker) |
||
| 175 | elif action == CALC_MINUTE_PERFORMANCE: |
||
| 176 | handle_benchmark(dt) |
||
| 177 | minute_msg, daily_msg = \ |
||
| 178 | self._get_minute_message(dt, algo, algo.perf_tracker) |
||
| 179 | |||
| 180 | yield minute_msg |
||
| 181 | |||
| 182 | if daily_msg: |
||
| 183 | yield daily_msg |
||
| 184 | |||
| 185 | risk_message = algo.perf_tracker.handle_simulation_end() |
||
| 186 | yield risk_message |
||
| 187 | |||
| 188 | @staticmethod |
||
| 189 | def _get_daily_message(dt, algo, perf_tracker): |
||
| 190 | """ |
||
| 191 | Get a perf message for the given datetime. |
||
| 192 | """ |
||
| 193 | perf_message = perf_tracker.handle_market_close_daily(dt) |
||
| 194 | perf_message['daily_perf']['recorded_vars'] = algo.recorded_vars |
||
| 195 | return perf_message |
||
| 196 | |||
| 197 | @staticmethod |
||
| 198 | def _get_minute_message(dt, algo, perf_tracker): |
||
| 199 | """ |
||
| 200 | Get a perf message for the given datetime. |
||
| 201 | """ |
||
| 202 | rvars = algo.recorded_vars |
||
| 203 | |||
| 204 | minute_message, daily_message = perf_tracker.handle_minute_close(dt) |
||
| 205 | minute_message['minute_perf']['recorded_vars'] = rvars |
||
| 206 | |||
| 207 | if daily_message: |
||
| 208 | daily_message["daily_perf"]["recorded_vars"] = rvars |
||
| 209 | |||
| 210 | return minute_message, daily_message |
||
| 211 |