Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

V1 #7

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions src/indicators.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Binary file modified test/__pycache__/test_functions.cpython-36.pyc
Binary file not shown.
7 changes: 6 additions & 1 deletion test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
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])