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