Home > Mobile >  Trying to run a function based on the price only for the last candle
Trying to run a function based on the price only for the last candle

Time:07-22

I have a block of conditions that is trying to identify in which 'channel' the current price is. But for some reason, it is not run only for the last price candle. What I mean is that I have fib calculated on multiple channels while the current price can only be in one channel. If the price went through the channel in the past I don't want to consider it. Any ideas ?

if (close[0] >= sma350_0382[0] and close[0] <= sma350_050[0])
    Calculate_fib(sma350_0382, sma350_050)
if (close[0] >= sma350_0786 and close[0] <= sma350)
    Calculate_fib(sma350_0786, sma350)
if (close[0] >= sma350 and close[0] <= sma350_1272)
    Calculate_fib(sma350, sma350_1272)
if (close[0] >= sma350_1272 and close[0] <= sma350_1618)
    Calculate_fib(sma350_1272, sma350_1618)
.....

CodePudding user response:

You will have to put one more condition to check if this is the last bar. Other wise close[0] will pick up each of the historical candle.

if barstate.islast

CodePudding user response:

Create boolean variables to remember if a channel has been visited and find a condition where those booleans have to be reset.

var bool channel1 = false
var bool channel2 = false
var bool channel3 = false
...

if (close[0] >= sma350_0382[0] and close[0] <= sma350_050[0] and not channel1)
    Calculate_fib(sma350_0382, sma350_050)
    channel1 := true
if (close[0] >= sma350_0786 and close[0] <= sma350 and not channel2)
    Calculate_fib(sma350_0786, sma350)
    channel2 := true
if (close[0] >= sma350 and close[0] <= sma350_1272 and not channel3)
    Calculate_fib(sma350, sma350_1272)
    channel3 := true

// condition to reset the channels
if condition
   channel1 := false
   channel2 := false
   channel3 := false
    ...
  • Related