def getPairQuote(jqETFID, jqFutureID, sdate, unitETFVolumns): fMultiply= cfg.getFutureMultiply(jqFutureID) mdata= cfg.getMergeData(jqETFID, jqFutureID, sdate) mdata['bidp']= mdata.apply(lambda x:getMDPrice(x.b1_p, x.b2_p, x.b3_p, x.b4_p, x.b5_p, x.b1_v, x.b2_v, x.b3_v, x.b4_v, x.b5_v, unitETFVolumns), axis = 1) mdata['askp']= mdata.apply(lambda x:getMDPrice(x.a1_p, x.a2_p, x.a3_p, x.a4_p, x.a5_p, x.a1_v, x.a2_v, x.a3_v, x.a4_v, x.a5_v, unitETFVolumns), axis = 1) mdata= mdata[['bidp', 'askp', 'fb1_p', 'fa1_p']] mdata['lesf']= mdata['askp']*unitETFVolumns- mdata['fb1_p']* fMultiply mdata['self']= mdata['bidp']*unitETFVolumns- mdata['fa1_p']* fMultiply return mdata
def getPairStatInfo(jqETFID, jqFutureID, listTradeDay, nIndex, nParamDay= 5): listdata= [] for i in range(nParamDay): mdata= cfg.getMergeData(jqETFID, jqFutureID, listTradeDay[nIndex- 1- i]) listdata.append(mdata[['current', 'fcurrent']]) mdata= pd.concat(listdata) inid= mdata.mean() fMultiply= cfg.getFutureMultiply(jqFutureID) unitETFVolumns= np.round(inid['fcurrent']* fMultiply/ inid['current'], -2) mdata['basis']= mdata['current']*unitETFVolumns- mdata['fcurrent']* fMultiply basisMean= np.round( mdata['basis'].mean()) basisStd= np.round( mdata['basis'].std()) return [unitETFVolumns, basisMean, basisStd]
return pdTradeInfo, pdAllTrade if __name__ == '__main__': t0 = ti.time() jqFutureID = 'IC2009.CCFX' jqETFID = '510500.XSHG' listTradeDay = cfg.getTradeDays() nIndex = -1 nTestDay = 5 tradeThreshold = 1 positionThreshold = 5 levelThreshold = 8 nTradeSilence = 3 nParamDay = 5 mdata = cfg.getMergeData(jqETFID, jqFutureID, listTradeDay[nIndex]) statInfo = getPairStatInfo(jqETFID, jqFutureID, listTradeDay, nParamDay, nIndex) #unitETFVolumns, basisMean, basisStd print('day: ' + listTradeDay[nIndex]) print('unitETFVolumns: %d' % statInfo[0]) print('basisMean: %d' % statInfo[1]) print('basisStd: %d' % statInfo[2]) pqdata = getPairQuote(jqETFID, jqFutureID, listTradeDay[nIndex], statInfo[0]) iniPosition = pd.DataFrame(columns=('volume', 'price')) pdTrade, pdPosition, statInfo = simuDay(jqETFID, jqFutureID, listTradeDay, nIndex, nParamDay, tradeThreshold, positionThreshold, levelThreshold, nTradeSilence, iniPosition) print('day: %s, P&L: %0.2f' % (listTradeDay[nIndex], statInfo[-1])) pdTradeInfo, pdAllTrade = PairTradeStrategy(jqETFID, jqFutureID, nParamDay,