Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bid/Ask price for OnMarketData

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

    Bid/Ask price for OnMarketData

    Hi

    I am using NinjaTrader 7 with Kinetick, and the Market Replay functionality. I am trying to match up what comes in to the time & sales window with the trades that I receive in the OnMarketData() method of a NT strategy. In particular I am trying to decide whether a trade is a market sell (which trades at the bid price), or a market buy (which trades at the ask price). I compare the values of MarketDataEventArgs.price with GetCurrentBid/Ask() and make a decision.

    What I am seeing is that although trades are indicated in the T&S window as being sells done at the low of the day (they have a red background), sometimes if I obtain the values of GetCurrentBid() and GetCurrentAsk() then the market appears to have already moved down a tick. Therefore in NT7 I am recording that trade as a BUY, when in fact it should be a sell.

    I'm guessing that by the time I receive the OnMarketData event the market quote has already moved down. Is there a way to get a synchronised copy of what the bid/ask was when the trade happened?

    #2
    Hello endian675,

    These updates (bid/ask/last) are not synchronized, but you may be able to improve accuracy accessing directly from the OnMarketData() handler. Declare your variables in variables region so they're available within whole context of script, assign bid ask respectively during their updates and then make comparisons to these variables during last updates.


    Code:
    #region Variables
    private double bidPrice;
    private double askPrice;
    #endregion
    
    
    protected override void OnMarketData(MarketDataEventArgs e)
    {
    
    	if (e.MarketDataType == MarketDataType.Ask) 
    		askPrice = e.Price;
    
    	else if (e.MarketDataType == MarketDataType.Bid)
    		bidPrice = e.Price;
    
    
    	else if (e.MarketDataType == MarketDataType.Last)
    	{
    		if (e.Price == askPrice)
    			Print("Last price traded at ask");
    
    		else if (e.Price == bidPrice)
    			Print("Last price traded at bid");
    
    		else
    			Print("Last price occurred at neither bid/ask");
    	}
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hello endian675,

      These updates (bid/ask/last) are not synchronized, but you may be able to improve accuracy accessing directly from the OnMarketData() handler. Declare your variables in variables region so they're available within whole context of script, assign bid ask respectively during their updates and then make comparisons to these variables during last updates.


      Code:
      #region Variables
      private double bidPrice;
      private double askPrice;
      #endregion
      
      
      protected override void OnMarketData(MarketDataEventArgs e)
      {
      
          if (e.MarketDataType == MarketDataType.Ask) 
              askPrice = e.Price;
      
          else if (e.MarketDataType == MarketDataType.Bid)
              bidPrice = e.Price;
      
      
          [COLOR=Red][B]else[/B][/COLOR] if (e.MarketDataType == MarketDataType.Last)
          {
              if (e.Price == askPrice)
                  Print("Last price traded at ask");
      
              else if (e.Price == bidPrice)
                  Print("Last price traded at bid");
      
              else
                  Print("Last price occurred at neither bid/ask");
          }
      }
      Ryan, I think that the else that I have bolded in red should not be there. It seems that the code is trying to determine if the last price was bid or ask (or not), so it needs to run regardless of the the determination of the bid or ask prices. (i.e., that test should not be part of the bid/ask price determing loop, or it will not run after the price is determined).

      Am I the one who is making a mistake instead?

      Comment


        #4
        Hi koganam,

        I took a second look at it and feel it's appropriate here. The structure just divides all bid / ask / last updates. Comparisons are only made during last updates. Since the single property check (MarketDataType) enforces exclusivitiy by itself, it should be the same results whether it's else if or if there so it's more of a style preference.
        Last edited by NinjaTrader_RyanM1; 01-25-2012, 04:43 PM.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks, I will try this and report back. Personally I think the comparison should be done with a switch() statement rather than a big if-else

          Code:
          switch(e.MarketDataType)
          {
            case MarketDataType.Bid:
            break;
            case MarketDataType.Ask:
            break;
            case MarketDataType.Last:
            break;
          }

          Comment


            #6
            Right- switch should work just as well. Good luck and let us know how it goes.
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            651 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            370 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            577 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X