如何从上一期开始计算`lib.ema`?

时间:2018-01-12 08:58:28

标签: javascript zenbot

您好我需要在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
    }
  }
}

1 个答案:

答案 0 :(得分: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