Page 1 of 1

Ratio between two symbols

Posted: Mon Mar 14, 2022 9:21 pm
by Alex100
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)

Re: Ratio between two symbols

Posted: Tue Mar 15, 2022 9:32 am
by processingclouds
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)


Re: Ratio between two symbols

Posted: Tue Mar 15, 2022 10:15 am
by Alex100
Works great, thank you kindly!

Alex