0
votes

I understand buy stop appears when the price is above the current market price.

I need to understand why does the Buy stop order appear here. I don't quite understand how does RSI Signal Indicator affects the buy stop ?

This is the code

//if above 70 , overbought , if below 30, then oversold
strategy(title="Stop Order RSI Strategy", shorttitle="Stop Order RSI", format=format.price, precision=2)

//Inputs
i_oversold = input(30 , title="Oversold")
i_overbought = input(70 , title="Overbought")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)

//Calculations
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//Plotting
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

goLong = rsi < i_oversold
goShort = rsi > i_overbought
strategy.entry("Buy Stop" , stop=high , when = goLong , long = true)
strategy.entry("Sell Stop" , stop=low , when = goShort , long = strategy.short)

enter image description here

Appreciate if someone can take their time to explain what is going on here because I am very lost.

1
I've put that piece of code into a strategy. It doesn't generate a signal at that bar. Please edit your question and post the whole script. - Bjorn Mistiaen
@BjornMistiaen I have already updated and pasted the whole script. Thank you very much for looking into it. - desh

1 Answers

1
votes

Your script is behaving correctly.
This is my rendition of your code, with added //@version=4 as the first line, and a debug section at the end.

//@version=4
//if above 70 , overbought , if below 30, then oversold
strategy(title="Stop Order RSI Strategy", shorttitle="Stop Order RSI", format=format.price, precision=2)

//Inputs
i_oversold = input(30 , title="Oversold")
i_overbought = input(70 , title="Overbought")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)

//Calculations
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//Plotting
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

goLong = rsi < i_oversold
goShort = rsi > i_overbought
strategy.entry("Buy Stop" , stop=high , when = goLong , long = true)
strategy.entry("Sell Stop" , stop=low , when = goShort , long = strategy.short)

// === DEBUG ===
plotchar(rsi, "rsi", "")
plotchar(i_oversold, "i_oversold", "")
plotchar(i_overbought, "i_overbought", "")
plotchar(goLong, "goLong", "")
plotchar(goShort, "goShort", "")

When you hover over a bar, the debug section shows the value of the variables at that bar in the data window, like this: Data window

On 11 Mar '20 you see that goLong is 0.

On 12 Mar '20 you see that goLong becomes 1.
That means that Pine will look to go long on the next bar, with a stop order at the high of the current bar.
So, it's looking to go long on 13 Mar '20 at 16.6427452, which is the high of 12 Mar '20.
On 13 Mar '20, this stop price is never reached, so it does not go long.

On that same 13 Mar '20, you see that goLong is still 1.
The same process repeats. Pine will look to go long on the next bar, with a stop order at the high of the current bar. So, it's looking to go long on 14 Mar '20 at 12.3718699, which is the high of 13 Mar '20.
Again, on 14 Mar '20, this stop price is never reached, so it does not go long.

On 14 Mar '20, you see that goLong is still 1.
The same process repeats again. Pine will look to go long on the next bar, with a stop order at the high of the current bar. So, it's looking to go long on 15 Mar '20 at 11.4392802, which is the high of 14 Mar '20.
This time, on 15 Mar '20, this stop price is reached, and the system enters a long position.