Skip to content

Commit dfefb79

Browse files
committed
Fixed a bug caused by using integer position instead of index in sell_some_options
1 parent f504e1b commit dfefb79

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

backtester/backtester.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,21 @@ def _rebalance_portfolio(self, date, stocks, options, sma_days):
192192
def _sell_some_options(self, date, to_sell, current_options):
193193
sold = 0
194194
total_costs = sum([current_options[i]['cost'] for i in range(len(current_options))])
195-
for i, (exit_cost, inventory_row) in enumerate(zip(total_costs, self._options_inventory.iterrows())):
196-
if (to_sell - sold < -exit_cost * inventory_row[1]['totals']['qty']) and (to_sell - sold) > 0:
195+
for (exit_cost, (row_index, inventory_row)) in zip(total_costs, self._options_inventory.iterrows()):
196+
if (to_sell - sold < -exit_cost * inventory_row['totals']['qty']) and (to_sell - sold) > 0:
197197
qty_to_sell = (to_sell - sold) // exit_cost
198198
if qty_to_sell != 0:
199-
trade_log_append = self._options_inventory.iloc[i].copy()
199+
trade_log_append = self._options_inventory.loc[row_index].copy()
200200
trade_log_append['totals', 'qty'] = qty_to_sell
201201
trade_log_append['totals', 'date'] = date
202202
trade_log_append['totals', 'cost'] = exit_cost
203-
for j, leg in enumerate(self._options_strategy.legs):
203+
for i, leg in enumerate(self._options_strategy.legs):
204204
trade_log_append[leg.name, 'order'] = ~trade_log_append[leg.name, 'order']
205-
trade_log_append[leg.name, 'cost'] = current_options[j].iloc[i]['cost']
205+
trade_log_append[leg.name, 'cost'] = current_options[i].loc[row_index]['cost']
206206

207207
self.trade_log = self.trade_log.append(trade_log_append, ignore_index=True)
208-
self._options_inventory.at[i, ('totals', 'date')] = date
209-
self._options_inventory.at[i, ('totals', 'qty')] += qty_to_sell
208+
self._options_inventory.at[row_index, ('totals', 'date')] = date
209+
self._options_inventory.at[row_index, ('totals', 'qty')] += qty_to_sell
210210

211211
sold += (qty_to_sell * exit_cost)
212212

0 commit comments

Comments
 (0)