Skip to content

Commit

Permalink
Merge pull request #110 from bukosabino/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
bukosabino committed Dec 11, 2019
2 parents 96b497f + 37a5230 commit 0bce3f5
Show file tree
Hide file tree
Showing 27 changed files with 1,196 additions and 52,570 deletions.
52,058 changes: 0 additions & 52,058 deletions data/datas.csv

This file was deleted.

458 changes: 78 additions & 380 deletions examples_to_use/Pipeline.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples_to_use/all_features_example.py
Expand Up @@ -5,7 +5,7 @@
import ta

# Load data
df = pd.read_csv('../data/datas.csv', sep=',')
df = pd.read_csv('../ta/tests/data/datas.csv', sep=',')

# Clean nan values
df = ta.utils.dropna(df)
Expand Down
2 changes: 1 addition & 1 deletion examples_to_use/bollinger_band_features_example.py
Expand Up @@ -4,7 +4,7 @@
import ta

# Load data
df = pd.read_csv('../data/datas.csv', sep=',')
df = pd.read_csv('../ta/tests/data/datas.csv', sep=',')

# Clean nan values
df = ta.utils.dropna(df)
Expand Down
12 changes: 12 additions & 0 deletions examples_to_use/roc.py
@@ -0,0 +1,12 @@
"""This is a example adding volume features.
"""
import pandas as pd
import ta

# Load data
df = pd.read_csv('../ta/tests/data/datas.csv', sep=',')

# Clean nan values
df = ta.utils.dropna(df)

ta.momentum.roc(close=df['Close'])
22 changes: 11 additions & 11 deletions examples_to_use/visualize_features.ipynb
Expand Up @@ -24,18 +24,18 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Load data\n",
"df = pd.read_csv('../data/datas.csv', sep=',')\n",
"df = pd.read_csv('../ta/tests/data/datas.csv', sep=',')\n",
"df = ta.utils.dropna(df)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -44,7 +44,7 @@
"(46306, 8)"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -165,7 +165,7 @@
"18 5.572793 "
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -176,16 +176,16 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/vant/projects/python/ta/env-ta/lib/python3.7/site-packages/ta/trend.py:468: RuntimeWarning: invalid value encountered in double_scalars\n",
"/home/vant/projects/python/ta/env-ta/lib/python3.7/site-packages/ta/trend.py:543: RuntimeWarning: invalid value encountered in double_scalars\n",
" dip[i] = 100 * (self._dip[i]/self._trs[i])\n",
"/home/vant/projects/python/ta/env-ta/lib/python3.7/site-packages/ta/trend.py:472: RuntimeWarning: invalid value encountered in double_scalars\n",
"/home/vant/projects/python/ta/env-ta/lib/python3.7/site-packages/ta/trend.py:547: RuntimeWarning: invalid value encountered in double_scalars\n",
" din[i] = 100 * (self._din[i]/self._trs[i])\n"
]
}
Expand All @@ -197,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -206,7 +206,7 @@
"(46306, 69)"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
2 changes: 1 addition & 1 deletion examples_to_use/volume_features_example.py
Expand Up @@ -4,7 +4,7 @@
import ta

# Load data
df = pd.read_csv('../data/datas.csv', sep=',')
df = pd.read_csv('../ta/tests/data/datas.csv', sep=',')

# Clean nan values
df = ta.utils.dropna(df)
Expand Down
49 changes: 25 additions & 24 deletions ta/momentum.py
Expand Up @@ -34,14 +34,12 @@ def __init__(self, close: pd.Series, n: int = 14, fillna: bool = False):

def _run(self):
diff = self._close.diff(1)
which_dn = diff < 0

up, dn = diff, diff*0
up[which_dn], dn[which_dn] = 0, -up[which_dn]

