1

I have to plot entries and exits by simulating a strategy within an indicator. The strategy buys if the candle is green and sells when the RSI indicator is overbought. The script plots each signal but does not follow a real strategy in which a new order is only opened if the previous one is closed. I have to teach my script this; each entry corresponds to an exit. To do this I declared a bool variable "isLong" which becomes "true" when the entry condition is met. Then the close of the trade and the corresponding signal should only be plot if the trade has been opened so I have declared that the close should only occur if "isLong==true".

The script still does not work correctly. Signals are opened and closed without following logic. I cannot find any examples of this online. I would be pleased and grateful if someone could explain to me how to configure this part of the code by explaining the reasoning.

//@version=5
indicator("Test", overlay = false)

isLong = false 

entryCondition = close > open 
exitCondition = ta.crossover(ta.rsi(close, 14), 70)

if entryCondition 
    isLong := true

if isLong == true and exitCondition 
    isLong := false 

plotshape(entryCondition, color = color.green)
plotshape(exitCondition, color = color.green)
Alex
  • 25
  • 5

2 Answers2

0

Generally, your way of thinking is absolutely correct, so is the structure. One key thing missing: var.
Docs

Normally, a syntax of assignment of variables, which doesn’t include the keyword var, results in the value of the variable being overwritten with every update of the data. Contrary to that, when assigning variables with the keyword var, they can “keep the state” despite the data updating, only changing it when conditions within if-expressions are met.

So in your case you start with isLong = false on each new bar overriding your potential long update. To solve this extend it as follows: var isLong = false.

Notes:
If you want to disallow pyramiding your long positions, meaning there can be only 1 long position, use this:
if entryCondition and isLong == false
By plotting make sure you plot according to the same conditions as is your entry/exit conditionals:

plotshape(entryCondition and isLong == false, color = color.green)
plotshape(isLong == true and exitCondition, color = color.red)


One last note:
It's useful to plot the individual parts of your conditionals in order to make sure they have any chance of fulfilling your conditions. In your simple case this is your rsi. This way you can always check if your conditions are programmed correctly:

plot(ta.rsi(close, 14))
elod008
  • 1,227
  • 1
  • 5
  • 15
0

You should always remember pine is like a for loop that runs every code for each candle. you should use var statement to prevent this problem

by doing this isLong is only false in the first candle and if it change to true it remains that way, until it changes back to false. also use labels, because you can call them from inside a condition, which you can't do for plotshape

//@version=5
indicator("Test", overlay = true)

var long = false 

if close > open and long == false 
     long := true 
     label.new(bar_index, na, yloc = yloc.abovebar, style = label.style_circle, color = color.green, size = size.tiny)
if ta.crossover(ta.rsi(close, 14), 70)
     long := false 
label.new(bar_index, na, yloc = yloc.belowbar, style = label.style_circle, color = color.red, size = size.tiny)

also you can add this statement to first condition to enter when rsi is below 70

and ta.rsi(close, 14) < 70 
madness
  • 11
  • 3