From 19987f111497dd723877c8538a5d65d11a8d0935 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 28 Feb 2024 05:43:20 -0800 Subject: [PATCH] Add the differences between the backtest and trade spread opening prices to the report --- trade_report.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/trade_report.py b/trade_report.py index 2bf5e69..735b1bb 100644 --- a/trade_report.py +++ b/trade_report.py @@ -12,6 +12,9 @@ trade_data = trades(trade_date) strategy = trade_data['Strategy'].iloc[0] traded_entry_times = set() +trade_open_prices = {} +backtest_open_prices = {} + for _, trade in trade_data.iterrows(): entry_time = str(trade["Entry Time"])[-8:] print(f'Entry Time: {entry_time}') @@ -19,10 +22,12 @@ for _, trade in trade_data.iterrows(): spreads = trade['Spreads'] for spread in spreads: - legs = spread['Legs'] open_price = spread['Open'] + spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' + trade_open_prices[(entry_time, spread_type)] = open_price # Assuming the first leg is always short and the second is long. + legs = spread['Legs'] short_leg = legs[0] long_leg = legs[1] @@ -39,9 +44,11 @@ for _, backtest in backtest_data.iterrows(): spreads = backtest['Spreads'] for spread in spreads: - legs = spread['Legs'] open_price = spread['Open'] + spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' + backtest_open_prices[(entry_time, spread_type)] = open_price + legs = spread['Legs'] short_leg = legs[0] long_leg = legs[1] @@ -49,4 +56,15 @@ for _, backtest in backtest_data.iterrows(): print(f' Backtest Long Strike: {long_leg["Strike"]}, Type: {long_leg["Type"]}') print(f' Backtest Opening Price: {open_price}') -print(f'Backtest Profit: {backtest_profit(trade_date)}') \ No newline at end of file +print(f'Backtest Profit: {backtest_profit(trade_date)}') + +price_differences = [] + +for (entry_time, spread_type), backtest_open_price in backtest_open_prices.items(): + trade_open_price = trade_open_prices[(entry_time, spread_type)] + price_difference = backtest_open_price - trade_open_price + price_differences.append(price_difference) + print(f'{entry_time} {spread_type} Spread Difference: {round(price_difference, 2)}') + +average_price_difference = sum(price_differences) / len(price_differences) +print(f'Average Spread Difference: {round(average_price_difference, 2)}') \ No newline at end of file