Support automating the backtests of multiple credit and delta targeted strategies

This commit is contained in:
moshferatu 2024-02-15 08:37:18 -08:00
parent 6e6b697c3c
commit fb1d1f2725
2 changed files with 42 additions and 39 deletions

View File

@ -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()

View File

@ -1,33 +1,22 @@
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):
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',
strategy_name,
call_spread_strategy,
put_spread_strategy,
start_date,
@ -35,12 +24,26 @@ def run_backtest(start_date: datetime, end_date: datetime):
)
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)
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_backtest(start_date, end_date)
run_backtests(start_date, end_date)