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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        368 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        571 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X