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