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 hourly

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

    vwap hourly

    Hi,

    I'm looking to modify the following vwap to make it a hourly vwap:

    My first indicator, a VWAP indicator similar to the included OrderflowVWAP, but should be usable in strategies and supports up to 5 deviation levels. Plus it looks dope out of the box. Happy to refine it if anyone has suggestions.


    I modified the line 105 :

    Code:
    if (Bars.IsFirstBarOfSession)
    by

    Code:
    if (Times[0][0].Hour != Times[0][1].Hour)
    but it doesn't work.

    Could you help me ?

    Regards
    Nico


    #2
    Hello nico1119,

    Are you trying to recreate the same effect if you were to apply the indicator to a 1 hour chart?

    If so you would need to use AddDataSeries to add a hourly series to use for calculations. https://ninjatrader.com/support/help...=AddDataSeries

    The logic would have to be isolated to the hourly series using a BarsInProgress condition:

    Code:
    if(BarsInProgress == 1)
    {
        /// indicator logic
    }
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you for your quick answer.
      I'm not trying to recreate the same effect if I was to apply the indicator to a 1 hour chart.

      Let me reformulate.
      Instead of having a daily reset for my vwap, I want to have an hourly reset. Every new hour, a new vwap start.

      Do you know what I mean ?

      Comment


        #4
        Hello nico1119,

        For that type of reset you would have to use DateTime math to find when an hour had elapsed based on the data. That would also require using a series less than or equal to 1 hour, if you use a bars type greater than 1 hour you won't be able to use the bar times.

        One idea would be to store the time at the first reset which is:
        Code:
        if (Bars.IsFirstBarOfSession)
        {
            myStoredTime = Time[0];
        }
        Then each update after that you subtract the saved time from the current time to find a difference. If the difference is an hour or more then do the reset and also set the variable to the current time to repeat the process.

        Code:
        TimeSpan difference = Time[0] - myStoredTime ;
        You can then check if the timespan has greater than or equal to 60 minutes:
        Code:
        if(difference.TotalMinutes >= 60)
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thank you for your answer.
          It works with TimeFrame unit but not with renko for example

          Code:
          protected override void OnBarUpdate()
          {
          hl3 = ((High[0] + Low[0] + Close[0]) / 3);
          
          
          if (Bars.IsFirstBarOfSession)
          {
          myStoredTime = Time[0];
          }
          
          TimeSpan difference = Time[0] - myStoredTime ;
          
          //if (Times[0][0].Hour != Times[0][1].Hour)
          if(difference.TotalMinutes >= 60)
          {
          myStoredTime = Time[0];
          iCumVolume = VOL()[0];
          iCumTypicalVolume = VOL()[0] * hl3;
          v2Sum = VOL()[0] * hl3 * hl3;
          }
          else
          {
          iCumVolume = iCumVolume + VOL()[0];
          iCumTypicalVolume = iCumTypicalVolume + ( VOL()[0] * hl3 );
          v2Sum = v2Sum + VOL()[0] * hl3 * hl3;
          }
          
          curVWAP = (iCumTypicalVolume / iCumVolume);
          deviation = Math.Sqrt(Math.Max( v2Sum/iCumVolume - curVWAP*curVWAP ,0));
          
          PlotVWAP[0] = curVWAP;
          
          switch(NumDeviations)
          {
          case 1:
          PlotDevOne();
          break;
          case 2:
          PlotDevTwo();
          break;
          case 3:
          PlotDevThree();
          break;
          case 4:
          PlotDevFour();
          break;
          case 5:
          PlotDevFive();
          break;
          default:
          PlotVWAP[0] = curVWAP;
          break;
          }
          
          }
          Last edited by nico1119; 05-24-2022, 03:15 PM.

          Comment


            #6
            Jesse,

            Is it possible to add a series (1 hour in my case) and trigger a new bar on this series ?
            Last edited by nico1119; 05-24-2022, 03:21 PM.

            Comment


              #7
              Hello nico1119,

              If you are using a non time based series and the bars elapse longer than an hour or in frequency's that fall outside the time you want that wont work to use time as a factor because the bar times come from the bars type. The time series relates to the bar times so you would need to use either a secondary series or OnMarketData to do the check for the time difference using the ticks time instead of the bars time.

              If you don't need to work with historical resets you can use OnMarketData to avoid adding a secondary series or having to change any other logic in the script.



              Code:
              protected override void OnMarketData(MarketDataEventArgs e)
              {
                   TimeSpan difference = e.Time - myStoredTime ;
                  if(difference.TotalMinutes >= 60)
                  {
                  }
              }

              This assumes the first stored time is still set from OnBarUpdate based on the bar times. This also will only work in realtime.

              You can alternatively add a 1 tick secondary series using AddDataSeries to do this type of logic from OnBarUpdate. That would involve using the BarsInProgress to isolate the original logic that the indicator has in BarsInProgress 0 and then execute the other logic which needs to be decoupled from the bar times in BarsInProgress 1.

              Code:
              if(BarsInProgress == 0)
              {
                  // original logic
              }
              if(BarsInProgress == 1)
              {
                   TimeSpan difference = Time[0] - myStoredTime ;
                  if(difference.TotalMinutes >= 60)
                  {
                  }
              }
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse,

                Thank you for your answer.

                Alternatively, can I just add a 1 hour secondary series, and trigger when a bar close on this series ?

                Comment


                  #9
                  Hello nico1119,

                  If that works toward the end result you wanted you could do that. I would suggest to add the series and BarsInProgress conditions like we had earlier discussed. In post 2 I had suggested using a secondary series however you said that was not correct so the other suggestion was added. You could do just a reset in the BarsInProgress 1 condition, it really depends on what overall changes you wanted to make to the existing code.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Jesse, it works!

                    One more question, how can I adjust the secondary series from the control panel of the indicator, in the case I want to switch from 1 hour series to 2 hours series without modify the code ?

                    Comment


                      #11
                      Hello nico1119,

                      There is not currently a way I could suggest for that, AddDataSeries is not intended to be used dynamically. There is a note in the AddDataSeries page that mentions this, it is because dynamically adding series based on variables is not reliable and may fail to correctly load the data or changes after it had been applied.

                      You can technically make a user input property which is an int and use that but the only way to use the script and know it used the right setting is to re apply it every time. You won't be able to safely just change the value and let it reload, you would need to remove it and re apply it using the new value each time.
                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Haiasi, 04-25-2024, 06:53 PM
                      2 responses
                      16 views
                      0 likes
                      Last Post Massinisa  
                      Started by Creamers, Today, 05:32 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post Creamers  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      12 responses
                      1,785 views
                      0 likes
                      Last Post Leafcutter  
                      Started by poplagelu, Today, 05:00 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post poplagelu  
                      Started by fx.practic, 10-15-2013, 12:53 AM
                      5 responses
                      5,407 views
                      0 likes
                      Last Post Bidder
                      by Bidder
                       
                      Working...
                      X