Begin custom VWAP plot from first bar on chart


Category:
0
0

input startDateYyyyMmDd = 20200805;
def beyondStartDate = if GetYYYYMMDD() >= startDateYyyyMmDd then 1 else 0;
plot VWAP = TotalSum(if beyondStartDate then (((high + low + close) / 3) * volume) else 0) / TotalSum(if beyondStartDate then volume else 0);
VWAP.SetDefaultColor(Color.ORANGE);
VWAP.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(1);

Hi, is there a way to replace the current Date criteria in this code with:

DaysFromDate(First(yyyymmdd));#
So that the study will anchor to the 1st bar of whatever chart Time Frame I am viewing? 
My Objective is to get the study to track my 2-day, 3-day views without having to go in and manually change the date in the study.


Thanks for your time!!!
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on August 5, 2020 12:15 pm
386 views
0
Often times we find ourselves trying to solve for a problem that is not the most straight forward solution. When this happens we get stuck in the weeds trying to solve a problem while the real solution is very simple. So tell me about your goal. "so that the study will anchor to the 1 bar of whatever chart time frame I am viewing". So just what does that mean? "anchor". And what is that first bar? Is that the very first bar at the far left side of the chart? Or do you mean the first bar of each trading session?
( at August 5, 2020 1:53 pm)
0
Hi Pete, G'morning. I apologize up front if my vernacular conflates with any code-speak...I am not a coder at all. This is a multi day vwap to use in conjunction with my standard vwap. I flip between 1, 2, 3, 4 and 5 day charts and like to see how this line changes in each of those views...helps to identify support and resistance areas when a stock is on-the-move. "Anchor" may have been the wrong word...sorry for confusion!! Scenario: If I flip to a 2-day chart, I would like for this line to begin on the opening candle 2 days ago and if I flip to a 3-day chart I would like for this line to start its calculations and plotting on the opening candle 3 days ago and so on. Not being a coder, but understanding the BASIC premises of coding, I tried doing my due diligence at solving my problem myself and came across that 'DaysFromDate...' code and have tried to plug that in myself. But, I cannot solve the logic errors that it creates. It may not be the right solution--IDK. Thx!
( at August 6, 2020 5:39 am)
0
PERFECT!!! I see your point...both work exactly the same reference the starting point. You must spend so much time toning down people who over-complicate things. I appreciate your dedication to your craft. Last question...just want to confirm that you are the direct beneficiary of the donations tab on the left side of this page? I never want something for nothing...you've helped me and I would like to help you--just want to make sure those donations go to you. Thanks, Pete!!
( at August 6, 2020 10:22 am)
0
Over-complicate things.... Yes, I resemble that remark. Spent many years doing that until I began to learn the shortcuts. That's why it's so easy for me to spot now. Donations, we don't accept donations because Hahn-Tech is a for profit business. But yes there is a link in the side bar for "Voluntary Contributions". But that is exactly what it's for. I don't provide solutions here in return for voluntary contributions. The only reason it's there is because years ago someone asked for a way to express their appreciation for the free content we publish.
( at August 6, 2020 11:44 am)
0
Private answer

Ok so from the clarifications provided in the comments section above we understand the request is to have the computation begin on the first bar of the chart regardless what number of trading session are displayed on the chart. In order to solve this we don't need to work with any date/time components at all. We simply replace the section that uses the date/time element with a statement that is true from the very first bar all the way through to the last bar on the chart:

def firstBarAndBeyond = BarNumber() >= 1;
plot vwapValue = TotalSum(if firstBarAndBeyond then (((high + low + close) / 3) * volume) else 0) / TotalSum(if firstBarAndBeyond then volume else 0);
vwapValue.SetDefaultColor(Color.ORANGE);
vwapValue.SetStyle(Curve.SHORT_DASH);
vwapValue.SetLineWeight(1);

However there is a way to cut this down even further by completely removing everything in the code that tries to pick a starting point for the computations:

plot vwapValue = TotalSum(((high + low + close) / 3) * volume) / TotalSum(volume);
vwapValue.SetDefaultColor(Color.ORANGE);
vwapValue.SetStyle(Curve.SHORT_DASH);
vwapValue.SetLineWeight(1);

Also notice I have replaced the plot named VWAP with a new variable named vwapValue. Why? Because vwap is a reserved keyword in the Thinkorswim language:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/vwap

We need to be aware of these details. In this case it does not cause a problem. However when you take a reserved keyword and assign it as a variable name you change the way that reserved keyword behaves throughout the rest of the code. That will eventually get you in trouble, even though you will see mistakes like this in the code provided by the built-in chart studies that come with Thinkorswim. (the VWAP chart study being one of them)

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 6, 2020 8:58 am