Please make sure the title you enter for the question explains the context of your request. This will assist the rest of our viewers who are searching for a specific solution. The title you entered originally "Problem with VWAP study" has been updated to "Cross above VWAP after three candles below VWAP".
Rather than try to fix your the code you provided I will create my own solution from scratch:
def myVwap = Reference VWAP();
def crossAbove = close[1] < myVwap[1] and close > myVwap;
def closeBelow = close < myVwap;
plot signal = if Lowest(closeBelow[1], 3) > 0 and crossAbove then 75 else 0;
If you are interested to know the plain English version of the code you provide I will explain that here:
- current vwap value is less than the previous bar's vwap value
- current close crosses above vwap value
- the close of the previous three or more bars must below the vwap value
In most cases where the close crosses above the vwap value the vwap value of the current bar is greater than the vwap value of the previous bar. Your code works exactly as it was designed. You did not provide any details explaining what it failed to do so this is about all the assistance I can provide.
Edit: Someone else posted a new question asking how to convert this code to plot as an arrow on the chart. Rather than respond to that request I will include it here since I believe that most viewers will prefer the arrow on the chart versus the value of 75 or zero.
All you need to do is replace the last statement from the code listed above with the following two lines:
plot signal = Lowest(closeBelow[1], 3) > 0 and crossAbove;
signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);