首页 > 解决方案 > 标记水平 EMA 线(变量)

问题描述

这是我使用 atm 的代码。(感谢比约恩·米斯蒂安)


len1 = input(10, minval=1, title="Length")
len2 = input(21, minval=1, title="Length")
len3 = input(55, minval=1, title="Length")
len4 = input(100, minval=1, title="Length")
len5 = input(200, minval=1, title="Length")
src  = input(close, title="Source")

var bool show_hlines = input(true, "Show horizontal lines", input.bool)
var bool show_emas   = not show_hlines

var color_entryema    = color.green
var color_fastema     = color.orange
var color_mediumema   = color.red
var color_slowema     = color.white
var color_veryslowema = color.purple


var line_entryema    = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.both, color=show_hlines ? color_entryema    : na, style=line.style_solid)
var line_fastema     = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.both, color=show_hlines ? color_fastema     : na, style=line.style_solid)
var line_mediumema   = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.both, color=show_hlines ? color_mediumema   : na, style=line.style_solid)
var line_slowema     = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.both, color=show_hlines ? color_slowema     : na, style=line.style_solid)
var line_veryslowema = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.both, color=show_hlines ? color_veryslowema : na, style=line.style_solid)

f_moveLine(_id, _x, _y) =>
    line.set_xy1(_id, _x,   _y)
    line.set_xy2(_id, _x+1, _y)

entryema    = ema(src, len1)
fastema     = ema(src, len2)
mediumema   = ema(src, len3)
slowema     = ema(src, len4)
veryslowema = ema(src, len5)

if (hour==0 and minute==0 and year(time)==year(timenow) and month(time)==month(timenow) and dayofmonth(time)==dayofmonth(timenow))
    f_moveLine(line_entryema,    time, entryema)
    f_moveLine(line_fastema,     time, fastema)
    f_moveLine(line_mediumema,   time, mediumema)
    f_moveLine(line_slowema,     time, slowema)
    f_moveLine(line_veryslowema, time, veryslowema)
    
plot(entryema,    color=show_emas ? color_entryema    : na, linewidth=2, title="Entry EMA")
plot(fastema,     color=show_emas ? color_fastema     : na, linewidth=2, title="Fast EMA")
plot(mediumema,   color=show_emas ? color_mediumema   : na, linewidth=2, title="Medium EMA")
plot(slowema,     color=show_emas ? color_slowema     : na, linewidth=2, title="Slow EMA")
plot(veryslowema, color=show_emas ? color_veryslowema : na, linewidth=2, title="Veryslow EMA") 

现在我想用 10、21 EMA 等标记这些水平 EMA 线。我已经为每天和每周关闭水平线添加了一个现成的代码,并尝试为此使用“相同”代码,但似乎没有任何效果。我遵循了相同的程序,但我可能遗漏了一些重要的东西。

lblOffset = input(30, title="Label Offset")
showWeekly = input(true, type=input.bool, title="Show Weekly?")
wValue = input("Previous Week", title="Candle Selection", options=["Previous Week", "Current Week"])
wColor = input("orange", title="Weekly color", options=["aqua","black","blue","fuchsia","gray","green","lime","maroon","navy","olive","orange","purple","red","silver","teal","white","yellow"])
wStyle = input("solid", title="Weekly style", options=['solid','dotted','dashed'])
wWidth = input(1, title="Weekly Width")
showDaily = input(true, type=input.bool, title="Show Daily?")
dValue = input("Previous Day", title="Candle Selection", options=["Previous Day", "Current Day"])
dColor = input("blue", title="Daily color", options=["aqua","black","blue","fuchsia","gray","green","lime","maroon","navy","olive","orange","purple","red","silver","teal","white","yellow"])
dStyle = input("solid", title="Daily style", options=['solid','dotted','dashed'])
dWidth = input(1, title="Daily Width")


var wCloseLine = line(na)
var wCloseLbl = label(na)
var int wFrom = 0
var int wTo = 0

wClose = wValue == "Previous Week" ? security(syminfo.tickerid, 'W', close[1], gaps = barmerge.gaps_off,  lookahead = barmerge.lookahead_on)[1] : security(syminfo.tickerid, 'W', close, gaps = barmerge.gaps_off,  lookahead = barmerge.lookahead_on)

if change(time('W'))
    wFrom := bar_index[1]
    wTo   := bar_index    
    

var dCloseLine = line(na)
var dCloseLbl = label(na)
var int dFrom = 0
var int dTo = 0

dClose = dValue == "Previous Day" ? security(syminfo.tickerid, 'D', close[1], gaps = barmerge.gaps_off,  lookahead = barmerge.lookahead_on)[1] : security(syminfo.tickerid, 'D', close, gaps = barmerge.gaps_off,  lookahead = barmerge.lookahead_on)

if change(time('D'))
    dFrom := bar_index[1]
    dTo   := bar_index    


f_calc_bar_time(offset) => ret = time + ((time-time[1]) * offset)


f_set_color(selection)=>
    ret = color.black
    if selection == "gray"
        ret := color.gray
    if selection == "green"
        ret := color.green
    if selection == "aqua"
        ret := color.aqua
    if selection == "blue"
        ret := color.blue
    if selection == "fuchsia"
        ret := color.fuchsia
    if selection == "lime"
        ret := color.lime
    if selection == "maroon"
        ret := color.maroon
    if selection == "navy"
        ret := color.navy
    if selection == "white"
        ret := color.white
    if selection == "yellow"
        ret := color.yellow
    if selection == "olive"
        ret := color.olive
    if selection == "orange"
        ret := color.orange
    if selection == "purple"
        ret := color.purple
    if selection == "red"
        ret := color.red
    if selection == "silver"
        ret := color.silver
    if selection == "teal"
        ret := color.teal
    ret


