From 2e6d5e0f0985a800697127559a4d41c28e07bc7c Mon Sep 17 00:00:00 2001 From: Anthony Wratten Date: Thu, 6 Jul 2017 16:51:19 +1000 Subject: [PATCH] Exponential Moving Average --- part 3/botindicators.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/part 3/botindicators.py b/part 3/botindicators.py index 93e790b..4dbe2d9 100644 --- a/part 3/botindicators.py +++ b/part 3/botindicators.py @@ -8,6 +8,19 @@ def movingAverage(self, dataPoints, period): if (len(dataPoints) > 1): return sum(dataPoints[-period:]) / float(len(dataPoints[-period:])) + def EMA(self, dataPoints, period, position=None, previous_ema=None): + """https://www.oanda.com/forex-trading/learn/forex-indicators/exponential-moving-average""" + if (len(dataPoints) < period + 2): + return None + c = 2 / float(period + 1) + if not previous_ema: + return self.EMA(dataPoints, period, period, self.movingAverage(dataPoints[-period*2 + 1:-period + 1], period)) + else: + current_ema = (c * dataPoints[-position]) + ((1 - c) * previous_ema) + if position > 0: + return self.EMA(dataPoints, period, position - 1, current_ema) + return previous_ema + def momentum (self, dataPoints, period=14): if (len(dataPoints) > period -1): return dataPoints[-1] * 100 / dataPoints[-period]