Plot intraday high from bar that moving average crosses above VWAP


Category:
0
0

Hi Pete, Thanks for your great coding resources. Please consider this situation. I’m keeping track of the intraday high until a moving average crosses above the VWAP from below. After that I want to stop calculating the intraday high (I use larger timeframes).

I don’t care if the moving average opens above the VWAP, only if it comes down below VWAP and crosses above. In other words, I want to stop calculating the intraday high once the moving average has crossed VWAP from below only.

I can’t use a movingaverage >= VWAP condition because if the moving average opens above the VWAP, I don’t want my code to care about it. Likewise, I can’t use the crosses function because it only considers one candle.

I find that a solution to my problem lies in an if statement if I can make the below “else 0” condition do nothing instead. Is that possible?

def HasItCrossed = if (crosses(movingAverage, VWAP, CrossingDirection.ABOVE)) then 1 else 0;

rec RecordHigh = if (HasItCrossed == 0) then if (high > RecordHigh[1]) then high else RecordHigh[1] else RecordHigh[1]

Another option- see if the movingaverage has crossed above since open? Can that be done?

Thanks!

Marked as spam
Posted by (Questions: 3, Answers: 11)
Asked on May 18, 2020 10:04 am
117 views
0
Private answer

The solution depends on what you are doing with that intraday high after the moving average crosses above the VWAP.

"After that I want to stop calculating the intraday high".

So do you just want the plot that displays the intraday high to stop plotting values after this point?

Or do you want to capture the last value of that intraday high and carry if forward for the rest of the trading session?

Once we understand what you want I will provide the solution here. I will also update the title of your question because it's really confusing and has zero chance of showing up in a search for someone trying to find this specification solution.

 

Edit: The request has been explained in the comment section below. Which is to capture the value of the intraday high at the point the moving average crosses above the VWAP. I really don't like to have posts like this in the forum because the complete code is not provided and nobody else gets to benefit from this solution. We don't provide on-off solutions that only benefit a single person. If you do not wish to publish your code in the forum then you need to submit this as a custom project request. Otherwise, we can provide a solution only after you have provided your fully functional code so that everyone visiting this post in the future is able to make full use of the solution.

Update

Final Edit: The author of the post has provided their fully functioning code in the comment section below. So I now have a solution to this request.

