| 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 | def _process_snapshot(self, dt, snapshot, instant_fill): |
||
| 161 | """ |
||
| 162 | Process a stream of events corresponding to a single datetime, possibly |
||
| 163 | returning a perf message to be yielded. |
||
| 164 | |||
| 165 | If @instant_fill = True, we delay processing of events until after the |
||
| 166 | user's call to handle_data, and we process the user's placed orders |
||
| 167 | before the snapshot's events. Note that this introduces a lookahead |
||
| 168 | bias, since the user effectively is effectively placing orders that are |
||
| 169 | filled based on trades that happened prior to the call the handle_data. |
||
| 170 | |||
| 171 | If @instant_fill = False, we process Trade events before calling |
||
| 172 | handle_data. This means that orders are filled based on trades |
||
| 173 | occurring in the next snapshot. This is the more conservative model, |
||
| 174 | and as such it is the default behavior in TradingAlgorithm. |
||
| 175 | """ |
||
| 176 | |||
| 177 | # Flags indicating whether we saw any events of type TRADE and type |
||
| 178 | # BENCHMARK. Respectively, these control whether or not handle_data is |
||
| 179 | # called for this snapshot and whether we emit a perf message for this |
||
| 180 | # snapshot. |
||
| 181 | any_trade_occurred = False |
||
| 182 | benchmark_event_occurred = False |
||
| 183 | |||
| 184 | if instant_fill: |
||
| 185 | events_to_be_processed = [] |
||
| 186 | |||
| 187 | # Assign process events to variables to avoid attribute access in |
||
| 188 | # innermost loops. |
||
| 189 | # |
||
| 190 | # Done here, to allow for perf_tracker or blotter to be swapped out |
||
| 191 | # or changed in between snapshots. |
||
| 192 | perf_process_trade = self.algo.perf_tracker.process_trade |
||
| 193 | perf_process_transaction = self.algo.perf_tracker.process_transaction |
||
| 194 | perf_process_order = self.algo.perf_tracker.process_order |
||
| 195 | perf_process_benchmark = self.algo.perf_tracker.process_benchmark |
||
| 196 | perf_process_split = self.algo.perf_tracker.process_split |
||
| 197 | perf_process_dividend = self.algo.perf_tracker.process_dividend |
||
| 198 | perf_process_commission = self.algo.perf_tracker.process_commission |
||
| 199 | perf_process_close_position = \ |
||
| 200 | self.algo.perf_tracker.process_close_position |
||
| 201 | blotter_process_trade = self.algo.blotter.process_trade |
||
| 202 | blotter_process_benchmark = self.algo.blotter.process_benchmark |
||
| 203 | |||
| 204 | # Containers for the snapshotted events, so that the events are |
||
| 205 | # processed in a predictable order, without relying on the sorted order |
||
| 206 | # of the individual sources. |
||
| 207 | |||
| 208 | # There is only one benchmark per snapshot, will be set to the current |
||
| 209 | # benchmark iff it occurs. |
||
| 210 | benchmark = None |
||
| 211 | # trades and customs are initialized as a list since process_snapshot |
||
| 212 | # is most often called on market bars, which could contain trades or |
||
| 213 | # custom events. |
||
| 214 | trades = [] |
||
| 215 | customs = [] |
||
| 216 | closes = [] |
||
| 217 | |||
| 218 | # splits and dividends are processed once a day. |
||
| 219 | # |
||
| 220 | # The avoidance of creating the list every time this is called is more |
||
| 221 | # to attempt to show that this is the infrequent case of the method, |
||
| 222 | # since the performance benefit from deferring the list allocation is |
||
| 223 | # marginal. splits list will be allocated when a split occurs in the |
||
| 224 | # snapshot. |
||
| 225 | splits = None |
||
| 226 | # dividends list will be allocated when a dividend occurs in the |
||
| 227 | # snapshot. |
||
| 228 | dividends = None |
||
| 229 | |||
| 230 | for event in snapshot: |
||
| 231 | if event.type == DATASOURCE_TYPE.TRADE: |
||
| 232 | trades.append(event) |
||
| 233 | elif event.type == DATASOURCE_TYPE.BENCHMARK: |
||
| 234 | benchmark = event |
||
| 235 | elif event.type == DATASOURCE_TYPE.SPLIT: |
||
| 236 | if splits is None: |
||
| 237 | splits = [] |
||
| 238 | splits.append(event) |
||
| 239 | elif event.type == DATASOURCE_TYPE.CUSTOM: |
||
| 240 | customs.append(event) |
||
| 241 | elif event.type == DATASOURCE_TYPE.DIVIDEND: |
||
| 242 | if dividends is None: |
||
| 243 | dividends = [] |
||
| 244 | dividends.append(event) |
||
| 245 | elif event.type == DATASOURCE_TYPE.CLOSE_POSITION: |
||
| 246 | closes.append(event) |
||
| 247 | else: |
||
| 248 | raise log.warn("Unrecognized event=%s".format(event)) |
||
| 249 | |||
| 250 | # Handle benchmark first. |
||
| 251 | # |
||
| 252 | # Internal broker implementation depends on the benchmark being |
||
| 253 | # processed first so that transactions and commissions reported from |
||
| 254 | # the broker can be injected. |
||
| 255 | if benchmark is not None: |
||
| 256 | benchmark_event_occurred = True |
||
| 257 | perf_process_benchmark(benchmark) |
||
| 258 | for txn, order in blotter_process_benchmark(benchmark): |
||
| 259 | if txn.type == DATASOURCE_TYPE.TRANSACTION: |
||
| 260 | perf_process_transaction(txn) |
||
| 261 | elif txn.type == DATASOURCE_TYPE.COMMISSION: |
||
| 262 | perf_process_commission(txn) |
||
| 263 | perf_process_order(order) |
||
| 264 | |||
| 265 | for trade in trades: |
||
| 266 | self.update_universe(trade) |
||
| 267 | any_trade_occurred = True |
||
| 268 | if instant_fill: |
||
| 269 | events_to_be_processed.append(trade) |
||
| 270 | else: |
||
| 271 | for txn, order in blotter_process_trade(trade): |
||
| 272 | if txn.type == DATASOURCE_TYPE.TRANSACTION: |
||
| 273 | perf_process_transaction(txn) |
||
| 274 | elif txn.type == DATASOURCE_TYPE.COMMISSION: |
||
| 275 | perf_process_commission(txn) |
||
| 276 | perf_process_order(order) |
||
| 277 | perf_process_trade(trade) |
||
| 278 | |||
| 279 | for custom in customs: |
||
| 280 | self.update_universe(custom) |
||
| 281 | |||
| 282 | for close in closes: |
||
| 283 | self.update_universe(close) |
||
| 284 | perf_process_close_position(close) |
||
| 285 | |||
| 286 | if splits is not None: |
||
| 287 | for split in splits: |
||
| 288 | # process_split is not assigned to a variable since it is |
||
| 289 | # called rarely compared to the other event processors. |
||
| 290 | self.algo.blotter.process_split(split) |
||
| 291 | perf_process_split(split) |
||
| 292 | |||
| 293 | if dividends is not None: |
||
| 294 | for dividend in dividends: |
||
| 295 | perf_process_dividend(dividend) |
||
| 296 | |||
| 297 | if any_trade_occurred: |
||
| 298 | new_orders = self._call_handle_data() |
||
| 299 | for order in new_orders: |
||
| 300 | perf_process_order(order) |
||
| 301 | |||
| 302 | if instant_fill: |
||
| 303 | # Now that handle_data has been called and orders have been placed, |
||
| 304 | # process the event stream to fill user orders based on the events |
||
| 305 | # from this snapshot. |
||
| 306 | for trade in events_to_be_processed: |
||
| 307 | for txn, order in blotter_process_trade(trade): |
||
| 308 | if txn is not None: |
||
| 309 | perf_process_transaction(txn) |
||
| 310 | if order is not None: |
||
| 311 | perf_process_order(order) |
||
| 312 | perf_process_trade(trade) |
||
| 313 | |||
| 314 | if benchmark_event_occurred: |
||
| 315 | return self.generate_messages(dt) |
||
| 316 | else: |
||
| 317 | return () |
||
| 318 | |||
| 388 |