> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.ninjatrader.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.ninjatrader.com/_mcp/server.

# OnMarketData()

## Definition

An event driven method which is called and guaranteed to be in the correct sequence for every change in level one market data for the underlying instrument. **OnMarketData()** can include but is not limited to the bid, ask, last price and volume.

1. This is a real-time data stream and can be CPU intensive if your program code is compute intensive (not optimal).
2. By default, this method is not called on historical data (backtest), however it can be called historically by using [TickReplay](/developer/desktop-sdk/guides/educational-resources/developing-for-tick-replay).
3. If used with [TickReplay](/developer/desktop-sdk/guides/educational-resources/developing-for-tick-replay), please keep in mind Tick Replay ONLY replays the Last market data event, and only stores the best inside bid/ask price at the time of the last trade event. You can think of this as the equivalent of the bid/ask price at the time a trade was reported. As such, historical bid/ask market data events (i.e., bid/ask volume) DO NOT work with Tick Replay. To obtain those values, you need to use a [historical bid/ask series](/developer/desktop-sdk/guides/educational-resources/using-historical-bid-ask-serie) separately from TickReplay through **OnBarUpdate()**. More information can be found under [Developing for Tick Replay](/developer/desktop-sdk/guides/educational-resources/developing-for-tick-replay).
4. With [multi-time frame and instrument strategies](/developer/desktop-sdk/guides/educational-resources/multi-time-frame-instruments), a subscription will be created on all bars series added in your indicator or strategy (even if the instrument is the same). The market data subscription behavior occurs both in real-time and during [TickReplay](/developer/desktop-sdk/guides/educational-resources/developing-for-tick-replay) historical.
5. Do not leave an unused **OnMarketData()** method declared in your NinjaScript object. This will unnecessarily attach a data stream to your strategy which uses unnecessary CPU cycles.
6. Should you wish to run comparisons against prior values you will need to store and update local variables to track the relevant values.
7. The **OnMarketData()** method is expected to be called after [OnBarUpdate()](/developer/desktop-sdk/references/common/onbarupdate).

## Method Return Value

This method does not return a value.

## Syntax

You must override the method in your strategy or indicator with the following syntax.

**protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)**

The NinjaScript code wizards can automatically generate the method syntax for you when creating a new script.

## Parameters

| Parameter            | Description                                                                                                                                    |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| **marketDataUpdate** | [MarketDataEventArgs](/developer/desktop-sdk/references/common/onmarketdata/marketdataeventargs) representing the recent change in market data |

## Examples

```csharp
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
     // Print some data to the Output window
     if (marketDataUpdate.MarketDataType == MarketDataType.Last) 
           Print(string.Format("Last = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
     else if (marketDataUpdate.MarketDataType == MarketDataType.Ask)
         Print(string.Format("Ask = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
     else if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
         Print(string.Format("Bid = {0} {1}", marketDataUpdate.Price, marketDataUpdate.Volume));
}
```