How To Calculate Commodity Channel Index (CCI) using Golang?

Member

by pink , in category: Technology , 2 months ago

How To Calculate Commodity Channel Index (CCI) using Golang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dion.waelchi , 2 months ago

@pink 

To calculate Commodity Channel Index (CCI) using Golang, you can use the following formula:


CCI = (Typical Price - MA) / (0.015 * Mean Deviation)


Here's a sample Golang code to calculate CCI for a given set of typical prices:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
	"fmt"
	"math"
)

func main() {
	// Sample set of typical prices
	typicalPrices := []float64{100, 105, 110, 115, 120, 125, 130}

	// Calculate typical price
	var typicalPriceSum float64
	for _, price := range typicalPrices {
		typicalPriceSum += price
	}
	typicalPrice := typicalPriceSum / float64(len(typicalPrices))

	// Calculate mean deviation
	var meanDeviationSum float64
	for _, price := range typicalPrices {
		meanDeviationSum += math.Abs(price - typicalPrice)
	}
	meanDeviation := meanDeviationSum / float64(len(typicalPrices))

	// Calculate CCI
	MA := 20 // Assuming moving average value is 20
	cci := (typicalPrice - float64(MA)) / (0.015 * meanDeviation)

	fmt.Printf("CCI: %.2f
", cci)
}


In this code snippet, we first calculate the typical price by averaging the given set of typical prices. Then, we calculate the mean deviation by finding the average absolute difference between each typical price and the typical price. Finally, we apply the CCI formula to calculate the CCI value. You can adjust the values in the typicalPrices array and the moving average value (MA) to calculate CCI for different sets of data.