def run_trade_logic(self, symbol_info, raw_data, para_dic): MS = para_dic['MS'] MM = para_dic['MM'] ML = para_dic['ML'] raw_data['Unnamed: 0'] = range(raw_data.shape[0]) beginindex = raw_data.ix[0, 'Unnamed: 0'] raw_data['MA_Short'] = MA.calMA(raw_data['close'], MS) raw_data['MA_Mid'] = MA.calMA(raw_data['close'], MM) raw_data['MA_Long'] = MA.calMA(raw_data['close'], ML) raw_data['MA_True'], raw_data['MA_Cross'] = dfCross( raw_data, 'MA_Short', 'MA_Mid') raw_data['next_strtime'] = raw_data['strtime'].shift(-1).fillna( method='ffill') raw_data['next_open'] = raw_data['open'].shift(-1).fillna( method='ffill') raw_data['next_utc'] = raw_data['utc_time'].shift(-1).fillna( method='ffill') goldcrosslist = pd.DataFrame({ 'goldcrosstime': raw_data.loc[raw_data['MA_Cross'] == 1, 'next_strtime'] }) goldcrosslist['goldcrossutc'] = raw_data.loc[raw_data['MA_Cross'] == 1, 'next_utc'] goldcrosslist['goldcrossindex'] = raw_data.loc[ raw_data['MA_Cross'] == 1, 'Unnamed: 0'] - beginindex goldcrosslist['goldcrossprice'] = raw_data.loc[raw_data['MA_Cross'] == 1, 'next_open'] # 取出死叉点 deathcrosslist = pd.DataFrame({ 'deathcrosstime': raw_data.loc[raw_data['MA_Cross'] == -1, 'next_strtime'] }) deathcrosslist['deathcrossutc'] = raw_data.loc[raw_data['MA_Cross'] == -1, 'next_utc'] deathcrosslist['deathcrossindex'] = raw_data.loc[ raw_data['MA_Cross'] == -1, 'Unnamed: 0'] - beginindex deathcrosslist['deathcrossprice'] = raw_data.loc[ raw_data['MA_Cross'] == -1, 'next_open'] goldcrosslist = goldcrosslist.reset_index(drop=True) deathcrosslist = deathcrosslist.reset_index(drop=True) # 生成多仓序列(金叉在前,死叉在后) if goldcrosslist.ix[0, 'goldcrossindex'] < deathcrosslist.ix[ 0, 'deathcrossindex']: longcrosslist = pd.concat([goldcrosslist, deathcrosslist], axis=1) else: # 如果第一个死叉的序号在金叉前,则要将死叉往上移1格 longcrosslist = pd.concat( [goldcrosslist, deathcrosslist.shift(-1)], axis=1) longcrosslist = longcrosslist.set_index(pd.Index( longcrosslist['goldcrossindex']), drop=True) # 生成空仓序列(死叉在前,金叉在后) if deathcrosslist.ix[0, 'deathcrossindex'] < goldcrosslist.ix[ 0, 'goldcrossindex']: shortcrosslist = pd.concat([deathcrosslist, goldcrosslist], axis=1) else: # 如果第一个金叉的序号在死叉前,则要将金叉往上移1格 shortcrosslist = pd.concat( [deathcrosslist, goldcrosslist.shift(-1)], axis=1) shortcrosslist = shortcrosslist.set_index(pd.Index( shortcrosslist['deathcrossindex']), drop=True) # 取出开多序号和开空序号 openlongindex = raw_data.loc[ (raw_data['MA_Cross'] == 1) & (raw_data['MA_Short'] >= raw_data['MA_Long']) & (raw_data['MA_Mid'] >= raw_data['MA_Long'])].index openshortindex = raw_data.loc[ (raw_data['MA_Cross'] == -1) & (raw_data['MA_Short'] <= raw_data['MA_Long']) & (raw_data['MA_Mid'] <= raw_data['MA_Long'])].index # 从多仓序列中取出开多序号的内容,即为开多操作 longopr = longcrosslist.loc[openlongindex] longopr['tradetype'] = 1 longopr.rename(columns={ 'goldcrosstime': 'opentime', 'goldcrossutc': 'openutc', 'goldcrossindex': 'openindex', 'goldcrossprice': 'openprice', 'deathcrosstime': 'closetime', 'deathcrossutc': 'closeutc', 'deathcrossindex': 'closeindex', 'deathcrossprice': 'closeprice' }, inplace=True) # 从空仓序列中取出开空序号的内容,即为开空操作 shortopr = shortcrosslist.loc[openshortindex] shortopr['tradetype'] = -1 shortopr.rename(columns={ 'deathcrosstime': 'opentime', 'deathcrossutc': 'openutc', 'deathcrossindex': 'openindex', 'deathcrossprice': 'openprice', 'goldcrosstime': 'closetime', 'goldcrossutc': 'closeutc', 'goldcrossindex': 'closeindex', 'goldcrossprice': 'closeprice' }, inplace=True) # 结果分析 result = pd.concat([longopr, shortopr]) result = result.sort_index() result = result.reset_index(drop=True) # result.drop(result.shape[0] - 1, inplace=True) result = result.dropna() slip = symbol_info.getSlip() result['ret'] = ((result['closeprice'] - result['openprice']) * result['tradetype']) - slip result['ret_r'] = result['ret'] / result['openprice'] return result
def run_trade_logic(self, symbol_info, raw_data, para_dic): setname = para_dic['Setname'] para_n1 = para_dic['N1'] para_m1 = para_dic['M1'] para_m2 = para_dic['M2'] para_n = para_dic['N'] para_ema_n = para_dic['MaN'] """ LC := REF(CLOSE,1); BACKGROUNDSTYLE(1); RSI1:SMA(MAX(CLOSE-LC,0),N1,1)/SMA(ABS(CLOSE-LC),N1,1)*100; RMA1:EMA(RSI1,M1); RMA2:EMA(RMA1,M2); RSINEW:=RMA1-RMA2; X:= 2*EMA2(RSINEW,ROUND(N/2,0))-EMA2(RSINEW,N); HURSI:10*EMA2(X,ROUND(SQRT(N),0)),COLORSTICK; """ rsi1 = pd.Series(RSI.rsi(raw_data['close'], para_n1)) rsi_ema1 = MA.calEMA(rsi1, para_m1) rsi_ema2 = MA.calEMA(rsi_ema1, para_m2) rsi_new = rsi_ema1 - rsi_ema2 hull_rsi = MA.hull_ma(rsi_new, para_n) # raw_data['RSI1'] = rsi1 raw_data['RSI_EMA1'] = rsi_ema1 # raw_data['RSI_EMA2'] = rsi_ema2 # raw_data['RSI_NEW'] = rsi_new raw_data['HullRsi'] = hull_rsi raw_data['EMA'] = MA.calEMA(raw_data['close'], para_ema_n) raw_data['zero'] = 0 raw_data['Unnamed: 0'] = range(raw_data.shape[0]) # 计算M金叉和死叉 raw_data['HullRsi_True'], raw_data['HullRsi_Cross'] = dfCross( raw_data, 'HullRsi', 'zero') raw_data['next_strtime'] = raw_data['strtime'].shift(-1).fillna( method='ffill') raw_data['next_open'] = raw_data['open'].shift(-1).fillna( method='ffill') raw_data['next_utc'] = raw_data['utc_time'].shift(-1).fillna( method='ffill') #raw_data['next_index'] = raw_data['Unnamed: 0'].shift(-1).fillna(method='ffill') # ================================ 找出买卖点================================================ # 1.先找出SAR金叉的买卖点 # 2.找到结合判决条件的买点 # 3.从MA买点中滤出真实买卖点 # 取出金叉点 goldcrosslist = pd.DataFrame({ 'goldcrosstime': raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'next_strtime'] }) goldcrosslist['goldcrossutc'] = raw_data.loc[ raw_data['HullRsi_Cross'] == 1, 'next_utc'] goldcrosslist['goldcrossindex'] = raw_data.loc[ raw_data['HullRsi_Cross'] == 1, 'Unnamed: 0'] goldcrosslist['goldcrossprice'] = raw_data.loc[ raw_data['HullRsi_Cross'] == 1, 'next_open'] #goldcrosslist['goldcrossrsi'] = raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'RSI_EMA1'] # 取出死叉点 deathcrosslist = pd.DataFrame({ 'deathcrosstime': raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'next_strtime'] }) deathcrosslist['deathcrossutc'] = raw_data.loc[ raw_data['HullRsi_Cross'] == -1, 'next_utc'] deathcrosslist['deathcrossindex'] = raw_data.loc[ raw_data['HullRsi_Cross'] == -1, 'Unnamed: 0'] deathcrosslist['deathcrossprice'] = raw_data.loc[ raw_data['HullRsi_Cross'] == -1, 'next_open'] #deathcrosslist['deathcrossrsi'] = raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'RSI_EMA1'] goldcrosslist = goldcrosslist.reset_index(drop=True) deathcrosslist = deathcrosslist.reset_index(drop=True) # 生成多仓序列(金叉在前,死叉在后) if goldcrosslist.ix[0, 'goldcrossindex'] < deathcrosslist.ix[ 0, 'deathcrossindex']: longcrosslist = pd.concat([goldcrosslist, deathcrosslist], axis=1) else: # 如果第一个死叉的序号在金叉前,则要将死叉往上移1格 longcrosslist = pd.concat( [goldcrosslist, deathcrosslist.shift(-1)], axis=1) longcrosslist = longcrosslist.set_index(pd.Index( longcrosslist['goldcrossindex']), drop=True) # 生成空仓序列(死叉在前,金叉在后) if deathcrosslist.ix[0, 'deathcrossindex'] < goldcrosslist.ix[ 0, 'goldcrossindex']: shortcrosslist = pd.concat([deathcrosslist, goldcrosslist], axis=1) else: # 如果第一个金叉的序号在死叉前,则要将金叉往上移1格 shortcrosslist = pd.concat( [deathcrosslist, goldcrosslist.shift(-1)], axis=1) shortcrosslist = shortcrosslist.set_index(pd.Index( shortcrosslist['deathcrossindex']), drop=True) # 取出开多序号和开空序号 # openlongindex = raw_data.loc[ # (raw_data['HullRsi_Cross'] == 1) & (raw_data['RSI_EMA1'] < para_rsi1_up)].index # openshortindex = raw_data.loc[ # (raw_data['HullRsi_Cross'] == -1) & (raw_data['RSI_EMA1'] > para_rsi1_down)].index openlongindex = raw_data.loc[(raw_data['HullRsi_Cross'] == 1) & ( raw_data['close'] > raw_data['EMA'])].index openshortindex = raw_data.loc[(raw_data['HullRsi_Cross'] == -1) & ( raw_data['close'] < raw_data['EMA'])].index # 从多仓序列中取出开多序号的内容,即为开多操作 longopr = longcrosslist.loc[openlongindex] longopr['tradetype'] = 1 longopr.rename(columns={ 'goldcrosstime': 'opentime', 'goldcrossutc': 'openutc', 'goldcrossindex': 'openindex', 'goldcrossprice': 'openprice', 'goldcrossrsi': 'open_rsi', 'deathcrosstime': 'closetime', 'deathcrossutc': 'closeutc', 'deathcrossindex': 'closeindex', 'deathcrossprice': 'closeprice' }, inplace=True) # 从空仓序列中取出开空序号的内容,即为开空操作 shortopr = shortcrosslist.loc[openshortindex] shortopr['tradetype'] = -1 shortopr.rename(columns={ 'deathcrosstime': 'opentime', 'deathcrossutc': 'openutc', 'deathcrossindex': 'openindex', 'deathcrossprice': 'openprice', 'deathcrossrsi': 'open_rsi', 'goldcrosstime': 'closetime', 'goldcrossutc': 'closeutc', 'goldcrossindex': 'closeindex', 'goldcrossprice': 'closeprice' }, inplace=True) # 结果分析 result = pd.concat([longopr, shortopr]) result = result.sort_index() result = result.reset_index(drop=True) result = result.dropna() slip = symbol_info.getSlip() result['ret'] = ((result['closeprice'] - result['openprice']) * result['tradetype']) - slip result['ret_r'] = result['ret'] / result['openprice'] return result
def run_trade_logic(self, symbol_info, raw_data, para_dic): MACD_S = para_dic['MS'] MACD_L = para_dic['ML'] MACD_M = para_dic['MM'] MA_N = para_dic['MA'] # print setname raw_data['Unnamed: 0'] = range(raw_data.shape[0]) # 计算MACD macd = MA.calMACD(raw_data['close'], MACD_S, MACD_L, MACD_M) # 普通MACD #macd = MACD.hull_macd(raw_data['close'], MACD_S, MACD_L, MACD_M) # hull_macd raw_data['DIF'] = macd[0] raw_data['DEA'] = macd[1] raw_data['MA'] = MA.calEMA(raw_data['close'], MA_N) # raw_data['MA'] = MA.hull_ma(raw_data['close'], MA_N) raw_data['next_strtime'] = raw_data['strtime'].shift(-1).fillna( method='ffill') raw_data['next_open'] = raw_data['open'].shift(-1).fillna( method='ffill') raw_data['next_utc'] = raw_data['utc_time'].shift(-1).fillna( method='ffill') # 计算MACD的金叉和死叉 raw_data['MACD_True'], raw_data['MACD_Cross'] = dfCross( raw_data, 'DIF', 'DEA') # ================================ 找出买卖点================================================ goldcrosslist = pd.DataFrame({ 'goldcrosstime': raw_data.loc[raw_data['MACD_Cross'] == 1, 'next_strtime'] }) goldcrosslist['goldcrossutc'] = raw_data.loc[raw_data['MACD_Cross'] == 1, 'next_utc'] goldcrosslist['goldcrossindex'] = raw_data.loc[ raw_data['MACD_Cross'] == 1, 'Unnamed: 0'] goldcrosslist['goldcrossprice'] = raw_data.loc[ raw_data['MACD_Cross'] == 1, 'next_open'] # 取出死叉点 deathcrosslist = pd.DataFrame({ 'deathcrosstime': raw_data.loc[raw_data['MACD_Cross'] == -1, 'next_strtime'] }) deathcrosslist['deathcrossutc'] = raw_data.loc[ raw_data['MACD_Cross'] == -1, 'next_utc'] deathcrosslist['deathcrossindex'] = raw_data.loc[ raw_data['MACD_Cross'] == -1, 'Unnamed: 0'] deathcrosslist['deathcrossprice'] = raw_data.loc[ raw_data['MACD_Cross'] == -1, 'next_open'] goldcrosslist = goldcrosslist.reset_index(drop=True) deathcrosslist = deathcrosslist.reset_index(drop=True) # 生成多仓序列(金叉在前,死叉在后) if goldcrosslist.ix[0, 'goldcrossindex'] < deathcrosslist.ix[ 0, 'deathcrossindex']: longcrosslist = pd.concat([goldcrosslist, deathcrosslist], axis=1) else: # 如果第一个死叉的序号在金叉前,则要将死叉往上移1格 longcrosslist = pd.concat( [goldcrosslist, deathcrosslist.shift(-1)], axis=1) longcrosslist = longcrosslist.set_index(pd.Index( longcrosslist['goldcrossindex']), drop=True) # 生成空仓序列(死叉在前,金叉在后) if deathcrosslist.ix[0, 'deathcrossindex'] < goldcrosslist.ix[ 0, 'goldcrossindex']: shortcrosslist = pd.concat([deathcrosslist, goldcrosslist], axis=1) else: # 如果第一个金叉的序号在死叉前,则要将金叉往上移1格 shortcrosslist = pd.concat( [deathcrosslist, goldcrosslist.shift(-1)], axis=1) shortcrosslist = shortcrosslist.set_index(pd.Index( shortcrosslist['deathcrossindex']), drop=True) # 取出开多序号和开空序号 openlongindex = raw_data.loc[(raw_data['MACD_Cross'] == 1) & ( raw_data['close'] > raw_data['MA'])].index openshortindex = raw_data.loc[(raw_data['MACD_Cross'] == -1) & ( raw_data['close'] < raw_data['MA'])].index # 从多仓序列中取出开多序号的内容,即为开多操作 longopr = longcrosslist.loc[openlongindex] longopr['tradetype'] = 1 longopr.rename(columns={ 'goldcrosstime': 'opentime', 'goldcrossutc': 'openutc', 'goldcrossindex': 'openindex', 'goldcrossprice': 'openprice', 'deathcrosstime': 'closetime', 'deathcrossutc': 'closeutc', 'deathcrossindex': 'closeindex', 'deathcrossprice': 'closeprice' }, inplace=True) # 从空仓序列中取出开空序号的内容,即为开空操作 shortopr = shortcrosslist.loc[openshortindex] shortopr['tradetype'] = -1 shortopr.rename(columns={ 'deathcrosstime': 'opentime', 'deathcrossutc': 'openutc', 'deathcrossindex': 'openindex', 'deathcrossprice': 'openprice', 'goldcrosstime': 'closetime', 'goldcrossutc': 'closeutc', 'goldcrossindex': 'closeindex', 'goldcrossprice': 'closeprice' }, inplace=True) # 结果分析 result = pd.concat([longopr, shortopr]) result = result.sort_index() result = result.reset_index(drop=True) result = result.dropna() slip = symbol_info.getSlip() result['ret'] = ((result['closeprice'] - result['openprice']) * result['tradetype']) - slip result['ret_r'] = result['ret'] / result['openprice'] return result
def run_trade_logic(self, symbol_info, raw_data, para_dic): para_n1 = para_dic['N1'] para_m1 = para_dic['M1'] para_m2 = para_dic['M2'] para_n = para_dic['N'] para_tunnel_n = para_dic['TN'] rsi1 = pd.Series(RSI.rsi(raw_data['close'], para_n1)) rsi_ema1 = MA.calEMA(rsi1, para_m1) rsi_ema2 = MA.calEMA(rsi_ema1, para_m2) rsi_new = rsi_ema1 - rsi_ema2 hull_rsi = MA.hull_ma(rsi_new, para_n) raw_data['RSI_EMA1'] = rsi_ema1 raw_data['HullRsi'] = hull_rsi raw_data['TOP_EMA'] = MA.calEMA(raw_data['high'], para_tunnel_n) raw_data['BOTTOM_EMA'] = MA.calEMA(raw_data['low'], para_tunnel_n) raw_data['zero'] = 0 raw_data['Unnamed: 0'] = range(raw_data.shape[0]) # 计算M金叉和死叉 raw_data['HullRsi_True'], raw_data['HullRsi_Cross'] = dfCross(raw_data, 'HullRsi', 'zero') raw_data['next_strtime'] = raw_data['strtime'].shift(-1).fillna(method='ffill') raw_data['next_open'] = raw_data['open'].shift(-1).fillna(method='ffill') raw_data['next_utc'] = raw_data['utc_time'].shift(-1).fillna(method='ffill') # ================================ 找出买卖点================================================ # 1.先找出SAR金叉的买卖点 # 2.找到结合判决条件的买点 # 3.从MA买点中滤出真实买卖点 # 取出金叉点 goldcrosslist = pd.DataFrame({'goldcrosstime': raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'next_strtime']}) goldcrosslist['goldcrossutc'] = raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'next_utc'] goldcrosslist['goldcrossindex'] = raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'Unnamed: 0'] goldcrosslist['goldcrossprice'] = raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'next_open'] #goldcrosslist['goldcrossrsi'] = raw_data.loc[raw_data['HullRsi_Cross'] == 1, 'RSI_EMA1'] # 取出死叉点 deathcrosslist = pd.DataFrame({'deathcrosstime': raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'next_strtime']}) deathcrosslist['deathcrossutc'] = raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'next_utc'] deathcrosslist['deathcrossindex'] = raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'Unnamed: 0'] deathcrosslist['deathcrossprice'] = raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'next_open'] #deathcrosslist['deathcrossrsi'] = raw_data.loc[raw_data['HullRsi_Cross'] == -1, 'RSI_EMA1'] goldcrosslist = goldcrosslist.reset_index(drop=True) deathcrosslist = deathcrosslist.reset_index(drop=True) # 生成多仓序列(金叉在前,死叉在后) if goldcrosslist.ix[0, 'goldcrossindex'] < deathcrosslist.ix[0, 'deathcrossindex']: longcrosslist = pd.concat([goldcrosslist, deathcrosslist], axis=1) else: # 如果第一个死叉的序号在金叉前,则要将死叉往上移1格 longcrosslist = pd.concat([goldcrosslist, deathcrosslist.shift(-1)], axis=1) longcrosslist = longcrosslist.set_index(pd.Index(longcrosslist['goldcrossindex']), drop=True) # 生成空仓序列(死叉在前,金叉在后) if deathcrosslist.ix[0, 'deathcrossindex'] < goldcrosslist.ix[0, 'goldcrossindex']: shortcrosslist = pd.concat([deathcrosslist, goldcrosslist], axis=1) else: # 如果第一个金叉的序号在死叉前,则要将金叉往上移1格 shortcrosslist = pd.concat([deathcrosslist, goldcrosslist.shift(-1)], axis=1) shortcrosslist = shortcrosslist.set_index(pd.Index(shortcrosslist['deathcrossindex']), drop=True) # 取出开多序号和开空序号 # openlongindex = raw_data.loc[ # (raw_data['HullRsi_Cross'] == 1) & (raw_data['RSI_EMA1'] < para_rsi1_up)].index # openshortindex = raw_data.loc[ # (raw_data['HullRsi_Cross'] == -1) & (raw_data['RSI_EMA1'] > para_rsi1_down)].index openlongindex = raw_data.loc[(raw_data['HullRsi_Cross'] == 1) & (raw_data['close'] > raw_data['TOP_EMA'])].index openshortindex = raw_data.loc[(raw_data['HullRsi_Cross'] == -1) & (raw_data['close'] < raw_data['BOTTOM_EMA'])].index # 从多仓序列中取出开多序号的内容,即为开多操作 longopr = longcrosslist.loc[openlongindex] longopr['tradetype'] = 1 longopr.rename(columns={'goldcrosstime': 'opentime', 'goldcrossutc': 'openutc', 'goldcrossindex': 'openindex', 'goldcrossprice': 'openprice', 'goldcrossrsi': 'open_rsi', 'deathcrosstime': 'closetime', 'deathcrossutc': 'closeutc', 'deathcrossindex': 'closeindex', 'deathcrossprice': 'closeprice'}, inplace=True) # 从空仓序列中取出开空序号的内容,即为开空操作 shortopr = shortcrosslist.loc[openshortindex] shortopr['tradetype'] = -1 shortopr.rename(columns={'deathcrosstime': 'opentime', 'deathcrossutc': 'openutc', 'deathcrossindex': 'openindex', 'deathcrossprice': 'openprice', 'deathcrossrsi': 'open_rsi', 'goldcrosstime': 'closetime', 'goldcrossutc': 'closeutc', 'goldcrossindex': 'closeindex', 'goldcrossprice': 'closeprice'}, inplace=True) # 结果分析 result = pd.concat([longopr, shortopr]) result = result.sort_index() result = result.reset_index(drop=True) result = result.dropna() slip = symbol_info.getSlip() result['ret'] = ((result['closeprice'] - result['openprice']) * result['tradetype']) - slip result['ret_r'] = result['ret'] / result['openprice'] return result