Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Logging each tick as trading at the bid/ask

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

    Logging each tick as trading at the bid/ask

    Hello all,

    I'd like to develop an execution algorithm that takes into account recent trades and whether they occurred at the bid or at the ask.

    I'm wondering if I can record the most recent Bid and Ask during the OnMarketData() method (using GetCurrentBid() and GetCurrentAsk()) and whenever the OnBarUpdate() method runs (with CalculateOnBarClose==false), compare the most recent Close[0] with the most recent Bid & Ask and look for a match.

    My question is if OnBarUpdate() is called at the start of every bar, even if there was no trade, and, if so, which method should I use to find the price at which the last trade occurred?

    All the best,

    Ken

    #2
    Hello Kaydgee,

    There won't be a bar without having a trade. OnBarUpdate() is called for every trade.

    You may want to work with last price value of OnMarketData() though, to align the values you're seeking to compare. You're then capturing the values all within the same event.

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      There won't be a bar without having a trade. OnBarUpdate() is called for every trade.
      Good point.

      As for capturing all data in OnMarketData(), would something like the below section of code work?

      Code:
      protected override void OnMarketData(MarketDataEventArgs e) 
      { 
          if (e.MarketDataType == MarketDataType.Last)
          {
              double lastPrice = e.Price;
              if (lastPrice == currentBid) Print("Last trade at bid");
              else if (lastPrice == currentAsk) Print("Last trade at ask");
              else Print("Last trade at neither bid nor ask");
          }
          else if (e.MarketDataType == MarketDataType.Ask) double currentAsk = e.Price;
          else if (e.MarketDataType == MarketDataType.Bid) double currentBid = e.Price;
      }

      Comment


        #4
        Yes, that looks like a good approach, although would probably need to declare your variables so they're available outside of their else if blocks.
        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        59 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        143 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        161 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        97 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        276 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X