| Conditions | 19 |
| Total Lines | 58 |
| 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.finance.check_order_triggers() 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 | # |
||
| 37 | return self._volume_for_bar |
||
| 38 | |||
| 39 | @abc.abstractproperty |
||
| 40 | def process_order(self, price, volume, order, dt): |
||
| 41 | pass |
||
| 42 | |||
| 43 | def simulate(self, current_orders, dt, price, volume): |
||
| 44 | self._volume_for_bar = 0 |
||
| 45 | |||
| 46 | for order in current_orders: |
||
| 47 | if order.open_amount == 0: |
||
| 48 | continue |
||
| 49 | |||
| 50 | order.check_triggers(price, dt) |
||
| 51 | if not order.triggered: |
||
| 52 | continue |
||
| 53 | |||
| 54 | try: |
||
| 55 | txn = self.process_order(order, price, volume, dt) |
||
| 56 | except LiquidityExceeded: |
||
| 57 | break |
||
| 58 | |||
| 59 | if txn: |
||
| 60 | self._volume_for_bar += abs(txn.amount) |
||
| 61 | yield order, txn |
||
| 62 | |||
| 63 | def __call__(self, current_orders, dt, price, volume, **kwargs): |
||
| 64 | return self.simulate(current_orders, dt, price, volume, **kwargs) |
||
| 65 | |||
| 66 | |||
| 67 | class VolumeShareSlippage(SlippageModel): |
||
| 68 | |||
| 69 | def __init__(self, volume_limit=0.25, price_impact=0.1): |
||
| 70 | self.volume_limit = volume_limit |
||
| 71 | self.price_impact = price_impact |
||
| 72 | |||
| 73 | def __repr__(self): |
||
| 74 | return """ |
||
| 75 | {class_name}( |
||
| 76 | volume_limit={volume_limit}, |
||
| 77 | price_impact={price_impact}) |
||
| 78 | """.strip().format(class_name=self.__class__.__name__, |
||
| 79 | volume_limit=self.volume_limit, |
||
| 80 | price_impact=self.price_impact) |
||
| 81 | |||
| 82 | def process_order(self, order, price, volume, dt): |
||
| 83 | max_volume = self.volume_limit * volume |
||
| 84 | |||
| 85 | # price impact accounts for the total volume of transactions |
||
| 86 | # created against the current minute bar |
||
| 87 | remaining_volume = max_volume - self.volume_for_bar |
||
| 88 | if remaining_volume < 1: |
||
| 89 | # we can't fill any more transactions |
||
| 90 | raise LiquidityExceeded() |
||
| 91 | |||
| 92 | # the current order amount will be the min of the |
||
| 93 | # volume available in the bar or the open amount. |
||
| 94 | cur_volume = int(min(remaining_volume, abs(order.open_amount))) |
||
| 95 | |||
| 187 |