Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

VWAP - end time referring to next day

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    VWAP - end time referring to next day

    Hi,

    I'm trying to configure the VWAP indicator here: http://www.ninjatrader.com/support/f...catid=4&id=335

    I ran into a problem when setting the start and end times. Because of my time zone, I would like the start time to be 16:00 and the end time to be 07:00 (the next day), but the indicator seems to be programmed to assume that start and end times refer to the same, hence the VWAP does not get plotted.

    Is there a quick way to amend the ninjascript so that if the end time is "less than" the start time, it refers to the next day?

    Thanks!

    #2
    Hello wintermelon,

    Thank you for your post.

    Unfortunately, the Start and End Time are set to hh:mm:ss so there wouldn't be a way to set that to check for the next day. However, it may be possible to create a new setting for the DayOfWeek. The DayOfWeek setting could be used to check the dates against each other and set the Start and End Times to specific days as well as times.

    There may be an even simpler method to achieve what you wish to do here. What is the Time Zone of your PC set to? And what instrument or instruments are you applying the VWAP to?

    If you can set your Time Zone on your PC to match the Time Zone of the market you wish to use the VWAP on you could get around changing the code of the VWAP.

    Please let me know if you have any questions.

    Comment


      #3
      I'll be applying the VWAP to U.S. e-mini futures and eurex futures (fdax, fesx). Since I may trade instruments in other time zones as well, it may not be that feasible to change my PC timezone to a particular market.

      I was thinking whether instead of adding an additional parameter, would it be simpler to just add 1 to make it the next day if the end time is less than start time.

      Currently the code is
      Code:
      sessionend = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, Bars.Session.NextEndTime.Hour, Bars.Session.NextEndTime.Minute, Bars.Session.NextEndTime.Second);
      startTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, startTime.Hours, startTime.Minutes, startTime.Seconds);
      endTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, endTime.Hours, endTime.Minutes, endTime.Seconds);
      				
      // Checks to ensure End Time is not before Start Time: if it is set End Time to SessionEnd Time
      if (DateTime.Compare(endTimeDate, startTimeDate) < 0)
      	endTimeDate = sessionend;
      I tried changing the IF part to
      Code:
      // Checks to ensure End Time is not before Start Time: if it is set End Time to SessionEnd Time
      if (DateTime.Compare(endTimeDate, startTimeDate) < 0)
      	endTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day + 1, endTime.Hours, endTime.Minutes, endTime.Seconds);
      Basically I added 1 to the day of the endTimeDate variable. I compiled the code but it doesn't work. The VWAP line disappears instead.

      Comment


        #4
        Hello wintermelon,

        Thank you for your response.

        In this case you would need to add in the use of DayOfWeek in order to be able to span across multiple sessions.

        For information on DayOfWeek please visit the following link: http://msdn.microsoft.com/en-us/libr...dayofweek.aspx

        Please let me know if I may be of further assistance.

        Comment


          #5
          Hi Patrick,

          It does not span across multiple sessions, the VWAP would still be within the same session for the instrument.

          For example, say my PC timezone is +8 GMT. The U.S. market would be open from 22:30 to 05:00 on my ninjatrader (i.e. 09:30 to 16:00 EST). While it spans across the 12 midnight mark, it is still within the same trading session.

          In this case, is using the DayOfWeek still necessary? What is the reason why simply incrementing the day by 1 doesn't work (ignoring the end of month / end of year issues for now)?

          Comment


            #6
            Hello wintermelon,

            Thank you for your response.

            Incrementing the day by 1 should work, but remember you going to need to do this for the sessionend as well.
            Code:
            				sessionend = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day + 1, Bars.Session.NextEndTime.Hour, Bars.Session.NextEndTime.Minute, Bars.Session.NextEndTime.Second);
            Please let me know if adding the + 1 to sessionend resolves this matter.

            Comment


              #7
              Originally posted by wintermelon View Post
              I'll be applying the VWAP to U.S. e-mini futures and eurex futures (fdax, fesx). Since I may trade instruments in other time zones as well, it may not be that feasible to change my PC timezone to a particular market.

              I was thinking whether instead of adding an additional parameter, would it be simpler to just add 1 to make it the next day if the end time is less than start time.

              Currently the code is
              Code:
              sessionend = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, Bars.Session.NextEndTime.Hour, Bars.Session.NextEndTime.Minute, Bars.Session.NextEndTime.Second);
              startTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, startTime.Hours, startTime.Minutes, startTime.Seconds);
              endTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day, endTime.Hours, endTime.Minutes, endTime.Seconds);
               
              // Checks to ensure End Time is not before Start Time: if it is set End Time to SessionEnd Time
              if (DateTime.Compare(endTimeDate, startTimeDate) < 0)
                  endTimeDate = sessionend;
              I tried changing the IF part to
              Code:
              // Checks to ensure End Time is not before Start Time: if it is set End Time to SessionEnd Time
              if (DateTime.Compare(endTimeDate, startTimeDate) < 0)
                  endTimeDate = new DateTime(Bars.Session.NextBeginTime.Year, Bars.Session.NextBeginTime.Month, Bars.Session.NextBeginTime.Day + 1, endTime.Hours, endTime.Minutes, endTime.Seconds);
              Basically I added 1 to the day of the endTimeDate variable. I compiled the code but it doesn't work. The VWAP line disappears instead.
              That is not where the problem lies, as those times are absolute DateTimes. The problem lies in where they used NT functions to change the DateTime structures. Modify those lines to also use absolute DateTime structures.

              These are the lines:
              Code:
                 // If Time is outside range of Start Time and End Time do not calculate further
                 if (ToTime(Time[0]) < ToTime(startTimeDate) || (ToTime(endTimeDate) != 0 && ToTime(Time[0]) > ToTime(endTimeDate)))
                  return;
                 //Recalculates VWAP on start of every new session   
                 if ((useSessionBegin && Bars.FirstBarOfSession && FirstTickOfBar) ||
                  (useSessionBegin == false && FirstTickOfBar && ToTime(Time[1]) < ToTime(startTimeDate) && ToTime(Time[0]) >= ToTime(startTimeDate)))
              Just remove the "ToTime" directives.

              Try this:
              Code:
                 // If Time is outside range of Start Time and End Time do not calculate further
                 if (Time[0] < startTimeDate || Time[0] > endTimeDate)
                  return;
                 //Recalculates VWAP on start of every new session   
                 if ((useSessionBegin && Bars.FirstBarOfSession && FirstTickOfBar) ||
                  (useSessionBegin == false && FirstTickOfBar && Time[1] < startTimeDate && Time[0] >= startTimeDate))

              Comment


                #8
                Thanks Koganam and Patrick for your help!

                I tried Koganam's code and found that the VWAP line only appears on Feb 12, 2013. It does not appear on other dates.

                After some debugging, I found that the startTimeDate, endTimeDate, and sessionend are ALWAYS Feb 12, 2013 for some reason. The code section that Koganam pointed out served only as a time "filter", the date is totally ignored and only the time is used, so all dates will pass through so long as the time is within bounds. Removing the "ToTime" directives resulted in the date being taken into account in the "filter", and prevented the calculation of VWAP for dates other than Feb 12, 2013.

                Hence I amended it to take into account the additional case where end time is less than start time

                Code:
                // If Time is outside range of Start Time and End Time do not calculate further
                if (DateTime.Compare(endTimeDate, startTimeDate) < 0)			
                {
                	if (ToTime(Time[0]) < ToTime(startTimeDate) && (ToTime(Time[0]) > ToTime(endTimeDate)))
                		return;
                }
                else
                {
                	if (ToTime(Time[0]) < ToTime(startTimeDate) || (ToTime(endTimeDate) != 0 && ToTime(Time[0]) > ToTime(endTimeDate)))
                		return;
                }
                I also commented out the setting of endTimeDate to sessionend so that endTimeDate is unchanged.
                Code:
                // Checks to ensure End Time is not before Start Time: if it is set End Time to SessionEnd Time
                // if (DateTime.Compare(endTimeDate, startTimeDate) < 0)
                //	endTimeDate = sessionend;
                I tested it with various start / end times, both within the same day and across a day boundary, and it seems to work fine now.

                Thanks Koganam for pointing out that key section that was causing the problem!

                Comment


                  #9
                  Originally posted by wintermelon View Post
                  Hi,

                  I'm trying to configure the VWAP indicator here: http://www.ninjatrader.com/support/f...catid=4&id=335

                  I ran into a problem when setting the start and end times. Because of my time zone, I would like the start time to be 16:00 and the end time to be 07:00 (the next day), but the indicator seems to be programmed to assume that start and end times refer to the same, hence the VWAP does not get plotted.

                  Is there a quick way to amend the ninjascript so that if the end time is "less than" the start time, it refers to the next day?

                  Thanks!
                  Hi wintermelon,

                  a few words on VWAPs. If you code any VWAP you would like that the VWAP matches either

                  - the electronic session of the instrument that you trade

                  - the regular session of the instrument that you trade

                  For that purpose NinjaTrader has session templates. The correct way of doing it, would be that the VWAP uses those session templates. If you use a segmented session template, for example

                  - for FDAX the sessions 8:00AM - 9:00 AM CET / 9:00 AM - 5:30 PM CET / 5:30 PM to 10:00 PM CET (CET = Central European Time)

                  - for ES the sessions 5:00 PM - 8:30 AM CT / 8:30 AM - 3:15 PM CT / 3:15 PM - 4:15 PM CT (CT = US Central Time)

                  then the VWAP can both be displayed for the electronic or the regular session.

                  The VWAP in the downloads of this forum is not properly coded and

                  - sometimes produces erratic values (the bug is already correct with a different version called hVWAP)

                  - may halt your PC, if you use it with CalculateOnBarClose = false.

                  I have coded a few other versions, which are well-behaved and available for free download. Below are a few CPU charts that show what happens, if you use the VWAP from the downloads. I have done tests in replay at speed x500 on a 1-min charts. The fastest version uses a recursive algorithm, which is explained in Big Mike's Forum, where it can be downloaded. The NinjaTrader Forum VWAP slowed down the replay from x500 to x50, which means that you will be in trouble, if you use it with COBC = false.

                  Also my indicators take into account the double day sessions for CME and NYMEX traded futures on President's Day, Martin Luther King Day, Memorial Day, Independence Day, Labour Day and Thanksgiving. The inidcators adapt to the session template chosen for the chart.

                  The indicators - daily, weekly, monthly VWAPs and TWAPs - are available for download here or you can request them by sending me a private message.

                  The best futures trading community on the planet: futures trading, market news, trading charts, trading platforms, trading strategies


                  Harry
                  Attached Files

                  Comment


                    #10
                    Hi Harry,

                    Thanks for the info and your session vwap v40 indicator. Is it possible to put in user-definable hours for the session to calculate the VWAP, without changing the ETH / RTH session templates?

                    The reason why I would like to custom the time period of the VWAP, and not conform to ETH / RTH, is that most times, I would like the VWAP calculation to start slightly earlier than the RTH because I deem the price/volume action then to be relevant already.

                    For example, if there is a major event happening at 8.30am EST. I may want to have the VWAP start at 8am EST to start capturing the action just before the event.

                    In most other days, the action starting at 9am EST also gives good information for incorporating into the VWAP.

                    Comment


                      #11
                      Originally posted by wintermelon View Post
                      Hi Harry,

                      Thanks for the info and your session vwap v40 indicator. Is it possible to put in user-definable hours for the session to calculate the VWAP, without changing the ETH / RTH session templates?

                      The reason why I would like to custom the time period of the VWAP, and not conform to ETH / RTH, is that most times, I would like the VWAP calculation to start slightly earlier than the RTH because I deem the price/volume action then to be relevant already.

                      For example, if there is a major event happening at 8.30am EST. I may want to have the VWAP start at 8am EST to start capturing the action just before the event.

                      In most other days, the action starting at 9am EST also gives good information for incorporating into the VWAP.
                      Hi Wintermelon,

                      I have an experimental version that allows to introduce an offset. I have not yet published it. If you wish to test it, send me a private message. A screenshot is attached.

                      Harry
                      Attached Files

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by gentlebenthebear, Today, 01:30 AM
                      2 responses
                      13 views
                      0 likes
                      Last Post gentlebenthebear  
                      Started by Kaledus, Today, 01:29 PM
                      2 responses
                      8 views
                      0 likes
                      Last Post Kaledus
                      by Kaledus
                       
                      Started by frankthearm, Yesterday, 09:08 AM
                      13 responses
                      45 views
                      0 likes
                      Last Post frankthearm  
                      Started by PaulMohn, Today, 12:36 PM
                      2 responses
                      16 views
                      0 likes
                      Last Post PaulMohn  
                      Started by Conceptzx, 10-11-2022, 06:38 AM
                      2 responses
                      56 views
                      0 likes
                      Last Post PhillT
                      by PhillT
                       
                      Working...
                      X