From 0569f34e1f95c0c042eff4fee5b523058947a5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Rodr=C3=ADguez=20Chatruc?= Date: Fri, 20 Mar 2020 11:53:14 -0300 Subject: [PATCH 1/2] Balance now has stock qty disaggregated by symbol --- backtester/backtester.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backtester/backtester.py b/backtester/backtester.py index dd39223..96ed9de 100644 --- a/backtester/backtester.py +++ b/backtester/backtester.py @@ -304,6 +304,10 @@ class Backtest: add['puts capital'] = puts_value add['stocks qty'] = self._stocks_inventory['qty'].sum() + for _index, row in self._stocks_inventory.iterrows(): + symbol = row['symbol'] + add[symbol + ' qty'] = row['qty'] + # sort=False means we're assuming the updates are done in chronological order, i.e, # the dates in add are the immediate successors to the ones at the end of self.balance. # Pass sort=True to ensure self.balance is always sorted chronologically if needed. From de0fed75cde2f8f5c9b15f446e50ca7eae567037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Rodr=C3=ADguez=20Chatruc?= Date: Fri, 20 Mar 2020 15:04:58 -0300 Subject: [PATCH 2/2] Modified test_backtest to keep only the balance columns used for the test --- backtester/test/backtester/test_backtester.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backtester/test/backtester/test_backtester.py b/backtester/test/backtester/test_backtester.py index 1aaacbf..fe91edf 100644 --- a/backtester/test/backtester/test_backtester.py +++ b/backtester/test/backtester/test_backtester.py @@ -13,7 +13,14 @@ def test_backtest(sample_stock_portfolio, sample_stocks_datahandler, sample_opti stocks=sample_stock_portfolio) tl_long, balance_long = bt.trade_log, bt.balance - last_day_balance_long = balance_long.iloc[-1].values + symbols = [stock.symbol for stock in sample_stock_portfolio] + balance_columns = ['total capital', 'cash'] + symbols + [ + 'options qty', 'calls capital', 'puts capital', 'stocks qty', 'options capital', 'stocks capital', '% change', + 'accumulated return' + ] + + last_day_balance_long = balance_long.iloc[-1] + last_day_balance_long = last_day_balance_long[balance_columns].values leg_1_costs = tl_long['leg_1']['cost'] leg_2_costs = tl_long['leg_2']['cost'] @@ -40,7 +47,8 @@ def test_backtest(sample_stock_portfolio, sample_stocks_datahandler, sample_opti stocks=sample_stock_portfolio) tl_short, balance_short = bt.trade_log, bt.balance - last_day_balance_short = balance_short.iloc[-1].values + last_day_balance_short = balance_short.iloc[-1] + last_day_balance_short = last_day_balance_short[balance_columns].values leg_1_costs = tl_short['leg_1']['cost'] leg_2_costs = tl_short['leg_2']['cost']