I am familiar with building moving averages (whether SMA or EMA) from candle/OHLC data or indeed from a ticker which reports the price at a fixed interval.

But I would like to extend this to a raw trades feed I have access to. Now clearly trades can come at irregular intervals and depending on the resolution of the feed, likely I may have multiple trades with the same time.

Of course one approach is to aggregate trades into fixed time intervals - basically building my own candles (or 'buckets'). But then some intervals might have zero trades so this approach has issues.

Is there a way to extend SMA/EMA calculations to arbitrary trade times, given a trade time resolution of say 1 second?

  • 1
    Volume weighted moving average over n periods. Jun 3 at 21:13
  • 1
    Aggregate the trades into fixed time intervals and omit the intervals that have no trades from your calculation. Jun 3 at 23:27
  • @BobBaerker I was wondering if gaps would skew the average. I suppose for gaps I could carry on the last value but that would also skew in a different way. I suppose either has a place and it's finding which is more useful?
    – Mr. Boy
    Jun 4 at 10:46

To create moving average for a non-timeseries data. You need to consider normalize your data with timestamp. You can fill the gaps between trades with scaled datas, values that smooth the curve from your last trade to your next trade. Make use of the e constant, you can multiply the last value with a result from a function of e to time.

For example: Your last trade is 1405, your next trade is 1550, the time span is n.

You can make something like: 1405, e/(n-1) * 1405, e/(n-2) * 1405, ... e/(n-t) * 1405 < 1550 (This is just an example, the real function must be tested)

It will smooth the curve and does nothing to the shape of your intended moving average.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.