簡體   English   中英

如何使用 request.security() 從另一個證券請求數據以正確顯示 - Pine Script v5 - TradingView

[英]How to request data from another security to show correctly with request.security() - Pine Script v5 - TradingView

有誰知道是否可以使用 request.security() 請求字符串數據以同時獲取有關多個符號的信息? 我編譯的腳本沒有錯誤,但問題是警報消息中的一些數據是針對主要符號而不是安全符號的。 示例:如果我設置了一個警報並且在我創建警報時碰巧出現在圖表上的符號,比如 AAPL,那么每次都會為列表中的任何一個符號觸發警報(“安全符號” ) 它每次都會顯示 AAPL 的交換 (syminfo.prefix) 而不是列表中的符號,例如 COINBASE:BTCUSD = NASDAQ。

我已經嘗試了所有我能想到的方法,但無法獲得正確的信息。 否則我會得到一個錯誤。 這個錯誤出現得最多:“Type series__string cannot be used as tuple element in request.security 'expression' argument” 閱讀文檔后,我想我明白了為什么我不能在元組中請求字符串。 那么,是否有可能在多個符號上請求諸如 syminfo.prefix 之類的信息?

大部分腳本來自 TradingView 博客文章: https://www.tradingview.com/blog/en/our-new-alerts-allow-for-dynamic-messages-22588/

我的腳本:

//@version=5

indicator('trigger RSI alert for multiple symbols')

// Just testing to see if something like this would work
my_func(_ticker) =>
    dTicker = str.tostring(syminfo.ticker) + ' : '
    dTitle = str.tostring(syminfo.prefix)
    returnString = ' test ' + dTicker + dTitle 
    returnString

// Most of the following is from the TradingView blog post
f_triggerRsi(_ticker) =>
    _r = ta.rsi(close, 14)
    _x = ta.crossover(_r, 60)
    _y = ta.crossunder(_r, 40)
    _p = close
    _c = syminfo.prefix
    _rt = barstate.isrealtime
    _tn = timenow
    // Trying few things:
    //_s = syminfo.prefix (can't use string in tuple error message)  
    //_ts = timenow (doesn't work)
    //_j = str.split(syminfo.ticker, ":") 
    
    [_rsi, _co, _cu, _px, _rt_bar] = request.security(_ticker, timeframe.period, [_r, _x, _y, _p, _rt])
    _msg = _ticker + ', ' + timeframe.period + ': ' + 'Test: ' + my_func(_ticker) 
    if _co and _rt_bar
        _msg += 'RSI (' + str.tostring(_rsi) + ') crossing up 60 level ' + 'Price: ' + str.tostring(_px, "#.##") + 'Test1: ' + syminfo.prefix 
        alert(_msg, alert.freq_once_per_bar) 

    else if _cu and _rt_bar
        _msg += 'RSI (' + str.tostring(_rsi) + ')  crossing down 40 level ' + 'Price: ' + str.tostring(_px, "#.##") + 'Test2: ' + syminfo.prefix 
        alert(_msg, alert.freq_once_per_bar) 
       
// This script currently returns:
//  GOOD: ticker correctly with _ticker
//  GOOD: RSI value correctly with _rsi
//  GOOD: timeframe correctly (but it's inherantly correct)
//  BAD: exchange incorrectly, returns the exchange of the main symbol and not the value of the security symbols
//  BAD: timestamp is always the time of the main symbol not the security symbols 

// Data I need in the alert message
//  a) tickerid that triggered the alert (NASDAQ:AAPL, COINBASE:BTCUSD, etc.)
//  b) timeframe that the ticker is on (Daily, 30 Minute, etc.)
//  c) the exchange separately without the ticker (syminfo.prefix)
//  d) the symbol without the exchange (syminfo.ticker)
//  d) timestamp - the time the alert for the security tickerid was triggered at the exchange time

// Security Symbols 
f_triggerRsi(syminfo.tickerid) 
f_triggerRsi('NASDAQ:MSFT') 
f_triggerRsi('NYSE:PLTR')
f_triggerRsi('NASDAQ:AAPL')
f_triggerRsi('NASDAQ:DWAC')
f_triggerRsi('NASDAQ:PHUN')
f_triggerRsi('NASDAQ:SBEA')
f_triggerRsi('NASDAQ:SEAC')
f_triggerRsi('NASDAQ:CFVI')
f_triggerRsi('NASDAQ:MSFT')
f_triggerRsi('NASDAQ:NVDA')
f_triggerRsi('NASDAQ:FB')
f_triggerRsi('NASDAQ:AMD')
f_triggerRsi('NASDAQ:TSLA')
f_triggerRsi("COINBASE:BTCUSD")
f_triggerRsi("COINBASE:ETHUSD")
f_triggerRsi("COINBASE:LTCUSD")
f_triggerRsi("BINANCE:SOLUSDT")
f_triggerRsi("FTX:RAYUSD")
f_triggerRsi("BINANCE:ICPUSDT")

syminfo.* 變量鏈接到當前圖表,並且不可能在證券 function 中傳遞它。您需要解決這個問題。 以下面的 Se 代碼為例。

`
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Phwi
//@version=5
indicator("Get Prefix with out using syminfo.prefix", overlay = false)

comp_override = input(defval=false, title='Override Auto Detection of Index')
sym = input.symbol('OMXCOP:OMXC25', title='Index override')


f_BSMA(symbol) =>


    pos_start = str.pos(symbol, "")  // Get start position of ":" character
    pos_stop = str.pos(symbol, ":")  // Get position of ":" character. Stop position

    tkr= str.substring(symbol, pos_start, pos_stop) // Get Exchange
    
    ex = str.replace_all(str.replace_all(tkr, '_DLY', ''), '_DL', '')  // "_DLY" suffix in syminfo.prefix workaround

    //Get Indeks to compare to
    comparativeTickerId = comp_override and sym != '' ? sym : syminfo.type == 'index' ? 'NYSE:OOI' : syminfo.type == 'cfd' ? 'NYSE:OOI' : syminfo.type == 'forex' ? 'INDEX:DXY' : ex == 'LSE' ? 'UKXGBP' : ex == 'ASX' ? 'ASX:AST' : ex == 'OSL' ? 'OSL:OBX' : ex == 'OMXSTO' ? 'OMXSTO:OMXS30' : ex == 'OMXCOP' ? 'OMXCOP:OMXC25' : ex == 'OMXHEX' ? 'OMXHEX:OMXH25' : ex == 'OMXICE' ? 'OMXICE:OMXI10' : ex == 'XETR' ? 'XETR:DAX' : ex == 'FWB' ? 'XETR:DAX' : ex == 'SWB' ? 'XETR:DAX' : ex == 'EURONEXT' ? 'EURONEXT:PX1' : ex == 'EURONEXT' ? 'EURONEXT:AEX' : ex == 'EURONEXT' ? 'EURONEXT:BEL20' : ex == 'EURONEXT' ? 'EURONEXT:PSI20' : ex == 'DJCFD' ? 'AMEX:SPY' : ex == 'NYSE' ? 'AMEX:SPY' : ex == 'BATS' ? 'AMEX:SPY' : ex == 'NASDAQ' ? 'AMEX:SPY' : ex == 'OTC' ? 'AMEX:SPY' : sym  // last entry is the fallback, no match will default to the override ticker as comparison. by default OMXC25
  
   

    if barstate.islastconfirmedhistory
        label.new(bar_index, high, text =  comparativeTickerId)

   
f_BSMA(syminfo.tickerid)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM