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

Tracking aggressive orders (above the ask and below the bid)

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

    Tracking aggressive orders (above the ask and below the bid)

    How do I track the prints coming through time and sales in NinjaScript?

    I'd like to make an indicator that plots in the lower portion of the screen the ratio of orders: [(above ask)/ask] / [(below bid)/bid].

    If this is possible, is anyone able to point me in the right direction?

    #2
    I believe you should use OnMarketData https://ninjatrader.com/support/help...marketdata.htm

    Comment


      #3
      Originally posted by Leeroy_Jenkins View Post
      I believe you should use OnMarketData https://ninjatrader.com/support/help...marketdata.htm
      Leeeeeeeroy Jeeeeeeeeeenkinssss.

      Thanks for the reply!

      I have been looking into this and it does seem like I'll need to add a data series for the bid and also for the ask.

      Likely some sort of logic to label, store and increment the values as they come in.

      As far as how to do that I have no idea and am planning on allocating several hours to the task this weekend. I have other things I'd like to do so with the limited time I'm hoping that this wheel has been invented before and someone can point me in the right direction.

      In the event this bridge hasn't been crossed (would be surprising considering the value in identifying this level of aggression on the tape) I'll keep this thread updated. Would be stoked if others shared their ideas here too.

      Comment


        #4
        Just came across this post and will be looking at this as a starting point

        https://ninjatrader.com/support/foru...70#post1130270

        Edit:
        print Above ask Above bid from T & S Hello, How can I get this print analog from T&S columns (filters) Above ask Above bid Need a real example of how I can get it
        Last edited by butt_toast; 07-24-2021, 12:25 PM.

        Comment


          #5
          Interesting post that is relevant to the discussion:

          There is no possible trade ouside the bid/ask spread. Period. The exchange will always match the closest price (best bid/ask) and execute at that price, moving the bracket (bid/ask) if there is not enough to match the order.

          That being said, trades outside bid/ask are basically a data transfer issue. Because they do not say the trade happened outside bid/ask, but it happened outside bid/ask KNOWN TO THE COMPUTER.

          This can be because of bad data sources (example: IB updates bid/ask with all other info only x times per second, so in the meantime a trade CAN happen outside), or because of the way the information is handled on the side of the data provider. I am not even aware whether the exchange GUARANTEES (like: always) that bid/ask moves will be under all conditions (fast market) published in proper order. Especially not during the execution of ONE trade.

          Example:

          I want to buy 100 ES. there is an offer of 50 at 1000, 75 at 1000.25.

          My trade will thus execute 50 at 1000, 25 at 1000.25.

          Practically, depending on granularity.... the exchange MAY Publish "trade 50 at 1000, new best bid is 75 at 1000.25, trade 25 at 1000.25, new best bid is 50 at 1000.25", OR it may decide to publish "trade 50 at 1000, 25 at 1000,25, new best bid is 50 at 1000.25". The later is less data.... but it means that part of the trade happens outside bid/ask on the client side, although technically naturally it happend within the at that time valid bid/ask spread.

          Plus the data provider may optimize (not Zen-Fire/Rithmic, but some others may just decide to).

          So, at the end, you talk of data granularity issues here, possibly even at the exchange.
          Link: https://ninjatrader.com/support/foru...126#post269126

          I am perfectly happy tracking what "appears" to be trades being executed above/below the ask/bid.

          My goal is to have a rough measure of aggression.

          If someone comes in and smashes the bid by placing a market order of sufficient size to wipe out the next couple of levels on the bid below the market, I'd love to track that even if the actual bid on the exchange was at that level.

          So it looks like using level 2 in NT is the best way to pull the current bid/ask? I say this because if the NT devs use this to tag executions as being AABB (above the ask below the bid) using this data rather than level 1, then I'm going to do so as well since they're likely way smarter than I am.

          Based on this, I'm going to try to take the second example posted by NinjaTrader_Kate and try to work with that.

          I am just now learning NinjaScript so I now need to piece together a way to track the data in the ratio I described in the original post.



          Last edited by butt_toast; 07-24-2021, 12:45 PM.

          Comment


            #6
            Because I'm a huge noob with coding (not trading) I'm going to start with the more simple of the two examples I've found.

            I've only written strategies so far, never an indicator.

            So I'm going to start with simply trying to get this code to plot anything as a lower indicator. Then eventually I'll hopefully get a basic type of AABB ratio passed through to that lower indicator, then eventually make it more sophisticated by using the example that uses level 2 data to determine AABB.

            But first I need to figure out how to get this code below to plot AABB as a line in an indicator at the bottom of the chart.


            Code:
            //This namespace holds Indicators in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Indicators
            {
            public class messingWithLevelOneExample : Indicator
            {
            private string AboveWhich;
            
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"A simple reproduction of the T&S window.";
            Name = "messingWithLevelOneExample";
            Calculate = Calculate.OnEachTick;
            IsOverlay = false;
            DisplayInDataBox = false;
            DrawOnPricePanel = false;
            //Disable this property if your indicator requires custom values that cumulate with each new market data event.
            //See Help Guide for additional information.
            IsSuspendedWhileInactive = true;
            }
            else if (State == State.Historical)
            {
            ClearOutputWindow();
            
            AboveWhich = string.Empty;
            
            Draw.TextFixed(this, "note", "messingWithLevelOneExample prints to the NinjaScript Output window\r\nNew -> NinjaScript Output", TextPosition.BottomLeft);
            }
            }
            
            protected override void OnBarUpdate() {
            
            
            }
            
            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
            {
            if (marketDataUpdate.MarketDataType != MarketDataType.Last)
            return;
            
            if (marketDataUpdate.Price > marketDataUpdate.Ask)
            {
            AboveWhich = "Above Ask";
            }
            else if (marketDataUpdate.Price < marketDataUpdate.Bid)
            {
            AboveWhich = "Below Bid";
            }
            
            Print(string.Format("{0:HH:mm:ss} | last: {1:0.00}, ask: {2:0.00}, bid: {3:0.00} | volume: {4} | color: {5}", marketDataUpdate.Time, marketDataUpdate.Price, marketDataUpdate.Ask, marketDataUpdate.Bid, marketDataUpdate.Volume, AboveWhich));
            }
            }
            }
            Last edited by butt_toast; 07-24-2021, 01:22 PM.

            Comment


              #7
              butt_toast here, this is the user name I'll be using moving forward.

              When I created that user name it was a throwaway account and I didn't expect to be using this forum as much as I am, and I feel like it's most respectful to the community to have a more normal name.

              That being said, I made this thread today. I was working on just getting some boiler plate code to simply plot any value whatsoever, and hedgeplay was generous enough to help me out.

              So tomorrow I will be working on using the above code with the code from the linked thread to try to get some rudimentary version of this aggression ratio working. Eventually I hope to improve the code to follow the example linked in this thread that uses level 2 but I'm trying to build this one brick at a time considering how much I don't know about NinjaScript (and coding in general).

              Comment


                #8
                Hello WalterSkinner,

                The current ask and bid can be found with GetCurrentAsk() and GetCurrentBid().



                Is the line at the bottom of the chart a straight horizontal line or does this need to be a plot that will have different values.

                A line can be drawn with Draw.Line() and Draw.HorizontalLine().



                Below is a link to a forum post with helpful information about getting started with NinjaScript and C#.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Is the line at the bottom of the chart a straight horizontal line or does this need to be a plot that will have different values.
                  https://ninjatrader.com/support/foru...040#post786040
                  I was thinking histogram but I'm open to any possibilities because I'm not sure what I'll be able to do. Will find out tomorrow morning.

                  Comment


                    #10
                    I'm trying to make sure this code has no errors before adding the bid/ask data.

                    The output window shows this error: "Indicator 'Examples Indicator': Error on calling 'OnStateChange' method: Added Plots or Lines must have a unique name"

                    Which strikes me as odd since I'm not of an indi by that name. The only file in Documents\NinjaTrader 8\bin\Custom\Indicators that starts with the letter "E" is "ExampleTnSPrintsAboveAskBelowBid" and is not applied to any chart. In fact, no indicator is applied to any chart.

                    I think this might be a thing to reach out to tech support for?

                    I was hoping to figure out which property the auto-generated comment is referring to (the one about cumulative values which I think will apply to what I'm working on here), but first I feel like I've got to get to the bottom of this mystery indicator.

                    Regardless here's the code:

                    Code:
                    public class AGGRESSIONv1 : Indicator
                    {
                    private Series<double> plotOne;
                    protected override void OnStateChange()
                    {
                    if (State == State.SetDefaults)
                    {
                    Description = @"This version hopefully will create a histogram to a chart and produce no errors in the log";
                    Name = "AGGRESSIONv1";
                    Calculate = Calculate.OnBarClose;
                    IsOverlay = false;
                    DisplayInDataBox = true;
                    PaintPriceMarkers = true;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive = true;
                    AddPlot(new Stroke(Brushes.Black), PlotStyle.Bar, "Aggression Ratio");
                    AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
                    }
                    else if (State == State.Configure)
                    {
                    plotOne = new Series<double>(this);
                    }
                    }
                    
                    protected override void OnBarUpdate()
                    {
                    if(CurrentBar < 0) return;
                    if(CurrentBar > Count-50)
                    {
                    double plotOneLocal = -0.2;
                    plotOne[0] = plotOneLocal;
                    Value[0] = plotOne[0];
                    //Value[1] = -1;
                    }
                    }
                    
                    
                    }//end class

                    Comment


                      #11
                      Originally posted by WalterSkinner View Post

                      The output window shows this error: "Indicator 'Examples Indicator': Error on calling 'OnStateChange' method: Added Plots or Lines must have a unique name"

                      Which strikes me as odd since I'm not of an indi by that name. The only file in Documents\NinjaTrader 8\bin\Custom\Indicators that starts with the letter "E" is "ExampleTnSPrintsAboveAskBelowBid" and is not applied to any chart. In fact, no indicator is applied to any chart.

                      I think this might be a thing to reach out to tech support for?
                      The other night I ran into an issue duplicating the TnS example script from this thread. The editor would allow me to right click (in the editor) > save as > rename > compile but the new files stopped appearing when I right click (in chart) > indicators. Was very confused and walked away for a bit.

                      There was a plot from that code called "Example Indicator" and once I removed each file that I duplicated the other night that didn't show up when trying to add to a chart, this issue was resolved. So fortunately it is unrelated to the code I'm working with now.

                      Comment


                        #12
                        Originally posted by WalterSkinner View Post

                        I was hoping to figure out which property the auto-generated comment is referring to (the one about cumulative values which I think will apply to what I'm working on here)

                        Code:
                        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                        //See Help Guide for additional information.
                        IsSuspendedWhileInactive = true;
                        It's most likely referring to this one.

                        https://ninjatrader.com/support/help...erence_wip.htm

                        So because I may not want to prevent values from cumulating I should switch this to false?

                        Comment


                          #13
                          Hello WalterSkinner,

                          IsSuspendedWhileInactive affects windows that are minimized and tabs that are not selected when there are multiple tabs in a window.
                          This is for efficiency. When the tab is not selected or is minimized, the indicator queues data events. When the tab is selected or restored, the queued events are run through and the indicator catches up.


                          The link you have provided is to the entire language reference in the help guide. Was there something specific in this you were trying to link?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello WalterSkinner,

                            IsSuspendedWhileInactive affects windows that are minimized and tabs that are not selected when there are multiple tabs in a window.
                            This is for efficiency. When the tab is not selected or is minimized, the indicator queues data events. When the tab is selected or restored, the queued events are run through and the indicator catches up.


                            The link you have provided is to the entire language reference in the help guide. Was there something specific in this you were trying to link?
                            Thanks.

                            I must have accidentally copied the url from the wrong tab, I have about a hundred tabs open to various parts of the language reference.

                            Super glad you replied today because I had a question about GetCurrentAsk() and GetCurrentBid() vs. the way of getting this data from post #6:

                            Do you suggest doing it using these methods instead? If so, what are the pros/cons vs. what is shown in post #6?

                            I'm happy to go with whatever is easiest at the start, this is not meant to be a precise indicator.


                            Comment


                              #15
                              Hello WalterSkinner,

                              Using GetCurrentAsk() vs capturing every ask market update depends on the needs of the script.

                              If you need a snapshot of the current ask from time to time, use GetCurrentAsk().

                              If you are collecting / aggregating ask updates or reacting to changes in the ask, use OnMarketData().
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,235 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,415 views
                              0 likes
                              Last Post Traderontheroad  
                              Working...
                              X