Add the current date to the volatility regime filter in order to support decisions on whether to trade prior to the market open

This commit is contained in:
moshferatu 2024-02-06 09:17:18 -08:00
parent 626608f1f6
commit 21934e9e15

View File

@ -1,7 +1,7 @@
from datetime import datetime, timedelta
from dotenv import load_dotenv
from os import getenv
from pandas import DataFrame, Series
from pandas import concat, DataFrame, Series
from database.ohlc import ohlc
@ -17,15 +17,34 @@ class VolatilityRegimeFilter(BacktestFilter):
def filter(self) -> DataFrame:
data_start_date = datetime.strptime(getenv('OPTION_DATA_START_DATE'), '%Y-%m-%d')
now = datetime.now()
vix_data = ohlc(
symbol = 'VIX.XO',
timeframe = '1d',
start_date = data_start_date - timedelta(weeks = 52),
end_date = datetime.now()
end_date = now
)
vix_data.rename(columns = {'Timestamp': 'Date'}, inplace = True)
vix_data['Date'] = vix_data['Date']
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Filtering is based on the previous day's close, so the current date can be included even though the
data may not be availble yet.
This allows for utilizing the filter in live trading to decide whether to trade prior to market open.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vix_data = concat([
vix_data,
DataFrame({
'Date': [datetime.combine(now, datetime.min.time())],
'Open': [0.0],
'High': [0.0],
'Low': [0.0],
'Close': [0.0],
'Volume': [0.0]
})],
ignore_index = True
)
percent_rank = lambda x: Series(x).rank(pct = True).iloc[-1]
vix_data['Percent Rank'] = vix_data['Close'].shift(1).rolling(window = 252).apply(percent_rank)