Pine Script技术求助:修改收盘价与支撑/阻力线的检测逻辑
问题:Pine Script修改支撑/阻力线收盘价检测逻辑
我并非专业程序员,正自行尝试解决交易策略的脚本问题。我需要实现检测K线收盘价低于支撑线且高于阻力线的功能,当前已编写一段Pine Script代码如下:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © LonesomeThecolor.blue //@version=4 study("EthStrategy1h15m", overlay =true) lb = input(5, title="Left Bars", minval = 1) rb = input(5, title="Right Bars", minval = 1) showsupres = input(true, title="Support/Resistance", inline = "srcol") supcol = input(color.lime, title ="", inline = "srcol") rescol = input(color.red, title="", inline = "srcol") srlinestyle = input(line.style_solid, title = "Line Style/Width", inline ="style") srlinewidth = input(1, title = "", minval = 1, maxval = 5, inline ="style") ph = pivothigh(lb, rb) pl = pivotlow(lb, rb) hl = iff(ph, 1, iff(pl, -1, na)) // Trend direction zz = iff(ph, ph, iff(pl, pl, na)) // similar to zigzag but may have multiple highs/lows zz :=iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz) zz :=iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz) hl := iff(hl==-1 and valuewhen(hl, hl, 1)==1 and zz > valuewhen(zz, zz, 1), na, hl) hl := iff(hl==1 and valuewhen(hl, hl, 1)==-1 and zz < valuewhen(zz, zz, 1), na, hl) zz := iff(na(hl), na, zz) findprevious()=> // finds previous three points (b, c, d, e) ehl = iff(hl==1, -1, 1) loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0 xx = 0 for x=1 to 1000 if hl[x]==ehl and not na(zz[x]) loc1 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc2 := zz[x] xx := x + 1 break ehl := iff(hl==1, -1, 1) for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc3 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc4 := zz[x] break [loc1, loc2, loc3, loc4] float a = na, float b = na, float c = na, float d = na, float e = na if not na(hl) [loc1, loc2, loc3, loc4] = findprevious() a := zz b := loc1 c := loc2 d := loc3 e := loc4 _hh = zz and (a > b and a > c and c > b and c > d) _ll = zz and (a < b and a < c and c < b and c < d) _hl = zz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d)) _lh = zz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d)) float res = na, float sup = na res := iff(_lh, zz, res[1]) sup := iff(_hl, zz, sup[1]) int trend = na trend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1]))) res := iff((trend == 1 and _hh) or (trend == -1 and _lh), zz, res) sup := iff((trend == 1 and _hl) or (trend == -1 and _ll), zz, sup) rechange = res != res[1] suchange = sup != sup[1] var line resline = na var line supline = na if showsupres if rechange line.set_x2(resline, bar_index) line.set_extend(resline, extend = extend.right) resline := line.new(x1 = bar_index - rb, y1 = res, x2 = bar_index, y2 = res, color = rescol, extend = extend.right, style = srlinestyle, width = srlinewidth) if suchange line.set_x2(supline, bar_index) line.set_extend(supline, extend = extend.right) supline := line.new(x1 = bar_index - rb, y1 = sup, x2 = bar_index, y2 = sup, color = supcol, extend = extend.right, style = srlinestyle, width = srlinewidth) lowerOrSameClose = barstate.isconfirmed and (close <= close[1]) plotshape(series=lowerOrSameClose, style=shape.diamond, color = color.red)
现需完成两项修改:
- 将代码末尾
lowerOrSameClose条件中的close[1]替换为支撑线,实现检测收盘价低于或等于支撑线的功能; - 新增逻辑,实现检测收盘价高于或等于阻力线的功能。
解决方案
关键说明
代码中的supline和resline是用于绘制图形的line对象实例,不能直接与价格数值(如close)比较。支撑线和阻力线的价格值已经存储在sup和res变量中,直接使用这两个变量即可完成价格对比。
修改步骤
- 替换支撑线检测逻辑:将
lowerOrSameClose中的close[1]替换为sup,同时增加not na(sup)判断,避免支撑线未生成时的无效信号。 - 新增阻力线检测逻辑:定义
higherOrSameClose变量,检测收盘价高于等于res且K线确认,同样增加not na(res)判断。 - 新增阻力线信号图形:添加对应的
plotshape绘制阻力线触发的信号,用不同样式区分支撑线信号。
修改后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © LonesomeThecolor.blue //@version=4 study("EthStrategy1h15m", overlay =true) lb = input(5, title="Left Bars", minval = 1) rb = input(5, title="Right Bars", minval = 1) showsupres = input(true, title="Support/Resistance", inline = "srcol") supcol = input(color.lime, title ="", inline = "srcol") rescol = input(color.red, title="", inline = "srcol") srlinestyle = input(line.style_solid, title = "Line Style/Width", inline ="style") srlinewidth = input(1, title = "", minval = 1, maxval = 5, inline ="style") ph = pivothigh(lb, rb) pl = pivotlow(lb, rb) hl = iff(ph, 1, iff(pl, -1, na)) // Trend direction zz = iff(ph, ph, iff(pl, pl, na)) // similar to zigzag but may have multiple highs/lows zz :=iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz) zz :=iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz) hl := iff(hl==-1 and valuewhen(hl, hl, 1)==1 and zz > valuewhen(zz, zz, 1), na, hl) hl := iff(hl==1 and valuewhen(hl, hl, 1)==-1 and zz < valuewhen(zz, zz, 1), na, hl) zz := iff(na(hl), na, zz) findprevious()=> // finds previous three points (b, c, d, e) ehl = iff(hl==1, -1, 1) loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0 xx = 0 for x=1 to 1000 if hl[x]==ehl and not na(zz[x]) loc1 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc2 := zz[x] xx := x + 1 break ehl := iff(hl==1, -1, 1) for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc3 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc4 := zz[x] break [loc1, loc2, loc3, loc4] float a = na, float b = na, float c = na, float d = na, float e = na if not na(hl) [loc1, loc2, loc3, loc4] = findprevious() a := zz b := loc1 c := loc2 d := loc3 e := loc4 _hh = zz and (a > b and a > c and c > b and c > d) _ll = zz and (a < b and a < c and c < b and c < d) _hl = zz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d)) _lh = zz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d)) float res = na, float sup = na res := iff(_lh, zz, res[1]) sup := iff(_hl, zz, sup[1]) int trend = na trend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1]))) res := iff((trend == 1 and _hh) or (trend == -1 and _lh), zz, res) sup := iff((trend == 1 and _hl) or (trend == -1 and _ll), zz, sup) rechange = res != res[1] suchange = sup != sup[1] var line resline = na var line supline = na if showsupres if rechange line.set_x2(resline, bar_index) line.set_extend(resline, extend = extend.right) resline := line.new(x1 = bar_index - rb, y1 = res, x2 = bar_index, y2 = res, color = rescol, extend = extend.right, style = srlinestyle, width = srlinewidth) if suchange line.set_x2(supline, bar_index) line.set_extend(supline, extend = extend.right) supline := line.new(x1 = bar_index - rb, y1 = sup, x2 = bar_index, y2 = sup, color = supcol, extend = extend.right, style = srlinestyle, width = srlinewidth) // 检测收盘价低于等于支撑线(K线确认后触发) lowerOrSameSupport = barstate.isconfirmed and not na(sup) and (close <= sup) // 检测收盘价高于等于阻力线(K线确认后触发) higherOrSameResistance = barstate.isconfirmed and not na(res) and (close >= res) // 绘制支撑线信号(红色钻石,K线下方) plotshape(series=lowerOrSameSupport, style=shape.diamond, color=color.red, location=location.belowbar) // 绘制阻力线信号(绿色上箭头,K线上方) plotshape(series=higherOrSameResistance, style=shape.labelup, color=color.green, location=location.abovebar)
内容的提问来源于stack exchange,提问作者Runabout
相关产品推荐
相关产品推荐

