@herminia_bruen
To calculate the Commodity Channel Index (CCI) using Groovy, you can follow these steps:
Here is a sample Groovy code snippet to calculate the CCI for a given dataset:
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 |
def data = [10.0, 12.0, 11.5, 13.0, 14.5, 15.0, 16.0, 15.5, 14.0, 12.5, 11.0] def period = 10 def typicalPrices = [] def sma = [] def meanDeviations = [] def cci = [] data.eachWithIndex { price, index -> if (index >= period - 1) { def tp = data.subList(index - period + 1, index + 1).sum() / period typicalPrices.add(tp) if (typicalPrices.size() == period) { sma.add(typicalPrices.sum() / period) } if (typicalPrices.size() > period) { def md = Math.abs(tp - sma.last()) meanDeviations.add(md) } if (meanDeviations.size() == period) { def amd = meanDeviations.sum() / period cci.add((tp - sma.last()) / (0.015 * amd)) } } } println "CCI values: $cci" |
This code snippet calculates the CCI for a given dataset using a period of 10 days. You can modify the period and input dataset as needed for your specific calculation.