In [1]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor
In [2]:
%matplotlib qt
sns.set_context('paper')
sns.set_style("dark", rc={'axes.facecolor': 'black', 'grid.color': 'red',
'grid.linestyle': '--',
'figure.facecolor': 'grey'})
In [95]:
sns.axes_style()
Out[95]:
In [234]:
plt.plot(np.arange(10))
plt.plot(np.arange(10, 0, -1))
Out[234]:
In [3]:
import matplotlib.finance as mf
In [5]:
hft = pd.read_hdf('HFT_SR_RM_MA_TA.hdf')
In [6]:
hft.head
Out[6]:
In [7]:
hft.items
Out[7]:
In [8]:
# output cols to csv
cols = hft.items
cols = np.array(cols).astype(str)
np.savetxt('cols.csv', cols, delimiter=',', fmt='%s')
In [9]:
hft.major_axis
Out[9]:
In [10]:
hft.minor_axis
Out[10]:
In [11]:
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 [12]:
mor_len
Out[12]:
In [13]:
rm = hft.minor_xs('RM0001')
type(rm)
Out[13]:
In [129]:
plt.figure(figsize=(20,10))
plt.plot(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].index,
rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'])
Out[129]:
In [15]:
rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last']
Out[15]:
In [16]:
print rm.ix['2015-11-23 15:00:00', ['open', 'close', 'volume', 'last', 'high', 'low', 'openInterest']]
In [17]:
ta = hft.minor_xs('MA0001')
print ta.ix['2015-11-23 15:00:00', ['open', 'close', 'volume', 'last', 'high', 'low', 'openInterest']]
In [18]:
rm.iloc[:5, :12]
Out[18]:
In [19]:
rm.iloc[:5, 12:24]
Out[19]:
In [20]:
rm.iloc[:5, 24:]
Out[20]:
In [21]:
# for col, ser in rm.iteritems():
# print col, ser.dtype, ser.std(axis=0)
# #ser.plot(marker='*', linestyle='-')
# #plt.show()
In [22]:
# rm_20 = rm.ix[night_len + mor_len + aftn_len + 1: night_len + mor_len + aftn_len +night_len + mor_len + aftn_len, :]
# for col, ser in rm_20.iteritems():
# print col, ser.dtype, ser.std(axis=0)
# print ser.mean(axis=0)
# #ser.plot(marker='*', linestyle='-')
# #plt.show()
In [125]:
rm.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00': 20000, :5]
Out[125]:
In [56]:
rm_index_str = rm.index.format()
In [113]:
rm_index_str = pd.Series(data=rm_index_str, index=rm.index)
In [114]:
rm_index_str
Out[114]:
In [135]:
rm_index_str2 = rm.index.strftime('%T')
In [138]:
rm_index_str2 = pd.Series(data=rm_index_str2, index=rm.index)
In [17]:
chg_Vol = pd.Series(data=rm.ix[:, 'volume'].diff(), index=rm.index)
chg_OpenInt = pd.Series(data=rm.ix[:, 'openInterest'].diff(), index=rm.index)
In [62]:
day_len
Out[62]:
In [61]:
len(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'])
Out[61]:
In [110]:
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(2,1,1)
ax.plot(rm.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00', 'last'].values, lw=0.5, color='#f5f112')
labels = rm_index_str[day_len: day_len*2 +1]
ax.set_xticklabels(labels)
ax1 = ax.twinx()
ax1.plot(rm.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00', 'openInterest'].values, lw=0.5, color='white')
# make ax and ax1 to be shown in the upper half of the figure
# ax.set_ylim(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].min()
# -
# (rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].max()
# - rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].min()),
# rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].max()
# )
# ax1.set_ylim(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].min()
# -
# (rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].max()
# - rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].min()),
# rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].max()
# )
# mf.volume_overlay2(ax2,
# rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'],
# chg_Vol.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00'],
# colorup='#77d879', colordown='#db3f3f', alpha=.4)
ax2 = fig.add_subplot(2,1,2)
ax2.plot(chg_Vol.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00'])
# make ax2 to be shown in the lowwer half of the figure
#ax2.set_ylim(0, chg_Vol.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00'].values.max()*2)
Out[110]:
In [189]:
def myplot(myprc, myopen, myvol, mylabels, fs=(15,10)):
fig = plt.figure(figsize=fs)
ax = fig.add_subplot(2,1,1)
ax.plot(myprc, lw=0.5, color='#f5f112')
#labels = rm_index_str[day_len: day_len*2 +1]
#ax.set_xticklabels(mylabels, fontsize=8, rotation=45)
ax1 = ax.twinx()
ax1.plot(myopen, lw=0.5, color='white')
# make ax and ax1 to be shown in the upper half of the figure
# ax.set_ylim(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].min()
# -
# (rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].max()
# - rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].min()),
# rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'last'].max()
# )
# ax1.set_ylim(rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].min()
# -
# (rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].max()
# - rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].min()),
# rm.ix['2015-11-22 21:00:00': '2015-11-23 15:00:00', 'openInterest'].max()
# )
ax2 = fig.add_subplot(2,1,2)
ax2.plot(myvol)
# make ax2 to be shown in the lowwer half of the figure
#ax2.set_ylim(0, chg_Vol.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00'].values.max()*2)
plt.show()
In [195]:
t1, t2 = '2015-11-22 21:00:01', '2015-11-23 15:00:00'
myplot(rm.ix[t1: t2, 'last'], rm.ix[t1: t2, 'openInterest'], chg_Vol.ix[t1: t2], rm_index_str[t1: t2], fs=(24,10))
In [214]:
t1, t2 = '2015-11-22 21:15:01', '2015-11-22 21:25:00'
myplot(rm.ix[t1: t2, 'last'].values,
rm.ix[t1: t2, 'openInterest'].values,
chg_Vol.ix[t1: t2].values,
rm_index_str2[t1: t2].values,
fs=(18,8))
rm.ix[t1: t2, ['TotalAskLot', 'TotalBidLot']].plot(figsize=(18,8))
rm.ix[t1: t2, ['askPrc_0', 'askPrc_1', 'askPrc_2', 'askPrc_3',
'askPrc_4', 'bidPrc_0', 'bidPrc_1', 'bidPrc_2', 'bidPrc_3',
'bidPrc_4', ]].plot(figsize=(18,8))
Out[214]:
In [181]:
rm.ix[t1: t2, ['askQty_0', 'askQty_1', 'askQty_2', 'askQty_3',
'askQty_4', 'bidQty_0', 'bidQty_1', 'bidQty_2', 'bidQty_3',
'bidQty_4']].plot(figsize=(18,8))
Out[181]:
In [202]:
t1, t2 = '2015-11-22 22:00:01', '2015-11-22 23:30:00'
myplot(rm.ix[t1: t2, 'last'],
rm.ix[t1: t2, 'openInterest'].values,
chg_Vol.ix[t1: t2].values,
rm_index_str2[t1: t2].values,
fs=(18,8))
rm.ix[t1: t2, ['TotalAskLot', 'TotalBidLot']].plot(figsize=(18,8))
rm.ix[t1: t2, ['askPrc_0', 'askPrc_1', 'askPrc_2', 'askPrc_3',
'askPrc_4', 'bidPrc_0', 'bidPrc_1', 'bidPrc_2', 'bidPrc_3',
'bidPrc_4', ]].plot(figsize=(18,8))
Out[202]:
In [22]:
def prc_total(df, myvol, t1, t2, fs=(15,10)):
fig = plt.figure(figsize=fs)
ax1 = fig.add_subplot(411)
ax1.plot(df.ix[t1: t2, 'last'], color='#f5f112', marker='*')
plt.hold()
ax3 = fig.add_subplot(413, sharex=ax1)
ax3.plot(df.ix[t1: t2, 'TotalBidLot'] - df.ix[t1: t2, 'TotalAskLot'], color='white')
# ax3.plot(df.ix[t1: t2, 'TotalAskLot'], lw=0.5, color='green')
ax2 = fig.add_subplot(412, sharex=ax1)
ax2.plot(np.zeros_like(myvol.ix[t1: t2].values), color='orange')
ax2.plot(myvol.ix[t1: t2], color='orange', marker='*')
ax2_2 = ax2.twinx()
ax2_2.plot(df.ix[t1: t2, 'openInterest'], color='white', lw=0.4, marker='*')
ax4 = fig.add_subplot(414, sharex=ax1)
ax4.plot(df.ix[t1: t2, ['bidQty_0', 'bidQty_1', 'bidQty_2', 'bidQty_3', 'bidQty_4']].sum(axis=1).values, color='red')
ax4.plot(df.ix[t1: t2, ['askQty_4', 'askQty_3', 'askQty_2', 'askQty_1', 'askQty_0']].sum(axis=1).values, color='green')
return fig
In [286]:
t1, t2 = '2015-11-22 22:00:01', '2015-11-22 23:30:00'
thefig = prc_total(rm, chg_Vol, t1, t2, fs=(20,16))
multi = MultiCursor(thefig.canvas, thefig.axes, color='c', lw=1)
thefig.show()
In [288]:
t1, t2 = '2015-11-22 21:15:01', '2015-11-22 21:25:00'
thefig = prc_total(rm, chg_Vol, t1, t2, fs=(20,16))
multi = MultiCursor(thefig.canvas, thefig.axes, color='c', lw=1)
thefig.show()
In [290]:
t1, t2 = '2015-11-23 11:00:01', '2015-11-23 14:20:00'
thefig = prc_total(rm, chg_Vol, t1, t2, fs=(20,16))
multi = MultiCursor(thefig.canvas, thefig.axes, color='c', lw=1)
thefig.show()
In [25]:
t1, t2 = '2015-12-16 21:00:01', '2015-12-17 15:00:00'
thefig = prc_total(rm, chg_Vol, t1, t2, fs=(20,16))
multi = MultiCursor(thefig.canvas, thefig.axes, color='c', lw=1)
thefig.show()
In [354]:
rm.ix['2015-11-22 21:00:01': '2015-11-23 15:00:00',:].ix[76070: 76120, ['volume', 'TotalAskLot',
'askQty_4', 'askQty_3', 'askQty_2', 'askQty_1', 'askPrc_1',
'askQty_0', 'askPrc_0', 'last', 'bidPrc_0', 'bidQty_0', 'bidQty_1', 'bidQty_2', 'bidQty_3', 'bidQty_4',
'TotalBidLot', ]]
Out[354]:
In [330]:
np.where(abs(rm.ix[:, 'last'].diff()) > 2)
Out[330]:
In [ ]:
t1, t2 = 0, 2700000
thefig = prc_total(rm, chg_Vol, t1, t2, fs=(20,16))
multi = MultiCursor(thefig.canvas, thefig.axes, color='c', lw=1)
thefig.show()