options-backtesting/backtest_iron_condor_example.py

41 lines
1.4 KiB
Python

from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType
from datetime import datetime
from plotting import BacktestChart, plot
def create_strategies(entry_time: str, number_of_contracts: int = 1):
call_spread_strategy = DeltaTargetStrategy(
delta_target = 0.10,
option_type = OptionType.CALL,
number_of_contracts = number_of_contracts,
spread_width = 50,
stop_loss_multiple = 1.00,
trade_entry_time = entry_time
)
put_spread_strategy = DeltaTargetStrategy(
delta_target = -0.10,
option_type = OptionType.PUT,
number_of_contracts = number_of_contracts,
spread_width = 50,
stop_loss_multiple = 1.00,
trade_entry_time = entry_time
)
return call_spread_strategy, put_spread_strategy
if __name__ == '__main__':
start_date = datetime(2024, 1, 1)
end_date = datetime.now()
call_spread_strategy, put_spread_strategy = create_strategies(entry_time = '10:05:00')
backtest_result = backtest_iron_condor(
f'Iron Condor @ {call_spread_strategy.trade_entry_time}',
call_spread_strategy,
put_spread_strategy,
start_date,
end_date
)
print(backtest_result)
plot(BacktestChart(
dates = backtest_result['Date'],
profit = backtest_result['Cumulative Profit'],
title = f'Iron Condor @ {call_spread_strategy.trade_entry_time}'
))