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