Skip to content

Commit 6452708

Browse files
committed
Fix ta-lib calculations for cryptos
1 parent 64c80d9 commit 6452708

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

talib/upstream/src/ta_func/ta_utility.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ void TA_S_INT_stddev_using_precalc_ma( const float *inReal,
254254
* must be carefully choosen to work in the domain of the tested values.
255255
* Do a search on Google for a more generalize algo.
256256
*/
257-
#define TA_EPSILON (0.00000000000001)
257+
// #define TA_EPSILON (0.00000000000001)
258+
#define TA_EPSILON (0.000000000000000001)
258259
#define TA_REAL_EQ(x,v,ep) (((v-ep)<x)&&(x<(v+ep)))
259260
#define TA_IS_ZERO(v) (((-TA_EPSILON)<v)&&(v<TA_EPSILON))
260261
#define TA_IS_ZERO_OR_NEG(v) (v<TA_EPSILON)

tests/test_func.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import numpy as np
2-
from numpy.testing import (
3-
assert_array_equal,
4-
assert_array_almost_equal,
5-
assert_allclose
6-
)
72
import pytest
8-
93
import talib
4+
from numpy.testing import (assert_allclose, assert_array_almost_equal,
5+
assert_array_equal, assert_array_less)
106
from talib import func
117

128

@@ -159,11 +155,27 @@ def test_RSI():
159155
0.00000024, 0.00000024, 0.00000023,
160156
0.00000023, 0.00000023], dtype='float64')
161157
result = func.RSI(a, 10)
162-
assert_array_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,0,0,0,0,0,0,0,0,0,0])
158+
assert_array_almost_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,33.333333333333329,51.351351351351347,39.491916859122398,51.84807024709005,42.25953803191981,52.101824405061215,52.101824405061215,43.043664867691085,43.043664867691085,43.043664867691085])
159+
# assert_array_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,0,0,0,0,0,0,0,0,0,0])
163160
result = func.RSI(a * 100000, 10)
164161
assert_array_almost_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,33.333333333333329,51.351351351351347,39.491916859122398,51.84807024709005,42.25953803191981,52.101824405061215,52.101824405061215,43.043664867691085,43.043664867691085,43.043664867691085])
165162

166163

164+
165+
def test_BBANDS():
166+
# Bollinger bands for small numbers fix
167+
inputs = np.array([
168+
0.00000010,
169+
0.00000011,
170+
0.00000012,
171+
0.00000013,
172+
0.00000014
173+
])
174+
bollinger = func.BBANDS(inputs, matype=0, timeperiod=2)
175+
assert_array_less(bollinger[1], bollinger[0])
176+
assert_array_less(bollinger[2], bollinger[1])
177+
178+
167179
def test_MAVP():
168180
a = np.array([1,5,3,4,7,3,8,1,4,6], dtype=float)
169181
b = np.array([2,4,2,4,2,4,2,4,2,4], dtype=float)
@@ -181,8 +193,8 @@ def test_MAVP():
181193

182194

183195
def test_MAXINDEX():
184-
import talib as func
185196
import numpy as np
197+
import talib as func
186198
a = np.array([1., 2, 3, 4, 5, 6, 7, 8, 7, 7, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 15])
187199
b = func.MA(a, 10)
188200
c = func.MAXINDEX(b, 10)

0 commit comments

Comments
 (0)