from datetime import datetime, timedelta from dotenv import load_dotenv from os import getenv 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(',')] for entry_time in available_entry_times(): 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) 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_backtests(start_date, end_date)