| 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 | def transact_stub(slippage, commission, event, open_orders): |
||
| 38 | """ |
||
| 39 | This is intended to be wrapped in a partial, so that the |
||
| 40 | slippage and commission models can be enclosed. |
||
| 41 | """ |
||
| 42 | for order, transaction in slippage(event, open_orders): |
||
| 43 | if transaction and transaction.amount != 0: |
||
| 44 | direction = math.copysign(1, transaction.amount) |
||
| 45 | per_share, total_commission = commission.calculate(transaction) |
||
| 46 | transaction.price += per_share * direction |
||
| 47 | transaction.commission = total_commission |
||
| 48 | yield order, transaction |
||
| 49 | |||
| 50 | |||
| 51 | def transact_partial(slippage, commission): |
||
| 52 | return partial(transact_stub, slippage, commission) |
||
| 53 | |||
| 54 | |||
| 55 | class LiquidityExceeded(Exception): |
||
| 56 | pass |
||
| 57 | |||
| 58 | |||
| 59 | class SlippageModel(with_metaclass(abc.ABCMeta)): |
||
| 60 | |||
| 61 | @property |
||
| 62 | def volume_for_bar(self): |
||
| 63 | return self._volume_for_bar |
||
| 64 | |||
| 65 | @abc.abstractproperty |
||
| 66 | def process_order(self, event, order): |
||
| 67 | pass |
||
| 68 | |||
| 69 | def simulate(self, event, current_orders): |
||
| 70 | |||
| 71 | self._volume_for_bar = 0 |
||
| 72 | |||
| 73 | for order in current_orders: |
||
| 74 | |||
| 75 | if order.open_amount == 0: |
||
| 76 | continue |
||
| 77 | |||
| 78 | order.check_triggers(event) |
||
| 79 | if not order.triggered: |
||
| 80 | continue |
||
| 81 | |||
| 82 | try: |
||
| 83 | txn = self.process_order(event, order) |
||
| 84 | except LiquidityExceeded: |
||
| 85 | break |
||
| 86 | |||
| 87 | if txn: |
||
| 88 | self._volume_for_bar += abs(txn.amount) |
||
| 89 | yield order, txn |
||
| 90 | |||
| 91 | def __call__(self, event, current_orders, **kwargs): |
||
| 92 | return self.simulate(event, current_orders, **kwargs) |
||
| 93 | |||
| 94 | |||
| 95 | class VolumeShareSlippage(SlippageModel): |
||
| 219 |