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 Modification

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

    VWAP Modification

    I am looking to modify a basic Standard VWAP indicator

    here is the code

    protected override void OnBarUpdate()
    {
    if (Bars.IsFirstBarOfSession)
    {
    iCumVolume = VOL()[0];
    iCumTypicalVolume = VOL()[0] * ((High[0] + Low[0] + Close[0]) / 3);
    }
    else
    {
    iCumVolume = iCumVolume + VOL()[0];
    iCumTypicalVolume = iCumTypicalVolume + (VOL()[0] * ((High[0] + Low[0] + Close[0]) / 3));
    }

    PlotVWAP[0] = (iCumTypicalVolume / iCumVolume);
    }


    ********************************************

    As the Standard VWAP starts at the opening bar I assume the conditions and calculations are based on starting here each day: if (Bars.IsFirstBarOfSession)

    I want to change to Start at a specified bar LIKE Highest High plotted on a 5 minute chart in the last 78 bars

    Thats it. Any Help appreicated
    DTS

    #2
    Hello DTSSTS,

    Thanks for your post.

    HighestBar() could be used to get the number of bars ago the highest price value occurred within the specified look-back period.

    For example, if you are running the script on a 5-minute chart then HighestBar(High, 78) could be used to get the number of bars ago the highest price value occurred within the last 78 bars.

    You may consider subtracting that bars ago value returns by HighestBar() from the CurrentBar to get the bar number that the Highest High occurred on.

    See this help guide documentation for more information and sample code: https://ninjatrader.com/support/help...highestbar.htm

    If you are running the chart on an interval other than 5-minutes, AddDataSeries() would be used to add a secondary 5-minute bar object to the script. If this is the first added series in your script, your logic that you want to process on the added 5-minute series would go in BarsInProgress 1.

    See the help guide documentation below.

    Working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
    AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm
    BarsInProgress: https://ninjatrader.com/support/help...inprogress.htm

    Let me know if I may assist further.
    Last edited by NinjaTrader_BrandonH; 04-18-2023, 12:41 PM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello DTS,

      You can set a bool when the condition is true.

      Then require the bool to be true to evaluate the logic.

      Code:
      private startLogic = false;
      
      if (HighestBar(High, 78) == 0)
      {
      startLogic = true;
      }
      
      if (startLogic == true)
      {
      // your logic here
      }
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        double maxHigh = MAX(High, 78);

        if (Bars.Count >= 78 && High == maxHigh)
        {
        // Do something when the current bar's High is the highest of the last 78 bars
        }


        WOULD THE Above work? or is HighestBar bettter and is the double needed

        after I get past this part I want to try and add Multiple dataseries to the indicator, with inputs (so if placed on a weird bar type ie renko or such the vwap will still calculate correctly because is will be minutes bar type

        thanks

        Comment


          #5
          Hello DTSSTS.

          Thanks for your note.

          The code you shared could possibly work if you want to execute logic when there are greater than or equal to 78 bars in the primary series and the current High is equal to the maxHigh variable.

          Otherwise, you could use HighestBar() as mentioned in the previous posts on this thread. If you plan on working with multiple data series in a single script, you might find that using HighestBar() works best depending on your overall goal

          You could consider testing each approach to see which works best for your goals.

          Please let me know if I may further assist.
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Thanks I will work with both if needed. I have ran across something I have not before, Working with additional dataseries added

            iCumTypicalVolume1 = VOL()[0] * ((Highs[1][0] + Lows[1][0] + Closes[1][0]) / 3);

            I changed the High to Highs[1] etc.

            WHAT DO I DO WITH: VOL()[0] (never worked with VOL in different dataseries before)

            Thanks

            Comment


              #7
              Hello DTSSTS,

              Thanks for your note.

              To get the Volume of an added series, you could consider using Volumes from the VolumeSeries.

              Volumes[int barSeriesIndex][int barsAgo]

              ​Volumes[0][0] would get the volume of the current bar on the primary series. Volumes[1][0] would get the volume of the current bar on the first added secondary series.

              See this help guide page for more information about Volumes: https://ninjatrader.com/support/help...es_volumes.htm

              When working with Multi-Timeframe/Multi-Instrument NinjaScripts, this help guide page is important to review to gain a good understanding of how these scripts function: https://ninjatrader.com/support/help...nstruments.htm
              Last edited by NinjaTrader_BrandonH; 04-19-2023, 08:39 AM.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Volues[1][0] replaces VOL()[0]

                or is that

                Volumes[1][0] replaces VOL()[0]

                Thanks again

                ALSO seems that i need to define the HighestBar, my plots are not using the HighestBar of the 78

                using this if (HighestBar(Highs[1], 78) == 0)

                Maybe I need use a double

                double maxHigh1 = MAX(Highs[1], 78);

                would that be doable - I am still not really seeing where the Original Code finds its focus on the starting point. so that I can focus on the HighestBar of the last 78
                Last edited by DTSSTS; 04-18-2023, 04:23 PM.

                Comment


                  #9
                  Hello DTSSTS,

                  Thanks for your note.

                  Volumes[1][0] could be used to get the volume of the added secondary bars object. "Volues[1][0]" was a typo and I have corrected it in the above post.

                  HighestBar(Highs[1], 78) would return an int value representing a number of bars ago the highest price value occurred calculated from the added secondary High PriceSeries.

                  See the 'Method Return Value' section of this help guide page: https://ninjatrader.com/support/help...highestbar.htm

                  For example, in the code Chelsea shared the condition would check if HighestBar(High, 78) returns an int value equal to 0 and flips a bool to true. You could print out the HighestBar() value in your script to see how exactly it is evaluating.

                  Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                  https://ninjatrader.com/support/foru...121#post791121
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Yes I thought was typo, just wanted to confirm

                    I reviewed the method Return Value help guide and have tried many items to and compile without errors, before I had an inaccurate plot, now i have no plot with Log Error
                    Object Reference not set to an instance of an object

                    I think my issue relates to this part of Method: int highestBarsAgo = HighestBar(High, Bars.BarsSinceNewTradingDay);

                    I do not have a reference to Bar as I did not use this part because I am not only looking for the High to be ONLY in todays session ie NewTradingDay
                    so i have difficulty knowing what replaces that

                    i used: int high1BarsAgo = HighestBar(Highs[1], 78); (TO SEE WHAT WOULD HAPPEN) COULD NOT figure out the syntax and the content of the code for the Bars.BarsSince ?

                    Thanks again, I am trying

                    Comment


                      #11
                      Hello DTSSTS,

                      Thanks for your note.

                      You mentioned you are using if (HighestBar(Highs[1], 78) == 0) in your script on post # 8. This specific line of code would not cause the error you mentioned.

                      For example, you could view the attached script which demonstrates using the code mentioned above.

                      The error message "Object reference not set to the instance of an object" means that something in the script is returning null at the time you are accessing it. Debugging steps would need to be taken to determine what exactly is returning null in the script.

                      Below is a link to a forum post that demonstrates using debugging prints to understand the behavior of a script.
                      https://ninjatrader.com/support/foru...121#post791121

                      Let us know if we may assist further.​
                      Attached Files
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        that was helpful, the current bars < 80 was more bars than I had in my code, so that got back my plot, but I am still not referencing the highest high for the plot

                        This issue is still that I am not referencing the Bars as they are not in the code regarding the methods
                        i cannot figure out how to alter this int highestBarsAgo = HighestBar(High, Bars.BarsSinceNewTradingDay);

                        to be included in this int high1BarsAgo = HighestBar(Highs[1], 78);

                        thanks

                        the prints are showing various bar numbers , but the plot is not referenceing the bars which i think further confirms the above info
                        Last edited by DTSSTS; 04-19-2023, 12:27 PM.

                        Comment


                          #13
                          Hello DTSSTS,

                          Thanks for your note.

                          "This issue is still that I am not referencing the Bars as they are not in the code regarding the methods. i cannot figure out how to alter this int highestBarsAgo = HighestBar(High, Bars.BarsSinceNewTradingDay); to be included in this int high1BarsAgo = HighestBar(Highs[1], 78);"

                          I am not sure I fully understand what you mean by the above statements.

                          Are you trying to get the Highest High price that occurred within the last 78 bars?

                          Note that HighestBar() only returns the number of bars ago the highest price value occurred. It does not return the highest price.

                          To get the highest price from HighestBar(), you would need to do something like the sample code seen on the HighestBar() help guide page.

                          // store the highest bars ago value
                          int highestBarsAgo = HighestBar(Highs[1], 78);

                          //evaluate high price from highest bars ago value
                          double highestPrice = High[highestBarsAgo];


                          If you are not trying to get the highest price from the highest bars ago value, please provide a brief description explaining what exactly you are trying to reference or what exactly the value is you are trying to get so I may accurately assist.


                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            your example i have tryed from the method help link. NOT looking for the Price of the HighestBar, Calculating VWAP and the HighestBar is the starting point for the calculations (AS the standard vwap calculates from the first bar of the session IE Anchored VWAP anchored to the HighestBar of last 78 of the dataseries

                            I FOUND THIS in another vwap code where the author is plotting deviations of the vwap, He included an input to Anchor the vwap to a single starting point by using timeofday and DATE. I believe something like that is the solution to my issue except I am using the HighestBar as the starting point. I DO NOW HAVE THE PLOT plotting from a prior starting point but it is not highestbar

                            here is the code form the other author in the State.Configure area

                            if (State == State.Configure)
                            {
                            if ( UseAnchor )
                            {
                            anchorDateTime = new DateTime( AnchorDate.Year, AnchorDate.Month, AnchorDate.Day, AnchorTime.TimeOfDay.Hours, AnchorTime.TimeOfDay.Minutes, 0 );
                            startBarIndex = Bars.GetBar( anchorDateTime );
                            }
                            }​

                            he is starting the BarIndex by Bars.GetBar of the anchor DateTime

                            I need to substiture the anchorDateTime definition with the HighestBar HighestBar(Highs[1], 78);

                            I have tried many variations with this but never compile when I try that route

                            any ideas appreciated

                            with the rule as true, then the plot changes every bar so not actually using the HighestBar as the starting point

                            IF I change the chelse rule to false then the below happens
                            IT APPEARS THAT once the chart has enough bars the starting point is established as a starting point and then Never changes and it needs to be replaced
                            Last edited by DTSSTS; 04-19-2023, 02:00 PM.

                            Comment


                              #15
                              Hello DTSSTS,

                              Thanks for that clarification.

                              "I need to substiture the anchorDateTime definition with the HighestBar HighestBar(Highs[1], 78)​"

                              You could consider passing in HighestBar to Time[] to get the Time of that HighestBar if you want that specific logic used. For example;

                              int highestBarsAgo = HighestBar(Highs[1], 78);

                              Time[highestBarsAgo];

                              Time: https://ninjatrader.com/support/help...eries_time.htm
                              Times: https://ninjatrader.com/support/help...ries_times.htm

                              The code you shared from the other author gets a time (anchorDateTime) and then gets a bar index at that time (Bars.GetBar(anchorDateTime )).

                              HighestBar() gets a bar index in the form of a BarsAgo. An index would be CurrentBar - BarsAgo. Therefore the bar index would be CurrentBar - HighestBar().

                              Please let me know if I may assist further.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by MarianApalaghiei, Today, 12:35 AM
                              1 response
                              6 views
                              0 likes
                              Last Post MarianApalaghiei  
                              Started by Rogers101, 05-05-2024, 11:30 AM
                              17 responses
                              55 views
                              0 likes
                              Last Post Rogers101  
                              Started by haas88, 03-21-2024, 02:22 AM
                              13 responses
                              153 views
                              0 likes
                              Last Post haas88
                              by haas88
                               
                              Started by YongJane, Today, 01:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post YongJane  
                              Started by MSerag, 05-06-2024, 11:52 PM
                              4 responses
                              25 views
                              0 likes
                              Last Post MSerag
                              by MSerag
                               
                              Working...
                              X