Nodejs MetaAPI Cloud / 计算移动平均值
P粉715274052
P粉715274052 2023-09-03 09:12:59
0
2
437
<p>我正在制作一个使用metaapi.cloud的交易机器人,我正在尝试计算移动平均值(快速/指数),但它返回给我无效值,这是我的代码:</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 = []; 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; } </pre> <p>这是我的使用方法:</p> <pre class="brush:js;toolbar:false;">async onTick(infos) { let sma = await this.movingAverage(infos.symbol, this.params.fast, "S"); console.log('SMA ' + sma[0]); } </pre> <p>现在,当我测试它时,SMA 应该返回“1906.6963”,但它给我的是“1900.7813” 也许我使用了错误的方法来计算它们? 如果有人有解决办法!提前致谢。</p>
P粉715274052
P粉715274052

全部回复(2)
P粉071743732
  • 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>
P粉563446579

我发现了问题。它来自 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 端...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!