Commodity Channel Index (SDK Trading)

The Commodity Channel Index is an oscillator introduced in 1980. The indicator has grown in popularity since its introduction and is now a very common tool for traders in identifying cyclical trends not only in currencies, Forex but also equities and commodities. CCI would be relatively high when quotes are far above their average value and on the contrary, CCI would be relatively low when quotes are far below their average value.

Commodity Channel Index (SDK Trading)

registerIndicator("cci", "Commodity Channel Index(v1.0)", function (context) {
    var dataInput = getDataInput(context, 0)
    var dataOutput = getDataOutput(context, "cci")
    var dataOutputHL = getDataOutput(context, "cciHighLevel")
    var dataOutputLL = getDataOutput(context, "cciLowLevel")
    var dataOutputSma = getDataOutput(context, "sma")
    var highLevel = getIndiParameter(context, "highLevel")
    var lowLevel = getIndiParameter(context, "lowLevel")
    var period = getIndiParameter(context, "period")
    var cciFactor = 0.015 / period;

    var calculatedLength = getCalculatedLength(context)

    var ptr = calculatedLength

    if (ptr > 0) {
        ptr--
    } else {
        ptr = period - 1

        for (var i = 0; i < period - 1; i++) {
            dataOutput[i] = 0
            dataOutputHL[i] = highLevel
            dataOutputLL[i] = lowLevel
        }
    }

    sma(dataInput, dataOutputSma, calculatedLength, period)

    var sum, tmp, ptr2

    while (ptr < dataInput.length) {
        sum = 0
        ptr2 = ptr - period + 1
        while (ptr2 <= ptr) {
            sum += Math.abs(dataInput[ptr2] - dataOutputSma[ptr])
            ptr2++
        }
        tmp = sum * cciFactor

        if (0 == tmp) {
            dataOutput[ptr] = 0
        } else {
            dataOutput[ptr] = (dataInput[ptr] - dataOutputSma[ptr]) / tmp
        }

        dataOutputHL[ptr] = highLevel
        dataOutputLL[ptr] = lowLevel

        ptr++
    }
},[{
    name: "period",
    value: 14,
    required: true,
    type: PARAMETER_TYPE.INTEGER,
    range: [1, 100]
},{
    name: "highLevel",
    value: 100,
    required: false,
    type: PARAMETER_TYPE.NUMBER,
    range: [1, 200]
},{
    name: "lowLevel",
    value: -100,
    required: false,
    type: PARAMETER_TYPE.NUMBER,
    range: [-200, -1]
}],
[{
    name: DATA_NAME.HLC3,
    index: 0
}],
[{
    name: "cci",
    visible: true,
    renderType: RENDER_TYPE.LINE,
    color: "steelblue"
},{
    name: "cciHighLevel",
    visible: true,
    renderType: RENDER_TYPE.DASHARRAY,
    color: "#AAAAAA"
},{
    name: "cciLowLevel",
    visible: true,
    renderType: RENDER_TYPE.DASHARRAY,
    color: "#AAAAAA"
},{
    name: "sma",
    visible: false
}],
WHERE_TO_RENDER.SEPARATE_WINDOW)