Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing time-weighted moving average

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?

like image 667
Adamski Avatar asked Apr 14 '12 21:04

Adamski


People also ask

How do you calculate time-weighted average?

A time-weighted average is equal to the sum of the portion of each time period (as a decimal, such as 0.25 hour) multiplied by the levels of the substance or agent during the time period divided by the hours in the workday (usually 8 hours).

How do you calculate weighted moving average?

A Weighted moving average (WMA) attaches greater weight to the most recent data. The weighting is calculated from the sum of days. Weighted values are calculated by multiplying today's price by 5/15, yesterday by 4/15, and so on. The weighted moving average is the sum of the 5 weighted values.

How do you calculate period of moving average?

A moving average is a technical indicator that investors and traders use to determine the trend direction of securities. It is calculated by adding up all the data points during a specific period and dividing the sum by the number of time periods.

How do you calculate weighted average weight?

To find a weighted average, multiply each number by its weight, then add the results. If the weights don't add up to one, find the sum of all the variables multiplied by their weight, then divide by the sum of the weights.


2 Answers

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.

like image 83
Anonymous Avatar answered Sep 28 '22 16:09

Anonymous


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.

like image 45
Raymond Burkholder Avatar answered Sep 28 '22 14:09

Raymond Burkholder