Computing time-weighted moving average
Asked Answered
C

4

5

I have a time series of stock prices and wish to compute the moving average over a ten minute window (see diagram below). As price ticks occur sporadically (i.e. they are not periodic) it seems fairest to calculate a time-weighted moving average.

Time Series

In the diagram there are four price changes: A, B, C and D, with the latter three occurring inside the window. Note that because B only occurs some time into the window (say 3 minutes), the value of A still contributes to the computation.

In fact, as far as I can tell the computation should be solely based on the values of A, B and C (not D) and the durations between them and the next point (or in the case of A: the duration between the start of the time window and B). Initially D will not have any effect as its time weighting will be zero. Is this correct?

Assuming this is correct, my concern is that the moving average will "lag" more than the non-weighted computation (which would account for the value of D immediately), However, the non-weighted computation has its own disadvantages:

  • "A" would have as much effect on the result as the other prices despite being outside the time window.
  • A sudden flurry of fast price ticks would heavily bias the moving average (although perhaps this is desirable?)

Can anyone offer any advice over which approach seems best, or whether there's an alternative (or hybrid) approach worth considering?

Conspectus answered 14/4, 2012 at 21:35 Comment(0)
W
4

Your reasoning is correct. What do you want to use the average for though? Without knowing that it's hard to give any advice.

Perhaps an alternative would be to consider your running average A, and when a new value V comes in, calculate the new average A' to be (1-c)*A+c*V, where c is between 0 and 1. This way the more recent ticks have a stronger influence, and the effect of old ticks dissipates over time. You could even have c depend on the time since the previous ticks (c becoming smaller as the ticks get closer).

In the first model (weighting) the average would be different each second (as old readings get lower weight and new readings higher) so it's always changing which may not be desirable. With the second approach, the prices make sudden jumps as new prices get introduced and old ones disappear from window.

Willms answered 14/4, 2012 at 21:50 Comment(2)
Thanks; adjusting c based on the time since the previous tick is a great idea. It means I can factor in a time weighting without having to maintain a lot of additional state.Conspectus
That formula is known as Brown's simple exponential smoothing. I'm reading about it on Wikipedia right now, so just thought I'd chime in.Gudrunguelderrose
C
9

The two suggestions come from the discrete world, but you might find an inspiration for your particular case.

Have a look at exponential smoothing. In this approach you introduce the smoothing factor (α ∈ [0;1]) that allows you to alter the influence of the recent elements on the “forecast” value (older elements are assigned exponentially decreasing weights):

st=αxt-1+(1+α)st-1; s1=x0

I have created a simple animation of how the exponential smoothing would track the a uniform time series x=[1 1 1 1 3 3 2 2 2 1] with three different α={0.3, 0.6, 0.9}:

enter image description here

Have also a look at some of the reinforcement learning techniques (look at the different discounting methods) for example TD-learning and Q-Learning.

Caucasoid answered 14/4, 2012 at 21:57 Comment(0)
C
6

In expanding Tom's answer, the formula for taking into consideration the spacing between ticks can be formalized (close ticks have proportionately lower weighting):

eman = u * eman-1 + (v - u) * xn-1 + (1 - v) * xn

where:

a = ( tn - tn-1) / T
that is, a is a ratio of delta of arrival time over averaging interval

and

u = e-a

and

v = 1 (use previous point), or
v = (1 - u) / a (linear interpolation>, or
v = u (next point)

Further information is found on page 59 of the book An Introduction To High Frequency Finance.

Caz answered 22/12, 2013 at 23:25 Comment(1)
Another reference for this algorithm can be found here: eckner.com/papers/…Tynan
W
4

Your reasoning is correct. What do you want to use the average for though? Without knowing that it's hard to give any advice.

Perhaps an alternative would be to consider your running average A, and when a new value V comes in, calculate the new average A' to be (1-c)*A+c*V, where c is between 0 and 1. This way the more recent ticks have a stronger influence, and the effect of old ticks dissipates over time. You could even have c depend on the time since the previous ticks (c becoming smaller as the ticks get closer).

In the first model (weighting) the average would be different each second (as old readings get lower weight and new readings higher) so it's always changing which may not be desirable. With the second approach, the prices make sudden jumps as new prices get introduced and old ones disappear from window.

Willms answered 14/4, 2012 at 21:50 Comment(2)
Thanks; adjusting c based on the time since the previous tick is a great idea. It means I can factor in a time weighting without having to maintain a lot of additional state.Conspectus
That formula is known as Brown's simple exponential smoothing. I'm reading about it on Wikipedia right now, so just thought I'd chime in.Gudrunguelderrose
E
3

Yes, the moving average will of course lag. This is because its value is historic information: it summarizes samples of the price over the last 10 minutes. This kind of average is inherently "laggy". It has a built in five minute offset (because a box average without offset would be based on +/- 5 minutes, centered on the sample). If the price has been at A for a long time and then changes one time to B, it takes 5 minutes for the average to reach (A + B) / 2.

If you want to average/smooth a function without any shift in the domain, the weight has to be evenly distributed around the sample point. But this is impossible to do for prices occurring in real time, since future data is unavailable.

If you want a recent change, like D, to have a bigger impact, use an average which gives a bigger weight to recent data, or a shorter time period, or both.

One way to smooth data is simply to use a single accumulator (the "smoothed estimator") E and take periodic samples of the data S. E is updated as follows:

E = E + K(S - E)

I.e. a fraction K (between 0 and 1) of the difference between the current price sample S and the estimator E is added to E. Suppose the price has been at A for a long time, so that E is at A, and then suddenly changes to B. The estimator will start moving toward B in a exponential way (like heating/cooling, charging/discharging of a capacitor, etc). At first it will make a big jump, and then smaller and smaller increments. How fast it moves depends on K. If K is 0, the estimator doesn't move at all, and if K is 1 it moves instantly. With K you can adjust how much weight you give to the estimator versus the new sample. More weight is given to more recent samples implicitly, and the sample window basically extends to infinity: E is based on every value sample that ever occurred. Though of course very old ones have next to no influence on the current value. A very simple, beautiful method.

Extempore answered 14/4, 2012 at 21:50 Comment(1)
This is the same as Tom's answer. His formula for the new value of the estimator is (1 - K)E + KS, which is algebraically the same as E + K(S - E): it is a "linear blending function" between the current estimator E and the new sample S where the value of K [0, 1] controls the blend. Writing it that way is nice and useful. If K is 0.7, we take 70% of S, and 30% of E, which is the same as adding 70% of the difference between E and S back to E.Extempore

© 2022 - 2024 — McMap. All rights reserved.