1

嗨,我需要在 Zenbot 中计算 EMA,但从上一期开始,如何计算?

      return function ema (s, key, length, source_key) {
    if (!source_key) source_key = 'close'
    if (s.lookback.length >= length) {
      var prev_ema = s.lookback[0][key]
      if (typeof prev_ema === 'undefined' || isNaN(prev_ema)) {
        var sum = 0
        s.lookback.slice(0, length).forEach(function (period) {
          sum += period[source_key]
        })
        prev_ema = sum / length
      }
      var multiplier = 2 / (length + 1)
      s.period[key] = (s.period[source_key] - prev_ema) * multiplier + prev_ema
    }
  }
}
4

1 回答 1

2

最近想通了同样的事情,可以用这个commit作为参考。

+++ b/extensions/strategies/wavetrend/strategy.js
@@ -0,0 +1,172 @@
...
+    onPeriod: function (s, cb) {
+      if (s.period.wto) {
+        s.signal = null // hold
+        let prev_wto = s.lookback[0].wto
+        let wto = s.period.wto
+        let prev_hcl3 = s.lookback[0].hcl3
+        let hcl3 = s.period.hcl3
+        let prev_ema = s.lookback[0].ema
+        let ema = s.period.ema
+
+        if (!s.sell_signal_close)
+          s.sell_signal_close = 0
+        if (!s.buy_signal_close)
+          s.buy_signal_close = 0
+        if (!s.sell_pct_orig)
+          s.sell_pct_orig = s.sell_pct
+        if (!s.buy_pct_orig)
+          s.buy_pct_orig = s.sell_pct
于 2018-01-16T09:17:15.250 回答