47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
from backtesting import available_entry_times, backtest_iron_condor
|
|
from backtesting.delta_target_strategy import DeltaTargetStrategy
|
|
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(
|
|
delta_target = 0.10,
|
|
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(
|
|
delta_target = -0.10,
|
|
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
|
|
|
|
def run_backtest(start_date: datetime, end_date: datetime):
|
|
for entry_time in available_entry_times():
|
|
call_spread_strategy, put_spread_strategy = create_strategies(entry_time)
|
|
backtest_results = backtest_iron_condor(
|
|
f'10 Delta Iron Condor @ {call_spread_strategy.trade_entry_time}',
|
|
call_spread_strategy,
|
|
put_spread_strategy,
|
|
start_date,
|
|
end_date
|
|
)
|
|
|
|
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)
|
|
|
|
if __name__ == '__main__':
|
|
# TODO: Update backtest to eliminate the need for setting this to midnight.
|
|
end_date = datetime.now().replace(hour = 0, minute = 0, second = 0, microsecond = 0)
|
|
start_date = end_date - timedelta(days = 1)
|
|
run_backtest(start_date, end_date) |