@cathrine_goyette
Here's how you can calculate the Commodity Channel Index (CCI) using Kotlin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
fun calculateCCI(prices: List<Double>, period: Int): List<Double> {
val cciValues = mutableListOf<Double>()
for (i in period until prices.size) {
val tp = prices.subList(i - period, i).sum() / period // Typical Price calculation
val meanDeviation = prices.subList(i - period, i).map { Math.abs(it - tp) }.sum() / period // Mean Deviation calculation
val cci = (tp - prices[i]) / (0.015 * meanDeviation) // CCI calculation
cciValues.add(cci)
}
return cciValues
}
fun main() {
val prices = listOf(10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0) // Sample prices data
val period = 4 // CCI period
val cciValues = calculateCCI(prices, period)
println("CCI values: $cciValues")
}
|
In this code snippet, the calculateCCI function takes a list of prices and a period as input parameters, and calculates the CCI values for the given prices based on the specified period. The function returns a list of calculated CCI values.
In the main function, we provide a sample list of prices and a period, and then call the calculateCCI function to calculate the CCI values. Finally, the calculated CCI values are printed to the console.
You can modify the sample prices and period values as needed to calculate the CCI for different datasets.