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

OnMarketData() e.MarketDataType.Last execution side

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

    OnMarketData() e.MarketDataType.Last execution side

    Hello support

    Am I right that the price and value of trade happened in a market gives e parameter with possible value MarketDataType.Last?

    What does the values of MarketDataType.Ask and MarketDataType.Bid mean?

    The Time and Sales tool print in blue trades at bid and in green at ask. How can I determine at which side e.MarketDataType.Last trade happened?

    Regards.

    #2
    Hello alex_bbfg,

    Thank you for your inquiry.

    I would suggest reading the OnMarketData() section in the NinjaTrader help guide for more information about OnMarketData(): http://ninjatrader.com/support/helpG...marketdata.htm

    MarketDataType.Last, MarketDataType.Bid, and MarketDataType.Ask are different types of prices (the last, bid, and ask prices) of the instrument.

    To check if the last price was on the bid or ask side, you could do something like the sample below in OnMarketData():
    Code:
    private double bidPrice = 0;
    private double askPrice = 0;
    
    protected override void OnMarketData(MarketDataEventArgs e)
    {
         if (e.MarketDataType == MarketDataType.Ask)
              askPrice = e.Price;
    
         if (e.MarketDataType == MarketDataType.Bid)
              bidPrice = e.Price;
    
         if (e.MarketDataType != MarketDataType.Last)
              return;
    
         if (e.Price >= askPrice)
              Print("ASK");
         else if (e.Price <= bidPrice)
              Print("BID");
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hello Zachary G.

      Thank you very much for quick reply.

      Am I right that if for example there was a trade on a market at 95.5 price, it means that 95.5 is the last and at bid for instance type at the same time. If so why do you need the last type of price? if a trade happened on a market it is either at bid or ask. Why do you need last type of price?

      It is really good practice that you start give the code samples replying on questions.

      Regards

      Comment


        #4
        Hello alex_bbfg,

        The last price is the last traded price of the instrument.

        I would suggest reading through these two articles on Investopedia for more information about bid and ask:



        This link on Stack Exchange further explains the differences between these prices: http://money.stackexchange.com/quest...-current-price
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          NinjaTrader_ZacharyG

          the question is

          Is it true that OnMarketData() method gives the price of the one particular trade last and bid or ask types at the same time.

          is it true that the following code gives me a print of your Time and Sales tool in blue color

          if(e.MarketDataType == MarketDataType.Last & e.MarketDataType == MarketDataType.Bid)
          {
          Color.Blue;
          }

          is it true that the following code gives me a print of your Time and Sales tool in green color

          if(e.MarketDataType == MarketDataType.Last & e.MarketDataType == MarketDataType.Ask)
          {
          Color.Green;
          }

          Regards

          Comment


            #6
            Hello alex_bbfg,

            Please note that MarketDataType.Last and MarketDataType.Bid/Ask are two different prices that will not always match.

            The last price is the last traded price of the instrument. This price will not be updated until a trade was made on the instrument by you or someone else. The ask and bid prices update dynamically based on the market.

            OnMarketData() is called on every change in level one market data.

            Please take a look at the NinjaTrader help guide at this link for an example of how to print the bid/ask price: http://ninjatrader.com/support/helpG...marketdata.htm

            The code you have provided will not compile. You will need to enter a Print statement in order to Print something. Unfortunately, there is no way to change the color of what is printed out to the output window.

            Example:
            Code:
            if (e.MarketDataType == MarketDataType.Bid)
                 Print("Bid = " + e.Price); // prints the bid price
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              Hello Zachary G.

              thank you for reply.

              I've got that I need the Last type of the price in my script.

              My idea is to build own T&S tool with custom settings. And I need to color trades in blue and green as your T&S does. How can I determine (which method or parameter use) which trade was at bid/ask as your T&S does.

              Regards

              Comment


                #8
                Hello alex_bbfg,

                Unfortunately, it is not possible to color text in the Output Window.

                The sample code I have provided in my first response will determine what side of the market the last trade was on.
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Zachary G.

                  Thank you for your reply.

                  I'm going to use winform and listview control to my T&S tool. It is not a problem to color text there.

                  I've got your idea how to the determine last price side. I thought that data provider gives that information and I do not need to use logic for it.

                  I guess GetCurrentAsk() and GetCurrentBid() are better methods to determine a price to compere with the last one.

                  Regards

                  Comment


                    #10
                    Hello alex_bbfg,

                    GetCurrentAsk() and GetCurrentBid() will only be called when OnBarUpdate() is called.

                    For absolute real-time ask and bid data, you would want to use OnMarketData(). This is called on each update of level I market data.

                    To output the current Ask and Bid prices, you can use for example:
                    Code:
                    protected override void OnMarketData(MarketDataEventArgs e)
                    {
                         if (e.MarketDataType == MarketDataType.Ask)
                              Print("Ask: " + e.Price);
                         else if (e.MarketDataType == MarketDataType.Bid)
                              Print("Bid: " + e.Price);
                    }
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Zachary G.

                      I thought that OnMarketData() gives changes in 5 ticks up and 5 ticks down from last price depth. So, the change in 3rd ticks depth can gives incorrect result.

                      Regards

                      Comment


                        #12
                        Hello alex_bbfg,

                        OnMarketData() is called on every change of level I market data.

                        Please read this link in our help guide for more information about OnMarketData(): http://ninjatrader.com/support/helpG...marketdata.htm
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13
                          I think there should be a bit more code.

                          I tried using the code provided:

                          if (e.Price >= askPrice)
                          Print("ASK");
                          else if (e.Price <= bidPrice)
                          Print("BID");

                          I found I was not collecting the data correctly. I made the following modification:
                          If (e.Price == askPrice) . . . (do something)
                          else if (e.Price == bidPrice) . . .(do something)
                          else if (e.Price > askPrice) . . .(do something)
                          else if (e.Price < bidPrice) . . . (do something)


                          Testing for equality first gets the data I want.

                          Let's assume the price is 70.88, the lastBidPrice is 70.87 and the lastAskPrice is 70.88.

                          I would want to categorize the trade as having been at the ask price. But if I test for greater than or equal to the Bid price, I would not categorize the trade correctly.

                          Any clarification/correction would be appreciated.

                          Comment


                            #14
                            Hello bulldogcajun,

                            Generally speaking, if the price is less than or equal the bid price, the market data event would qualify as a bid, when if the price is greater or equal to the ask, the event would qualify as an ask.

                            I may suggest referencing one of the open source indicators that come with NinjaTrader 7 for a full working example. The VolumeProfile included with the platform uses OnMarketData() for measuring volume between the bid and the ask.

                            Code:
                            protected override void OnMarketData(MarketDataEventArgs e)
                            {
                                if (e.MarketDataType == MarketDataType.Ask)
                                {
                                    askPrice = e.Price;
                                    return;
                                }
                                if (e.MarketDataType == MarketDataType.Bid)
                                {
                                    bidPrice = e.Price;
                                    return;
                                }
                                if (e.MarketDataType != MarketDataType.Last || ChartControl == null || askPrice == 0 || bidPrice == 0)
                                    return;
                            
                                if (Bars != null && !Bars.Session.InSession(DateTime.Now, Bars.Period, true, Bars.BarsType))
                            		return;
                            
                            	double	price	= e.Price;
                            	long	volume	= e.Volume;
                            
                            	if (!volumeInfo.ContainsKey(price))
                            		volumeInfo.Add(price, new VolumeInfoItem());
                            
                            	VolumeInfoItem volumeInfoItem = volumeInfo[price];
                            
                            	if (price >= askPrice) 
                            		volumeInfoItem.up += volume;
                            	else 
                                    if (price <= bidPrice)
                            		    volumeInfoItem.down += volume;
                            	    else
                            		    volumeInfoItem.neutral += volume;
                            }
                            As a simpler look, we can reference the BuySellVolume indicator from NinjaTrader 8. MarketDataEventsArgs in NT8 include the bid and the ask price which makes using OnMarketData a little bit easier.

                            From the BuySellVolume indicator in NT8:
                            Code:
                            protected override void OnMarketData(MarketDataEventArgs e)
                            {
                            	if(e.MarketDataType == MarketDataType.Last)
                            	{
                            		if(e.Price >= e.Ask)
                            		{
                            			buys += e.Volume;
                            		}
                            		else if (e.Price <= e.Bid)
                            		{
                            			sells += e.Volume;
                            		}
                            	}
                            }
                            Please let us know if you have any additional questions.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              OnMarketData volume calculation

                              In NinjaTrader 7 I have created a custom indicator which color codes a bar based on a minimum trade size occurring. The following code fragment shows some of the logic in the OnMarketData event:

                              protected override void OnMarketData(MarketDataEventArgs e)
                              {
                              // // Print some data to the Output window
                              // if (e.MarketDataType == MarketDataType.Last)
                              // Print("Last = " + e.Price + " " + e.Volume);
                              //

                              if (e.MarketDataType == MarketDataType.Ask && e.Volume >= MINTRADESIZE)
                              {
                              TradeRepository t = new TradeRepository(); //class for storing trade

                              t.TradeTime = e.Time;
                              t.Direction = MarketDataType.Ask;
                              t.BarNumber = CurrentBar;
                              t.Volume = e.Volume;
                              t.MarketPrice = e.Price;
                              t.ID = e.ClassId;

                              Repository.Add(t); //list of type TradeRepository
                              tickCounter++;


                              //Print("Ask = " + e.Price + " " + e.Volume);

                              }

                              }


                              As the OnMarketData event fires on each tick is it certain that each trade stored in this way is unique or is it possible that the same trade (tick) somehow fires more than once and create duplicated items? Can a trade be uniquely defined using a combination of time, type(bid or ask) ,volume, market price so that if duplication within the OnMarketData event occurs then a filter can be added to prevent duplicate items from being stored?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GwFutures1988, Today, 02:48 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by ScottWalsh, 04-16-2024, 04:29 PM
                              6 responses
                              30 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by frankthearm, Today, 09:08 AM
                              10 responses
                              36 views
                              0 likes
                              Last Post frankthearm  
                              Started by mmenigma, Today, 02:22 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by NRITV, Today, 01:15 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post NRITV
                              by NRITV
                               
                              Working...
                              X