Pinescript如何获取两次stoch上行交叉间最低stoch值对应K线价格
实现方案
默认你使用的是TradingView Pine Script,可通过以下两种方式获取对应pivotlow的bar索引及K线价格:
方案1:直接计算区间最低值偏移(推荐,和现有逻辑兼容)
你可以直接计算两次stoch上行交叉区间内的K值最低位偏移,无需依赖已算出的stochLow,和你现有pivotlow的取值逻辑完全一致:
// 计算两次交叉之间的K线总数量 lookbackLength = stochCrossUpAt - stochCrossUpAtPrevious // 获取区间内stoch K值最低的bar相对于当前bar的偏移量 lowestBarOffset = ta.lowestbars(k, lookbackLength) // 转换为绝对bar_index targetBarIndex = bar_index - lowestBarOffset // 读取对应K线的开盘价、收盘价 targetOpen = open[lowestBarOffset] targetClose = close[lowestBarOffset]
方案2:基于已有stochLow反向匹配索引
如果你需要基于已经计算完成的stochLow反向查找对应的bar信息,可以用ta.valuewhen函数实现:
// 匹配stochLow出现时的bar_index targetBarIndex = ta.valuewhen(k == stochLow, bar_index, 0) // 匹配对应K线的开盘价、收盘价 targetOpen = ta.valuewhen(k == stochLow, open, 0) targetClose = ta.valuewhen(k == stochLow, close, 0)
注意:如果区间内存在多个相同的stoch最低值,上述代码默认返回最近一次匹配的bar信息,如需返回最早出现的最低值,将
ta.valuewhen的最后一个参数从0修改为1即可。
内容的提问来源于stack exchange,提问作者Thierry Verhaegen
相关产品推荐
相关产品推荐

