From ee5be026864160369d0f41d992551f0bf8699819 Mon Sep 17 00:00:00 2001 From: epociask Date: Sat, 9 May 2020 04:12:37 -0500 Subject: [PATCH 1/2] added true range functions.. need to be unit tested --- src/indicators.pyx | 74 +++++++++++++++--- .../__pycache__/test_functions.cpython-36.pyc | Bin 2812 -> 3059 bytes test/test_functions.py | 6 +- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/indicators.pyx b/src/indicators.pyx index 7c70d88..64383fb 100644 --- a/src/indicators.pyx +++ b/src/indicators.pyx @@ -3,6 +3,24 @@ from libc.math cimport sqrt from libc.stdlib cimport malloc, free from cpython cimport array + +cpdef float TRUERANGE(dict candle1, dict candle2): + return abs(candle2['high'] - candle1['low']) + + +cpdef float ATR(list candles, int period): + cdef int size = len(candles) + cdef float sum_vals = 0.0 + cdef int i + for i in xrange(size-period+1, size): + sum_vals += TRUERANGE(candles[i-1], candles[i]) + + return sum_vals/period + + +cpdef float SMOOTHED_ATR(list candles, int period): + return ATR(candles, period-1) + (TRUERANGE(candles[2], candles[-1]) / period) + cpdef float SMA(double[:] closes, int period): """ Simple Moving Average function @@ -127,6 +145,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 +174,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 +212,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 e0b31c3d46d3017c0d87adf644a387c81c147b45..069430381a566046e56d588c83231de78e8c7bb5 100644 GIT binary patch delta 518 zcmYk2y-EW?6ou!`{$w+WpivWzASRONg4kFIHWGgdiGrp|;UXCcirMJwZlNHGu@o%k z1#ARMC17RgQv_eY+E&ne)m7&(A9pV2?w*+2J18;{${c@Vb%HGL1&w9H(x zdP)v%F?Xm7{G9hMFvdc*4#A7$5TMj8JuKm*j$PHM!47ZgW1@!?xuT==lMIWUC-+Y z&ug`1bU=05^Ynzs=kqwO#tB72iQp1u^oA%vMjwg6vXfK^?75=uME+h#U;lng%a2-z L$3MCCov3{Ro?CA4 delta 233 zcmew?{zsI{n3tDpam)I+i|iY@mNGK>Oy0og%M#3>DYTiBsey^lih+TlgdvNugeirw znQ8J&7DW-J6y_AxbmnFzMurpyph`BsB96(IS(+GyCWo=!;T8kxC=v$|5}R$=dKj5r zGEF|suF5F2`6K%bMl~g%%*+2kP^1oIX|ff`gOngo#q8?qSR^%hJ!dkrpC Date: Sun, 17 May 2020 09:46:13 -0500 Subject: [PATCH 2/2] removing true range functions... after running benchmark tests found plain python is more effecient --- src/indicators.pyx | 15 --------------- .../__pycache__/test_functions.cpython-36.pyc | Bin 3059 -> 3303 bytes test/test_functions.py | 1 + 3 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/indicators.pyx b/src/indicators.pyx index 64383fb..12a2b3a 100644 --- a/src/indicators.pyx +++ b/src/indicators.pyx @@ -4,23 +4,8 @@ from libc.stdlib cimport malloc, free from cpython cimport array -cpdef float TRUERANGE(dict candle1, dict candle2): - return abs(candle2['high'] - candle1['low']) -cpdef float ATR(list candles, int period): - cdef int size = len(candles) - cdef float sum_vals = 0.0 - cdef int i - for i in xrange(size-period+1, size): - sum_vals += TRUERANGE(candles[i-1], candles[i]) - - return sum_vals/period - - -cpdef float SMOOTHED_ATR(list candles, int period): - return ATR(candles, period-1) + (TRUERANGE(candles[2], candles[-1]) / period) - cpdef float SMA(double[:] closes, int period): """ Simple Moving Average function diff --git a/test/__pycache__/test_functions.cpython-36.pyc b/test/__pycache__/test_functions.cpython-36.pyc index 069430381a566046e56d588c83231de78e8c7bb5..8fb56108cc61bc6ede12e002d5327e793b0d58ac 100644 GIT binary patch delta 303 zcmew?{#=sFn3tDpdDXr+6V8oXOBoqsCU0O2t8jJKM@8uSs9L>!oAp+!bFmbSg TAqxjP2OAJ_a