Conditions | 2 |
Total Lines | 63 |
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:
1 | # |
||
110 | def test_order_rejection(self): |
||
111 | blotter = Blotter() |
||
112 | |||
113 | # Reject a nonexistent order -> no order appears in new_order, |
||
114 | # no exceptions raised out |
||
115 | blotter.reject(56) |
||
116 | self.assertEqual(blotter.new_orders, []) |
||
117 | |||
118 | # Basic tests of open order behavior |
||
119 | open_order_id = blotter.order(24, 100, MarketOrder()) |
||
120 | second_order_id = blotter.order(24, 50, MarketOrder()) |
||
121 | self.assertEqual(len(blotter.open_orders[24]), 2) |
||
122 | open_order = blotter.open_orders[24][0] |
||
123 | self.assertEqual(open_order.status, ORDER_STATUS.OPEN) |
||
124 | self.assertEqual(open_order.id, open_order_id) |
||
125 | self.assertIn(open_order, blotter.new_orders) |
||
126 | |||
127 | # Reject that order immediately (same bar, i.e. still in new_orders) |
||
128 | blotter.reject(open_order_id) |
||
129 | self.assertEqual(len(blotter.new_orders), 2) |
||
130 | self.assertEqual(len(blotter.open_orders[24]), 1) |
||
131 | still_open_order = blotter.new_orders[0] |
||
132 | self.assertEqual(still_open_order.id, second_order_id) |
||
133 | self.assertEqual(still_open_order.status, ORDER_STATUS.OPEN) |
||
134 | rejected_order = blotter.new_orders[1] |
||
135 | self.assertEqual(rejected_order.status, ORDER_STATUS.REJECTED) |
||
136 | self.assertEqual(rejected_order.reason, '') |
||
137 | |||
138 | # Do it again, but reject it at a later time (after tradesimulation |
||
139 | # pulls it from new_orders) |
||
140 | blotter = Blotter() |
||
141 | |||
142 | new_open_id = blotter.order(24, 10, MarketOrder()) |
||
143 | new_open_order = blotter.open_orders[24][0] |
||
144 | self.assertEqual(new_open_id, new_open_order.id) |
||
145 | # Pretend that the trade simulation did this. |
||
146 | blotter.new_orders = [] |
||
147 | |||
148 | rejection_reason = "Not enough cash on hand." |
||
149 | blotter.reject(new_open_id, reason=rejection_reason) |
||
150 | rejected_order = blotter.new_orders[0] |
||
151 | self.assertEqual(rejected_order.id, new_open_id) |
||
152 | self.assertEqual(rejected_order.status, ORDER_STATUS.REJECTED) |
||
153 | self.assertEqual(rejected_order.reason, rejection_reason) |
||
154 | |||
155 | # You can't reject a filled order. |
||
156 | blotter = Blotter() # Reset for paranoia |
||
157 | blotter.slippage_func = FixedSlippage() |
||
158 | filled_id = blotter.order(24, 100, MarketOrder()) |
||
159 | filled_order = None |
||
160 | blotter.current_dt = self.sim_params.trading_days[-1] |
||
161 | txns, _ = blotter.get_transactions(self.data_portal) |
||
162 | for txn in txns: |
||
163 | filled_order = blotter.orders[txn.order_id] |
||
164 | |||
165 | self.assertEqual(filled_order.id, filled_id) |
||
166 | self.assertIn(filled_order, blotter.new_orders) |
||
167 | self.assertEqual(filled_order.status, ORDER_STATUS.FILLED) |
||
168 | self.assertNotIn(filled_order, blotter.open_orders[24]) |
||
169 | |||
170 | blotter.reject(filled_id) |
||
171 | updated_order = blotter.orders[filled_id] |
||
172 | self.assertEqual(updated_order.status, ORDER_STATUS.FILLED) |
||
173 | |||
233 |