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

OnMarketDepth – max level size?

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

    OnMarketDepth – max level size?

    Hello,

    What is the most efficient way to figure out which level on DOM has the largest size for bid and ask? I have the following logic within OnMarketDepth event (ask example):

    maxValue=0;
    maxAskSizeLevel=0;
    for(int i=0; i<5; i++)
    {
    if (e.MarketDepth.Ask[i].Volume > maxValue)
    maxValue = e.MarketDepth.Ask[i].Volume;
    maxAskSizeLevel = i;
    }

    But I am afraid that with all the changes that take place it might be too slow. Is there anyway to get this data with some kind of max function?

    Thanks,
    redduke

    #2
    You are on the right track but instead of looping through each level, just check the incoming event against your stored variables. The looping is not necessary.

    Create some variables local to your indicator/strategy.

    private int maxAskValue = 0;
    private int maxAskPosition = 0;

    then...

    Code:
    if (e.MarketDataType == MarketDataType.Ask && e.Volume > maxAskValue)
    {
        maxAskValue = e.Volume;
        maxAskPosition = e.Position;
    }
    RayNinjaTrader Customer Service

    Comment


      #3
      Hi Ray,

      Thanks for this info. This logic would work if all I needed was the largest value ever. If this logic is put in place, and let’s say that is a huge number that appears only once on ask of 10,000 contracts, this number would constantly be displayed until I stop and restart since no other number will top it.

      What I need is the max value at this point of time, which changes many times during even 1 second. The for loop does the job but many times it can not catch up with all DOM changes.

      Is there any other way?

      Thanks,
      redduke

      Comment


        #4
        Ah..got it.

        Not sure what you mean too slow. Your logic should be fine.
        RayNinjaTrader Customer Service

        Comment


          #5
          It works fine when DOM updates are less frequent, but as soon as updates intensify, it many times shows the wrong value, eventually corrects and then the cycle renews.

          Thanks,
          redduke

          Comment


            #6
            Best build your own book then. The depth object is multi-threaded and the values you are accessing can already be updated as a new event comes in. You are guaranteed to get all events though.

            Here is a reference - http://www.ninjatrader-support.com/v...ead.php?t=3478
            RayNinjaTrader Customer Service

            Comment


              #7
              Ray,

              Thanks. I will experiment with it.

              Regards,
              redduke

              Comment


                #8
                I figured out what the issue was. It had nothing to do with frequent updates, I got a bit confused in the beginning when I started looking for the error. All it was just missing brackets on if statement (I highlighted them in bold). Without them, the last (fifth) level data was always selected. I tested the code this morning, and it worked fine even during very fast market conditions.

                maxValue=0;
                maxAskSizeLevel=0;
                for(int i=0; i<5; i++)
                {
                if (e.MarketDepth.Ask[i].Volume > maxValue)
                {
                maxValue = e.MarketDepth.Ask[i].Volume;
                maxAskSizeLevel = i;
                }
                }

                Regards,
                redduke

                Comment


                  #9
                  Where would this code go if using the samplemarketdepth provided on the forum?

                  Code:
                  maxValue=0;
                  maxAskSizeLevel=0;
                  for(int i=0; i<5; i++)
                  {
                  if (e.MarketDepth.Ask[i].Volume > maxValue)
                  [B]{[/B]
                  maxValue = e.MarketDepth.Ask[i].Volume;
                  maxAskSizeLevel = i;
                  [B]}[/B]
                  }

                  Comment


                    #10
                    Would be used in your OnMarketDepth() method suprsnipes.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Bertrand View Post
                      Would be used in your OnMarketDepth() method suprsnipes.
                      Thanks for your reply. I get the following error 'Cannot implicitly convert type 'long' to 'int'. An explicit conversation exists (are you missing a cast?)' when attempting to add the code to OnMarketDepth() and the 2 lines of code below to the variables section

                      private int maxValue=0;
                      private int maxAskSizeLevel=0;

                      Comment


                        #12
                        Please try defining them as long values instead, there was a change for NT7 where Volume is now represented by a long return instead of an int as prior.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Bertrand. I'm able to print the two elements now if I could bother you for just one further question. What could be causing the following error message;

                          'You are accessing an index with a value that is invalid since it is out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart'.

                          Comment


                            #14
                            Glad to hear, could be multiple things, but most likely tied to not having a high enough check for enough CurrentBars in your OnBarUpdate() section - http://www.ninjatrader.com/support/f...ead.php?t=3170
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Ok, I'll check it out cheers

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,403 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              94 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X