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