Abhay Sahu
Abhay Sahu

Reputation: 11

Pinescript ATR of different securities

I wanted to use volatility of index for trade filters in my strategy.

In my pinescript code, I can get OHLC values of other stocks/index through security() function. How do I calculate ATR values of the same stock/index. Pinescript ATR function only has argument of length which calculates ATR of chart security which as been selected for backtest. How do I calculate ATR for foreign securities in pinescript?

Upvotes: 1

Views: 1084

Answers (1)

vitruvius
vitruvius

Reputation: 21342

Pass ta.atr() as the expression parameter to request.security() function. So, the security function would call the atr function on that symbol.

Here, I'm on BINANCE:BTCUSDT and request the ATR data of FX:EURUSD. You can see that it plots the atr value of FX:EURUSD and not BINANCE:BTCUSDT.

//@version=5
indicator("My Script", overlay=true, precision=5)
atr_eurusd = request.security("FX:EURUSD", "1D", ta.atr(14))
plot(atr_eurusd)

enter image description here

Upvotes: 2

Related Questions