Nodejs MetaAPI Cloud / 計算移動平均值
P粉715274052
P粉715274052 2023-09-03 09:12:59
0
2
439
<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.movi​​ngAverage(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學習者快速成長!