Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reset(able) VWAP

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

    Reset(able) VWAP

    Hi,
    I found the following code for a VWAP indicator.

    I want to reset this, say every hour, instead of day, any idea how to go about it?

    ========================
    amespace NinjaTrader.NinjaScript.Indicators
    {
    public class VWAP8 : Indicator
    {
    double iCumVolume = 0;
    double iCumTypicalVolume = 0;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Volume Weighted Average Price";
    Name = "VWAP8";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Black, "PlotVWAP");
    }
    }

    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);
    }
    =====================


    Thanks

    #2
    Hello SuperDriveGuy,

    The condition 'if (Bars.IsFirstBarOfSession)' is the condition that is triggering on the first bar of a new session.

    You could instead have this condition trigger if the Time[0].Minute is 0 (meaning its the end of an hour).

    Or you could save the bar time to a variable when the condition is true, and then in the condition ensure the time of the current bar is 1 hour greater than the saved value. (Time[0].AddHours(1) > savedTimeValue)
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    30 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, Yesterday, 02:41 AM
    0 responses
    13 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    22 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    39 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    34 views
    0 likes
    Last Post CarlTrading  
    Working...
    X