Relative Vigor Index (SDK Trading)

The Relative Vigor Index (RVI) is a technical analysis indicator to measure the strength of a trend by comparing a financial instrument's closing price to its trading range. It's based on the tendency for price quotes to close higher than the value by that they open in uptrends and to close lower than the value by that they open in downtrends.

Relative Vigor Index (SDK Trading)

registerIndicator("rvi", "Relative Vigor Index(v1.0)", function (context) {
    var dataInputOpen = getDataInput(context, 0)
    var dataInputHigh = getDataInput(context, 1)
    var dataInputLow = getDataInput(context, 2)
    var dataInputClose = getDataInput(context, 3)
    var dataOutputMain = getDataOutput(context, "main")
    var dataOutputSignal = getDataOutput(context, "signal")
    var period = getIndiParameter(context, "period")

    var calculatedLength = getCalculatedLength(context)

    var ptr = calculatedLength

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

        for (var i = 0; i < period + 2; i++) {
            dataOutputMain[i] = 0
            dataOutputSignal[i] = 0
        }
    }

    var upTmp, downTmp, tmp, dTmp

    while (ptr < dataInputOpen.length) {
        tmp = 0
        dTmp = 0

        for (var i = ptr; i > ptr - period; i--) {
            upTmp = ((dataInputClose[i] - dataInputOpen[i]) + 2 * (dataInputClose[i - 1] - dataInputOpen[i - 1]) + 2 * (dataInputClose[i - 2] - dataInputOpen[i - 2]) + (dataInputClose[i - 3] - dataInputOpen[i - 3])) / 6
            downTmp = ((dataInputHigh[i] - dataInputLow[i]) + 2 * (dataInputHigh[i - 1] - dataInputLow[i - 1]) + 2 * (dataInputHigh[i - 2] - dataInputLow[i - 2]) + (dataInputHigh[i - 3] - dataInputLow[i - 3])) / 6

            tmp += upTmp
            dTmp += downTmp
        }

        if (0 == dTmp) {
            dataOutputMain[ptr] = tmp
        } else {
            dataOutputMain[ptr] = tmp / dTmp
        }

        ptr++
    }

    ptr = calculatedLength

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

    while (ptr < dataInputOpen.length) {
        dataOutputSignal[ptr] = (dataOutputMain[ptr] + 2 * dataOutputMain[ptr - 1] + 2 * dataOutputMain[ptr - 2] + dataOutputMain[ptr - 3]) / 6

        ptr++
    }
},[{
    name: "period",
    value: 14,
    required: true,
    type: PARAMETER_TYPE.INTEGER,
    range: [1, 100]
}],
[{
    name: DATA_NAME.OPEN,
    index: 0
},{
    name: DATA_NAME.HIGH,
    index: 1
},{
    name: DATA_NAME.LOW,
    index: 2
},{
    name: DATA_NAME.CLOSE,
    index: 3
}],
[{
    name: "main",
    visible: true,
    renderType: RENDER_TYPE.LINE,
    color: "#6CBA81"
},{
    name: "signal",
    visible: true,
    renderType: RENDER_TYPE.LINE,
    color: "#ECAE93"
}],
WHERE_TO_RENDER.SEPARATE_WINDOW)