Support automating the backtests of multiple credit and delta targeted strategies
This commit is contained in:
parent
6e6b697c3c
commit
fb1d1f2725
@ -8,7 +8,7 @@ from dotenv import load_dotenv
|
|||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
from download_spx_quotes import download_quotes
|
from download_spx_quotes import download_quotes
|
||||||
from run_backtest import run_backtest
|
from run_backtest import run_backtests
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ def backtest_automation():
|
|||||||
|
|
||||||
end_date = datetime.now()
|
end_date = datetime.now()
|
||||||
start_date = end_date - timedelta(days = 1)
|
start_date = end_date - timedelta(days = 1)
|
||||||
run_backtest(start_date, end_date)
|
run_backtests(start_date, end_date)
|
||||||
except:
|
except:
|
||||||
send_message('Backtest automation failed!', to_user_id = getenv('USER_ID'))
|
send_message('Backtest automation failed!', to_user_id = getenv('USER_ID'))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -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 datetime import datetime, timedelta
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from os import getenv
|
||||||
|
|
||||||
def create_strategies(entry_time: str):
|
from backtesting import available_entry_times, backtest_iron_condor
|
||||||
call_spread_strat = DeltaTargetStrategy(
|
from backtesting.credit_targeting import create_strategies as credit_strategies
|
||||||
delta_target = 0.10,
|
from backtesting.credit_targeting import iron_condor_strategy as credit_iron_condor_strategy
|
||||||
option_type = OptionType.CALL,
|
from backtesting.delta_targeting import create_strategies as delta_strategies
|
||||||
number_of_contracts = 1,
|
from backtesting.delta_targeting import iron_condor_strategy as delta_iron_condor_strategy
|
||||||
spread_width = 50,
|
from backtesting.option_spread_strategy import OptionSpreadStrategy
|
||||||
stop_loss_multiple = 1.00,
|
from database.backtest import insert
|
||||||
trade_entry_time = entry_time
|
|
||||||
)
|
load_dotenv()
|
||||||
put_spread_strat = DeltaTargetStrategy(
|
|
||||||
delta_target = -0.10,
|
def run_backtest(strategy_name: str, start_date: datetime, end_date: datetime,
|
||||||
option_type = OptionType.PUT,
|
call_spread_strategy: OptionSpreadStrategy, put_spread_strategy: OptionSpreadStrategy):
|
||||||
number_of_contracts = 1,
|
|
||||||
spread_width = 50,
|
backtest_results = backtest_iron_condor(
|
||||||
stop_loss_multiple = 1.00,
|
strategy_name,
|
||||||
trade_entry_time = entry_time
|
call_spread_strategy,
|
||||||
)
|
put_spread_strategy,
|
||||||
return call_spread_strat, put_spread_strat
|
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():
|
for entry_time in available_entry_times():
|
||||||
call_spread_strategy, put_spread_strategy = create_strategies(entry_time)
|
for credit_target in credit_targets:
|
||||||
backtest_results = backtest_iron_condor(
|
strategy_name = credit_iron_condor_strategy(credit_target)
|
||||||
f'10 Delta Iron Condor',
|
call_spread_strategy, put_spread_strategy = credit_strategies(credit_target, entry_time)
|
||||||
call_spread_strategy,
|
run_backtest(strategy_name, start_date, end_date, call_spread_strategy, put_spread_strategy)
|
||||||
put_spread_strategy,
|
|
||||||
start_date,
|
|
||||||
end_date
|
|
||||||
)
|
|
||||||
|
|
||||||
if not backtest_results.empty:
|
for delta_target in delta_targets:
|
||||||
# TODO: Think of a better way to handle this.
|
strategy_name = delta_iron_condor_strategy(delta_target)
|
||||||
backtest_results.drop('Cumulative Profit', axis = 1, inplace = True)
|
call_spread_strategy, put_spread_strategy = delta_strategies(delta_target, entry_time)
|
||||||
print(backtest_results)
|
run_backtest(strategy_name, start_date, end_date, call_spread_strategy, put_spread_strategy)
|
||||||
insert(backtest_results)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
end_date = datetime.now()
|
end_date = datetime.now()
|
||||||
start_date = end_date - timedelta(days = 1)
|
start_date = end_date - timedelta(days = 1)
|
||||||
run_backtest(start_date, end_date)
|
run_backtests(start_date, end_date)
|
Loading…
Reference in New Issue
Block a user