Add helper methods to construct call and put spread strategies for credit and delta targeting

This commit is contained in:
moshferatu 2024-02-04 05:56:28 -08:00
parent 8670609517
commit f9a02978e4
3 changed files with 48 additions and 23 deletions

View File

@ -1,30 +1,15 @@
from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType
from backtesting import backtest_iron_condor
from backtesting.delta_targeting import create_strategies
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')
call_spread_strategy, put_spread_strategy = create_strategies(
delta_target = 0.10,
entry_time = '10:05:00'
)
backtest_result = backtest_iron_condor(
f'Iron Condor @ {call_spread_strategy.trade_entry_time}',
call_spread_strategy,

View File

@ -1,7 +1,27 @@
from dataclasses import dataclass
from .option_spread_strategy import OptionSpreadStrategy
from .option_type import OptionType
@dataclass
class CreditTargetStrategy(OptionSpreadStrategy):
credit_target: float
credit_target: float
def create_strategies(credit_target: float, entry_time: str, number_of_contracts: int = 1):
call_spread_strategy = CreditTargetStrategy(
credit_target = credit_target,
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 = CreditTargetStrategy(
credit_target = credit_target,
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

View File

@ -1,7 +1,27 @@
from dataclasses import dataclass
from .option_spread_strategy import OptionSpreadStrategy
from .option_type import OptionType
@dataclass
class DeltaTargetStrategy(OptionSpreadStrategy):
delta_target: float
delta_target: float
def create_strategies(delta_target: float, entry_time: str, number_of_contracts: int = 1):
call_spread_strategy = DeltaTargetStrategy(
delta_target = delta_target,
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 = -delta_target,
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