input marketOpen = 930;
input marketClose = 1600;
plot currentVWAP = reference VWAP;
def openCounter = SecondsFromTime(marketOpen);
def closeCounter = SecondsTillTime(marketClose);
def marketHours = if openCounter >= 0 and closeCounter >= 0 then 1 else 0;
plot maOne = MovAvgExponential(close, 15, 0, no);
rec dailyHigh = if (marketHours and maOne < currentVWAP) then if high > dailyHigh[1] then high else dailyHigh[1] else dailyHigh[1];
def beforeExpCross = if (marketHours) then dailyHigh[-1] else Double.NaN;
def newDay = GetDay() <> GetDay()[1];
def crossAbove = maOne[1] < currentVWAP[1] and maOne > currentVWAP;
rec trackHigh = if newDay then beforeExpCross else if crossAbove then Double.NaN else if beforeExpCross > trackHigh[1] then beforeExpCross else trackHigh[1];
plot highBeforeCross = trackHigh;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 18, 2020 10:51 am
0
I’d like to capture the last value and carry it forward. This piece is working for me already if the movingaverage just starts below VWAP, but because I have a condition that validates that this is the case, if the moving average opens above VWAP, the intraday high doesn’t start recording. Hope it’s clear.
( at May 18, 2020 1:09 pm)
0
I have updated my answer to explain the details of your request. However until we have a fully functioning chart study included in this post I will not be able to provide a solution.
( at May 18, 2020 2:29 pm)
0
Hi, here is the chart study. It’s a modification of the hi-lo study I found here (thanks for that it’s really helpful!) Just a bit of clarification. I’m not looking to plot the day high at the point that the moving average crosses the VWAP. I want to continue to plot the day high UNTIL the moving average crosses above the VWAP from below to see the progression of the breakout level. The challenge for me is that the day high is not plotting if the moving average starts above the VWAP. It’s working fine if the day high starts below the VWAP. In other words, I want to continue to plot the day high UNTIL the moving average crosses above the VWAP from below. Is there a way to see if an event has occurred since open, rather than just for the current candle? In this case, I would like to check if the moving average has crossed above the VWAP since open. Here is the code as I can’t seem to edit the first post. [code] input marketOpen = 930; input marketClose = 1600; input priceType = PriceType.LAST; input price = close; input averageType = AverageType.SIMPLE; input trueRangeAverageType = AverageType.SIMPLE; def currentVWAP = reference VWAP; def OpenCounter = SecondsFromTime(marketOpen); def CloseCounter = SecondsTillTime(marketClose); def MarketHours = if OpenCounter >= 0 and CloseCounter >= 0 then 1 else 0; def CrossOfInterest = MovAvgExponential(close, 15, 0, no); rec DailyHigh = if (MarketHours and CrossOfInterest (less than) currentVWAP) then if high > DailyHigh[1] then high else DailyHigh[1] else DailyHigh[1]; plot BeforeExpCross = if (MarketHours) then DailyHigh[-1] else Double.NaN; BeforeExpCross.SetDefaultColor(Color.YELLOW); [/code] The site doesn’t seem to like the less than sign, so please replace (less than) with the less than sign. Thank you!
( at May 23, 2020 11:31 am)
0
I have updated my answer to include the solution based on the code you provided. Four new lines of code were added. Several lines that were not in use were removed and I corrected the camelCase of your variable names.
( at May 23, 2020 2:50 pm)
0
Hi, thanks so much. No plot is showing on the chart though. Does it have something to do with newDay or marketHours? Thanks
( at May 25, 2020 1:00 pm)
0
When I tested it the line plotted up until the point it was supposed to plot and then it stops plotting until the next day begins. It does not work if you include extended hours on the chart. I did not check that before. But that seems to be related to the way your original code was constructed.
( at May 25, 2020 2:57 pm)
0
It’s not plotting anything here, it’s just my normal plot. Can I upload a picture of the chart?
( at May 25, 2020 3:18 pm)
0
There is no way to attach a screenshot to a comment. So you would have to upload the screenshot to a cloud drive and include a link to that in your comment.
( at May 25, 2020 3:39 pm)
0
Please find it here: https://drive.google.com/open?id=1FtlPWdUwfMBIjwJ7nKRVJNNiW-q30mtV
( at May 26, 2020 6:11 am)
0
Yes, I told you the code does not plot the line if you included extended hours on the chart. I stated that in my comment dated May 25. No, I do not have any more time to work on this in the forum so I will not be able to correct the rest of your code to address this.
( at May 26, 2020 8:05 am)
0
It seems to only work on the daily chart... I haven't included extended hours, the marketOpen is set to 930. Sorry please clarify what I have to fix
( at May 26, 2020 8:32 am)
0
Your chart, includes extended hours. That was perfectly clear on the screenshot you provided. This has nothing to do with the time inputs of the study. Remove extended hours from the chart and it works.
( at May 26, 2020 9:38 am)
0
Awesome, I see it now. But when the moving average is above the VWAP, it isn't recording the high accurately, see the left side of the chart here: https://drive.google.com/open?id=1d9ZpQv6dyGbAXbmvp17mYpBrIksI4oMN
( at May 26, 2020 10:35 am)
0
Sorry but I have already explained I have no time available to work on this. Did you read my previous comment where I clearly stated this? "I do not have any more time to work on this in the forum so I will not be able to correct the rest of your code" I have a limited amount of time to provide a solution to each post in the forum. When I built this and tested it everything worked perfectly for the conditions already specified.
( at May 26, 2020 12:38 pm)
0
Your code didn’t work but I found a fix. I used a compoundvalue sum of crossAbove to know if the cross occurred in the past. Your code only considered the single candle that the cross occurs. With the compound value, I’m able to differentiate when the moving average is above VWAP at market open, vs. after it has crossed above VWAP and I can adjust the daily high accordingly. Thanks for your help.
( at May 26, 2020 2:08 pm)
0
Ok, feel free to post your solution using the box at the bottom of this thread. Thanks!
( at May 26, 2020 2:31 pm)
0
Thank you! Just out of curiousity, what is causing the issue to include extended hours? Is it the getDay() function?
( at May 27, 2020 5:26 am)
0
FYI if anyone is looking to convert this script to consider pre-market, just replace the newDay instantiation to "def newDay = if (SecondsFromTime(marketOpen) == 0) then 1 else 0;". This checks that you are in your first candle regardless of time frame.
( at June 2, 2020 10:19 am)