使用Request.Security调用高时间框架时筛选器结果空白问题求助
问题:低时间框架蜡烛收盘时,request.security获取高低点的筛选结果空白
我尝试用Request.Security获取前一日的高点和低点,代码如下:
dh_func(sym) => sema = ta.ema(close, ema_len) ////////// /// Get Days High/// [h1,h2,l1,l2] = request.security(sym, 'D', [high[0],high[1],low[0],low[1]]) //[h1,h2,l1,l2] = rp_security(sym, 'D') dh = (ta.cross(sema,math.max(h1,h2)) and isToday) or (ta.cross(close,math.max(h1,h2)) and isToday) dl = (ta.cross(sema,math.min(l1,l2)) and isToday) or (ta.cross(close,math.min(l1,l2)) and isToday) [dh,dl]
代码运行正常,但低时间框架的蜡烛收盘时,筛选器结果会变成空白。试过用f_security,问题依旧。
问题原因
直接在request.security的返回数组里用high[1]、low[1]这类索引偏移,会在低时间框架蜡烛收盘时出现数据同步问题:此时日线当前K线(high[0])还未收盘,high[1]实际指向的是前前一日的数据,导致math.max(h1,h2)或math.min(l1,l2)的取值逻辑异常,最终让dh/dl的判断结果为空。
解决方法
方案1:在request.security内部处理历史高低点
把历史高低点的计算放在request.security内部,避免外部索引偏移带来的错位:
dh_func(sym) => sema = ta.ema(close, ema_len) // 在日线级别内部计算最近两日的高低点 [day_high_curr, day_high_prev, day_low_curr, day_low_prev] = request.security(sym, 'D', [ high, ta.valuewhen(ta.change(time), high, 1), // 当日、前一日高点 low, ta.valuewhen(ta.change(time), low, 1) // 当日、前一日低点 ]) max_h = math.max(day_high_curr, day_high_prev) min_l = math.min(day_low_curr, day_low_prev) dh = (ta.cross(sema, max_h) and isToday) or (ta.cross(close, max_h) and isToday) dl = (ta.cross(sema, min_l) and isToday) or (ta.cross(close, min_l) and isToday) [dh, dl]
方案2:用ta.valuewhen稳定获取历史数据
通过ta.change(time('D'))触发日线切换事件,用ta.valuewhen精准获取最近两日的高低点:
dh_func(sym) => sema = ta.ema(close, ema_len) // 获取日线级别的高低点序列 daily_high = request.security(sym, 'D', high) daily_low = request.security(sym, 'D', low) // 基于日线时间切换事件,获取当日和前一日的高低点 h1 = ta.valuewhen(ta.change(time('D')), daily_high, 0) // 当日高点 h2 = ta.valuewhen(ta.change(time('D')), daily_high, 1) // 前一日高点 l1 = ta.valuewhen(ta.change(time('D')), daily_low, 0) // 当日低点 l2 = ta.valuewhen(ta.change(time('D')), daily_low, 1) // 前一日低点 max_h = math.max(h1, h2) min_l = math.min(l1, l2) dh = (ta.cross(sema, max_h) and isToday) or (ta.cross(close, max_h) and isToday) dl = (ta.cross(sema, min_l) and isToday) or (ta.cross(close, min_l) and isToday) [dh, dl]
内容的提问来源于stack exchange,提问作者naim shaikh
相关产品推荐
相关产品推荐

