2024-01-17 19:37:37 +00:00
|
|
|
from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType
|
|
|
|
from datetime import datetime
|
2024-01-30 19:17:46 +00:00
|
|
|
from plotting import BacktestChart, plot
|
2024-01-17 19:37:37 +00:00
|
|
|
|
|
|
|
def create_strategies(entry_time: str, number_of_contracts: int = 1):
|
|
|
|
call_spread_strategy = DeltaTargetStrategy(
|
2024-02-01 13:56:47 +00:00
|
|
|
delta_target = 0.10,
|
2024-01-17 19:37:37 +00:00
|
|
|
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(
|
2024-02-01 13:56:47 +00:00
|
|
|
delta_target = -0.10,
|
2024-01-17 19:37:37 +00:00
|
|
|
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__':
|
2024-02-01 13:56:47 +00:00
|
|
|
start_date = datetime(2024, 1, 1)
|
2024-01-17 19:37:37 +00:00
|
|
|
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)
|
2024-01-30 19:17:46 +00:00
|
|
|
|
|
|
|
plot(BacktestChart(
|
|
|
|
dates = backtest_result['Date'],
|
|
|
|
profit = backtest_result['Cumulative Profit'],
|
|
|
|
title = f'Iron Condor @ {call_spread_strategy.trade_entry_time}'
|
|
|
|
))
|