在 pinescript 中为来自 MTF 的行添加价格和时间范围标签

Add price and timeframe label to lines from MTF in pinescript

我想在我的图表中添加一个看起来像这样的标签。它是线路的价格和时间范围。我有一个允许用户使用 select 5 个时间帧的 MTF 指标,它会从中绘制 support/resistance 线。

我试过了,但我得到了一个什么都没有的标签。

l = label.new(bar_index, hL_01, style=label.style_label_center), label.delete(l[1])   

如果我将 oneSet 更改为 highLine1,则会出现错误。关于如何创建它以使其正确的任何建议?

indicator('MTF Trend Table', overlay=true)

// == USER INPUT ==
tableLocation = input.string(defval='Top', options=['Top', 'Bottom'], title='Info Table Location', group='Display', 
  tooltip='Place information table on the top of the pane or the bottom of the pane.')
lookback = input.int(defval=3, title='Pivot Lookback Period', group='Pivot Points', 
  tooltip='Looks for pivot points within this number of bars both left and right.')
showPivotPoints = input.bool(title='Show Historical Pivot Points?', defval=false, group='Pivot Points', 
  tooltip='Toggle this on to see the historical pivot points that were used.  Change the Lookback Period to adjust the 
  frequency of these points. The pivot points are only shown for the current chart timeframe - to see the Daily pivot pionts, use the Daily timeframe, etc.')
oneSet = input.timeframe(defval='', title='First Timeframe', group='Higher Timeframe Levels', 
  tooltip='Allows you to set different time frames for looking at the trend.')
//show_oneSet= input(defval=false, title="Show oneSet", group = 'Higher Timeframe Levels')
twoSet = input.timeframe(defval='15', title='Second Timeframe', group='Higher Timeframe Levels')
threeSet = input.timeframe(defval='240', title='Third Timeframe', group='Higher Timeframe Levels')
fourSet = input.timeframe(defval='D', title='Fourth Timeframe', group='Higher Timeframe Levels')
fiveSet = input.timeframe(defval='W', title='Fifth Timeframe', group='Higher Timeframe Levels')
//show_fiveSet= input(defval=false, title="Show fiveSet", group = 'Higher Timeframe Levels')
showMTFLevels = input.bool(title='Show Multiple Timeframe S/R Levels?', defval=true, group='Higher Timeframe Levels', 
  tooltip='Displays the pivot highs and lows of higher timeframes to use as support/resistance levels. When these levels break, 
  the trend will change on these higher timeframes.')
oneColorS = input.color(color.new(color.orange, 80), title='1st Timeframe    Support', group='Higher Timeframe Levels', inline='MTF1')
oneColorR = input.color(color.new(color.orange, 80), title=' Resistance', group='Higher Timeframe Levels', inline='MTF1')
twoColorS = input.color(color.new(color.blue, 80), title='2nd Timeframe   Support', group='Higher Timeframe Levels', inline='MTF2')
twoColorR = input.color(color.new(color.blue, 80), title=' Resistance', group='Higher Timeframe Levels', inline='MTF2')
threeColorS = input.color(color.new(color.white, 80), title='3rd Timeframe    Support', group='Higher Timeframe Levels', inline='MTF3')
threeColorR = input.color(color.new(color.white, 80), title=' Resistance', group='Higher Timeframe Levels', inline='MTF3')
fourColorS = input.color(color.new(color.red, 80), title='4th Timeframe    Support', group='Higher Timeframe Levels', inline='MTF4')
fourColorR = input.color(color.new(color.red, 80), title=' Resistance', group='Higher Timeframe Levels', inline='MTF4')
fiveColorS = input.color(color.new(color.yellow, 80), title='5th Timeframe    Support', group='Higher Timeframe Levels', inline='MTF5')
fiveColorR = input.color(color.new(color.yellow, 80), title=' Resistance', group='Higher Timeframe Levels', inline='MTF5')
levelWidth = input.int(defval=1, title='Line Width (pixels)', group='Higher Timeframe Levels')
showlabels = input(title="Show Labels ?", defval=true)
offset_val = input(title="Label Offset", defval=8)
//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) ==  
f_getHTF() =>
    ph = ta.pivothigh(high, lookback, lookback)
    pl = ta.pivotlow(low, lookback, lookback)
    highLevel = ta.valuewhen(ph, high[lookback], 0)
    lowLevel = ta.valuewhen(pl, low[lookback], 0)
    barsSinceHigh = ta.barssince(ph) + lookback
    barsSinceLow = ta.barssince(pl) + lookback
    timeSinceHigh = time[barsSinceHigh]
    timeSinceLow = time[barsSinceLow]
    [ph, pl, highLevel, lowLevel, barsSinceHigh, barsSinceLow, timeSinceHigh, timeSinceLow]

[ph_01, pl_01, hL_01, lL_01, bsSH_01, bsSL_01, tSH_01, tSL_01] = request.security(syminfo.tickerid, oneSet, f_getHTF())
[ph_02, pl_02, hL_02, lL_02, bsSH_02, bsSL_02, tSH_02, tSL_02] = request.security(syminfo.tickerid, twoSet, f_getHTF())
[ph_03, pl_03, hL_03, lL_03, bsSH_03, bsSL_03, tSH_03, tSL_03] = request.security(syminfo.tickerid, threeSet, f_getHTF())
[ph_04, pl_04, hL_04, lL_04, bsSH_04, bsSL_04, tSH_04, tSL_04] = request.security(syminfo.tickerid, fourSet, f_getHTF())
[ph_05, pl_05, hL_05, lL_05, bsSH_05, bsSL_05, tSH_05, tSL_05] = request.security(syminfo.tickerid, fiveSet, f_getHTF())

// Check to ensure boxes are all higher timeframe than the chart to remove glyph and gray out box if that's the case
tfInMinutes(simple string tf = "") =>
    float chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)
    
float chartTFInMinutes = tfInMinutes()
bool TF1Check = tfInMinutes(oneSet) < chartTFInMinutes
bool TF2Check = tfInMinutes(twoSet) < chartTFInMinutes
bool TF3Check = tfInMinutes(threeSet) < chartTFInMinutes
bool TF4Check = tfInMinutes(fourSet) < chartTFInMinutes
bool TF5Check = tfInMinutes(fiveSet) < chartTFInMinutes

// Current timeframe pivots
phC = ta.pivothigh(high, lookback, lookback)
plC = ta.pivotlow(low, lookback, lookback)

// Plot historical pivot points for debugging and configuring the lookback period.
plot(showPivotPoints ? phC : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow, 50), offset=-lookback)
plot(showPivotPoints ? plC : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow, 50), offset=-lookback)