f_set_style(selection)=>
    ret = line.style_solid
    if selection == "dotted"
        ret := line.style_dotted 
    if selection == "dashed"
        ret := line.style_dashed
    ret


f_delete(tf) =>
    if tf == 'Weekly'
        line.delete(wCloseLine)
        label.delete(wCloseLbl)
    if tf == 'Daily'
        line.delete(dCloseLine)
        label.delete(dCloseLbl)


if showWeekly
    f_delete('Weekly')
    wCloseLine := line.new(wFrom, wClose, wTo, wClose, color=f_set_color(wColor), extend=extend.right, width=wWidth, style=f_set_style(wStyle))
    wCloseLbl := label.new(f_calc_bar_time(lblOffset), wClose, xloc=xloc.bar_time, text="Weekly Close", style=label.style_none, textcolor=f_set_color(wColor))

if showDaily
    f_delete('Daily')
    dCloseLine := line.new(dFrom, dClose, dTo, dClose, color=f_set_color(dColor), extend=extend.right, width=dWidth, style=f_set_style(dStyle))
    dCloseLbl := label.new(f_calc_bar_time(lblOffset), dClose, xloc=xloc.bar_time, text="Daily Close", style=label.style_none, textcolor=f_set_color(dColor))```

标签: pine-script

解决方案


你的代码工作得很好。
我认为您在图表上向右滚动的距离不够远。
您将默认lblOffset设置为30,因此文本将在右侧 30 个小节处。
请参阅下面由您的代码制作的屏幕截图(未更改)。
它清楚地显示了Daily Close蓝线上的文字。

在此处输入图像描述

更新代码以显示标签:

//@version=4
//5 Horizontal EMA by -=Tre$aCo7n=-
//Modified from 4 Exponential Moving Averages from @vitastrato

study(title="5 Horizontal EMA", shorttitle="5 H. EMA", overlay=true)

len1 = input(10,  minval=8, title="Length entry ema")
len2 = input(21,  minval=8, title="Length fast ema")
len3 = input(55,  minval=8, title="Length medium ema")
len4 = input(100, minval=8, title="Length slow ema")
len5 = input(200, minval=8, title="Length very slow ema")
src  = input(close, title="Source")

var bool show_hlines = input(true, "Show horizontal lines", input.bool)
var bool show_emas   = not show_hlines

var color_entryema      = color.green
var color_fastema       = color.orange
var color_mediumema     = color.red
var color_slowema       = color.white
var color_veryslowema   = color.purple

var line_entryema       = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_entryema    : na, style=line.style_dashed)
var line_fastema        = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_fastema     : na, style=line.style_dashed)
var line_mediumema      = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_mediumema   : na, style=line.style_dashed)
var line_slowema        = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_slowema     : na, style=line.style_dashed)
var line_veryslowema    = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_veryslowema : na, style=line.style_dashed)

var label_entryema      = label.new(x=na, y=na, text=tostring(len1), xloc=xloc.bar_time, color=show_hlines ? color_entryema    : na, textcolor=show_hlines ? color_entryema    : na, style=label.style_none)
var label_fastema       = label.new(x=na, y=na, text=tostring(len2), xloc=xloc.bar_time, color=show_hlines ? color_fastema     : na, textcolor=show_hlines ? color_fastema     : na, style=label.style_none)
var label_mediumema     = label.new(x=na, y=na, text=tostring(len3), xloc=xloc.bar_time, color=show_hlines ? color_mediumema   : na, textcolor=show_hlines ? color_mediumema   : na, style=label.style_none)
var label_slowema       = label.new(x=na, y=na, text=tostring(len4), xloc=xloc.bar_time, color=show_hlines ? color_slowema     : na, textcolor=show_hlines ? color_slowema     : na, style=label.style_none)
var label_veryslowema   = label.new(x=na, y=na, text=tostring(len5), xloc=xloc.bar_time, color=show_hlines ? color_veryslowema : na, textcolor=show_hlines ? color_veryslowema : na, style=label.style_none)

f_moveLine(_id, _x, _y) =>
    line.set_xy1(_id, _x,   _y)
    line.set_xy2(_id, _x+1, _y)

f_moveLabel(_id, _x, _y) =>
    label.set_xy(_id, _x, _y)

entryema    = ema(src, len1)
fastema     = ema(src, len2)
mediumema   = ema(src, len3)
slowema     = ema(src, len4)
veryslowema = ema(src, len5)

if (hour==0 and minute==0 and year(time)==year(timenow) and month(time)==month(timenow) and dayofmonth(time)==dayofmonth(timenow))
    f_moveLine(line_entryema,      time, entryema)
    f_moveLine(line_fastema,       time, fastema)
    f_moveLine(line_mediumema,     time, mediumema)
    f_moveLine(line_slowema,       time, slowema)
    f_moveLine(line_veryslowema,   time, veryslowema)
    
    f_moveLabel(label_entryema,    time, entryema)
    f_moveLabel(label_fastema,     time, fastema)
    f_moveLabel(label_mediumema,   time, mediumema)
    f_moveLabel(label_slowema,     time, slowema)
    f_moveLabel(label_veryslowema, time, veryslowema)
    
plot(entryema,    color=show_emas ? color_entryema    : na, linewidth=1, title="Entry EMA")
plot(fastema,     color=show_emas ? color_fastema     : na, linewidth=1, title="Fast EMA")
plot(mediumema,   color=show_emas ? color_mediumema   : na, linewidth=1, title="Medium EMA")
plot(slowema,     color=show_emas ? color_slowema     : na, linewidth=1, title="Slow EMA")
plot(veryslowema, color=show_emas ? color_veryslowema : na, linewidth=1, title="Veryslow EMA")

推荐阅读