问题
This is the Code I use atm. (Thanks to Bjorn Mistiaen)
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")
Now I want to label these horizontal EMA lines with 10, 21 EMA etc. I already added a ready code for Labeled Daily and weekly close horizontal lines and tried to use the "same" code for this, but nothing seems to work. I have followed the same procedure but I am probably missing something important.
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))```
回答1:
Your code works just fine.
I think that you're not scrolling far enough to the right on your chart.
You have the default lblOffset
set at 30
, so the text will be 30 bars to the right.
See below screenshot that was made from your code (unchanged).
It clearly shows the text Daily Close
on the blue line.
Updated code to show labels:
//@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")
来源:https://stackoverflow.com/questions/65142238/label-horizontal-ema-lines-variables