Skip to content

Commit

Permalink
Merge pull request #8 from awratten/patch-7
Browse files Browse the repository at this point in the history
Exponential Moving Average
  • Loading branch information
bwentzloff committed Jul 22, 2017
2 parents 849390f + 2e6d5e0 commit 45033b4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions part 3/botindicators.py
Expand Up @@ -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]
Expand Down

0 comments on commit 45033b4

Please sign in to comment.