diff --git a/src/indicators.pyx b/src/indicators.pyx index 7c70d88..12a2b3a 100644 --- a/src/indicators.pyx +++ b/src/indicators.pyx @@ -3,6 +3,9 @@ from libc.math cimport sqrt from libc.stdlib cimport malloc, free from cpython cimport array + + + cpdef float SMA(double[:] closes, int period): """ Simple Moving Average function @@ -127,6 +130,7 @@ cpdef list EMA(double[:] closes, int period, float alpha = .3, int epsilon = 0): assert(not 0 <= epsilon < alpha, ("out of range, epsilon='%s'" % epsilon)) assert(period>0, ("out of range, period='%s'" %period)) + cdef float currentWeight cdef float numerator cdef float denominator @@ -155,6 +159,23 @@ cpdef list EMA(double[:] closes, int period, float alpha = .3, int epsilon = 0): finally: free(results) +# cpdef float EMA2(double[:] closes, int period): +# cdef float multiplier +# cdef int length = closes.shape[0] +# multiplier = (2 /(period + 1)) +# ema = [] +# sma = SMA(closes, period) +# cdef int j = 1 +# ema.append(first_ema) +# ema.append(( (s[period] - sma) * multiplier) + sma) + +# #now calculate the rest of the values +# for i in s[period+1:]: +# tmp = ( (i - ema[j]) * multiplier) + ema[j] +# j = j + 1 +# ema.append(tmp) + +# return ema cpdef (float, float, float, float, float, float, float, float, float) FIB_BANDS(double[:] closes, int period): @@ -176,17 +197,35 @@ cpdef (float, float, float, float, float, float, float, float, float) FIB_BANDS( -# cpdef (float, float, float) MACD(double[:] closes, int period): -# """ -# Moving Average Convergence Divergence............................................ -# @param closes: list of closing prices -# @param period: period to calculate for - -# """ -# float ema_26 = EMA(closes, 26) -# float ema_13 = EMA(closes, 13) -# float ema_5 = - +cpdef (float, float) MACD(double[:] closes, int short_period, int long_period, int fast_period): + """ + Moving Average Convergence Divergence............................................ + @param closes: list of closing prices + @param period: period to calculate for + @returns macd line, trend line tuple + """ + + cdef list ema_short + cdef list ema_long + cdef list trend_line + macd_line = [] + assert((long_period > short_period > fast_period) , (f"Out of range period value provided {short_period}, {long_period}, {fast_period}")) + + ema_long = EMA(closes, long_period) + ema_short = EMA(closes, short_period) + ema_long = ema_long[long_period-short_period : long_period] + cdef int length = len(ema_short) + cdef int i + print(ema_short) + print(ema_long) + for i in xrange(0, length): + print(i) + macd_line[i] = ema_short[i] - ema_long[i] + trend_line = EMA(macd_line, fast_period) + + print(trend_line[-1]) + print(macd_line[-1]) + return macd_line[-1], trend_line[-1] #TODO implement # cpdef float TMA(double[:] closes, int period): diff --git a/test/__pycache__/test_functions.cpython-36.pyc b/test/__pycache__/test_functions.cpython-36.pyc index e0b31c3..8fb5610 100644 Binary files a/test/__pycache__/test_functions.cpython-36.pyc and b/test/__pycache__/test_functions.cpython-36.pyc differ diff --git a/test/test_functions.py b/test/test_functions.py index cb9e827..8b58175 100644 --- a/test/test_functions.py +++ b/test/test_functions.py @@ -51,4 +51,9 @@ def testEMA(self): actual = np.array(testindicators.EMA(vals, 7)) print("actual", actual) print("expected", expected ) - self.assertEqual(expected[-1], actual[-1]) \ No newline at end of file + self.assertEqual(expected[-1], actual[-1]) + + + + def testMACD(self): + vals = np.array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6]) \ No newline at end of file