Skip to main content

Get Market Data

Retrieve current market data for a specific instrument, including bids, offers, last price, and more.
public async Task<MarketDataRestApi> GetMarketData(
    Instrument instrument, 
    short depth = 5, 
    Entry[] entries = null
)
public async Task<MarketDataRestApi> GetMarketData(
    string marketId, 
    string symbol, 
    short depth = 5, 
    Entry[] entries = null
)

Parameters

instrument
Instrument
required
The instrument to get market data for
marketId
string
required
Market identifier (e.g., “ROFX”, “MERV”)
symbol
string
required
Instrument symbol/ticker
depth
short
default:"5"
Order book depth (number of price levels)
entries
Entry[]
default:"all entries"
Specific market data entries to retrieve. If null, all entries are returned.Available entries: Bids, Offers, Last, Open, Close, SettlementPrice, SessionHighPrice, SessionLowPrice, Volume, OpenInterest, IndexValue, EffectiveVolume, NominalVolume

Response

Status
HttpStatusCode
HTTP status code of the response
Aggregated
bool
Whether data is aggregated
Data
Entries
Market data entries object containing:

Example

using Primary;
using Primary.Data;

var api = new Api(Api.DemoEndpoint);
await api.Login(Api.DemoUsername, Api.DemoPassword);

// Get all instruments
var instruments = await api.GetAllInstruments();
var instrument = instruments.First();

// Get market data with default depth (5)
var marketData = await api.GetMarketData(instrument);

// Access bid/offer data
if (marketData.Data.HasBids())
{
    decimal topBidPrice = marketData.Data.GetTopBidPrice();
    decimal topBidSize = marketData.Data.GetTopBidSize();
    Console.WriteLine($"Top Bid: {topBidPrice} x {topBidSize}");
}

if (marketData.Data.HasOffers())
{
    decimal topOfferPrice = marketData.Data.GetTopOfferPrice();
    decimal topOfferSize = marketData.Data.GetTopOfferSize();
    Console.WriteLine($"Top Offer: {topOfferPrice} x {topOfferSize}");
}

// Access last traded price
if (marketData.Data.HasLastPrice())
{
    Console.WriteLine($"Last: {marketData.Data.Last.Price}");
}

Get Specific Entries

// Only get last price and volume
var entries = new[] { Entry.Last, Entry.Volume };
var marketData = await api.GetMarketData(instrument, depth: 1, entries: entries);

Console.WriteLine($"Last Price: {marketData.Data.Last?.Price}");
Console.WriteLine($"Volume: {marketData.Data.Volume}");

Deep Order Book

// Get 10 levels of depth
var marketData = await api.GetMarketData(instrument, depth: 10);

// Display all bid levels
foreach (var bid in marketData.Data.Bids)
{
    Console.WriteLine($"Bid: {bid.Price} x {bid.Size}");
}

// Display all offer levels
foreach (var offer in marketData.Data.Offers)
{
    Console.WriteLine($"Offer: {offer.Price} x {offer.Size}");
}

Get Historical Trades

Retrieve historical trade data for a specific instrument and date range.
public async Task<IEnumerable<Trade>> GetHistoricalTrades(
    Instrument instrument,
    DateTime dateFrom,
    DateTime dateTo
)

Parameters

instrument
Instrument
required
Instrument to get trade history for
dateFrom
DateTime
required
Start date for historical data (format: yyyy-MM-dd)
dateTo
DateTime
required
End date for historical data (format: yyyy-MM-dd)

Response

Returns an IEnumerable<Trade> where each Trade contains:
Price
decimal
Trade price
Size
decimal
Trade size/quantity
DateTime
DateTime
Trade timestamp
ServerTime
long
Server timestamp in Unix epoch format

Example

var instrument = new Instrument
{
    Market = "MERV",
    Symbol = "MERV - XMEV - GGAL - 48hs"
};

var dateFrom = new DateTime(2024, 1, 1);
var dateTo = new DateTime(2024, 1, 31);

try
{
    var trades = await api.GetHistoricalTrades(instrument, dateFrom, dateTo);
    
    foreach (var trade in trades)
    {
        Console.WriteLine($"{trade.DateTime}: {trade.Size} @ {trade.Price}");
    }
    
    // Calculate statistics
    decimal avgPrice = trades.Average(t => t.Price);
    decimal totalVolume = trades.Sum(t => t.Size);
    
    Console.WriteLine($"Average Price: {avgPrice}");
    Console.WriteLine($"Total Volume: {totalVolume}");
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

Notes

The historical trades endpoint internally uses marketId = "MERV" and external = 1 parameters for data retrieval.
Large date ranges may return significant amounts of data. Consider paginating or limiting your date range for better performance.

Market Data Entries

The Entry enum defines all available market data types:
EntryDescription
BidsBest buy offers in the Market Book
OffersBest sell offers in the Market Book
LastLast price traded in the Market Book
OpenOpening price in the Market Book
CloseClosing price in the Market Book
SettlementPriceSettlement price (only for futures)
SessionHighPriceHighest price traded
SessionLowPriceLowest price traded
VolumeTraded volume in contracts/nominal
OpenInterestOpen interest in contracts (only for futures)
IndexValueCalculated index value (only for indices)
EffectiveVolumeEffective traded volume
NominalVolumeNominal traded volume

Helper Methods

The Entries class provides convenient helper methods:
// Check if data is available
bool hasBids = marketData.Data.HasBids();
bool hasOffers = marketData.Data.HasOffers();
bool hasLastPrice = marketData.Data.HasLastPrice();

// Get top of book
decimal topBidPrice = marketData.Data.GetTopBidPrice();
decimal topBidSize = marketData.Data.GetTopBidSize();
decimal topOfferPrice = marketData.Data.GetTopOfferPrice();
decimal topOfferSize = marketData.Data.GetTopOfferSize();

API Endpoint

The market data methods use the following REST endpoint:
GET {BaseUri}/rest/marketdata/get?symbol={symbol}&marketId={marketId}&entries={entries}&depth={depth}

Build docs developers (and LLMs) love