2024-02-15 16:37:18 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from os import getenv
|
|
|
|
|
2024-02-01 14:00:57 +00:00
|
|
|
from backtesting import available_entry_times, backtest_iron_condor
|
2024-02-15 16:37:18 +00:00
|
|
|
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
|
2024-01-17 19:39:18 +00:00
|
|
|
from database.backtest import insert
|
|
|
|
|
2024-02-15 16:37:18 +00:00
|
|
|
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
|
2024-01-17 19:39:18 +00:00
|
|
|
)
|
|
|
|
|
2024-02-15 16:37:18 +00:00
|
|
|
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(',')]
|
|
|
|
|
2024-02-01 14:00:57 +00:00
|
|
|
for entry_time in available_entry_times():
|
2024-02-15 16:37:18 +00:00
|
|
|
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)
|
2024-01-17 19:39:18 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-02-15 14:27:13 +00:00
|
|
|
end_date = datetime.now()
|
2024-01-18 14:28:08 +00:00
|
|
|
start_date = end_date - timedelta(days = 1)
|
2024-02-15 16:37:18 +00:00
|
|
|
run_backtests(start_date, end_date)
|