Algorithm to calculate the price volatility of a commodity

175 views Asked by At

I am trying to design an algorithm to calculate how volatile the price fluctuations of a commodity are.

The way I would like this to work is that if the price of a commodity constantly goes up and down, it should have a higher score than if the price of the commodity gradually increases and then falls in price rapidly.

Here is an example of what I mean:

Commodity A: 1 -> 2 -> 3 -> 2 -> 1 -> 3 -> 4 -> 2 -> 1

Commodity B: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 2

Commodity C: 1 -> 2 -> 3 -> 4 -> 5 -> 4 -> 3 -> 2-> 1

Commodity A has a 'wave' like pattern in that its price goes up and falls down on a regular basis.

Commodity B has a 'cliff' like pattern in that the price goes up gradually and then falls steeply.

Commodity C has a 'hill' like pattern in that the price rises gradually and then falls gradually.

A should receive the highest ranking, followed by C, followed by B. The more of a wave pattern the price of the commodity follows, the higher a ranking it should have.

Does have any suggestions for an algorithm that could do this?

Thanks!

1

There are 1 answers

6
zenwraight On BEST ANSWER

My Approach looks something like this.

For my algorithm, I am considering the above example.

A: 1 -> 2 -> 3 -> 2 -> 1 -> 3 -> 4 -> 2 -> 1

B: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 2

C: 1 -> 2 -> 3 -> 4 -> 5 -> 4 -> 3 -> 2-> 1

Now I will squash these list, by squash i mean taking the start value and end value of an increasing or decreasing sequence.

So, after squashing the list will look something like this.

A: 1 -> 3 -> 1 -> 4 -> 1
B: 1 -> 8 -> 2
C: 1 -> 5 -> 1

Now once this it done, I take the difference between i and i+1 element and then take the average and based on the average, I give them the rank.

So the difference between i and i+1 element will look something like this

      2    2     3      3
A: 1 --> 3 --> 1 --> 4 --> 1  

      7     6
B: 1 --> 8 --> 2

     4      4
C: 1 --> 5 --> 1

Now let's sum this difference and take the average.

A: (2+2+3+3)/4 = 2.5
B: (7+6)/2 = 6.5
C: (4+4)/2 = 4

Now we can assign ranks based on this average value where

A < C < B

Hope this helps!