Skip to content

Instantly share code, notes, and snippets.

@esahione
Created February 5, 2018 14:20
Show Gist options
  • Save esahione/3f57317ee716cc5a9e0578049f8418e5 to your computer and use it in GitHub Desktop.
Save esahione/3f57317ee716cc5a9e0578049f8418e5 to your computer and use it in GitHub Desktop.
calc_intraday_trend
import numpy as np
from pandas.tseries.offsets import *
from collections import defaultdict
import statsmodels.api as sm
result_coefficient = defaultdict(lambda: defaultdict(dict))
result_tvalues = defaultdict(lambda: defaultdict(dict))
for x, intraday_data in dataset.items():
intraday_data = intraday_data.set_index('time')
days = intraday_data.resample('B').count().volume
for day in days.index:
if days[day] < 10:
continue
eod = day + DateOffset(hours=23, minutes=59, seconds=59)
todays_data = intraday_data.loc[day:eod].close
todays_data = todays_data.pct_change()
todays_data = pd.DataFrame({'Y': todays_data,
'X = Y-1': todays_data.shift(1),
'X = Y-2': todays_data.shift(2),
'X = Y-3': todays_data.shift(3),
}).dropna()
for regressor in ['X = Y-1', 'X = Y-2', 'X = Y-3']:
model = sm.OLS(todays_data['Y'], todays_data[regressor])
model_result = model.fit()
result_coefficient[x][(regressor, day)] = model_result.params[regressor]
result_tvalues[x][(regressor, day)] = model_result.tvalues[regressor]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment