Add example backtest which applies a volatility regime filter
This commit is contained in:
parent
e02e831288
commit
626608f1f6
43
volatility_regime_backtest.py
Normal file
43
volatility_regime_backtest.py
Normal file
@ -0,0 +1,43 @@
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from pandas import concat
|
||||
|
||||
from backtesting import backtest_iron_condor
|
||||
from backtesting.credit_targeting import create_strategies
|
||||
from backtesting.filter import VolatilityRegimeFilter
|
||||
from plotting import BacktestChart, plot
|
||||
|
||||
load_dotenv()
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_date = datetime(2016, 1, 1)
|
||||
end_date = datetime.now()
|
||||
|
||||
credit_target = float(getenv('CREDIT_TARGET'))
|
||||
entry_times = getenv('ENTRY_TIMES').split(',')
|
||||
|
||||
backtest_results = []
|
||||
|
||||
for entry_time in entry_times:
|
||||
call_spread_strategy, put_spread_strategy = create_strategies(credit_target, entry_time)
|
||||
|
||||
backtest_result = backtest_iron_condor(
|
||||
f'${credit_target:.2f} Iron Condor @ {call_spread_strategy.trade_entry_time}',
|
||||
call_spread_strategy,
|
||||
put_spread_strategy,
|
||||
start_date,
|
||||
end_date,
|
||||
filters = [VolatilityRegimeFilter()]
|
||||
)
|
||||
|
||||
backtest_results.append(backtest_result)
|
||||
|
||||
combined_backtest_results = concat(backtest_results)
|
||||
summed_results = combined_backtest_results.groupby('Date')['Cumulative Profit'].sum().reset_index()
|
||||
|
||||
plot(BacktestChart(
|
||||
dates = summed_results['Date'],
|
||||
profit = summed_results['Cumulative Profit'],
|
||||
title = f'${credit_target:.2f} Iron Condor (Volatility Regime Filter)'
|
||||
))
|
Loading…
Reference in New Issue
Block a user