Search code examples
pine-script

How to add a condition to control entry or not after a condition matched


Is it available that add a judgement condition after a condition matched?

For example: In Day timeframe, I want to set the stratgey.entry if Close > Open and judge that if next day's open > close[1].

In code, it seems like..

LongCondition = close > open

As is:
If LongCondition
 strategy.entry('long',...)

To be:
If LongCondition
 If Open > close[1]

This is not work, how to achieve that condition after condition match?


Solution

  • You need to look at the historical data of your first condition with the History reference operator [].

    So, if your first condition is:

    first_condition = close > open
    

    And if your second (judgement condition) is:

    second_condition = open > close[1]
    

    Then you would do:

    buy_condition = first_condition[1] and second_condition
    
    if (buy_condition)
        strategy.entry("Long", strategy.long)
    

    first_condition[1] would tell you if your first condition was true on the previous bar. And then you can and your judgement condition with that and enter the trade.