Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

using of "var"

Mar 22, 2021 - 6:50 PM

Viewed 276 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/23312 COPY
  • Hi, I can't understand why e and maxmacd have different values. They should be equal.
    Can someone help me?:(
    study(My Script)
    a = input(26, title=slow average)
    b = input(12, title=fast average)
    c = input(9, title=Macd average)
    slow_average=ema(close,a)
    fast_average=ema(close,b)
    Macd=fast_average-slow_average
    aMacd=ema(Macd,c)

    var float maxmacd=0

    if crossunder(Macd,aMacd)
    maxmacd:=highest(high,5)
    e=valuewhen(crossunder(Macd,aMacd),highest(high,5),0)

    plot(maxmacd)

    plot(e)

    0
  • Hi

    Not sure about my answer

    This is because the highest in the if crossunder statement is executed at the crossunder only, whereas the highest in the valuewhen is executed at every candle.

    I know the valuewhen is built so that it's executed at the crossunder too but still ... I believe the discrepancy comes from the number of times this highest function is executed

    I would take this highest(high, 5) out of the IF condition so that it's executed at every candle

    //@version=4
    study("My Script")
    a = input(26, title="slow average")
    b = input(12, title="fast average")
    c = input(9, title="Macd average")
    slow_average=ema(close,a)
    fast_average=ema(close,b)
    Macd=fast_average-slow_average
    aMacd=ema(Macd,c)
    
    var float maxmacd=0
    
    
    my_high = highest(high,5)
    
    if crossunder(Macd,aMacd)
        maxmacd:=my_high
    
    e=valuewhen(crossunder(Macd,aMacd),my_high, 0)
    
    plot(maxmacd)
    
    plot(e)
    
    0
  • Dave (@dave):
    Hi
    Not sure about my answer
    This is because the highest in the if crossunder statement is executed at the crossunder only, whereas the highest in the valuewhen is executed at every candle.
    I know the valuewhen is built so that it's executed at the crossunder too but still ... I believe the discrepancy comes from the number of times this highest function is executed
    I would take this highest(high, 5) out of the IF condition so that it's executed at every candle

    Now I can understand, thanks a lot!!! :)

    1
CONTENTS