Heim  >  Fragen und Antworten  >  Hauptteil

Nodejs MetaAPI Cloud / Gleitenden Durchschnitt berechnen

<p>Ich erstelle einen Trading-Bot mit metaapi.cloud und versuche, den gleitenden Durchschnitt (schnell/exponentiell) zu berechnen, aber er gibt mir ungültige Werte zurück. Das ist mein Code: </p> <pre class="brush:js;toolbar:false;">async MovingAverage(symbol, period, type = "S") { let Candles = (await this.account.getHistoricalCandles(symbol, this.params.timeframe, null, period)).map(c => c.close); const result = []; sei Summe = 0; if (type === "S") { for (sei i = 0; i < Punkt; i++) { Summe += Kerzen[i]; } result.push(sum / period); for (let i = period; i < Candles.length; i++) { sum = sum - Kerzen[i - Punkt] + Kerzen[i]; result.push(Summe / Periode) } } else if (type === "E") { const-Gewicht = 2 / (Periode + 1); for (sei i = 0; i < Punkt; i++) { Summe += Kerzen[i]; } Summe /= Periode; result.push(sum); for (let i = period; i < Candles.length; i++) { Summe = (Kerzen[i] * Gewicht) + (Summe * (1 - Gewicht)); result.push(sum); } } anders { // throwError() } Rückgabeergebnis; } </pre> <p>So verwende ich es: </p> <pre class="brush:js;toolbar:false;">async onTick(infos) { let sma = waiting this.movingAverage(infos.symbol, this.params.fast, "S"); console.log('SMA ' + sma[0]); } </pre> <p>Wenn ich es jetzt teste, sollte der SMA „1906.6963“ zurückgeben, aber er gibt mir „1900.7813“ aus. Vielleicht berechne ich sie falsch? Falls jemand eine Lösung hat! Dank im Voraus. </p>
P粉715274052P粉715274052382 Tage vor444

Antworte allen(2)Ich werde antworten

  • P粉071743732

    P粉0717437322023-09-04 00:37:22

    • SMA 看起来正确,EMA 有许多不同的模式。由于您没有发布示例数据集,因此很难猜测从服务器传递的内容,但可能有很多无法转换的值,例如 NaN、null、空字符串或带逗号的数字、指数等。只是用具有错误值的数组替换了蜡烛。进行一些过滤,然后进行计算
    • 考虑 period > 数据集。在这种情况下,请将 period 设置为 length。

    在下面的示例中,将 period 设置为 1 以查看已处理的所有值,将 period 设置为非常大的数字以查看整个平均值。

    肯定还有其他我没有想到的边缘情况。为简洁起见,下面的示例使用 SMA。

    async function movingAverage(symbol, period, type = "S") {
            let candles = [1,2,3,"","",4, "0",0, null, "99,9123", undefined,"0.123e5", "wrongval", 9, 10, 20, 100]
            .map(d => parseFloat((d ?? "").toString().replace(",",".")))
            .filter(d => +d || +d === 0);
            const result = [];
            if (candles.length <= 0){return result}
            let sum = 0;
            period = Math.min(candles.length, period) || 1;
            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)
            }
            return result;
        }
        movingAverage("SPX",500).then(x => document.getElementById("result").textContent = x)
    <div id="result"></div>

    Antwort
    0
  • P粉563446579

    P粉5634465792023-09-04 00:18:21

    我发现了问题。它来自 MetaTrader 的 api,“getHistoricalCandles”无法按预期正常工作。 api中写的是:

    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

    这里的问题是 StartTime 参数,它绝对不会像他们所说的那样工作,当我将其留空时,或者当我放置 Date.now() 时,它会检索 5 小时前的蜡烛,为了检索绝对的最后一根蜡烛,我必须输入 Date.now()+10000000000,所以这可能是一个时区错误,暂时无法解决,因为它来自 api 端...

    Antwort
    0
  • StornierenAntwort