| Conditions | 18 |
| Total Lines | 106 |
| 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.transform() 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 | # |
||
| 81 | def transform(self): |
||
| 82 | """ |
||
| 83 | Main generator work loop. |
||
| 84 | """ |
||
| 85 | algo = self.algo |
||
| 86 | algo.data_portal = self.data_portal |
||
| 87 | handle_data = algo.event_manager.handle_data |
||
| 88 | current_data = self.current_data |
||
| 89 | |||
| 90 | data_portal = self.data_portal |
||
| 91 | |||
| 92 | # can't cache a pointer to algo.perf_tracker because we're not |
||
| 93 | # guaranteed that the algo doesn't swap out perf trackers during |
||
| 94 | # its lifetime. |
||
| 95 | # likewise, we can't cache a pointer to the blotter. |
||
| 96 | |||
| 97 | algo.perf_tracker.position_tracker.data_portal = data_portal |
||
| 98 | |||
| 99 | def every_bar(dt_to_use): |
||
| 100 | # called every tick (minute or day). |
||
| 101 | |||
| 102 | data_portal.current_dt = dt_to_use |
||
| 103 | self.simulation_dt = dt_to_use |
||
| 104 | algo.on_dt_changed(dt_to_use) |
||
| 105 | |||
| 106 | blotter = algo.blotter |
||
| 107 | perf_tracker = algo.perf_tracker |
||
| 108 | |||
| 109 | # handle any transactions and commissions coming out new orders |
||
| 110 | # placed in the last bar |
||
| 111 | new_transactions, new_commissions = \ |
||
| 112 | blotter.get_transactions(data_portal) |
||
| 113 | |||
| 114 | for transaction in new_transactions: |
||
| 115 | perf_tracker.process_transaction(transaction) |
||
| 116 | |||
| 117 | # since this order was modified, record it |
||
| 118 | order = blotter.orders[transaction.order_id] |
||
| 119 | perf_tracker.process_order(order) |
||
| 120 | |||
| 121 | if new_commissions: |
||
| 122 | for commission in new_commissions: |
||
| 123 | perf_tracker.process_commission(commission) |
||
| 124 | |||
| 125 | handle_data(algo, current_data, dt_to_use) |
||
| 126 | |||
| 127 | # grab any new orders from the blotter, then clear the list. |
||
| 128 | # this includes cancelled orders. |
||
| 129 | new_orders = blotter.new_orders |
||
| 130 | blotter.new_orders = [] |
||
| 131 | |||
| 132 | # if we have any new orders, record them so that we know |
||
| 133 | # in what perf period they were placed. |
||
| 134 | if new_orders: |
||
| 135 | for new_order in new_orders: |
||
| 136 | perf_tracker.process_order(new_order) |
||
| 137 | |||
| 138 | def once_a_day(midnight_dt): |
||
| 139 | # set all the timestamps |
||
| 140 | self.simulation_dt = midnight_dt |
||
| 141 | algo.on_dt_changed(midnight_dt) |
||
| 142 | data_portal.current_day = midnight_dt |
||
| 143 | |||
| 144 | # call before trading start |
||
| 145 | algo.before_trading_start(current_data) |
||
| 146 | |||
| 147 | perf_tracker = algo.perf_tracker |
||
| 148 | |||
| 149 | # handle any splits that impact any positions or any open orders. |
||
| 150 | sids_we_care_about = \ |
||
| 151 | list(set(list(perf_tracker.position_tracker.positions.keys()) + |
||
| 152 | list(algo.blotter.open_orders.keys()))) |
||
| 153 | |||
| 154 | if len(sids_we_care_about) > 0: |
||
| 155 | splits = data_portal.get_splits(sids_we_care_about, |
||
| 156 | midnight_dt) |
||
| 157 | if len(splits) > 0: |
||
| 158 | algo.blotter.process_splits(splits) |
||
| 159 | perf_tracker.position_tracker.handle_splits(splits) |
||
| 160 | |||
| 161 | def handle_benchmark(date): |
||
| 162 | algo.perf_tracker.all_benchmark_returns[date] = \ |
||
| 163 | self.benchmark_source.get_value(date) |
||
| 164 | |||
| 165 | with self.processor, ZiplineAPI(self.algo): |
||
| 166 | for dt, action in self.clock: |
||
| 167 | if action == BAR: |
||
| 168 | every_bar(dt) |
||
| 169 | elif action == DAY_START: |
||
| 170 | once_a_day(dt) |
||
| 171 | elif action == DAY_END: |
||
| 172 | # End of the day. |
||
| 173 | handle_benchmark(dt) |
||
| 174 | yield self._get_daily_message(dt, algo, algo.perf_tracker) |
||
| 175 | elif action == MINUTE_END: |
||
| 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 | |||
| 211 |