// == PLOT SUPPORT/RESISTANCE LINES ON THE HIGHER TIMEFRAMES ==
// Use a function to define the lines
f_line(x1, y1, y2, _color) =>
    var line id = na
    line.delete(id)
    id := line.new(x1, y1, time, y2, xloc.bar_time, extend.right, _color, width=levelWidth)
    id
    
// 1st Timeframe
highLine1 = TF1Check ? na : showMTFLevels ? f_line(tSH_01, hL_01, hL_01, oneColorR) : na
lowLine1 = TF1Check ? na : showMTFLevels ? f_line(tSL_01, lL_01, lL_01, oneColorS) : na
// 2nd Timeframe
highLine2 = TF2Check ? na : showMTFLevels ? f_line(tSH_02, hL_02, hL_02, twoColorR) : na
lowLine2 = TF2Check ? na : showMTFLevels ? f_line(tSL_02, lL_02, lL_02, twoColorS) : na
// 3rd Timeframe
highLine3 = TF3Check ? na : showMTFLevels ? f_line(tSH_03, hL_03, hL_03, threeColorR) : na
lowLine3 = TF3Check ? na : showMTFLevels ? f_line(tSL_03, lL_03, lL_03, threeColorS) : na
// 4TH Timeframe
highLine4 = TF4Check ? na : showMTFLevels ? f_line(tSH_04, hL_04, hL_04, fourColorR) : na
lowLine4 = TF4Check ? na : showMTFLevels ? f_line(tSL_04, lL_04, lL_04, fourColorS) : na
// 5TH Timeframe
highLine5 = TF5Check ? na : showMTFLevels ? f_line(tSH_05, hL_05, hL_05, fiveColorR) : na
lowLine5 = TF5Check ? na : showMTFLevels ? f_line(tSL_05, lL_05, lL_05, fiveColorS) : na

//CREATE PRICE AND TIMEFRAME LABEL
l = label.new(bar_index, close, highLine1, style=label.style_label_center), label.delete(l[1])  

您可以使用str.tostring()函数将您正在谈论的线路的价格和分辨率转换成字符串。

这里是一个显示开盘价和图表分辨率的小例子。

//@version=5
indicator("My script", overlay=true)

lbl_txt = str.tostring(open) + "(" + str.tostring(timeframe.period) + ")"

var label label1 = na
label1 := label.new(bar_index, low, text=lbl_txt, yloc=yloc.abovebar)
label.delete(label1[1])