从前一个较低的高点到最后一个较低的高点绘制趋势线
Drawing a Trendline from the previous lower high to the last lower high
请参阅下面的脚本和图像,例如,更高的高点和更低的低点。
下高的变量是:“_lh”。在图表上,我们可以看到较低的高点仅在 5 个柱线后出现(偏移量 = -5),这很好。
我想知道如何绘制一条从前一个较低的高点到刚刚发生的最后一个较低的高点(偏移量=-5)的趋势线,
并在价格交叉时为其添加警报条件?
我知道我应该使用 line.new() 构建函数,但我不知道如何调用前一个较低的高点。
谢谢
//@version=5
indicator("Lower Low trendlines", shorttitle = "Lower Low trendlines", overlay=true)
//////////////////////////////////////////////////
////////////// HIGHER HIGH LOWER LOW /////////////
//////////////////////////////////////////////////
// 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=5
//indicator('Higher High Lower Low Strategy', overlay=true, max_lines_count=500)
lb = input.int(5, title='Left Bars', minval=1, group="Higher High Lower Low")
rb = input.int(5, title='Right Bars', minval=1, group="Higher High Lower Low")
//showsupres = input.bool(true, title='Support/Resistance', inline='srcol', group="Higher High Lower Low")
showsup = input.bool(true, title="Support only", inline="srcol_sup", group="Higher High Lower Low")
showres = input.bool(true, title="Resistance only", inline="srcol_res", group="Higher High Lower Low")
supcol = input.color(color.lime, title='', inline="srcol_sup", group="Higher High Lower Low")
rescol = input.color(color.red, title='', inline="srcol_res", group="Higher High Lower Low")
srlinestyle = input.string(line.style_dotted, title='Line Style/Width', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='style', group="Higher High Lower Low")
srlinewidth = input.int(3, title='', minval=1, maxval=5, inline='style', group="Higher High Lower Low")
//changebarcol = input.bool(true, title='Change Bar Color', inline='bcol', group="Higher High Lower Low")
//bcolup = input.color(color.blue, title='', inline='bcol', group="Higher High Lower Low")
//bcoldn = input.color(color.black, title='', inline='bcol', group="Higher High Lower Low")
ph = ta.pivothigh(lb, rb)
pl = ta.pivotlow(lb, rb)
iff_1 = pl ? -1 : na // Trend direction
hl = ph ? 1 : iff_1
iff_2 = pl ? pl : na // similar to zigzag but may have multiple highs/lows
zz = ph ? ph : iff_2
valuewhen_1 = ta.valuewhen(hl, hl, 1)
valuewhen_2 = ta.valuewhen(zz, zz, 1)
zz := pl and hl == -1 and valuewhen_1 == -1 and pl > valuewhen_2 ? na : zz
valuewhen_3 = ta.valuewhen(hl, hl, 1)
valuewhen_4 = ta.valuewhen(zz, zz, 1)
zz := ph and hl == 1 and valuewhen_3 == 1 and ph < valuewhen_4 ? na : zz
valuewhen_5 = ta.valuewhen(hl, hl, 1)
valuewhen_6 = ta.valuewhen(zz, zz, 1)
hl := hl == -1 and valuewhen_5 == 1 and zz > valuewhen_6 ? na : hl
valuewhen_7 = ta.valuewhen(hl, hl, 1)
valuewhen_8 = ta.valuewhen(zz, zz, 1)
hl := hl == 1 and valuewhen_7 == -1 and zz < valuewhen_8 ? na : hl
zz := na(hl) ? na : zz
findprevious() => // finds previous three points (b, c, d, e)
ehl = hl == 1 ? -1 : 1
loc1 = 0.0
loc2 = 0.0
loc3 = 0.0
loc4 = 0.0
xx = 0
for x = 1 to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc1 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc2 := zz[x]
xx := x + 1
break
ehl := hl == 1 ? -1 : 1
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc3 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
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
e
_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)
plotshape(_hl, text='HL', title='Higher Low', style=shape.labelup, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0), location=location.belowbar, offset=-rb)
plotshape(_hh, text='HH', title='Higher High', style=shape.labeldown, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0), location=location.abovebar, offset=-rb)
plotshape(_ll, text='LL', title='Lower Low', style=shape.labelup, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.belowbar, offset=-rb)
plotshape(_lh, text='LH', title='Lower High', style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.abovebar, offset=-rb)
float res = na
float sup = na
res := _lh ? zz : res[1]
sup := _hl ? zz : sup[1]
int trend = na
iff_3 = close < sup ? -1 : nz(trend[1])
trend := close > res ? 1 : iff_3
res := trend == 1 and _hh or trend == -1 and _lh ? zz : res
sup := 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.none)
// resline := line.new(x1=bar_index - rb, y1=res, x2=bar_index, y2=res, color=rescol, extend=extend.right, style=srlinestyle, width=srlinewidth)
// resline
//
// if suchange
// line.set_x2(supline, bar_index)
// line.set_extend(supline, extend=extend.none)
// supline := line.new(x1=bar_index - rb, y1=sup, x2=bar_index, y2=sup, color=supcol, extend=extend.right, style=srlinestyle, width=srlinewidth)
// supline
if showres
if rechange
line.set_x2(resline, bar_index)
line.set_extend(resline, extend=extend.none)
resline := line.new(x1=bar_index - rb, y1=res, x2=bar_index, y2=res, color=rescol, extend=extend.right, style=srlinestyle, width=srlinewidth)
resline
if showsup
if suchange
line.set_x2(supline, bar_index)
line.set_extend(supline, extend=extend.none)
supline := line.new(x1=bar_index - rb, y1=sup, x2=bar_index, y2=sup, color=supcol, extend=extend.right, style=srlinestyle, width=srlinewidth)
supline
//iff_4 = trend == 1 ? bcolup : bcoldn
//barcolor(color=changebarcol ? iff_4 : na)
///////////////////////////////////////
////////////// TRENDLINES /////////////
///////////////////////////////////////
// How could we draw a trendline from the previous lower high to the last lower high, and add an alertcondition for it?
您需要使用数组来存储每个枢轴。
var pivots = array.new_float()
var pivotbars = array.new_int()
If(pivotlow)
array.push(pivots, pivotlow)
array.push(pivotbars, pivotbar)
然后您可以使用数组获取方法访问枢轴和枢轴柱。使用它们获取最后两个枢轴并画线。
请参阅下面的脚本和图像,例如,更高的高点和更低的低点。
下高的变量是:“_lh”。在图表上,我们可以看到较低的高点仅在 5 个柱线后出现(偏移量 = -5),这很好。 我想知道如何绘制一条从前一个较低的高点到刚刚发生的最后一个较低的高点(偏移量=-5)的趋势线, 并在价格交叉时为其添加警报条件?
我知道我应该使用 line.new() 构建函数,但我不知道如何调用前一个较低的高点。
谢谢
//@version=5
indicator("Lower Low trendlines", shorttitle = "Lower Low trendlines", overlay=true)
//////////////////////////////////////////////////
////////////// HIGHER HIGH LOWER LOW /////////////
//////////////////////////////////////////////////
// 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=5
//indicator('Higher High Lower Low Strategy', overlay=true, max_lines_count=500)
lb = input.int(5, title='Left Bars', minval=1, group="Higher High Lower Low")
rb = input.int(5, title='Right Bars', minval=1, group="Higher High Lower Low")
//showsupres = input.bool(true, title='Support/Resistance', inline='srcol', group="Higher High Lower Low")
showsup = input.bool(true, title="Support only", inline="srcol_sup", group="Higher High Lower Low")
showres = input.bool(true, title="Resistance only", inline="srcol_res", group="Higher High Lower Low")
supcol = input.color(color.lime, title='', inline="srcol_sup", group="Higher High Lower Low")
rescol = input.color(color.red, title='', inline="srcol_res", group="Higher High Lower Low")
srlinestyle = input.string(line.style_dotted, title='Line Style/Width', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='style', group="Higher High Lower Low")
srlinewidth = input.int(3, title='', minval=1, maxval=5, inline='style', group="Higher High Lower Low")
//changebarcol = input.bool(true, title='Change Bar Color', inline='bcol', group="Higher High Lower Low")
//bcolup = input.color(color.blue, title='', inline='bcol', group="Higher High Lower Low")
//bcoldn = input.color(color.black, title='', inline='bcol', group="Higher High Lower Low")
ph = ta.pivothigh(lb, rb)
pl = ta.pivotlow(lb, rb)
iff_1 = pl ? -1 : na // Trend direction
hl = ph ? 1 : iff_1
iff_2 = pl ? pl : na // similar to zigzag but may have multiple highs/lows
zz = ph ? ph : iff_2
valuewhen_1 = ta.valuewhen(hl, hl, 1)
valuewhen_2 = ta.valuewhen(zz, zz, 1)
zz := pl and hl == -1 and valuewhen_1 == -1 and pl > valuewhen_2 ? na : zz
valuewhen_3 = ta.valuewhen(hl, hl, 1)
valuewhen_4 = ta.valuewhen(zz, zz, 1)
zz := ph and hl == 1 and valuewhen_3 == 1 and ph < valuewhen_4 ? na : zz
valuewhen_5 = ta.valuewhen(hl, hl, 1)
valuewhen_6 = ta.valuewhen(zz, zz, 1)
hl := hl == -1 and valuewhen_5 == 1 and zz > valuewhen_6 ? na : hl
valuewhen_7 = ta.valuewhen(hl, hl, 1)
valuewhen_8 = ta.valuewhen(zz, zz, 1)
hl := hl == 1 and valuewhen_7 == -1 and zz < valuewhen_8 ? na : hl
zz := na(hl) ? na : zz
findprevious() => // finds previous three points (b, c, d, e)
ehl = hl == 1 ? -1 : 1
loc1 = 0.0
loc2 = 0.0
loc3 = 0.0
loc4 = 0.0
xx = 0
for x = 1 to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc1 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc2 := zz[x]
xx := x + 1
break
ehl := hl == 1 ? -1 : 1
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc3 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
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
e
_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)
plotshape(_hl, text='HL', title='Higher Low', style=shape.labelup, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0), location=location.belowbar, offset=-rb)
plotshape(_hh, text='HH', title='Higher High', style=shape.labeldown, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0), location=location.abovebar, offset=-rb)
plotshape(_ll, text='LL', title='Lower Low', style=shape.labelup, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.belowbar, offset=-rb)
plotshape(_lh, text='LH', title='Lower High', style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.abovebar, offset=-rb)
float res = na
float sup = na
res := _lh ? zz : res[1]
sup := _hl ? zz : sup[1]
int trend = na
iff_3 = close < sup ? -1 : nz(trend[1])
trend := close > res ? 1 : iff_3
res := trend == 1 and _hh or trend == -1 and _lh ? zz : res
sup := 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.none)
// resline := line.new(x1=bar_index - rb, y1=res, x2=bar_index, y2=res, color=rescol, extend=extend.right, style=srlinestyle, width=srlinewidth)
// resline
//
// if suchange
// line.set_x2(supline, bar_index)
// line.set_extend(supline, extend=extend.none)
// supline := line.new(x1=bar_index - rb, y1=sup, x2=bar_index, y2=sup, color=supcol, extend=extend.right, style=srlinestyle, width=srlinewidth)
// supline
if showres
if rechange
line.set_x2(resline, bar_index)
line.set_extend(resline, extend=extend.none)
resline := line.new(x1=bar_index - rb, y1=res, x2=bar_index, y2=res, color=rescol, extend=extend.right, style=srlinestyle, width=srlinewidth)
resline
if showsup
if suchange
line.set_x2(supline, bar_index)
line.set_extend(supline, extend=extend.none)
supline := line.new(x1=bar_index - rb, y1=sup, x2=bar_index, y2=sup, color=supcol, extend=extend.right, style=srlinestyle, width=srlinewidth)
supline
//iff_4 = trend == 1 ? bcolup : bcoldn
//barcolor(color=changebarcol ? iff_4 : na)
///////////////////////////////////////
////////////// TRENDLINES /////////////
///////////////////////////////////////
// How could we draw a trendline from the previous lower high to the last lower high, and add an alertcondition for it?
您需要使用数组来存储每个枢轴。
var pivots = array.new_float()
var pivotbars = array.new_int()
If(pivotlow)
array.push(pivots, pivotlow)
array.push(pivotbars, pivotbar)
然后您可以使用数组获取方法访问枢轴和枢轴柱。使用它们获取最后两个枢轴并画线。