Search code examples
javascriptnode.jsbotstradingmoving-average

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.


Solution

  • I found the problem. It come from the api of MetaTrader, the "getHistoricalCandles" dont work correctly as expected. In the api its written :

    getHistoricalCandles(symbol, timeframe, startTime, limit)
    symbol: symbol to retrieve candles for
    TimeFrame: define the timeframe according to which the candles must be generated
    StartTime: time to start loading candles from. Note that candles are loaded in backward direction, so this should be the latest time, **leave it empty to request latest candles**
    Limit: maximum of candles to retrieve, must be less or equals to 1000
    

    The problem here is the StartTime parameter, it absolutely dont work like they said, when I leave it empty, or when I put a Date.now() it retrieve candles from 5 hours before, and to retrieve the absolute last candle, I have to put Date.now()+10000000000, so this might be a timezone error, unsolvable for the moment cause its from api side...