0

The ideals are:

  • Draw red area on the screen when K is over 80 - no new red area will be drawing until K is reached under 20 before that
  • Draw green area on the screen when K is under 20 - no new green area will be drawing until K is reached under 80 before that

Currently I'm trying to limit the draw of the signal on the screen by changing the value of variables. But it does not seem to work as expected. Please help me identify the wrong on the logic and correct it here is the code

lookback = input(title = 'Period', defval = 13)
highest = ta.highest(high, lookback)
lowest = ta.lowest(low, lookback)
stochastic_K = ((close - lowest) / (highest - lowest)) * 100
upbefore = false
downbefore = false
// Im trying to change the variable value in there:

if stochastic_K > 80
    upbefore := true
    downbefore := false
if stochastic_K < 20
    upbefore := false
    downbefore := true

//color change
readyforbuy = downbefore == true
readyforsell = upbefore == true

backgroundcolor = readyforbuy? color.rgb(33, 149, 243, 63): readyforsell? color.rgb(255, 82, 82, 54): na
// Draw area
bgcolor(color = backgroundcolor)

Where did it goes wrong

As you can see from the image above, although the Stochastic is not going under 20 yet, but the green area still being draw. And I want to prevent that to improve my signal.

1 Answers1

0

In Pine Script, variables resets on each new bar. That means that if you use your code, each time stochastic_K > 80 is not met, upbefore will be equal to false regardless if stochastic_K under 20 or not.

If you want to save the value of stochastic_K and change it only if it goes under 20 than you'll need to use the var keyword. Same goes for the downbefore variable. It will be equal to false each time the condition stochastic_K < 20 is not met.

lookback = input(title = 'Period', defval = 13)
highest = ta.highest(high, lookback)
lowest = ta.lowest(low, lookback)
stochastic_K = ((close - lowest) / (highest - lowest)) * 100

var upbefore = false
var downbefore = false

// Im trying to change the variable value in there:

if stochastic_K > 80
    upbefore := true
    downbefore := false
if stochastic_K < 20
    upbefore := false
    downbefore := true

//color change
readyforbuy = downbefore == true
readyforsell = upbefore == true

backgroundcolor = readyforbuy? color.rgb(33, 149, 243, 63): readyforsell? color.rgb(255, 82, 82, 54): na
// Draw area
bgcolor(color = backgroundcolor)
mr_statler
  • 1,913
  • 2
  • 3
  • 14