Skip to content

Commit

Permalink
Fix ta-lib calculations for cryptos
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Sep 24, 2022
1 parent 64c80d9 commit 6452708
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion talib/upstream/src/ta_func/ta_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ void TA_S_INT_stddev_using_precalc_ma( const float *inReal,
* must be carefully choosen to work in the domain of the tested values.
* Do a search on Google for a more generalize algo.
*/
#define TA_EPSILON (0.00000000000001)
// #define TA_EPSILON (0.00000000000001)
#define TA_EPSILON (0.000000000000000001)
#define TA_REAL_EQ(x,v,ep) (((v-ep)<x)&&(x<(v+ep)))
#define TA_IS_ZERO(v) (((-TA_EPSILON)<v)&&(v<TA_EPSILON))
#define TA_IS_ZERO_OR_NEG(v) (v<TA_EPSILON)
Expand Down
28 changes: 20 additions & 8 deletions tests/test_func.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import numpy as np
from numpy.testing import (
assert_array_equal,
assert_array_almost_equal,
assert_allclose
)
import pytest

import talib
from numpy.testing import (assert_allclose, assert_array_almost_equal,
assert_array_equal, assert_array_less)
from talib import func


Expand Down Expand Up @@ -159,11 +155,27 @@ def test_RSI():
0.00000024, 0.00000024, 0.00000023,
0.00000023, 0.00000023], dtype='float64')
result = func.RSI(a, 10)
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])
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])
# 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])
result = func.RSI(a * 100000, 10)
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])



def test_BBANDS():
# Bollinger bands for small numbers fix
inputs = np.array([
0.00000010,
0.00000011,
0.00000012,
0.00000013,
0.00000014
])
bollinger = func.BBANDS(inputs, matype=0, timeperiod=2)
assert_array_less(bollinger[1], bollinger[0])
assert_array_less(bollinger[2], bollinger[1])


def test_MAVP():
a = np.array([1,5,3,4,7,3,8,1,4,6], dtype=float)
b = np.array([2,4,2,4,2,4,2,4,2,4], dtype=float)
Expand All @@ -181,8 +193,8 @@ def test_MAVP():


def test_MAXINDEX():
import talib as func
import numpy as np
import talib as func
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])
b = func.MA(a, 10)
c = func.MAXINDEX(b, 10)
Expand Down

0 comments on commit 6452708

Please sign in to comment.