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]
def simuDay(jqETFID, jqFutureID, listTradeDay, nIndex, tradeParam, iniPosition): pdTrade= pd.DataFrame(columns=('time', 'symbol', 'type', 'volume', 'price')) statInfo= getPairStatInfo(jqETFID, jqFutureID, listTradeDay, nIndex, tradeParam['nParamDay']) pqdata= getPairQuote(jqETFID, jqFutureID, listTradeDay[nIndex], statInfo[0]) if not 'future' in iniPosition.index: iniPosition.loc['future']= [0,0] if not 'etf' in iniPosition.index: iniPosition.loc['etf']= [0,0] fposition= -iniPosition.loc['future', 'volume'] etfPosition= iniPosition.loc['etf', 'volume'] diffETF= statInfo[0]* fposition- etfPosition if diffETF!= 0: row= pqdata.iloc[0] if diffETF> 0.1: pdTrade= pdTrade.append({'time': row.name, 'symbol': jqETFID, 'type': 'etf', 'volume': diffETF, 'price': row['askp']}, ignore_index=True) elif diffETF< -0.1: pdTrade= pdTrade.append({'time': row.name, 'symbol': jqETFID, 'type': 'etf', 'volume': diffETF, 'price': row['bidp']}, ignore_index=True) nNearTrade= 0 tradeSignal= 'hold' for time, row in pqdata.iterrows(): #check if near trade if nNearTrade> 0.1: nNearTrade-= 1 elif tradeSignal!= 'hold':#check tradeSignal nNearTrade= tradeParam['nTradeSilence'] if tradeSignal== 'buy': pdTrade= pdTrade.append({'time': time, 'symbol': jqETFID, 'type': 'etf', 'volume': statInfo[0], 'price': row['askp']}, ignore_index=True) pdTrade= pdTrade.append({'time': time, 'symbol': jqFutureID, 'type': 'future', 'volume': -1, 'price': row['fb1_p']}, ignore_index=True) fposition+= 1 elif tradeSignal== 'sell': pdTrade= pdTrade.append({'time': time, 'symbol': jqETFID, 'type': 'etf', 'volume': -statInfo[0], 'price': row['bidp']}, ignore_index=True) pdTrade= pdTrade.append({'time': time, 'symbol': jqFutureID, 'type': 'future', 'volume': 1, 'price': row['fa1_p']}, ignore_index=True) fposition-= 1 tradeSignal= 'hold' else:#check trade tradeSignal= checkTrade(row, statInfo, tradeParam['tradeThreshold'], tradeParam['positionThreshold'], tradeParam['levelThreshold'], fposition) pdPosition= pdTrade.groupby(['type']).agg({'volume':'sum', 'price':'count'}) if not 'future' in pdPosition.index: pdPosition.loc['future']= [0,0] if not 'etf' in pdPosition.index: pdPosition.loc['etf']= [0,0] pdPosition['volume']= pdPosition['volume'].add(iniPosition['volume']) statInfo.append(pdPosition.loc['future', 'volume']) row= pqdata.iloc[-1] etfClose= (row.bidp+ row.askp) /2 futureClose= (row.fb1_p+ row.fa1_p) /2 pdPosition.loc['etf', 'price']= etfClose pdPosition.loc['future', 'price']= futureClose fMultiply= cfg.getFutureMultiply(jqFutureID) nintraDayTrade= 0 PL= etfPosition* (etfClose- iniPosition.loc['etf', 'price'])+ iniPosition.loc['future', 'volume']* (futureClose- iniPosition.loc['future', 'price'])* fMultiply if 'future' in pdTrade.type.values: gdata= pdTrade.groupby(['type', 'volume']).agg({'price':'mean','time':'count'}) nintraDayTrade= gdata.loc[('future',slice(None)),'time'].max() for index, row in gdata.iterrows(): if index[0]== 'etf': PL+= index[1]* row.time* (etfClose- row.price) elif index[0]== 'future': PL+= index[1]* row.time* (futureClose- row.price)* fMultiply statInfo.append(nintraDayTrade) statInfo.append(PL) return pdPosition, statInfo