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

Updating CumulativeDelta for multiple instruments

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

    Updating CumulativeDelta for multiple instruments

    Hi folks!

    The documentation on OrderFlowCumulativeDelta is great but I'm having trouble applying it when I'm working with multiple instruments.

    The strategy I'm working on seeks to measure delta bias on an instrument separate from the primary series and then take action on the primary series. I'm confused on what BarsArray to update to and where to put the measurement logic.

    Here's the excerpt that highlights the concept:

    Code:
    public class CDmultipleInstrument : Strategy
    {
    private OrderFlowCumulativeDelta OrderFlowCumulativeDelta1;
    private string Instrument1;
    private int deltaBiasInstrument1;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Instrument1 = "...";
    deltaBiasInstrument1 = 0;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1); // BarsArray[1] - to execute orders on primary series
    AddDataSeries(Instrument1, Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last); // BarsArray[2] - to drive cumulative delta indiactor on Instrument 1
    AddDataSeries(Instrument1, Data.BarsPeriodType.Second, 10, Data.MarketDataType.Last); // BarsArray[3] - to measure cumulative delta on Instrument 1
    }
    else if (State == State.DataLoaded)
    {
    OrderFlowCumulativeDelta1 = OrderFlowCumulativeDelta(BarsArray[3],CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar,0);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (
    CurrentBars[0] <= BarsRequiredToTrade
    || CurrentBars[1] <= BarsRequiredToTrade
    || CurrentBars[2] <= BarsRequiredToTrade
    || CurrentBars[3] <= BarsRequiredToTrade
    )
    return;
    
    if (BarsInProgress == 0) // Primary series -- could be 10 second, UniRenko, or other bar type
    {
    if (
    deltaBiasInstrument1 > 0
    )
    {
    BackBrushes[1] = Brushes.Green;
    }
    else if (
    deltaBiasInstrument1 < 0
    )
    {
    BackBrushes[1] = Brushes.Red;
    }
    }
    
    if (BarsInProgress == 1) // Primary series 1 tick
    {
    }
    
    if (BarsInProgress == 2) // Instrument1 1 tick
    {
    // Sync Instrument1 cumulative delta indicators to primary series
    // First argument = primary series?
    // Second argument = 1 tick driver of the specific cumulative delta indicator?
    OrderFlowCumulativeDelta1.Update(0, 2);
    }
    
    if (BarsInProgress == 3) // Instrument1 10 second
    // Put delta measurement logic here?
    {
    if (
    OrderFlowCumulativeDelta1.DeltaClose[1] > 0
    )
    {
    deltaBiasInstrument1 = 1;
    }
    else if (
    OrderFlowCumulativeDelta1.DeltaClose[1] < 0
    )
    {
    deltaBiasInstrument1 = -1;
    }
    else
    {
    deltaBiasInstrument1 = 0;
    }
    }
    }
    ​​


    Am I synching the bars correctly with the Update() method? Is the delta logic on the secondary series in the right place?

    Many thanks!

    #2
    Hello andersonbd,

    Thanks for your post.

    The instrument agrument in the AddDataSeries() methods in your script should be hard-coded and not dependent on variables.

    From the AddDataSeries() help guide: "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided."

    AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

    The .Update() method should be called on the Order Flow Cumulative Delta when BarsInProgress 2 is processing if that is the 1-Tick series you want to sync the indicator to. The syntax however seems to be incorrect.

    OrderFlowCumulativeDelta1.Update(0, 2); should instead look something like OrderFlowCumulativeDelta1.Update(cumulativeDelta.B arsArray[2].Count - 1, 2);

    See the help guide documentation below for more information. The second reference sample on the Order Flow Cumulative Delta help guide page demonstrates calling the OrderFlowCumulativeDelta() method by reference.

    Order Flow Cumulative Delta: https://ninjatrader.com/support/help...ive_delta2.htm
    .Update(): https://ninjatrader.com/support/help...tml?update.htm


    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response, Brandon!

      Good copy on the AddDataSeries argument.

      I misunderstood the syntax for the .Update() and what it was actually trying to do - thanks for the clarification!

      However, the .Update() lines for the secondary instruments are throwing the OnBarUpdate error on bar 25: Index was outside the bounds of the array.

      What's weird is that if I comment the update lines out for the secondary series cumulative delta indicators, the strategy runs fine and the indicators are added to the chart. But with it in, the output window is showing prints that indicate these lines aren't working. Am I still missing something on how to properly update the CD indicators? The update for the primary indicators is working fine.


      Code:
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 1); // BarsArray[1]
      AddDataSeries("ZN 09-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last); // BarsArray[2]
      AddDataSeries("ZN 09-23", Data.BarsPeriodType.Second, 10, Data.MarketDataType.Last); // BarsArray[3]
      
      Print("State == State.Configure complete");
      }
      else if (State == State.DataLoaded)
      {
      cumulativeDelta1B = OrderFlowCumulativeDelta(BarsArray[0],CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar,0);
      cumulativeDelta1T = OrderFlowCumulativeDelta(BarsArray[0],CumulativeDeltaType.UpDownTick, CumulativeDeltaPeriod.Bar,0);
      cumulativeDelta2B = OrderFlowCumulativeDelta(BarsArray[3],CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar,0);
      cumulativeDelta2T = OrderFlowCumulativeDelta(BarsArray[3],CumulativeDeltaType.UpDownTick, CumulativeDeltaPeriod.Bar,0);
      
      AddChartIndicator(cumulativeDelta1B);
      AddChartIndicator(cumulativeDelta1T);
      AddChartIndicator(cumulativeDelta2B);
      AddChartIndicator(cumulativeDelta2T);
      
      Print("State == State.State.DataLoaded complete");
      
      }​
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress == 1)
      {
      Print("BarsInProgress == 1");
      Print("cumulativeDelta1B.BarsArray[1].Count = " + cumulativeDelta1B.BarsArray[1].Count);
      
      // Update primary cumulative delta indicators
      cumulativeDelta1B.Update(cumulativeDelta1B.BarsArray[1].Count - 1, 1);
      cumulativeDelta1T.Update(cumulativeDelta1B.BarsArray[1].Count - 1, 1);
      
      Print("Primary cumulative delta indicators updated");
      }​
      
      if (BarsInProgress == 2)
      {
      Print("BarsInProgress == 2");
      
      // Update secondary cumulative delta indicators 
      cumulativeDelta2B.Update(cumulativeDelta2B.BarsArray[2].Count - 1, 2);
      cumulativeDelta2T.Update(cumulativeDelta2T.BarsArray[2].Count - 1, 2);
      
      Print("secondary cumulative delta indicators updated");
      }​
      }

      Comment


        #4
        Hello andersonbd,

        Internally in the hosted Order Flow Cumulative Delta, only an additional 1 tick series is added with AddDataSeries(), which internally in that indicator is BarsArray[1].

        Change:
        if (BarsInProgress == 2)
        {
        Print("BarsInProgress == 2");

        // Update secondary cumulative delta indicators
        cumulativeDelta2B.Update( cumulativeDelta2B.BarsArray[2].Count - 1, 2);
        cumulativeDelta2T.Update( cumulativeDelta2T.BarsArray[2].Count - 1, 2);​

        To:

        cumulativeDelta2B.Update( cumulativeDelta2B.BarsArray[1].Count - 1, 2);
        cumulativeDelta2T.Update( cumulativeDelta2T.BarsArray[1].Count - 1, 2);


        (Keep this in the condition for BarsInProgress == 2, that's the correct BarsInProgress in the host script)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea. So interesting though... it still throws the error. It makes it to one bar beyond whatever BarsRequiredToTrade is set to and then terminates with the same "Index is outside the bounds of the array."

          if (BarsInProgress == 2)
          {
          Print("just before sync");
          // Sync secondary cumulative delta indicators to primary series
          cumulativeDelta2B.Update(cumulativeDelta2B.BarsArr ay[1].Count - 1, 2);
          cumulativeDelta2T.Update(cumulativeDelta2T.BarsArr ay[1].Count - 1, 2);
          Print("secondary cumulative delta indicators updated to primary series");
          }​

          In SetDefaults: BarsRequiredToTrade = 35;

          Output Window:

          CurrentBars[0]: 36 | Primary
          CurrentBars[1]: 21897 | Primary 1 tick
          CurrentBars[2]: 37528 | ZN 1 tick
          CurrentBars[3]: 1617 | ZN 10 second
          just before sync
          Strategy 'CDmultipleInstrument': Error on calling 'OnBarUpdate' method on bar 36: Index was outside the bounds of the array.​

          I will also note that when copying and pasting from the NinjaScript editor, I get a space added in the code (in bold). It's not their in the editor though.​​​​

          Any thoughts?

          Comment


            #6
            I was reading this to understand fundamentals of orderflow - https://ninjatrader.com/support/help...ed_order_handl ing.htm and was wondering if Ratio of Imbalance represents seller or buyer aggression is there a way to calculate the "cumulative of Ratio of Imbalance Counts per Bar" - what I mean is how many counts of imbalances (diagonal) are there in favor of sellers vs imbalances in favor of buyers are there against each bar. Probably find out the difference of those imbalances to show if the imbalance is higher than a threshold that confirms breakout.

            I am not finding any methodtype available - I can see barsType.Volumes[CurrentBar].GetAskVolumeForPrice(Close[0]));

            How do we read the data for Bid Volume and Ask Volume of each price levels within each bar? Current methods provide traditional ones - Close, Open, High and Low. But in the data series we are giving Strength Sensitivity = 20. I will assume in order to plot the pricelevels - Level 2 data points are available to read these individual Bid Vol and Ask Vol data.

            Any guidance with the codeset to display Cumulative Ratio of Imbalance or Weighted Ratio of Imbalance. Cumulative Delta really is a lagging indicator still. Delta and %Delta really gives the emotion of bullish or bearish sentiment of the bar. Whether next move of the bar would be bullish or bearish will be the aggression of imbalance. I am not seeing any method available to calculate it. I am a beginner in Ninjascript so - looking for some help.​

            Comment


              #7
              tanagoswami, I had been looking for something similar regarding bid and ask volume (idea was to look for big resting orders and trade in the same direction as those orders). My understanding from reading various documentation and forum posts is that, while bid and ask prices are available, volume is not.

              Comment


                #8
                Hello andersonbd,

                Thanks for your notes.

                Based on the code you shared I cannot tell exactly what in your script is causing the error to appear. It could be that you are using the wrong BarsArray in your code.

                If you are trying to do this for multiple instruments, I suggest that your first try to get the OrderFlowCumulativeDelta.DeltaClose[0] value for a single instrument and get that working. Then, you could duplicate that code and change the BarsInProgress and BarsArray being used to the correct secondary series for that copy of the code.

                The help guide shows how to access OrderFlowCumulativeDelta.DeltaClose[0] and your code would need to be structured similar to the help guide. Once you have that code working, you could create another set of code structured exactly the same way.

                Order Flow Cumulative Delta: https://ninjatrader.com/support/help...ive_delta2.htm
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello tanagoswami,

                  Thanks for your notes.

                  Imbalances from the Order Flow Volumetric Bars are not accessible in NinjaScript at this time. We are tracking interest in an existing feature request for this and I have added your vote. This request is being tracked under the number SFT-3586.

                  As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted on the Release Notes page of the Help Guide.

                  Release Notes — https://ninjatrader.com/support/help...ease_notes.htm
                  Brandon H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Segwin, 05-07-2018, 02:15 PM
                  14 responses
                  1,789 views
                  0 likes
                  Last Post aligator  
                  Started by Jimmyk, 01-26-2018, 05:19 AM
                  6 responses
                  837 views
                  0 likes
                  Last Post emuns
                  by emuns
                   
                  Started by jxs_xrj, 01-12-2020, 09:49 AM
                  6 responses
                  3,293 views
                  1 like
                  Last Post jgualdronc  
                  Started by Touch-Ups, Today, 10:36 AM
                  0 responses
                  13 views
                  0 likes
                  Last Post Touch-Ups  
                  Started by geddyisodin, 04-25-2024, 05:20 AM
                  11 responses
                  63 views
                  0 likes
                  Last Post halgo_boulder  
                  Working...
                  X