In [1]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.finance as mf
from matplotlib.widgets import MultiCursor
In [2]:
import statsmodels.tsa.stattools as stt
import scipy.signal as sgn
In [3]:
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
In [4]:
from matplotlib.mlab import PCA
In [26]:
%matplotlib inline
In [5]:
sns.set_context('paper')
sns.set_style("darkgrid")
In [5]:
sns.set_context('paper')
sns.set_style("dark", rc={'axes.facecolor': 'black', 'grid.color': 'red',
'grid.linestyle': '--',
'figure.facecolor': 'grey'})
In [7]:
hft = pd.read_hdf('HFT_SR_RM_MA_TA.hdf')
In [8]:
ta = hft.minor_xs('TA0001')
In [197]:
sr = hft.minor_xs('SR0001')
In [9]:
rm = hft.minor_xs('RM0001')
type(rm)
Out[9]:
In [9]:
night_len = int(4*3600*2.5)
mor_len = int(4*3600*2.25)
aftn_len = int(4*3600*1.5)
day_len = night_len + mor_len + aftn_len + 3
In [11]:
dates1 = pd.date_range('2015-11-19 21:01:01', '2015-12-31 21:01:01', freq='D')
In [12]:
dates2 = pd.date_range('2015-11-20 14:59:59', '2015-12-31 14:59:59', freq='D')
In [14]:
type(dates1)
Out[14]:
In [17]:
dates1.weekday
Out[17]:
In [18]:
trade_day1 = dates1[dates1.weekday != 5]
trade_day2 = dates2[np.logical_and(dates2.weekday != 5, dates2.weekday != 6)]
trade_day1
Out[18]:
In [94]:
rm.ix[trade_day2, ['high', 'highLimit', 'low', 'lowLimit']]
Out[94]:
In [100]:
temp = rm.ix[trade_day2, 'high'] - rm.ix[trade_day2, 'highLimit'] >-3
temp
Out[100]:
In [102]:
rm.ix[trade_day2,:].ix[temp, 'last']
Out[102]:
In [136]:
temp[0]
Out[136]:
In [167]:
for pinzhong in hft.minor_axis:
print '\n\n#-------------------------------------'
print pinzhong
xx = hft.minor_xs(pinzhong)
toohigh = xx.ix[trade_day2, 'high'] - xx.ix[trade_day2, 'highLimit'] > -2
toolow = xx.ix[trade_day2, 'low'] - xx.ix[trade_day2, 'lowLimit'] < 2
print 'too high: \n'
if toohigh.any() == True:
print xx.ix[trade_day2,:].ix[toohigh, ['high', 'highLimit']]
temp = (xx.ix[trade_day2,:].ix[toohigh, :].index)[0]
high = xx.ix[trade_day2,:].ix[toohigh, 'highLimit']
#fig1 = plt.figure(figsize=(15,10))
#ax1 = fig1.add_subplot(111)
xx.ix[temp - pd.Timedelta(18, unit='h'): temp, 'last'].plot(figsize=(15,10))
plt.hlines(high, temp - pd.Timedelta(18, unit='h'), temp, colors='r', linestyles='-')
plt.show()
print 'too low: \n'
if toolow.any() == True:
print xx.ix[trade_day2,:].ix[toolow, ['low', 'lowLimit']]
temp = (xx.ix[trade_day2,:].ix[toolow, :].index)[0]
low = xx.ix[trade_day2,:].ix[toolow, 'lowLimit']
#fig2 = plt.figure(figsize=(15,10))
#ax2 = fig2.add_subplot(111)
xx.ix[temp - pd.Timedelta(18, unit='h'): temp, 'last'].plot(figsize=(15,10))
plt.hlines(low, temp - pd.Timedelta(18, unit='h'), temp, colors='g', linestyles='-')
plt.show()
plt.show()
we can see that SR has no zhangting or dieting
In [10]:
ta.index[day_len*10 + 9]
Out[10]:
In [11]:
#------------------ ta_10day is my training dataset
ta_10day = ta.ix[:day_len*10 + 10, :]
In [12]:
def Letitforward(df, forwardnum):
df2 = df.shift(-forwardnum) - df
df2.dropna(inplace=True)
return df2
In [13]:
forward_ticks = 40
In [21]:
ta_10day_pm =Letitforward(ta_10day.ix[:, 'last'], forward_ticks)
In [17]:
plt.plot(ta_10day_pm)
plt.show()
Out[17]:
In [22]:
#----------------------------exclude last 36 ticks before ending
last_44_boolean = np.logical_and.reduce((ta_10day_pm.index.hour >= 14,
ta_10day_pm.index.minute >= 59,
ta_10day_pm.index.second >= 49))
last_boolean = ta_10day_pm.index.hour == 15
In [32]:
ta_10day_pm.ix[np.logical_or(last_44_boolean, last_boolean)]
Out[32]:
In [35]:
outlier_boolean = abs(ta_10day_pm) > 10
In [38]:
ta_10day_pm.ix[outlier_boolean].plot()
Out[38]:
In [23]:
ta_10day_pm = ta_10day_pm.ix[np.logical_not(np.logical_or(last_44_boolean, last_boolean))]
#ta_10day_pm.plot(figsize=(18,10))
In [24]:
pm_index = ta_10day_pm.index
In [25]:
ta_10day_last = ta_10day.ix[:, 'last']
In [ ]:
ta_10day_last_log =
In [28]:
res = sm.tsa.dseasonal_decompose(ta_10day_last)
res.plot()
In [ ]: