Market data snapshot

Is it possible to get a market data snapshot of market events? Let’s say that create a websocket connection and would like the data for the last X market data.

Is that possible?

I realized that the web trading platform has a code editor built in. In the map function, you can get a history object that you can iterate through to get previous data.

Does the websocket API provide any features like this?

You can use the asMuchAsElements field in your chart request to limit the retrieved bars to X amount, if that’s what you mean.

After I connect via websocket, can I request the last 100 ticks?

for example, in this sample code: Signaling Average True Range | Tradovate Custom Indicators

in the map funtion, a history object is passed in along with the trade data. You can call history.prior to get the previous trade data. You can akso iterate over the history object to get older trades.

Does the marketdata websocket have a similar feature?

The socket itself merely streams the data to you in realtime, if you want to do something with the data you’ll have to build something to consume, transform, or store it. In this case, really all you need to do is keep the data that comes to you in an array. Sort the data by time such that the newest data is at the tail of the array. Then when you want to find a previous value, you can just look back in the array however many values you need.

If you’re taking individual ticks and you want to look back 100 ticks, you’ll need to store at least that many at any given time. If you’re taking candles of 100 ticks, you will have a lookback of just 1 candle, so you’d only need to store a few. The implications of each configuration are different, of course.

To request the last 100 ticks from the websocket you can just use the md/getChart with a body something like this:

//for last 100 ticks, followed by present tick stream.
const body = {
  symbol: 'ESU2',
  chartDescription: {
    underlyingType: 'Tick',
    elementSize: 1,
    elementSizeUnit: 'UnderlyingUnits',
    withHistogram: true //or false, whatever you need
  },
  timeRange: {
    asMuchAsElements: 100
  }
}

After the last 100 ticks are retrieved, you’ll get the { eoh: true } end-of-history flag object. From this point on, data you receive will be in real time, so to keep measuring the last 100 ticks from present, you’ll have to add the incoming ticks to the sorted array and store at least 100 at a time, dropping the oldest ticks as new ones arrive. Use map (or whatever the python equivalent of Array.map is) with your indicator functions over that array to find their present values.

I can store the data but what if I want data before my websocket connection was created?

in the map function, that also streams in the data in realtime but there’s the history object that has the previous data. It’s not something that I can implement by myself.

When you ask for the asMuchAsElements value, you will get the last n elements ending in the current element. So you will actually be retrieving data from before the socket was created. All the history object does is allow you to look back in the array of data essentially. Beyond asMuchAsElements, you can make historical requests or requests for specific timeframes as well. You just need to use the asFarAsTimestamp and closestTimestamp properties of timeRange in the chart request. Check out this document about how to retrieve data beyond the maximum transmission of a single request.

just to confirm, if i change the element size when requesting tick charts to 150 like so,
‘element size’ : 150

I will get the bar data of the 150 tick chart?

let’s say I connect to the websocket, I am building my depth of market order book.

I have the price ladder, a simple example below.

{3940.0: 1311, 3940.5: 1239, 3940.25: 170, 3940.75: 1393, 3939.75: 940, 3941.0: 1954, 3941.25: 1049, 3939.5: 432, 3941.5: 724, 3941.75: 764, 3942.0: 481, 3942.25: 1024, 3942.5: 1748, 3942.75: 1592, 3943.0: 3066, 3943.25: 715, 3943.5: 216, 3943.75: 14, 3939.25: 102, 3939.0: 718, 3938.75: 211, 3938.5: 23, 3938.25: 246, 3938.0: -200, 3937.75: 389}

It’s a bit ugly but look at the price 3938.0; it shows -200, it’s impossible to have negative orders, but if I could say get the order book form 1 second prior; 10 second prior or some period before, I could use the older data to fix the order book and have the proper quantities.

I worked around this a bit by subscribing to the market data and then also subscribing to the DOM to get the orders.

It creates a bit of complexity parsing the messages but it’s the most straight forward way that I’ve found to get the data that I need.