| 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 | |||
| 161 | # handle any splits that impact any positions or any open orders. |
||
| 162 | sids_we_care_about = \ |
||
| 163 | list(set(list(perf_tracker.position_tracker.positions.keys()) + |
||
| 164 | list(algo.blotter.open_orders.keys()))) |
||
| 165 | |||
| 166 | if len(sids_we_care_about) > 0: |
||
| 167 | splits = data_portal.get_splits(sids_we_care_about, |
||
| 168 | midnight_dt) |
||
| 169 | if len(splits) > 0: |
||
| 170 | algo.blotter.process_splits(splits) |
||
| 171 | perf_tracker.position_tracker.handle_splits(splits) |
||
| 172 | |||
| 173 | def handle_benchmark(date): |
||
| 174 | algo.perf_tracker.all_benchmark_returns[date] = \ |
||
| 175 | self.benchmark_source.get_value(date) |
||
| 176 | |||
| 177 | with self.processor, ZiplineAPI(self.algo): |
||
| 178 | for dt, action in self.clock: |
||
| 179 | if action == BAR: |
||
| 180 | every_bar(dt) |
||
| 181 | elif action == DAY_START: |
||
| 182 | once_a_day(dt) |
||
| 183 | elif action == DAY_END: |
||
| 184 | # End of the day. |
||
| 185 | handle_benchmark(normalize_date(dt)) |
||
| 186 | yield self._get_daily_message(dt, algo, algo.perf_tracker) |
||
| 187 | elif action == MINUTE_END: |
||
| 188 | handle_benchmark(dt) |
||
| 189 | minute_msg, daily_msg = \ |
||
| 190 | self._get_minute_message(dt, algo, algo.perf_tracker) |
||
| 191 | |||
| 192 | yield minute_msg |
||
| 193 | |||
| 194 | if daily_msg: |
||
| 195 | yield daily_msg |
||
| 196 | |||
| 197 | risk_message = algo.perf_tracker.handle_simulation_end() |
||
| 198 | yield risk_message |
||
| 199 | |||
| 200 | @staticmethod |
||
| 201 | def _get_daily_message(dt, algo, perf_tracker): |
||
| 202 | """ |
||
| 203 | Get a perf message for the given datetime. |
||
| 204 | """ |
||
| 205 | perf_message = perf_tracker.handle_market_close_daily(dt) |
||
| 206 | perf_message['daily_perf']['recorded_vars'] = algo.recorded_vars |
||
| 207 | return perf_message |
||
| 208 | |||
| 209 | @staticmethod |
||
| 210 | def _get_minute_message(dt, algo, perf_tracker): |
||
| 211 | """ |
||
| 212 | Get a perf message for the given datetime. |
||
| 213 | """ |
||
| 214 | rvars = algo.recorded_vars |
||
| 215 | |||
| 216 | minute_message, daily_message = perf_tracker.handle_minute_close(dt) |
||
| 217 | minute_message['minute_perf']['recorded_vars'] = rvars |
||
| 218 | |||
| 219 | if daily_message: |
||
| 220 | daily_message["daily_perf"]["recorded_vars"] = rvars |
||
| 221 | |||
| 222 | return minute_message, daily_message |
||
| 223 |