Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing OrderFlowCumulativeDelta in Strategy Builder

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

    Accessing OrderFlowCumulativeDelta in Strategy Builder

    Is there a way I can access the Orderflow Cumulative Delta in Strategy Builder? When I try to add it to my strategy, it doesn't show up in the indicator list. Is there some work around to get it there? I was capable of writing strategies in NT7 but since NT8 my skill in C++ is just not adequate to program that way, hence I am using the Strategy Builder to try and get things done.
    Thanks for your help.
    DaveN

    #2
    Hello daven,

    The orderflow items have to be used in manual coding due to the way they need to work. You can see a sample of the code required for that in the help guide: https://ninjatrader.com/support/help...tsub=orderflow

    Comment


      #3
      I was afraid that was what I would hear. Perhaps I can build the basic strategy in strategy builder using a different average then open the code, and try dropping in the code from the code sample?
      Thanks for your help.
      DaveN

      Comment


        #4
        Hello daven,

        Yes that is one approach. There are some differences in how the strategy builder makes the structure of the code in OnBarUpdate which you would need to update. I would suggest to make a basic strategy that has 1 condition to enter and has a stop/target or exits and then test it so that you can see it works. After doing that unlock the code and attach the .cs file to this post. We can walk through the extra code the builder adds which needs to be changed to accommodate the orderflow items.

        Comment


          #5
          Okay. I've created a simple strategy which is a variation on a moving average crossover. I built it in the Strategy Builder, tested it, (it works and loses lots of money) and I then opened the code.I've posted it in this reply.
          Thanks ahead of time for helping me with this.
          Dave

          OFCumulativeDeltaSrategyOC.cs

          Comment


            #6


            The two main points of interest are the following blocks of code, the first one prevents a secondary series from calling OnBarUpdate which prevents orderflow items from working so that needs removed. The second one checks for data avaliablity, this is still required and may need modified later on depending on the BarsAgo you end up using with all of your code.


            Remove this block:
            Code:
            if (BarsInProgress != 0)
            return;

            Keep this block, if you ever use more than 2 bars ago in your code you need to update this code to reflect that. Replace the 2 with the maximum bars ago you used.
            Code:
            if (CurrentBars[0] < 2)
            return;



            After removing the first block of code you need to reformat the existing condition code. That would look like the following to match the orderflow code:


            Code:
            if (BarsInProgress == 0)
            {
            
                // your existing strategy builder set code:
                // Set 1
               if ((HMA1[0] > EMA1[0])
               && (CrossAbove(Close, EMA2, 2)))
               {
                  EnterLong(Convert.ToInt32(DefaultQuantity), @"OFCD_L");
               }
            
               // Set 2
               if ((HMA1[0] < EMA1[0])
               && (CrossBelow(Close, EMA2, 2)))
               {
                  EnterShort(Convert.ToInt32(DefaultQuantity), @"OFCD_S");
               }​
            
               // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
               Print("Delta Close: " + cumulativeDelta.DeltaClose[0]);
            }
            else if (BarsInProgress == 1)
            {
               // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
               cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);
            }​
            The BarsInProgress conditions are needed so that the secondary series used for the order flow items calls the code. In addition to the above you also need to add the private variable to your script and update OnStateChange to reflect the added indicator::

            Code:
            private OrderFlowCumulativeDelta cumulativeDelta;
            
            protected override void OnStateChange()
            {
            
            .....
            ​
            else if (State == State.Configure)
            {
                AddDataSeries(Data.BarsPeriodType.Tick, 1);
            }
            else if (State == State.DataLoaded)
            {
               myCounter = new Series<int>(this);
               EMA1 = EMA(Close, 30);
               SMA1 = SMA(Close, 70);
               EMA1.Plots[0].Brush = Brushes.Goldenrod;
               SMA1.Plots[0].Brush = Brushes.Fuchsia;
               AddChartIndicator(EMA1);
               AddChartIndicator(SMA1);
               cumulativeDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAs k, CumulativeDeltaPeriod.Session, 0);
            }​

            Comment


              #7
              ​Okay. I've made the changes you provided above, and it compiles. Now I just have to convert the ema logic to an hma of cumulative delta and add the test for crossover of the cumulative delta close above or below the hma of that indicator. What does the statement look like to create an hma of the cumulative delta close, and where do I put it?
              Again, thank you so much for your help.
              Dave

              Comment


                #8
                Hello daven,

                To use the HMA with that series would look like:

                Code:
                double value = HMA(cumulativeDelta.DeltaClose, 20)[0];
                Where you put it depends on where you use it. It would go in OnBarUpdate, beyond that depends on where you want to use it inside OnBarUpdate




                Comment


                  #9
                  Right now I have two ema averages and one HMA average. I think I just need to get rid of the two ema averages, convert the hma average to the hma of the cumulative delta as above, and then convert the average test to one which detects when the cumulative delta crosses above or below the hma of the cumulative delta.
                  So do I need to convert the following to this?

                  Convert:
                  HMA1 = HMA(Close, Convert.ToInt32(Period_S));

                  To
                  HMA1 = HMA(cumulativeDelta.DeltaClose, (Period_S));

                  Comment


                    #10
                    I'm also having some trouble figuring out how to state that the cumulativeDelta crosses above or below the HMA of the same indicator?

                    Sorry if my question is a dumb one. Been awhile since I did any kind of programming, (30 years and it was fortran)

                    Dave

                    Comment


                      #11
                      I seem to be messing a lot of stuff up now. Here's what I tried to do to enter the long position. I added some trading times which I wanted for later. It all compiled just fine when I added the trading time but now that I am trying to implement the crossover logic it is getting messy. Here's what I have so far.

                      // Long Entry
                      if ((Times[0][0].TimeOfDay >= Start_Trade_Time.TimeOfDay)
                      && (Times[0][0].TimeOfDay <= Stop_Trade_Time.TimeOfDay)
                      && (cumulativeDelta.DeltaClose[0] CrossBelow HMA1[0]))
                      {
                      EnterLong(Convert.ToInt32(DefaultQuantity), @"OFCD_L");

                      And here are all the error codes I am getting:


                      }​
                      Attached Files

                      Comment


                        #12
                        I am guessing you have gone home for the day. I am posting the code so you can see where I am. The code now compiles with no errors but when I test it I get no trades.

                        I will send the latest code by email to support and put you in the title.

                        Thanks for your help.

                        Dave

                        Comment


                          #13
                          One other issue. I turned on trace orders and the log shows a lot of the following message's, (all the same):

                          Strategy 'OFCumulativeDeltaSrategyOC': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

                          I also see it in the log file so clearly I screwed something up, just don't know what it is.

                          Dave

                          Comment


                            #14
                            Hello daven,

                            In the future please try to keep replies to a single post to allow for a response before asking more questions. Also please try to limit questions in your post to questions that relate to your original post, asking about other aspects of the script like time conditions is outside of the scope of the original question. That would be best to post in a new thread.

                            It looks like the Hma code you provided is correct, you would need to supply the cumulativeDelta.DeltaClose to it if you want the Hma to use that data.

                            To check cross above or below you can use the CrossAbove or CrossBelow methods. The cumulativeDelta.DeltaClose and HMA have different values so you would need to print those out to see if they can cross, if so you can use those series with CrossAbove and CrossBelow.

                            The image you provided has an error, that is not the correct syntax for CrossBelow. You can see an example here: https://ninjatrader.com/support/help...sub=CrossBelow

                            If you are getting no trades you may need to use prints to find out if your condition is becoming true or not. You can also output values to check if the conditions you made are valid.

                            Object reference not set to an instance of an object means an object being used was null, that happened in your OnStateChange override.



                            Comment


                              #15
                              Sorry for meandering around on this topic. If it is okay I have a couple of questions:

                              1. You said, "It looks like the Hma code you provided is correct, you would need to supply the cumulativeDelta.DeltaClose to it if you want the Hma to use that data." I thought I did that in the code already with the statement "double value = HMA (cumulativeDelta.DeltaClose, Period_S)[0]. Is that not right? How do I supply cumulativeDelta.DeltaClose to the HMA function, and where do I put it in the code?

                              2. You also indicated I didn't use the correct syntax for the cross below statement, but when I look at it and compare it to the example you provided I can't see the difference. Can you give me a little more guidance on this issue?

                              Again, apologies for meandering. I will do better going forward.

                              Thanks

                              Dave

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X