How To Compute Commodity Channel Index (CCI) using Erlang?

Member

by leonor , in category: Technology , a year ago

How To Compute Commodity Channel Index (CCI) using Erlang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by matilde , a year ago

@leonor 

Commodity Channel Index (CCI) measures the deviation of a financial instrument's price from its average price. It is typically calculated using the following formula:


CCI = (Price - SMA) / (0.015 * Mean Deviation)


Where:

  • Price is the current closing price of the asset
  • SMA is the simple moving average of the asset's closing prices over a specified period
  • Mean Deviation is the mean deviation of the asset's closing prices from its SMA over the specified period


To compute CCI using Erlang, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
-module(cci).
-export([compute_cci/2]).

compute_cci(Prices, Period) ->
    Averages = moving_average(Prices, Period),
    Deviations = [abs(X - lists:nth(Index, Averages)) || {X, Index} <- lists:zip(Prices, lists:seq(1, length(Prices)))],
    MeanDeviation = lists:sum(Deviations) / length(Deviations),
    {Price, _} = lists:last(Prices),
    SMA = lists:last(Averages),
    CCI = (Price - SMA) / (0.015 * MeanDeviation),
    CCI.


moving_average(Prices, Period) ->
    lists:map(fun(X) -> average(lists:sublist(Prices, X-Period, Period)) end, lists:seq(Period, length(Prices))).

average(List) when length(List) > 0 ->
    lists:sum(List) / length(List);
average(_) ->
    0.


In this code snippet, the compute_cci/2 function takes a list of prices and a period as input and computes the CCI value. It first calculates the moving averages of the prices using the moving_average function, then computes the mean deviation and finally calculates the CCI value using the formula mentioned above.


You can call the compute_cci/2 function with a list of prices and a specified period to get the CCI value for that particular asset.

Related Threads:

How To Compute Commodity Channel Index (CCI) using JavaScript?
How To Calculate Commodity Channel Index (CCI) using Golang?
How To Calculate Commodity Channel Index (CCI) in Visual Basic?
How To Compute Relative Strength Index (RSI) using Java?
How To Calculate Relative Strength Index (RSI) using Fortran?
How To Calculate Relative Strength Index (RSI) using Go?