Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator/Oscillator Showing Distance between VWAP and SD2 in OrderFlowVWAP

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

    Indicator/Oscillator Showing Distance between VWAP and SD2 in OrderFlowVWAP

    Hi Ninjatrader support team

    I'm trying to create a simple oscillator that shows the distance between VWAP and SD2 upper and lower in OrderFlowVWAP using two lines. Each line showing the distance between VWAP and upper/lower, respectively. The below compiles but I get the error message "Error on calling 'OnBarUpdate' method on bar -1". I can't understand why this error is coming up as I have added a CurrentBar condition.

    I also added the code snippets from teh language reference to manage the tick data.

    What am I missing? Thanks!

    public class VWOscillator : Indicator
    {

    private Series<double> vwap;
    private Series<double> s2Upper;
    private Series<double> s2Lower;
    private Series<double> distanceUpper;
    private Series<double> distanceLower;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionPriceOscillator;
    Name = "VWOscillator";
    IsSuspendedWhileInactive = true;

    AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
    AddPlot(Brushes.Red, "Distance Upper");
    AddPlot(Brushes.Green, "Distance Lower");
    }
    else if (State == State.DataLoaded)
    {
    vwap = new Series<double>(this);
    s2Upper = new Series<double>(this);
    s2Lower = new Series<double>(this);
    distanceUpper = new Series<double>(this);
    distanceLower = new Series<double>(this);

    }

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }

    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar <= 10) return;

    if (BarsInProgress == 0) {
    vwap[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
    VWAPStandardDeviations.Three, 1, 2, 2.5).VWAP[0];
    s2Upper[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
    VWAPStandardDeviations.Three, 1, 2, 2.5).StdDev2Upper[0];
    s2Lower[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
    VWAPStandardDeviations.Three, 1, 2, 2.5).StdDev2Lower[0];

    distanceUpper[0] = s2Upper[0] - vwap[0];
    distanceLower[0] = s2Lower[0] - vwap[0];

    // smoothEma[0] = emaFast[0] - emaSlow[0];
    Value[0] = distanceUpper[0];
    Value[1] = distanceLower[0];
    }

    else if (BarsInProgress == 1)
    {
    // We have to update the secondary tick series of the cached indicator using Tick Resolution to make sure the values we get in BarsInProgress == 0 are in sync
    OrderFlowVWAP(BarsArray[0], VWAPResolution.Tick, BarsArray[0].TradingHours,
    VWAPStandardDeviations.Three, 1, 2, 2.5).Update(OrderFlowVWAP(BarsArray[0], VWAPResolution.Tick,
    BarsArray[0].TradingHours, VWAPStandardDeviations.Three, 1, 2, 2.5).BarsArray[1].Count - 1, 1);
    }


    }


    #2
    Hello sandman55,

    Thanks for your first post.

    Where you are assigning the Upper and lower distance to the plots:
    Value[0] = distanceUpper[0];
    Value[1] = distanceLower[0];


    For the 2nd plot that will not work because Value only works with single plot indicators. For multi plot indicators you would need to use "Values", for example:

    Values[0][0] = distanceUpper[0]; // assign upper value to the first plots current bar (You could leave this as Value[0] if you wish)
    Values[1][0] = distanceLower[0]; // assign lower value to the 2nd added plots current bar


    References:

    Comment


      #3
      It worked Paul. Thanks

      Comment


        #4

        Hello. could they write it? Can you share it? (

        Comment


          #5
          Hello ivan2007007,

          Thanks for your post.

          Sandman has posted his code and I've shown the modifications needed.

          It would be up to him to post it in its finished form if he chooses to. Please note that what he is working on uses the Order Flow indicators which may not be available to you unless you have a lifetime license.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          606 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          353 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          560 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          561 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X