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

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:

    Paul H.NinjaTrader Customer Service

    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.
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by WHICKED, Today, 12:45 PM
          0 responses
          1 view
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by FrazMann, Today, 11:21 AM
          2 responses
          6 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by rjbtrade1, 11-30-2023, 04:38 PM
          2 responses
          80 views
          0 likes
          Last Post DavidHP
          by DavidHP
           
          Started by Spiderbird, Today, 12:15 PM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by lorem, Yesterday, 09:18 AM
          5 responses
          18 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X