emaup = up.ewm(com=self._n-1, min_periods=0).mean()
emadn = dn.ewm(com=self._n-1, min_periods=0).mean()
self._rsi = 100 * emaup / (emaup + emadn)
up = diff.where(diff > 0, 0.0)
dn = -diff.where(diff < 0, 0.0)
emaup = up.ewm(alpha=1/self._n, min_periods=0, adjust=False).mean()
emadn = dn.ewm(alpha=1/self._n, min_periods=0, adjust=False).mean()
rs = emaup / emadn
self._rsi = pd.Series(np.where(emadn == 0, 100, 100-(100/(1+rs))), index=self._close.index)

def rsi(self) -> pd.Series:
"""Relative Strength Index (RSI)
Expand Down Expand Up @@ -121,7 +119,7 @@ class TSIIndicator(IndicatorMixin):
Shows both trend direction and overbought/oversold conditions.
https://en.wikipedia.org/wiki/True_strength_index
https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index
Args:
close(pandas.Series): dataset 'Close' column.
Expand All @@ -138,9 +136,11 @@ def __init__(self, close: pd.Series, r: int = 25, s: int = 13, fillna: bool = Fa
self._run()

def _run(self):
m = self._close - self._close.shift(1, fill_value=self._close.mean())
m1 = m.ewm(self._r).mean().ewm(self._s).mean()
m2 = abs(m).ewm(self._r).mean().ewm(self._s).mean()
m = self._close - self._close.shift(1)
m1 = m.ewm(span=self._r, min_periods=0, adjust=False).mean().ewm(
span=self._s, min_periods=0, adjust=False).mean()
m2 = abs(m).ewm(span=self._r, min_periods=0, adjust=False).mean().ewm(
span=self._s, min_periods=0, adjust=False).mean()
self._tsi = m1 / m2
self._tsi *= 100

Expand All @@ -154,7 +154,7 @@ def tsi(self) -> pd.Series:
return pd.Series(tsi, name='tsi')


class UltimateOscillatorIndicator(IndicatorMixin):
class UltimateOscillator(IndicatorMixin):
"""Ultimate Oscillator
Larry Williams' (1976) signal, a momentum oscillator designed to capture
Expand Down Expand Up @@ -207,13 +207,13 @@ def __init__(self,
self._run()

def _run(self):
min_l_or_pc = self._close.shift(1, fill_value=self._close.mean()).combine(self._low, min)
max_h_or_pc = self._close.shift(1, fill_value=self._close.mean()).combine(self._high, max)
min_l_or_pc = self._close.shift(1).combine(self._low, min)
max_h_or_pc = self._close.shift(1).combine(self._high, max)
bp = self._close - min_l_or_pc
tr = max_h_or_pc - min_l_or_pc
avg_s = bp.rolling(self._s, min_periods=0).sum() / tr.rolling(self._s, min_periods=0).sum()
avg_m = bp.rolling(self._m, min_periods=0).sum() / tr.rolling(self._m, min_periods=0).sum()
avg_l = bp.rolling(self._len, min_periods=0).sum() / tr.rolling(self._len, min_periods=0).sum()
avg_s = bp.rolling(self._s, min_periods=self._s).sum() / tr.rolling(self._s, min_periods=self._s).sum()
avg_m = bp.rolling(self._m, min_periods=self._m).sum() / tr.rolling(self._m, min_periods=self._m).sum()
avg_l = bp.rolling(self._len, min_periods=self._len).sum() / tr.rolling(self._len, min_periods=self._len).sum()
self._uo = (100.0 * ((self._ws * avg_s) + (self._wm * avg_m) + (self._wl * avg_l))
/ (self._ws + self._wm + self._wl))

Expand All @@ -227,15 +227,15 @@ def uo(self) -> pd.Series:
return pd.Series(uo, name='uo')


class StochIndicator(IndicatorMixin):
class StochasticOscillator(IndicatorMixin):
"""Stochastic Oscillator
Developed in the late 1950s by George Lane. The stochastic
oscillator presents the location of the closing price of a
stock in relation to the high and low range of the price
of a stock over a period of time, typically a 14-day period.
https://www.investopedia.com/terms/s/stochasticoscillator.asp
https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full
Args:
close(pandas.Series): dataset 'Close' column.
Expand Down Expand Up @@ -472,7 +472,8 @@ class WilliamsRIndicator(IndicatorMixin):
Highest High = highest high for the look-back period
%R is multiplied by -100 correct the inversion and move the decimal.
From: https://www.investopedia.com/terms/w/williamsr.asp
https://school.stockcharts.com/doku.php?id=technical_indicators:williams_r
The Williams %R oscillates from 0 to -100. When the indicator produces
readings from 0 to -20, this indicates overbought market conditions. When
readings are -80 to -100, it indicates oversold market conditions.
Expand Down Expand Up @@ -607,7 +608,7 @@ def uo(high, low, close, s=7, m=14, len=28, ws=4.0, wm=2.0, wl=1.0, fillna=False
pandas.Series: New feature generated.
"""
return UltimateOscillatorIndicator(
return UltimateOscillator(
high=high, low=low, close=close, s=7, m=14, len=28, ws=4.0, wm=2.0, wl=1.0, fillna=fillna).uo()


Expand All @@ -632,7 +633,7 @@ def stoch(high, low, close, n=14, fillna=False):
pandas.Series: New feature generated.
"""

return StochIndicator(high=high, low=low, close=close, n=n, d_n=3, fillna=fillna).stoch()
return StochasticOscillator(high=high, low=low, close=close, n=n, d_n=3, fillna=fillna).stoch()


def stoch_signal(high, low, close, n=14, d_n=3, fillna=False):
Expand All @@ -653,7 +654,7 @@ def stoch_signal(high, low, close, n=14, d_n=3, fillna=False):
Returns:
pandas.Series: New feature generated.
"""
return StochIndicator(high=high, low=low, close=close, n=n, d_n=d_n, fillna=fillna).stoch_signal()
return StochasticOscillator(high=high, low=low, close=close, n=n, d_n=d_n, fillna=fillna).stoch_signal()


def wr(high, low, close, lbp=14, fillna=False):
Expand Down
1 change: 0 additions & 1 deletion ta/pipeline_wrapper.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from sklearn.base import BaseEstimator, TransformerMixin

import ta
Expand Down
7 changes: 5 additions & 2 deletions ta/tests/__init__.py
@@ -1,9 +1,12 @@
# from ta.tests.pipeline_wrapper import TestTAFeaturesTransform
from ta.tests.momentum import (TestKAMAIndicator, TestMFIIndicator,
TestRateOfChangeIndicator, TestRSIIndicator,
TestStochasticOscillator, TestTSIIndicator,
TestUltimateOscillator, TestWilliamsRIndicator)
from ta.tests.trend import (TestADXIndicator, TestCCIIndicator,
TestMACDIndicator, TestPSARIndicator,
TestVortexIndicator)
from ta.tests.utils import TestGeneral
from ta.tests.volatility import TestAverageTrueRange
from ta.tests.volatility import TestAverageTrueRange, TestBollingerBands
from ta.tests.volume import (TestAccDistIndexIndicator,
TestEaseOfMovementIndicator,
TestForceIndexIndicator,
Expand Down
43 changes: 43 additions & 0 deletions ta/tests/data/cs-bbands.csv
@@ -0,0 +1,43 @@
Date,Close,MiddleBand,20-day Standard Deviation,HighBand,LowBand,WidthBand,CrossUp,CrossDown
1-May-09,86.155700,,,,,,,
4-May-09,89.086700,,,,,,,
5-May-09,88.782900,,,,,,,
6-May-09,90.322800,,,,,,,
7-May-09,89.067100,,,,,,,
8-May-09,91.145300,,,,,,,
11-May-09,89.439700,,,,,,,
12-May-09,89.175000,,,,,,,
13-May-09,86.930200,,,,,,,
14-May-09,87.675200,,,,,,,
15-May-09,86.959600,,,,,,,
18-May-09,89.429900,,,,,,,
19-May-09,89.322100,,,,,,,
20-May-09,88.724100,,,,,,,
21-May-09,87.449700,,,,,,,
22-May-09,87.263400,,,,,,,
26-May-09,89.498500,,,,,,,
27-May-09,87.900600,,,,,,,
28-May-09,89.126000,,,,,,,
29-May-09,90.704300,88.707940,1.291961,91.291862,86.124018,5.167845,0,0
1-Jun-09,92.900100,89.045160,1.452054,91.949268,86.141052,5.808215,1,0
2-Jun-09,92.978400,89.239745,1.686425,92.612595,85.866895,6.745700,1,0
3-Jun-09,91.802100,89.390705,1.771747,92.934200,85.847210,7.086989,0,0
4-Jun-09,92.664700,89.507800,1.902075,93.311950,85.703650,7.608300,0,0
5-Jun-09,92.684300,89.688660,2.019895,93.728450,85.648870,8.079580,0,0
8-Jun-09,92.302100,89.746500,2.076546,93.899592,85.593408,8.306184,0,0
9-Jun-09,92.772500,89.913140,2.176557,94.266255,85.560025,8.706230,0,0
10-Jun-09,92.537300,90.081255,2.241921,94.565096,85.597414,8.967682,0,0
11-Jun-09,92.949000,90.382195,2.202359,94.786912,85.977478,8.809435,0,0
12-Jun-09,93.203900,90.658630,2.192185,95.043001,86.274259,8.768742,0,0
15-Jun-09,91.066900,90.863995,2.021805,94.907605,86.820385,8.087220,0,0
16-Jun-09,89.831800,90.884090,2.009411,94.902912,86.865268,8.037643,0,0
17-Jun-09,89.743500,90.905160,1.995080,94.895320,86.915000,7.980320,0,0
18-Jun-09,90.399400,90.988925,1.936044,94.861013,87.116837,7.744176,0,0
19-Jun-09,90.738700,91.153375,1.760127,94.673629,87.633121,7.040508,0,0
22-Jun-09,88.017700,91.191090,1.682751,94.556593,87.825587,6.731006,0,0
23-Jun-09,88.086700,91.120500,1.779126,94.678752,87.562248,7.116503,0,0
24-Jun-09,86.843900,91.067665,1.886418,94.840502,87.294828,7.545674,0,1
25-Jun-09,90.778100,91.150270,1.835059,94.820387,87.480153,7.340234,0,0
26-Jun-09,90.541600,91.142135,1.837377,94.816889,87.467381,7.349507,0,0
29-Jun-09,91.389400,91.066600,1.794097,94.654793,87.478407,7.176387,0,0
30-Jun-09,90.650000,90.950180,1.741022,94.432224,87.468136,6.964089,0,0
50 changes: 50 additions & 0 deletions ta/tests/data/cs-kama.csv
@@ -0,0 +1,50 @@
Date,Close,ABS (Close - Close 10 periods ago),ABS (Close - Prior Close),ER,2/(fastest + 1),2/(slowest + 1),SC,KAMA
28/05/2015,110.460000,,,,,,,
29/05/2015,109.800000,,0.660000,,,,,
01/06/2015,110.170000,,0.370000,,,,,
02/06/2015,109.820000,,0.350000,,,,,
03/06/2015,110.150000,,0.330000,,,,,
04/06/2015,109.310000,,0.840000,,,,,
05/06/2015,109.050000,,0.260000,,,,,
08/06/2015,107.940000,,1.110000,,,,,
09/06/2015,107.760000,,0.180000,,,,,
10/06/2015,109.240000,,1.480000,,,,,109.240000
11/06/2015,109.400000,1.060000,0.160000,0.184669,0.064516,0.666667,0.030876,109.244940
12/06/2015,108.500000,1.300000,0.900000,0.217391,0.064516,0.666667,0.038188,109.216492
15/06/2015,107.960000,2.210000,0.540000,0.359350,0.064516,0.666667,0.078904,109.117350
16/06/2015,108.550000,1.270000,0.590000,0.198748,0.064516,0.666667,0.033927,109.098101
17/06/2015,108.850000,1.300000,0.300000,0.204403,0.064516,0.666667,0.035193,109.089370
18/06/2015,110.440000,1.130000,1.590000,0.158931,0.064516,0.666667,0.025669,109.124040
19/06/2015,109.890000,0.840000,0.550000,0.113514,0.064516,0.666667,0.017654,109.137562
22/06/2015,110.700000,2.760000,0.810000,0.388732,0.064516,0.666667,0.089157,109.276864
23/06/2015,110.790000,3.030000,0.090000,0.432240,0.064516,0.666667,0.105488,109.436482
24/06/2015,110.220000,0.980000,0.570000,0.160656,0.064516,0.666667,0.026003,109.456856
25/06/2015,110.000000,0.600000,0.220000,0.097403,0.064516,0.666667,0.015170,109.465096
26/06/2015,109.270000,0.770000,0.730000,0.128548,0.064516,0.666667,0.020142,109.461166
29/06/2015,106.690000,1.270000,2.580000,0.158157,0.064516,0.666667,0.025520,109.390445
30/06/2015,107.070000,1.480000,0.380000,0.189258,0.064516,0.666667,0.031854,109.316529
01/07/2015,107.920000,0.930000,0.850000,0.111111,0.064516,0.666667,0.017272,109.292409
02/07/2015,107.950000,2.490000,0.030000,0.365639,0.064516,0.666667,0.081046,109.183612
06/07/2015,107.700000,2.190000,0.250000,0.336406,0.064516,0.666667,0.071333,109.077781
07/07/2015,107.970000,2.730000,0.270000,0.457286,0.064516,0.666667,0.115513,108.949818
08/07/2015,106.090000,4.700000,1.880000,0.605670,0.064516,0.666667,0.184230,108.422953
09/07/2015,106.030000,4.190000,0.060000,0.577931,0.064516,0.666667,0.170171,108.015742
10/07/2015,107.650000,2.350000,1.620000,0.271676,0.064516,0.666667,0.052032,107.996712
13/07/2015,109.540000,0.270000,1.890000,0.027523,0.064516,0.666667,0.006575,108.006859
14/07/2015,110.260000,3.570000,0.720000,0.449057,0.064516,0.666667,0.112169,108.259591
15/07/2015,110.380000,3.310000,0.120000,0.430429,0.064516,0.666667,0.104781,108.481770
16/07/2015,111.940000,4.020000,1.560000,0.478571,0.064516,0.666667,0.124389,108.911936
17/07/2015,113.590000,5.640000,1.650000,0.562874,0.064516,0.666667,0.162773,109.673398
20/07/2015,113.980000,6.280000,0.390000,0.618110,0.064516,0.666667,0.190717,110.494740
21/07/2015,113.910000,5.940000,0.070000,0.596386,0.064516,0.666667,0.179462,111.107650
22/07/2015,112.620000,6.530000,1.290000,0.696905,0.064516,0.666667,0.234409,111.462159
23/07/2015,112.200000,6.170000,0.420000,0.634121,0.064516,0.666667,0.199231,111.609159
24/07/2015,111.100000,3.450000,1.100000,0.374593,0.064516,0.666667,0.084145,111.566316
27/07/2015,110.180000,0.640000,0.920000,0.077670,0.064516,0.666667,0.012384,111.549147
28/07/2015,111.130000,0.870000,0.950000,0.102715,0.064516,0.666667,0.015968,111.542454
29/07/2015,111.550000,1.170000,0.420000,0.133409,0.064516,0.666667,0.020981,111.542613
30/07/2015,112.080000,0.140000,0.530000,0.018088,0.064516,0.666667,0.005686,111.545668
31/07/2015,111.950000,1.640000,0.130000,0.263666,0.064516,0.666667,0.049855,111.565826
03/08/2015,111.600000,2.380000,0.350000,0.385113,0.064516,0.666667,0.087860,111.568829
04/08/2015,111.390000,2.520000,0.210000,0.398734,0.064516,0.666667,0.092790,111.552235
05/08/2015,112.250000,0.370000,0.860000,0.062818,0.064516,0.666667,0.010474,111.559544

0 comments on commit 0bce3f5

Please sign in to comment.