'How to request data from another security to show correctly with request.security() - Pine Script v5 - TradingView
Does anyone know if it's possible to request string data using request.security() to get information on multiple symbols at the same time? The script I have compiles with no errors, but the problem is some of the data coming through in the alert message is for the main symbol and not for the security symbols. Example: If I set an alert and the symbol that happens to be on the chart at the time I created the alert, say AAPL, then every time the alert is triggered for any one of the symbols in the list (the "security symbols") it will show the exchange (syminfo.prefix) for AAPL every time and not for the symbols in the list, such as COINBASE:BTCUSD = NASDAQ.
I've tried everything I can think of, but cannot get the correct info to come through. Or I will get an error. This error comes up the most: "Type series__string cannot be used as tuple element in request.security 'expression' argument" and after reading the documentation I think I understand why I can't reqest string in tuple. So, is it even possible to request information like syminfo.prefix on multiple symbols?
Most of the script is from a TradingView blog post: https://www.tradingview.com/blog/en/our-new-alerts-allow-for-dynamic-messages-22588/
My Script:
//@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")
Solution 1:[1]
syminfo.* variables are linked to the current chart and it's not possible to pass it in a security function. You need to do a work around. Se code below as an exemple.
`
// 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
