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 | from copy import copy |
||
84 | def check_order_triggers(self, current_price): |
||
85 | """ |
||
86 | Given an order and a trade event, return a tuple of |
||
87 | (stop_reached, limit_reached). |
||
88 | For market orders, will return (False, False). |
||
89 | For stop orders, limit_reached will always be False. |
||
90 | For limit orders, stop_reached will always be False. |
||
91 | For stop limit orders a Boolean is returned to flag |
||
92 | that the stop has been reached. |
||
93 | |||
94 | Orders that have been triggered already (price targets reached), |
||
95 | the order's current values are returned. |
||
96 | """ |
||
97 | if self.triggered: |
||
98 | return (self.stop_reached, self.limit_reached, False) |
||
99 | |||
100 | stop_reached = False |
||
101 | limit_reached = False |
||
102 | sl_stop_reached = False |
||
103 | |||
104 | order_type = 0 |
||
105 | |||
106 | if self.amount > 0: |
||
107 | order_type |= BUY |
||
108 | else: |
||
109 | order_type |= SELL |
||
110 | |||
111 | if self.stop is not None: |
||
112 | order_type |= STOP |
||
113 | |||
114 | if self.limit is not None: |
||
115 | order_type |= LIMIT |
||
116 | |||
117 | if order_type == BUY | STOP | LIMIT: |
||
118 | if current_price >= self.stop: |
||
119 | sl_stop_reached = True |
||
120 | if current_price <= self.limit: |
||
121 | limit_reached = True |
||
122 | elif order_type == SELL | STOP | LIMIT: |
||
123 | if current_price <= self.stop: |
||
124 | sl_stop_reached = True |
||
125 | if current_price >= self.limit: |
||
126 | limit_reached = True |
||
127 | elif order_type == BUY | STOP: |
||
128 | if current_price >= self.stop: |
||
129 | stop_reached = True |
||
130 | elif order_type == SELL | STOP: |
||
131 | if current_price <= self.stop: |
||
132 | stop_reached = True |
||
133 | elif order_type == BUY | LIMIT: |
||
134 | if current_price <= self.limit: |
||
135 | limit_reached = True |
||
136 | elif order_type == SELL | LIMIT: |
||
137 | # This is a SELL LIMIT order |
||
138 | if current_price >= self.limit: |
||
139 | limit_reached = True |
||
140 | |||
141 | return (stop_reached, limit_reached, sl_stop_reached) |
||
142 | |||
240 |