1

In a nutshell: Once buy condition is true, stay true until selling condition is true, even if buy condition becomes false in between.

Basically I'm using 2 ema strategy and want to display only stoploss ema

maSmall = ta.ema(low, 50)      //1st 50 ema with low as source
maLarge = ta.ema(high, 50)      //2nd 50 ema with high as source

buyCondition = close > maLarge
shortCondition = close < maSmall

Basically I want buyCondition to stay true until shortCondition become true.

But when price is in between both ema my buyCondition on currrent candle becomes false.

I don't want it to let it happen and want buyCondition to become false only when close < maSmall.

Note: I'll use these condition in color variable when plotting the ema's

Amit
  • 13
  • 3

1 Answers1

2

In Pinescript, variables are overwritten on every bar. In your case, buyCondition will be set to false on every bar where the condition close > maLarge is not met, regardless if shortCondition is true or not.

To fix this, you'll need to use the var keyword:

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.

For example:

maSmall = ta.ema(low, 50)
maLarge = ta.ema(high, 50)

var buyCondition = false
var shortCondition = false

if close > maLarge
    buyCondition := true
    shortCondition := false

if close < maSmall
    buyCondition := false
    shortCondition := true
mr_statler
  • 1,913
  • 2
  • 3
  • 14