期货:高频日内交易
作者:互联网
高频交易基于低手续费,且交易判断成功的概率远大于失败的基础上的。
朴素的思路是判断拐点,在拐点处产生快速交易。
首先导入某一期货品种(分钟K线).
df = pd.read_csv("JqData/RB2205.csv", index_col='date',parse_dates=['date'])[['open','close','low','high']]
分K线走势是这样子的:
df = df[:120] df[['close']].plot() plt.show()
找到拐点,并标记出来。(注意:拐点判断有延时性,交易具有延时性)
算法;
求导(dx=1),根据本点的前两点,和上一拐点的性质和距离判断前一点是否是拐点(knee point),本点是否买入或卖出
# Create new columns to store the knee point flag, type, and open direction. df['kpFlag'] = np.NaN df['kpType'] = np.NaN df['oDirection'] = np.NaN prevKpClose = 0 minDistance = 3 prevKpType = KneePointType.Unknown for i in range(2, len(df)): if (df['dy1'][i-1] >=0) and (df['dy1'][i] < 0): # flat/up -> down df.loc[df.index[i-1], 'kpType'] = KneePointType.Down.name if (prevKpType != KneePointType.Down): df.loc[df.index[i-1], 'kpFlag'] = True prevKpType = KneePointType.Down if (df['close'][i-1] - prevKpClose >= minDistance * 1): df.loc[df.index[i], 'oDirection'] = OpenDirection.Sell.name prevKpClose = df.loc[df.index[i-1], 'close'] elif (df['dy1'][i-1] <=0) and (df['dy1'][i] > 0): # flat/down -> up df.loc[df.index[i-1], 'kpType'] = KneePointType.Up.name if (prevKpType != KneePointType.Up): df.loc[df.index[i-1], 'kpFlag'] = True prevKpType = KneePointType.Up if (df['close'][i-1] - prevKpClose <= minDistance * -1): df.loc[df.index[i], 'oDirection'] = OpenDirection.Buy.name prevKpClose = df.loc[df.index[i-1], 'close']
把拐点(K),买点(B),和卖点(S)图形化显示一下:
from matplotlib import pylab z = df[['close','kpFlag']] z.plot(marker='o') # Plot the data, with a marker set. #pylab.xlim(0,3) # Change the axes limits so that we can see the annotations. #pylab.ylim(0,4) plt.rcParams["figure.figsize"] = (36,20) ax = pylab.gca() for i in z.index: # iterate through each index in the dataframe v = df.loc[i, 'close'] f = df.loc[i, 'kpFlag'] d = df.loc[i, 'oDirection'] if f == True: ax.annotate('K',xy=(i,v), bbox=dict(boxstyle='round,pad=0.2', fc='pink', alpha=0.5),) if d == OpenDirection.Buy.name: ax.annotate('B',xy=(i,v),bbox=dict(boxstyle='round,pad=0.2', fc='red', alpha=0.5),fontsize=20) if d == OpenDirection.Sell.name: ax.annotate('S',xy=(i,v), bbox=dict(boxstyle='round,pad=0.2', fc='green', alpha=0.5), fontsize=20)
看起来这个震荡行情下的表现还不错,单边行情中还需要做一些微调
标签:loc,name,index,日内,KneePointType,df,期货,close,高频 来源: https://www.cnblogs.com/fdyang/p/16203865.html