Skip to content

Instantly share code, notes, and snippets.

@alexnask
Forked from anesiadis/test2.py
Last active January 19, 2017 12:41
Show Gist options
  • Save alexnask/fe0b8478a004c54b6455e24caad1b225 to your computer and use it in GitHub Desktop.
Save alexnask/fe0b8478a004c54b6455e24caad1b225 to your computer and use it in GitHub Desktop.
class MACD(Indicator):
def __init__(self, strategy, pair, short_period=12, long_period=26, weight_period=9):
self._weight_period = weight_period
self._weight = 2.0 / (weight_period + 1.0)
self._short_ema = EMA(strategy, pair, period=short_period)
self._long_ema = EMA(strategy, pair, period=long_period)
self._data = pandas.DataFrame(columns=['Value', 'MACD_line', 'Signal_line'])
super(MACD, self).__init__("MACD", strategy, pair)
def value(self):
return self._data['Value'][-1]
def data(self):
return self._data
def ready(self):
return self._short_ema.ready() and self._long_ema.ready()
def warmup(self):
return max(self._long_ema.warmup(), self._weight_period)
def feed(self, data):
MACD_line = self._short_ema.value() - self._long_ema.value()
if len(self.data) == 0
self._data.loc[data['Date']] = { 'MACD_line' : MACD_line, 'Signal_line' : MACD_line, 'Value' : 0 }
else:
prev_diff = self.value()
prev_macd = self._data['MACD_line'][-1]
signal_line = (prev_macd - prev_diff) * self._weight + prev_diff
self._data.loc[data['Date']] = { 'Signal_line' : signal_line, 'MACD_line' : MACD_line, 'Value' : MACD_line - signal_line }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment