绘图后的股票代码

Ticker labels after plot

我非常感谢您帮助我在下面的代码绘图后添加一个代码标签。下面是主要代码的摘录——我只使用了 1 个代码的行,但实际上,我总共有 32 个符号,所以为了这个查询的目的,我省略了其他代码的不必要的代码重复.

对其余代码的任何改进也将不胜感激。我使用 ticker.new 作为 1) 我不需要输入,而且当会话被列为扩展时(即使图表已经设置为扩展),图表似乎只正确显示)

//@version=5
indicator('NASDAQ Trend', overlay=true)

// SYMBOLS // 
s01 = ticker.new("NASDAQ", "AAL", session.extended)

// CALCULATIONS //
screener_func() =>
    //STACKED EMAs
    MA1 = ta.ema(close, 5)
    MA2 = ta.ema(close, 8)
    MA3 = ta.ema(close, 13)
    MA4 = ta.ema(close, 21)
    MA5 = ta.ema(close, 34)
    MA_Stack_Up = (MA1 > MA2) and (MA2 > MA3) and (MA3 > MA4) and (MA4 > MA5)
    
    //CONDITIONS
    Uptrend = MA_Stack_Up
    Reversal = ((MA1 < MA2) and (MA2 > MA3)) or ((MA1 > MA2) and (MA2 < MA3))
    
    //COLOR CODING
    Bar_Color = Uptrend ? color.new(color.green, 25) : Reversal ? color.new(color.yellow, 25) : color.new(color.red, 25)
    
    [Bar_Color]

// Security call
[TS01]= request.security(s01, timeframe.period, screener_func())

// PLOTS //
l_width = 3
shape = plot.style_circles
plot(1, color=TS01, style=shape, linewidth=l_width)

//LABELS//
L1= label.new(bar_index, 1, text=s01, style=label.style_none, textcolor=color.new(color.white, 0), size=size.small)
label.delete(L1[1])

我的问题是生成的标签是 ={"session":"extended","symbol":"NASDAQ:AAL"}。理想情况下,标签应该只是 AAL

您可以编写一个函数,从该字符串中提取您感兴趣的部分。如果您的字符串始终具有相同的格式 ={"session":"extended","symbol":"NASDAQ:AAL"}.

第 1 步: 使用 : 作为分隔符拆分字符串。然后你将有 4 个子字符串。

  • ={"session"
  • "extended","symbol"
  • "NASDAQ
  • AAL"} <-- 这就是你想要的 (索引: 3)

第 2 步: 删除最后两个字符。由于最后两个字符总是 "},return 一个字符串,直到最后两个字符。

getName(_str) =>
    string[] _pair = str.split(_str, ":")
    string[] _chars = str.split(array.get(_pair, 3), "")  // Index 3
    int _len = array.size(_chars) - 2  // Don't get the last two chars
    string[] _substr = array.new_string(0)
    _substr := array.slice(_chars, 0, _len)
    string _return = array.join(_substr, "")

然后在创建标签时调用这个函数:

//LABELS//
L1= label.new(bar_index, 1, text=getName(s01), style=label.style_none, textcolor=color.new(color.white, 0), size=size.small)
label.delete(L1[1])

当自动收报机可用时,做所有这些弦乐体操毫无意义。试试这个:

//@version=5
indicator('NASDAQ Trend', overlay=true)

// CALCULATIONS // {
// hey look – it's now foldable....
screener_func() =>
    // STACKED EMAs
    MA1 = ta.ema(close, 5)
    MA2 = ta.ema(close, 8)
    MA3 = ta.ema(close, 13)
    MA4 = ta.ema(close, 21)
    MA5 = ta.ema(close, 34)

    // CONDITIONS - no sense in allocating extra variables when hidden in function
    Uptrend = (MA1 > MA2) and (MA2 > MA3) and (MA3 > MA4) and (MA4 > MA5)
    Reversal = ((MA1 < MA2) and (MA2 > MA3)) or ((MA1 > MA2) and (MA2 < MA3))
    
    // DETERMINE COLOR CODING - just return the results 
    // vs assigning to a variable and then return the variable...
    [Uptrend ? color.new(color.green, 25) : Reversal ? color.new(color.yellow, 25) : color.new(color.red, 25)]
    
// }

// Define tickers and set up labels on last bar
// Note: request security limits the max number of tickers to 40
var tickers  = array.from("AA","AAL","AAPL")

// use the index of the symbol in the ticker array as price level to plot...
if barstate.islast
    for item in tickers
        label.new(bar_index+ 5, array.indexof(tickers,item), item, style=label.style_none, textcolor=color.new(color.white, 0), size=size.small)

// Since the ticker symbol must be know at complile time aka simple string,
// we have to call each symbol separately - what a pain...

// style variations here in case we want to change
var aStyle = plot.style_circles
var aLinewidth = 3

// AA  <-- Yep this is definitely not NASDAQ material...
temp1 = ticker.new("NYSE", "AA", session.extended)
[temp2] = request.security(temp1, timeframe.period, screener_func())
plot(0, color= temp2, style=aStyle, linewidth=aLinewidth)
    
// AAL
temp3 = ticker.new("NASDAQ", "AAL", session.extended)
[temp4] = request.security(temp3, timeframe.period, screener_func())
plot(1, color= temp4, style=aStyle, linewidth=aLinewidth)
    
// AAPL
temp5 = ticker.new("NASDAQ", "AAPL", session.extended)
[temp6] = request.security(temp5, timeframe.period, screener_func())
plot(2, color= temp6, style=aStyle, linewidth=aLinewidth)

output image