def cal_signal_(df0, df1, df2, strat_time, end_time, cs0, all_day=0): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数 tp_n, zuhe_limit, max_stop_win = cs0[-3:] # 配置临时变量 open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan stop_loss_price = np.nan for i in range(2, df0.shape[0]): #类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:9:15-16:29) trading_time_con = ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) if all_day > 0: trading_time_con |= True # 交易所日盘,开放 if trading_time_con: # 快捷变量 open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] close_bar = df0[i][4] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, close_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): # inner_trade_nums = 1 max_log_high = high_bar min_log_low = low_bar else: max_log_high = max(high_bar, max_log_high) min_log_low = min(low_bar, min_log_low) tp = df2[i][1] tp2 = df2[i - 1][1] zhyinzi = df2[i][2] ma_xiao = df2[i][3] ma_xiao2 = df2[i - 1][3] stop_line = df2[i][4] # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) & (df0[i, 6] <= end_time[0])) we_trade_con &=(((df0[i, 7] == strat_time[1]) & (df0[i, 8] > strat_time[2])) | \ ((df0[i, 7] == end_time[1]) & (df0[i, 8] < end_time[2])) |\ ((df0[i, 7] > strat_time[1]) & (df0[i, 7] < end_time[1]))) if all_day > 0: we_trade_con |= True # 信号生成 if we_trade_con: if now_pos == 0: long_condition = (tp > tp2) and (tp >= tp_n) and ( zhyinzi > zuhe_limit) # long_condition &= (close_bar>ma_xiao)and(last_close<ma_xiao2) #向上方向 short_condition = False #(ma_xiao < ma_zhong) and (ma_zhong < ma_da) if long_condition: df1[i][1] = 1 #buy signal max_log_high = high_bar # 空:突破最低线。 elif short_condition: min_log_low = low_bar df1[i][1] = -1 elif now_pos > 0: stop_loss_price = stop_line if ( max_log_high - now_open_prcie < 80) else max( now_open_prcie + 20, stop_line) stop_loss_con = (close_bar < stop_loss_price) and ( last_close >= stop_loss_price) stop_win_con = ( (max_log_high - now_open_prcie > max_stop_win)) and ( close_bar < ma_xiao) and (last_close >= ma_xiao2) close_pos = False if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 elif now_pos < 0: stop_loss_price = min_log_low stop_loss_con = (close_bar > stop_loss_price) and ( last_close <= stop_loss_price) stop_win_con = False #((close_bar < stop_win_price)) and (last_close >= stop_win_price) close_pos = False #(close_bar >me ) and (last_close <= me) if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: #非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 df2[i, 4] = stop_loss_price df2[i, 6] = min_log_low df2[i, 7] = max_log_high res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数 da, zhong, xiao, stop_n, max_stop_win = cs0 # 配置临时变量 open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan stop_loss_price = np.nan for i in range(2, df0.shape[0]): #类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:9:15-16:29) # trading_time_con = ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ # ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ # ((df0[i, 7] > 9) and (df0[i, 7] < 16)) trading_time_con = True # 交易所日盘,开放 if trading_time_con: # 快捷变量 open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] clsoe_bar = df0[i][4] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, clsoe_bar, last_close, sxf=1, slip=1) # === 当日变量加载 max_log_high = max(high_bar, df2[i - 1, 7]) min_log_low = min(low_bar, df2[i - 1, 6]) ma_da = df2[i][1] ma_da2 = df2[i - 1][1] ma_zhong = df2[i][2] ma_zhong2 = df2[i - 1][2] ma_xiao = df2[i][3] ma_xiao2 = df2[i - 1][3] if now_pos == 0: long_condition = (ma_xiao > ma_zhong) and (ma_xiao2 <= ma_zhong2) long_condition &= (ma_xiao > ma_da) # long_condition &= (clsoe_bar > ma_zhong)and(last_close <= ma_zhong2) #突破中线 short_condition = False #(ma_xiao < ma_zhong) and (ma_zhong < ma_da) if long_condition: df1[i][1] = 1 #buy signal max_log_high = high_bar # 空:突破最低线。 elif short_condition: min_log_low = low_bar df1[i][1] = -1 elif now_pos > 0: stop_loss_price = now_open_prcie - stop_n if ( max_log_high - now_open_prcie < stop_n) else now_open_prcie + 50 stop_loss_con = (clsoe_bar < stop_loss_price) and ( last_close >= stop_loss_price) stop_win_con = ( (max_log_high - now_open_prcie > max_stop_win)) and ( clsoe_bar < ma_xiao) and (last_close >= ma_xiao2) close_pos = (ma_xiao < ma_zhong) if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 elif now_pos < 0: stop_loss_price = min_log_low + stop_n stop_loss_con = (clsoe_bar > stop_loss_price) and ( last_close <= stop_loss_price) stop_win_con = False #((clsoe_bar < stop_win_price)) and (last_close >= stop_win_price) close_pos = False #(clsoe_bar >me ) and (last_close <= me) if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 # 记录指标 绘图等等 df2[i, 4] = stop_loss_price df2[i, 6] = min_log_low df2[i, 7] = max_log_high res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 'atr_day', '移动止赢', 'n_atr', '小均线', '中均线', '大均线', '缠绕指标'] # 6 7 8 9 10 11 12 # 配置参数:止盈,ma_len(atr),atr_倍数,交易次数 stop_win_n, ma_xiao_n,ma_zhong_n,ma_da_n,ma_day_da_n,cr_num, atr_sw_n = cs0 # 配置临时变量 inner_trade_nums = 1000 open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan log_stop_win = 0 for i in range(10, df0.shape[0]): # 类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:2:30-16:20) trading_time_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == strat_time[1]) and (df0[i, 8] > strat_time[2])) or \ ((df0[i, 7] == end_time[1]) and (df0[i, 8] < end_time[2])) or \ ((df0[i, 7] > strat_time[1]) and (df0[i, 7] < end_time[1])) # 交易所日盘,开放 if trading_time_con: # 快捷变量 open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] close_bar = df0[i][4] atr_n = df2[i][8]*atr_sw_n xiao_ma = df2[i][9] zhong_ma = df2[i][10] da_ma = df2[i][11] chanraoindex = df2[i][12] day_ma = df2[i][13] last_day_ma = df2[i-1][13] last_da = df2[i-1][11] last_zhong = df2[i-1][10] last_xiao = df2[i-1][9] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, close_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): inner_trade_nums = 100 max_log_high = high_bar min_log_low = low_bar log_stop_win = 0 else: max_log_high = max(max(open_bar,close_bar), max_log_high) min_log_low = min(min(open_bar,close_bar), min_log_low) if max_log_high > (now_open_prcie + stop_win_n): stop_loss_price = xiao_ma elif max_log_high > (now_open_prcie + stop_win_n): stop_loss_price = 0.5*(xiao_ma+zhong_ma) else: stop_loss_price = max(da_ma,(now_open_prcie - atr_n)) log_stop_win = max(log_stop_win,stop_loss_price) if we_trade_con: if now_pos == 0: log_stop_win = (da_ma - atr_n) long_condition = (xiao_ma > zhong_ma)and(last_xiao <= last_zhong)and( close_bar > da_ma) #小中金叉 long_condition&= (last_zhong<zhong_ma) #大周期均是向上 long_condition &= (chanraoindex < cr_num) #缠绕条件 long_condition &= (last_day_ma < day_ma) #大周期条件 if long_condition and inner_trade_nums > 0: inner_trade_nums -= 1 max_log_high = high_bar df1[i][1] = 1 elif now_pos > 0: stop_win_con = (close_bar < log_stop_win) stop_loss_con = False close_pos = False if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: # 非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 # ['开仓线','atr_day','小均线'] df2[i, 2] = log_stop_win df2[i, 3] = min_log_low df2[i, 4] = max_log_high # df2[i, 7] = log_stop_win res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0, sxf=1, slip=1): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数:止盈,ma_len(atr),atr_倍数,交易次数 stop_win_n, ma_len, da_ma_n,dazhouqi,sw_atrn,shift_n = cs0 trade_inner_nums =100 # 配置临时变量 inner_trade_nums = trade_inner_nums open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan max_sp = np.nan for i in range(10, df0.shape[0]): # 类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:9:30-16:00) trading_time_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == strat_time[1]) and (df0[i, 8] > strat_time[2])) or \ ((df0[i, 7] == end_time[1]) and (df0[i, 8] < end_time[2])) or \ ((df0[i, 7] > strat_time[1]) and (df0[i, 7] < end_time[1])) # 交易所日盘,开放 if trading_time_con: # 快捷变量 '6=小均线', 'n_atr','8移动止赢','9大均线','10max_n' open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] clsoe_bar = df0[i][4] swin_atr_n = df2[i-1][7] * sw_atrn ma_xiao = df2[i][6] da_ma = df2[i][9] max_n_p = df2[i-1][10] up_1 = df2[i-1][11] dn_1 = df2[i - int(shift_n)][12] big_zq = df2[i-1][12] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, clsoe_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): inner_trade_nums = trade_inner_nums max_log_high = high_bar min_log_low = low_bar else: max_log_high = max(max(open_bar,clsoe_bar), max_log_high) min_log_low = min(min(open_bar,clsoe_bar), min_log_low) stop_win_price = now_open_prcie - stop_win_n moving_swin_price = np.nan # 信号生成 if we_trade_con: if now_pos == 0: long_condition = (clsoe_bar < dn_1) and (last_close >= dn_1) and (ma_xiao<da_ma) and (ma_xiao<big_zq) if long_condition and inner_trade_nums > 0: inner_trade_nums -= 1 df1[i][1] = -1 max_sp = ma_xiao+swin_atr_n elif now_pos < 0: stop_loss_con = False if min_log_low < stop_win_price: moving_swin_price = ma_xiao else: moving_swin_price = ma_xiao+swin_atr_n max_sp = min(moving_swin_price,max_sp) stop_win_con = (clsoe_bar > max_sp) close_pos = False if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: # 非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 # ['开仓线','atr_day','小均线'] # df2[i, 1] = #stop_loss_price df2[i, 2] = stop_win_price df2[i, 3] = min_log_low df2[i, 4] = max_log_high df2[i, 8] = max_sp res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0, sxf=1, slip=1): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数:止盈,ma_len(atr),atr_倍数,交易次数 stop_win_n, ma_len, da_ma_n,sw_atrn,trade_inner_nums = cs0 # 配置临时变量 inner_trade_nums = trade_inner_nums min_n_p0 = 0 open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan for i in range(10, df0.shape[0]): # 类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:9:30-16:00) trading_time_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == strat_time[1]) and (df0[i, 8] > strat_time[2])) or \ ((df0[i, 7] == end_time[1]) and (df0[i, 8] < end_time[2])) or \ ((df0[i, 7] > strat_time[1]) and (df0[i, 7] < end_time[1])) # 交易所日盘,开放 if trading_time_con: # 快捷变量 open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] clsoe_bar = df0[i][4] swin_atr_n = df2[i-1][8] * sw_atrn ma_xiao = df2[i][7] da_ma = df2[i][10] da_ma_1 = df2[i-1][10] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, clsoe_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): inner_trade_nums = trade_inner_nums max_log_high = high_bar min_log_low = low_bar else: max_log_high = max(max(open_bar,clsoe_bar), max_log_high) min_log_low = min(min(open_bar,clsoe_bar), min_log_low) inner_low = df2[i - 1][3] if min_log_low < (now_open_prcie - stop_win_n): stop_loss_price = ma_xiao+swin_atr_n stop_win_con = (clsoe_bar > ma_xiao+swin_atr_n) elif min_log_low < now_open_prcie - stop_win_n * 0.5: stop_loss_price = ma_xiao +swin_atr_n*0.5 stop_win_con = (clsoe_bar > ma_xiao+swin_atr_n*0.5) else: stop_loss_price = (ma_xiao ) stop_win_con = (clsoe_bar > ma_xiao ) moving_swin_price = np.nan min_n_p = df2[i - 1][11] if we_trade_con: if now_pos == 0: short_condition = (clsoe_bar < min_n_p) and (last_close >= min_n_p) and (min_n_p < ma_xiao) short_condition |=( (clsoe_bar<da_ma) or (da_ma <= da_ma_1 ) ) # 空:突破最低线。 if short_condition and inner_trade_nums > 0: inner_trade_nums -= 1 df1[i][1] = -1 elif now_pos < 0: stop_loss_con = False close_pos = False if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: # 非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 # ['开仓线','atr_day','小均线'] df2[i, 2] = stop_loss_price df2[i, 3] = min_log_low df2[i, 4] = max_log_high df2[i, 9] = moving_swin_price df2[i - 1][11] = min_n_p res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0, sxf=1, slip=1): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数:止盈,ma_len(atr),atr_倍数,交易次数 stop_win_n, ma_len, da_ma_n, da_zq, sw_atrn, shift_n = cs0 trade_inner_nums = 1000 # 配置临时变量 inner_trade_nums = trade_inner_nums open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan max_sp = np.nan for i in range(10, df0.shape[0]): # 类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:9:30-16:00) trading_time_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == strat_time[1]) and (df0[i, 8] > strat_time[2])) or \ ((df0[i, 7] == end_time[1]) and (df0[i, 8] < end_time[2])) or \ ((df0[i, 7] > strat_time[1]) and (df0[i, 7] < end_time[1])) # 交易所日盘,开放 if trading_time_con: # '小均线', 'n_atr','移动止赢','大均线','max_n','up','dn','大周期','大均线','zsc_up','zsc_dn','zsc_ma','zsc0'] # 6'小均线', 'n_atr', '移动止赢', '大均线', 'max_n', # 11'up', 'dn', '大周期', '大均线2', 'zsc_up', # 16'zsc_dn', 'zsc_ma', 'zsc0' open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] clsoe_bar = df0[i][4] swin_atr_n = df2[i - 1][7] * sw_atrn ma_xiao = df2[i][6] da_ma = df2[i][9] dazhouqi = df2[i][13] da_ma2 = df2[i][14] z_up = df2[i][15] z_up_1 = df2[i - 1][15] z_dn = df2[i][16] z_me = df2[i][17] z_zsc0 = df2[i][18] z_zsc0_1 = df2[i - 1][18] # print(z_zsc0,z_up) # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, clsoe_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): inner_trade_nums = trade_inner_nums max_log_high = high_bar min_log_low = low_bar else: max_log_high = max(max(open_bar, clsoe_bar), max_log_high) min_log_low = min(min(open_bar, clsoe_bar), min_log_low) inner_low = df2[i - 1][3] stop_win_price = now_open_prcie + stop_win_n moving_swin_price = np.nan # 信号生成 if we_trade_con: if now_pos == 0: long_condition = (z_zsc0 > z_up_1) and ( z_zsc0_1 <= z_up_1) and (clsoe_bar > dazhouqi) and (da_ma2 < da_ma) if long_condition and inner_trade_nums > 0: inner_trade_nums -= 1 df1[i][1] = 1 max_sp = max_sp - swin_atr_n elif now_pos > 0: stop_loss_con = False close_pos = False if max_log_high > stop_win_price: moving_swin_price = ma_xiao else: moving_swin_price = ma_xiao - swin_atr_n stop_win_con = (clsoe_bar < moving_swin_price) if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: # 非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 # ['开仓线','atr_day','小均线'] # df2[i, 1] = #stop_loss_price df2[i, 2] = stop_win_price df2[i, 3] = min_log_low df2[i, 4] = max_log_high df2[i, 8] = moving_swin_price res0 = cal_tongji(df_input=df1) res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0
def cal_signal_(df0, df1, df2, strat_time, end_time, cs0): ''' # df0 == 原始time,ohlcc,:np.array :['candle_begin_time', 'open', 'high', 'low', 'close', 'volume', 'days', 'huors', 'minutes'] # df1 == 信号统计数据列:np.array :['candle_begin_time','signal', 'pos', 'opne_price', 'per_lr', 'sl'] # df2 == 指标列:np.array ''' # 配置参数:止盈,ma_len(atr),atr_倍数,交易次数 stop_win_n, ma_len,big_ma, base_line,extend_n = cs0 # 配置临时变量 inner_trade_nums = 1000 open_pos_size = 1 max_log_high = np.nan min_log_low = np.nan for i in range(10, df0.shape[0]): # 类似于vnpy:on_bar # 交易时间判断:日:1-28,and (时,分:2:30-16:20) trading_time_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == 9) and (df0[i, 8] >= 15)) or \ ((df0[i, 7] == 16) and (df0[i, 8] <= 29)) or \ ((df0[i, 7] > 9) and (df0[i, 7] < 16)) # 我们交易时间 we_trade_con = ((df0[i, 6] >= strat_time[0]) and (df0[i, 6] <= end_time[0])) and \ ((df0[i, 7] == strat_time[1]) and (df0[i, 8] > strat_time[2])) or \ ((df0[i, 7] == end_time[1]) and (df0[i, 8] < end_time[2])) or \ ((df0[i, 7] > strat_time[1]) and (df0[i, 7] < end_time[1])) # 交易所日盘,开放 if trading_time_con: # 快捷变量 open_bar = df0[i][1] high_bar = df0[i][2] low_bar = df0[i][3] last_close = df0[i - 1][4] clsoe_bar = df0[i][4] ma_xiao = df2[i][7] da_ma = df2[i][10] # === 仓位统计。 df1, now_open_prcie, now_pos = cal_per_pos(i, df1, open_pos_size, open_bar, clsoe_bar, last_close, sxf=1, slip=1) # === 当日变量加载 if ((df0[i, 7] == 9) and (df0[i, 8] == 15)): inner_trade_nums = 100 max_log_high = high_bar min_log_low = low_bar else: max_log_high = max(max(open_bar,clsoe_bar), max_log_high) min_log_low = min(min(open_bar,clsoe_bar), min_log_low) inner_low = df2[i - 1][3] if max_log_high > (now_open_prcie + stop_win_n): stop_loss_price = ma_xiao stop_win_con = (clsoe_bar < ma_xiao) # elif max_log_high > (now_open_prcie + stop_win_n2): # stop_loss_price = ma_xiao # stop_win_con = (clsoe_bar < ma_xiao) else: stop_loss_price = (max_log_high-clsoe_bar ) stop_win_con = (max_log_high-clsoe_bar > 50) moving_swin_price = np.nan if we_trade_con: if now_pos == 0: short_condition = (open_bar < clsoe_bar) and (open_bar - low_bar > abs(open_bar - clsoe_bar)*extend_n) short_condition &= clsoe_bar - open_bar >base_line short_condition &= clsoe_bar > da_ma # 空:突破最低线。 if short_condition and inner_trade_nums > 0: inner_trade_nums -= 1 max_log_high = high_bar df1[i][1] = 1 elif now_pos > 0: stop_loss_con = False close_pos = False if close_pos: df1[i][1] = 0 elif stop_loss_con: df1[i][1] = 0 elif stop_win_con: df1[i][1] = 0 else: # 非交易时间段 if now_pos != 0: df1[i][1] = 0 # 记录指标 绘图等等 # ['开仓线','atr_day','小均线'] df2[i, 2] = stop_loss_price df2[i, 3] = min_log_low df2[i, 4] = max_log_high df2[i, 9] = moving_swin_price res0 = cal_tongji(df_input=df1) # if not res0:return df0, df1, df2, cs0 res0 = np.concatenate((res0, cs0)) return df0, df1, df2, res0