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.Order.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 | # |
||
98 | def check_order_triggers(self, event): |
||
99 | """ |
||
100 | Given an order and a trade event, return a tuple of |
||
101 | (stop_reached, limit_reached). |
||
102 | For market orders, will return (False, False). |
||
103 | For stop orders, limit_reached will always be False. |
||
104 | For limit orders, stop_reached will always be False. |
||
105 | For stop limit orders a Boolean is returned to flag |
||
106 | that the stop has been reached. |
||
107 | |||
108 | Orders that have been triggered already (price targets reached), |
||
109 | the order's current values are returned. |
||
110 | """ |
||
111 | if self.triggered: |
||
112 | return (self.stop_reached, self.limit_reached, False) |
||
113 | |||
114 | stop_reached = False |
||
115 | limit_reached = False |
||
116 | sl_stop_reached = False |
||
117 | |||
118 | order_type = 0 |
||
119 | |||
120 | if self.amount > 0: |
||
121 | order_type |= BUY |
||
122 | else: |
||
123 | order_type |= SELL |
||
124 | |||
125 | if self.stop is not None: |
||
126 | order_type |= STOP |
||
127 | |||
128 | if self.limit is not None: |
||
129 | order_type |= LIMIT |
||
130 | |||
131 | if order_type == BUY | STOP | LIMIT: |
||
132 | if event.price >= self.stop: |
||
133 | sl_stop_reached = True |
||
134 | if event.price <= self.limit: |
||
135 | limit_reached = True |
||
136 | elif order_type == SELL | STOP | LIMIT: |
||
137 | if event.price <= self.stop: |
||
138 | sl_stop_reached = True |
||
139 | if event.price >= self.limit: |
||
140 | limit_reached = True |
||
141 | elif order_type == BUY | STOP: |
||
142 | if event.price >= self.stop: |
||
143 | stop_reached = True |
||
144 | elif order_type == SELL | STOP: |
||
145 | if event.price <= self.stop: |
||
146 | stop_reached = True |
||
147 | elif order_type == BUY | LIMIT: |
||
148 | if event.price <= self.limit: |
||
149 | limit_reached = True |
||
150 | elif order_type == SELL | LIMIT: |
||
151 | # This is a SELL LIMIT order |
||
152 | if event.price >= self.limit: |
||
153 | limit_reached = True |
||
154 | |||
155 | return (stop_reached, limit_reached, sl_stop_reached) |
||
156 | |||
258 |