diff --git a/backtest_scheduler.py b/backtest_scheduler.py index 10ab755..e06e092 100644 --- a/backtest_scheduler.py +++ b/backtest_scheduler.py @@ -8,7 +8,7 @@ from dotenv import load_dotenv from os import getenv from download_spx_quotes import download_quotes -from run_backtest import run_backtest +from run_backtest import run_backtests load_dotenv() @@ -18,7 +18,7 @@ def backtest_automation(): end_date = datetime.now() start_date = end_date - timedelta(days = 1) - run_backtest(start_date, end_date) + run_backtests(start_date, end_date) except: send_message('Backtest automation failed!', to_user_id = getenv('USER_ID')) traceback.print_exc() diff --git a/run_backtest.py b/run_backtest.py index 0ad843a..2e084d8 100644 --- a/run_backtest.py +++ b/run_backtest.py @@ -1,46 +1,49 @@ -from backtesting import available_entry_times, backtest_iron_condor -from backtesting.delta_targeting import DeltaTargetStrategy -from backtesting.option_type import OptionType -from database.backtest import insert from datetime import datetime, timedelta +from dotenv import load_dotenv +from os import getenv -def create_strategies(entry_time: str): - call_spread_strat = DeltaTargetStrategy( - delta_target = 0.10, - option_type = OptionType.CALL, - number_of_contracts = 1, - spread_width = 50, - stop_loss_multiple = 1.00, - trade_entry_time = entry_time - ) - put_spread_strat = DeltaTargetStrategy( - delta_target = -0.10, - option_type = OptionType.PUT, - number_of_contracts = 1, - spread_width = 50, - stop_loss_multiple = 1.00, - trade_entry_time = entry_time - ) - return call_spread_strat, put_spread_strat +from backtesting import available_entry_times, backtest_iron_condor +from backtesting.credit_targeting import create_strategies as credit_strategies +from backtesting.credit_targeting import iron_condor_strategy as credit_iron_condor_strategy +from backtesting.delta_targeting import create_strategies as delta_strategies +from backtesting.delta_targeting import iron_condor_strategy as delta_iron_condor_strategy +from backtesting.option_spread_strategy import OptionSpreadStrategy +from database.backtest import insert + +load_dotenv() + +def run_backtest(strategy_name: str, start_date: datetime, end_date: datetime, + call_spread_strategy: OptionSpreadStrategy, put_spread_strategy: OptionSpreadStrategy): + + backtest_results = backtest_iron_condor( + strategy_name, + call_spread_strategy, + put_spread_strategy, + start_date, + end_date + ) + + if not backtest_results.empty: + backtest_results.drop('Cumulative Profit', axis = 1, inplace = True) + print(backtest_results) + insert(backtest_results) + +def run_backtests(start_date: datetime, end_date: datetime): + credit_targets = [float(credit) for credit in getenv('CREDIT_TARGETS').split(',')] + delta_targets = [float(delta) for delta in getenv('DELTA_TARGETS').split(',')] -def run_backtest(start_date: datetime, end_date: datetime): for entry_time in available_entry_times(): - call_spread_strategy, put_spread_strategy = create_strategies(entry_time) - backtest_results = backtest_iron_condor( - f'10 Delta Iron Condor', - call_spread_strategy, - put_spread_strategy, - start_date, - end_date - ) + for credit_target in credit_targets: + strategy_name = credit_iron_condor_strategy(credit_target) + call_spread_strategy, put_spread_strategy = credit_strategies(credit_target, entry_time) + run_backtest(strategy_name, start_date, end_date, call_spread_strategy, put_spread_strategy) - if not backtest_results.empty: - # TODO: Think of a better way to handle this. - backtest_results.drop('Cumulative Profit', axis = 1, inplace = True) - print(backtest_results) - insert(backtest_results) + for delta_target in delta_targets: + strategy_name = delta_iron_condor_strategy(delta_target) + call_spread_strategy, put_spread_strategy = delta_strategies(delta_target, entry_time) + run_backtest(strategy_name, start_date, end_date, call_spread_strategy, put_spread_strategy) if __name__ == '__main__': end_date = datetime.now() start_date = end_date - timedelta(days = 1) - run_backtest(start_date, end_date) \ No newline at end of file + run_backtests(start_date, end_date) \ No newline at end of file