Search code examples
pine-scriptpine-script-v4

How to make a variable true or false in the "if" function in pine editor?


A very big problem I encountered when trading strategy in tradingview that I could not combine the two strategies. For example, I need a variable to be true whenever the price goes up in the Bollinger Band so that I can use it in the future. If anyone knows the solution to this problem, I will be happy to use his experiences.

if crossover(x > y)
    test = true
if test
    strategy.entry("Short", strategy.short, oca_type=strategy.oca.cancel, comment="Short")

Solution

  • You have to create a mutable variable and initialize it in the global scope first: Note that crossover function accept arguments separated with comma.

    var bool test = false
    if crossover(x, y)
        test := true
    if test
        ...
    

    or

    bool test = crossover(x, y) ? true : false
    if test
        ...