From 2c4fd834bb1a84ec12a12b7f7dc07b734a3426a2 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 5 Feb 2024 08:52:35 -0800 Subject: [PATCH] Add script for backtesting delta targeted strategies --- delta_targeting_backtest.py | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 delta_targeting_backtest.py diff --git a/delta_targeting_backtest.py b/delta_targeting_backtest.py new file mode 100644 index 0000000..9b8d6c1 --- /dev/null +++ b/delta_targeting_backtest.py @@ -0,0 +1,41 @@ +from datetime import datetime +from dotenv import load_dotenv +from os import getenv +from pandas import concat + +from backtesting import backtest_iron_condor +from backtesting.delta_targeting import create_strategies +from plotting import BacktestChart, plot + +load_dotenv() + +if __name__ == '__main__': + start_date = datetime(2016, 1, 1) + end_date = datetime.now() + + delta_target = float(getenv('DELTA_TARGET')) + entry_times = getenv('ENTRY_TIMES').split(',') + + backtest_results = [] + + for entry_time in entry_times: + call_spread_strategy, put_spread_strategy = create_strategies(delta_target, entry_time) + + backtest_result = backtest_iron_condor( + f'{int(delta_target * 100)} Delta Iron Condor @ {call_spread_strategy.trade_entry_time}', + call_spread_strategy, + put_spread_strategy, + start_date, + end_date + ) + + backtest_results.append(backtest_result) + + combined_backtest_results = concat(backtest_results) + summed_results = combined_backtest_results.groupby('Date')['Cumulative Profit'].sum().reset_index() + + plot(BacktestChart( + dates = summed_results['Date'], + profit = summed_results['Cumulative Profit'], + title = f'{int(delta_target * 100)} Delta Iron Condor' + )) \ No newline at end of file