Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Ratio between two symbols

Hello!

I'd like to plot the ratio between the close of the current symbol and the close of the S&P 500 'SPY' ETF. However, this ratio should be calculated ONLY on the DOWN days of the current symbol. When an UP day is encountered, the script should plot the ratio calculated on the LAST DOWN DAY, this way keeping the plotted line uninterrupted and unchanged.

Below you can see the code I have so far. The only problem is that on UP days, instead of plotting the ratio calculated on the last down day, it plots a ZERO (constantly breaking down the plotted line).

Can anyone help me, please? I'm new to Pine Scripting.

Thank you!

Alex

Code: Select all

study(title="My Script")
spy_close = security("SPY", period, close)
value = iff (close < close[1], close/spy_close, 0)
plot(value, color=black, transp=0)

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Ratio between two symbols

I hope you are doing great. Here is the indication that you are trying to create:

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
// vast op D tf gezet.
indicator("SPY me Up Down", "UPdn")

var float ratio = na
spyClose = request.security("SPY", timeframe.period, close)

if close < close[1]
    ratio := close/spyClose

plot(ratio, color=color.black)


Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Re: Ratio between two symbols

Works great, thank you kindly!

Alex

Return to “Pine Script Q&A”