Saturday, 22 July 2023

Nodejs MetaAPI Cloud / Calculate moving average

im making a trading bot who uses metaapi.cloud, I am trying to calculate the moving average (fast/exponential) but it returns me invalid values, here is my code :

async movingAverage(symbol, period, type = "S") {
        let candles = (await this.account.getHistoricalCandles(symbol, this.params.timeframe, null, period)).map(c => c.close);
        const result = [];
        let sum = 0;
        if (type === "S") {
            for (let i = 0; i < period; i++) {
                sum += candles[i];
            }
            result.push(sum / period);
            for (let i = period; i < candles.length; i++) {
                sum = sum - candles[i - period] + candles[i];
                result.push(sum / period)
            }
        } else if (type === "E") {
            const weight = 2 / (period + 1);
            for (let i = 0; i < period; i++) {
                sum += candles[i];
            }
            sum /= period;
            result.push(sum);

            for (let i = period; i < candles.length; i++) {
                sum = (candles[i] * weight) + (sum * (1 - weight));
                result.push(sum);
            }
        } else {
            // throw Error()
        }
        return result;
    }

Here is how I use it :

async onTick(infos) {
        let sma = await this.movingAverage(infos.symbol, this.params.fast, "S");
        console.log('SMA ' + sma[0]);
}

Right now when I test it, the SMA should return "1906.6963", but it gives me "1900.7813" Maybe im using the wrong way to calculate them? If someone has a solution! Thanks in advance.



from Nodejs MetaAPI Cloud / Calculate moving average

No comments:

Post a Comment