2024-02-01 14:00:57 +00:00
|
|
|
from backtesting import available_entry_times, backtest_iron_condor
|
2024-02-15 14:27:13 +00:00
|
|
|
from backtesting.delta_targeting import DeltaTargetStrategy
|
2024-01-17 19:39:18 +00:00
|
|
|
from backtesting.option_type import OptionType
|
|
|
|
from database.backtest import insert
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
def create_strategies(entry_time: str):
|
|
|
|
call_spread_strat = DeltaTargetStrategy(
|
2024-02-01 13:59:33 +00:00
|
|
|
delta_target = 0.10,
|
2024-01-17 19:39:18 +00:00
|
|
|
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(
|
2024-02-01 13:59:33 +00:00
|
|
|
delta_target = -0.10,
|
2024-01-17 19:39:18 +00:00
|
|
|
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
|
|
|
|
|
2024-01-18 14:28:08 +00:00
|
|
|
def run_backtest(start_date: datetime, end_date: datetime):
|
2024-02-01 14:00:57 +00:00
|
|
|
for entry_time in available_entry_times():
|
2024-01-17 19:39:18 +00:00
|
|
|
call_spread_strategy, put_spread_strategy = create_strategies(entry_time)
|
|
|
|
backtest_results = backtest_iron_condor(
|
2024-02-02 15:45:40 +00:00
|
|
|
f'10 Delta Iron Condor',
|
2024-01-17 19:39:18 +00:00
|
|
|
call_spread_strategy,
|
|
|
|
put_spread_strategy,
|
|
|
|
start_date,
|
|
|
|
end_date
|
|
|
|
)
|
2024-02-02 15:45:40 +00:00
|
|
|
|
2024-01-21 13:56:06 +00:00
|
|
|
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)
|
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)
|
|
|
|
run_backtest(start_date, end_date)
|