Cross above VWAP after three candles below VWAP


Category:
0
0

Hi,

The following should output a value of 75 when the following condition is met:  current candle closes above VWAP and 3 previous candles close below VWAP. It doesn’t work and I would like to figure out why and correct it. Any help appreciated.

declare lower;
def VWAPstudy = Reference VWAP();
def condition = VWAPstudy < VWAPstudy[1] and
close crosses above VWAPstudy and
sum(close[1] < VWAPstudy[1], 3) >= 3;
plot signal = If condition then 75 else 0;

 

 

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on September 4, 2020 10:51 am
251 views
0
Private answer

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:

  1. current vwap value is less than the previous bar's vwap value
  2. current close crosses above vwap value
  3. 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);

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on September 4, 2020 3:45 pm
0
Hi Peter. The problem with my code was that it would fail to find some expected matches. From your explanation of my code it seems this must be due to item 1 (current vwap value being less than previous bars vwap). Your new code behaves as I wanted. Many thanks for taking the time to explain and answer this.
( at September 4, 2020 10:48 pm)
0
Hi Pete, How would you tweak the above scan and arrow on the chart to show the opposite (Cross below VWAP after three candles above) Thanks again!
( at December 1, 2021 8:48 am)
0
That requires much more than a "tweak". Every line of code needs to be replaced.
( at December 1, 2021 2:56 pm)