Skip to content

Commit

Permalink
Merge pull request bukosabino#96 from bukosabino/feature/macd
Browse files Browse the repository at this point in the history
Feature/macd
  • Loading branch information
bukosabino authored Nov 15, 2019
2 parents 6886f3c + 6b4ba72 commit 46491bf
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ta/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ta.tests.pipeline_wrapper import TestTAFeaturesTransform
from ta.tests.trend import (TestADXIndicator, TestCCIIndicator,
TestVortexIndicator)
TestMACDIndicator, TestVortexIndicator)
from ta.tests.utils import TestGeneral
from ta.tests.volatility import TestAverageTrueRange
from ta.tests.volume import (TestAccDistIndexIndicator,
Expand Down
125 changes: 125 additions & 0 deletions ta/tests/data/cs-macd.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
Date,Close,short_ema,long_ema,MACD_line,MACD_signal,MACD_diff
11-Feb-09,30.2,30.2,30.2,,,
12-Feb-09,30.28,30.21230769,30.20592593,0.006381766382,,
13-Feb-09,30.45,30.24887574,30.22400549,0.02487025268,-0.1291444063,0.154014659
17-Feb-09,29.35,30.11058716,30.15926434,-0.04867717547,-0.1130509601,0.06437378466
18-Feb-09,29.35,29.99357375,30.09931883,-0.1057450787,-0.1115897839,0.005844705136
19-Feb-09,29.29,29.88533164,30.03936929,-0.1540376516,-0.1200793574,-0.03395829419
20-Feb-09,28.83,29.72297292,29.94978638,-0.2268134548,-0.1414261769,-0.08538727793
23-Feb-09,28.73,29.57020786,29.85943183,-0.2892239735,-0.1709857362,-0.1182382373
24-Feb-09,28.67,29.43171434,29.77132577,-0.3396114287,-0.2047108747,-0.134900554
25-Feb-09,28.85,29.34221983,29.70307942,-0.3608595895,-0.2359406177,-0.1249189718
26-Feb-09,28.64,29.23418601,29.62433279,-0.3901467857,-0.2667818513,-0.1233649344
27-Feb-09,27.68,28.99508047,29.48030814,-0.485227674,-0.3104710158,-0.1747566582
02-Mar-09,27.21,28.7204527,29.31213717,-0.5916844649,-0.3667137056,-0.2249707593
03-Mar-09,26.87,28.43576767,29.13123812,-0.6954704468,-0.4324650539,-0.2630053929
04-Mar-09,27.41,28.27795726,29.003739,-0.725781738,-0.4911283907,-0.2346533473
05-Mar-09,26.94,28.07211768,28.85086944,-0.7787517612,-0.5486530648,-0.2300986964
06-Mar-09,26.52,27.83333035,28.67821245,-0.8448821012,-0.6078988721,-0.2369832291
09-Mar-09,26.52,27.63127952,28.51834486,-0.8870653351,-0.6637321647,-0.2233331704
10-Mar-09,27.09,27.54800575,28.41254154,-0.8645357851,-0.7038928888,-0.1606428964
11-Mar-09,27.69,27.56985102,28.35901994,-0.7891689208,-0.7209480952,-0.06822082565
12-Mar-09,28.45,27.70525856,28.3657592,-0.6605006491,-0.708858606,0.04835795684
13-Mar-09,28.53,27.83214185,28.37792519,-0.5457833348,-0.6762435517,0.1304602169
16-Mar-09,28.67,27.96104311,28.39956036,-0.4385172529,-0.628698292,0.1901810391
17-Mar-09,29.01,28.12242109,28.44477811,-0.3223570205,-0.5674300377,0.2450730171
18-Mar-09,29.87,28.39127939,28.5503501,-0.1590707186,-0.4857581739,0.3266874553
19-Mar-09,29.8,28.60800563,28.64291676,-0.03491112915,-0.3955887649,0.3606776358
20-Mar-09,29.75,28.78369707,28.72492293,0.05877414612,-0.3047161827,0.3634903288
23-Mar-09,30.65,29.0708206,28.86752123,0.2032993715,-0.2031130719,0.4064124434
24-Mar-09,30.6,29.30607897,28.99585299,0.3102259798,-0.1004452615,0.4106712413
25-Mar-09,30.76,29.52975913,29.12653055,0.4032285819,0.0002895071518,0.4029390748
26-Mar-09,31.17,29.78210388,29.27789865,0.5042052237,0.1010726505,0.4031325732
27-Mar-09,30.89,29.95254944,29.39731357,0.5552358664,0.1919052936,0.3633305728
30-Mar-09,30.04,29.96600337,29.44491997,0.5210833971,0.2577409143,0.2633424828
31-Mar-09,30.66,30.07277208,29.5349259,0.5378461816,0.3137619678,0.2240842138
01-Apr-09,30.6,30.15388407,29.61382028,0.5400637913,0.3590223325,0.1810414588
02-Apr-09,31.97,30.43328652,29.78835211,0.644934411,0.4162047482,0.2287296628
03-Apr-09,32.1,30.68970398,29.95958529,0.7301186924,0.478987537,0.2511311553
06-Apr-09,32.03,30.89590337,30.11294934,0.7829540275,0.5397808351,0.2431731923
07-Apr-09,31.63,31.00884131,30.22532346,0.7835178481,0.5885282377,0.1949896104
08-Apr-09,31.85,31.13825034,30.34566987,0.7925804671,0.6293386836,0.1632417835
09-Apr-09,32.71,31.38005798,30.52080544,0.8592525422,0.6753214553,0.1839310869
13-Apr-09,32.76,31.59235675,30.6866717,0.9056850507,0.7213941744,0.1842908763
14-Apr-09,32.58,31.74430187,30.82691824,0.9173836252,0.7605920646,0.1567915606
15-Apr-09,32.13,31.80364004,30.92344282,0.8801972248,0.7845130966,0.09568412822
16-Apr-09,33.12,32.00615696,31.08615076,0.920006202,0.8116117177,0.1083944844
17-Apr-09,33.19,32.18828666,31.24199144,0.9462952161,0.8385484174,0.1077467987
20-Apr-09,32.52,32.23931948,31.33665874,0.9026607374,0.8513708814,0.05128985605
21-Apr-09,32.44,32.27019341,31.41838772,0.8518056817,0.8514578414,0.000347840223
22-Apr-09,33.22,32.4163175,31.55184048,0.864477012,0.8540616755,0.01041533642
23-Apr-09,32.83,32.47996096,31.64651897,0.8334419914,0.8499377387,-0.01649574731
24-Apr-09,33.62,32.65535158,31.79270275,0.862648833,0.8524799576,0.01016887539
27-Apr-09,33.75,32.82375903,31.93768773,0.8860713005,0.8591982261,0.02687307431
28-Apr-09,33.6,32.94318072,32.06082197,0.8823587459,0.8638303301,0.01852841582
29-Apr-09,34.08,33.11807599,32.21039071,0.9076852773,0.8726013195,0.03508395778
30-Apr-09,34.58,33.34298738,32.38591733,0.9570700495,0.8894950655,0.06757498394
01-May-09,34.22,33.4779124,32.5217753,0.9561370926,0.9028234709,0.05331362167
04-May-09,34.77,33.6766951,32.68831047,0.9883846382,0.9199357044,0.06844893379
05-May-09,34.74,33.84028047,32.84028747,0.9999930042,0.9359471644,0.06404583986
06-May-09,35.01,34.02023732,33.00100692,1.019230408,0.952603813,0.06662659472
07-May-09,34.94,34.16173927,33.14463603,1.017103241,0.9655036986,0.05159954209
08-May-09,34.42,34.20147169,33.23910744,0.9623642551,0.9648758099,-0.002511554751
11-May-09,34.4,34.23201451,33.32509948,0.9069150299,0.9532836539,-0.04636862395
12-May-09,34.16,34.22093535,33.38694396,0.8339913916,0.9294252014,-0.0954338098
13-May-09,33.34,34.08540684,33.38346663,0.7019402062,0.8839282024,-0.1819879962
14-May-09,33.39,33.97842117,33.38395059,0.5944705856,0.826036679,-0.2315660935
15-May-09,33.51,33.90635638,33.39328758,0.5130687964,0.7634431025,-0.2503743061
18-May-09,33.96,33.91460924,33.43526628,0.4793429639,0.7066230748,-0.2272801109
19-May-09,34.42,33.99236167,33.50820952,0.4841521496,0.6621288897,-0.1779767401
20-May-09,34.72,34.10430602,33.59797177,0.5063342507,0.6309699619,-0.1246357112
21-May-09,33.94,34.07902817,33.6233072,0.4557209766,0.5959201649,-0.1401991882
22-May-09,33.66,34.0145623,33.62602518,0.3885371183,0.5544435556,-0.1659064373
26-May-09,34.51,34.09078349,33.6915048,0.3992786866,0.5234105818,-0.1241318951
27-May-09,34.87,34.21066295,33.77880074,0.4318622095,0.5051009073,-0.07323869778
28-May-09,34.75,34.29363788,33.85074143,0.4428964545,0.4926600168,-0.04976356229
29-May-09,35.17,34.42846282,33.94846428,0.4799985386,0.4901277211,-0.01012918252
01-Jun-09,36.16,34.69485316,34.11228174,0.5825714132,0.5086164595,0.07395495366
02-Jun-09,36.45,34.96487575,34.28544606,0.6794296892,0.5427791055,0.1366505837
03-Jun-09,36.03,35.12874102,34.41467228,0.7140687408,0.5770370325,0.1370317083
04-Jun-09,36.45,35.33201163,34.56543729,0.7665743369,0.6149444934,0.1516298435
05-Jun-09,36.74,35.54862523,34.72651601,0.8221092133,0.6563774374,0.1657317759
08-Jun-09,36.61,35.71191365,34.86603334,0.8458803077,0.6942780114,0.1516022962
09-Jun-09,36.83,35.88392694,35.01151236,0.8724145804,0.7299053252,0.1425092552
10-Jun-09,36.84,36.0310151,35.14695589,0.8840592148,0.7607361032,0.1233231117
11-Jun-09,36.89,36.16316662,35.27607026,0.887096359,0.7860081543,0.1010882047
12-Jun-09,36.38,36.1965256,35.35784284,0.838682767,0.7965430769,0.04213969015
15-Jun-09,35.99,36.16475243,35.40466929,0.7600831406,0.7892510896,-0.029167949
16-Jun-09,35.86,36.11786744,35.43839749,0.6794699502,0.7672948617,-0.0878249115
17-Jun-09,35.88,36.08127245,35.47110879,0.6101636621,0.7358686218,-0.1257049597
18-Jun-09,35.73,36.02723054,35.49028592,0.5369446196,0.6960838214,-0.1591392018
19-Jun-09,36.07,36.03381045,35.5332277,0.5005827531,0.6569836077,-0.1564008546
22-Jun-09,35.6,35.96707038,35.5381738,0.428896587,0.6113662036,-0.1824696166
23-Jun-09,34.98,35.8152134,35.49682759,0.3183858119,0.5527701252,-0.2343843133
24-Jun-09,35.58,35.77902673,35.50298851,0.2760382154,0.4974237433,-0.2213855278
25-Jun-09,36.07,35.82379184,35.54498936,0.2788024835,0.4536994913,-0.1748970078
26-Jun-09,36.21,35.88320848,35.59424941,0.2889590755,0.4207514082,-0.1317923327
29-Jun-09,36.46,35.97194564,35.65837908,0.3135665584,0.3993144382,-0.08574787981
30-Jun-09,36.44,36.043954,35.71627693,0.3276770757,0.3849869657,-0.05730989004
01-Jul-09,36.54,36.12026877,35.77729345,0.3429753205,0.3765846367,-0.03360931614
02-Jul-09,35.81,36.07253511,35.77971616,0.292818956,0.3598315005,-0.06701254452
06-Jul-09,35.26,35.94752971,35.74121867,0.206311047,0.3291274098,-0.1228163628
07-Jul-09,35.21,35.8340636,35.70186913,0.1321944682,0.2897408215,-0.1575463533
08-Jul-09,34.59,35.6426692,35.61950846,0.02316074437,0.2364248061,-0.2132640617
09-Jul-09,34.73,35.50225856,35.55361894,-0.05136038674,0.1788677675,-0.2302281542
10-Jul-09,34.86,35.40344955,35.50223976,-0.0987902144,0.1233361711,-0.2221263855
13-Jul-09,35.31,35.38907269,35.48799978,-0.09892708548,0.0788835198,-0.1778106053
14-Jul-09,35.5,35.40613843,35.48888868,-0.08275025131,0.04655676558,-0.1293070169
15-Jul-09,36.63,35.59442483,35.57341545,0.02100937947,0.04144728836,-0.02043790888
16-Jul-09,37.14,35.83220562,35.68945875,0.142746875,0.06170720569,0.08103966935
17-Jul-09,37.27,36.05340476,35.80653588,0.2468688801,0.09873954058,0.1481293395
20-Jul-09,37.7,36.3067271,35.94679248,0.3599346234,0.1509785571,0.2089560662
21-Jul-09,37.87,36.54723063,36.0892523,0.4579783296,0.2123785116,0.245599818
22-Jul-09,38.38,36.82919515,36.25893731,0.5702578336,0.283954376,0.2863034576
23-Jul-09,39.17,37.18931897,36.47457158,0.7147473842,0.3701129777,0.3446344066
24-Jul-09,39.05,37.47557759,36.66534406,0.8102335292,0.458137088,0.3520964412
27-Jul-09,39.09,37.72395027,36.8449482,0.8790020641,0.5423100832,0.3366919809
28-Jul-09,39.27,37.96180407,37.02458167,0.9372224027,0.6212925471,0.3159298556
29-Jul-09,39.11,38.1384496,37.1790571,0.9593924981,0.6889125373,0.2704799608
30-Jul-09,39.86,38.40330351,37.37764546,1.025658043,0.7562616384,0.2693964045
31-Jul-09,39.53,38.57664143,37.53707913,1.039562296,0.8129217699,0.2266405257
03-Aug-09,39.74,38.75561967,37.70025846,1.055361214,0.8614096586,0.1939515551
04-Aug-09,39.87,38.9270628,37.86098005,1.066082746,0.902344276,0.1637384695
05-Aug-09,39.82,39.06443775,38.00609264,1.058345111,0.933544443,0.1248006679
06-Aug-09,39.61,39.14837041,38.12490059,1.023469812,0.9515295168,0.07194029512
07-Aug-09,39.75,39.24092881,38.24527833,0.9956504772,0.9603537089,0.03529676833
44 changes: 41 additions & 3 deletions ta/tests/trend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pandas as pd

from ta.tests.utils import TestIndicator
from ta.trend import (ADXIndicator, CCIIndicator, VortexIndicator, adx,
adx_neg, adx_pos, cci, vortex_indicator_neg,
vortex_indicator_pos)
from ta.trend import (MACD, ADXIndicator, CCIIndicator, VortexIndicator, adx,
adx_neg, adx_pos, cci, macd, macd_diff, macd_signal,
vortex_indicator_neg, vortex_indicator_pos)


class TestADXIndicator(TestIndicator):
Expand Down Expand Up @@ -49,6 +49,44 @@ def test_adx_neg2(self):
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)


class TestMACDIndicator(TestIndicator):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:moving_average_convergence_divergence_macd
"""

_filename = 'ta/tests/data/cs-macd.csv'

def test_macd(self):
target = 'MACD_line'
result = macd(close=self._df['Close'], n_slow=12, n_fast=26, fillna=False)
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_macd2(self):
target = 'MACD_line'
result = MACD(close=self._df['Close'], n_slow=12, n_fast=26, n_sign=9, fillna=False).macd()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_macd_signal(self):
target = 'MACD_signal'
result = macd_signal(close=self._df['Close'], n_slow=12, n_fast=26, n_sign=9, fillna=False)
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_macd_signal2(self):
target = 'MACD_signal'
result = MACD(close=self._df['Close'], n_slow=12, n_fast=26, n_sign=9, fillna=False).macd_signal()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_macd_diff(self):
target = 'MACD_diff'
result = macd_diff(close=self._df['Close'], n_slow=12, n_fast=26, n_sign=9, fillna=False)
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_macd_diff2(self):
target = 'MACD_diff'
result = MACD(close=self._df['Close'], n_slow=12, n_fast=26, n_sign=9, fillna=False).macd_diff()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)


class TestCCIIndicator(TestIndicator):
"""
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:commodity_channel_index_cci
Expand Down
7 changes: 4 additions & 3 deletions ta/trend.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self,
def _run(self):
self._emafast = ema(self._close, self._n_fast, self._fillna)
self._emaslow = ema(self._close, self._n_slow, self._fillna)
self._macd = self._emafast - self._emaslow
self._macd = self._emaslow - self._emafast
self._macd_signal = ema(self._macd, self._n_sign, self._fillna)
self._macd_diff = self._macd - self._macd_signal

Expand All @@ -120,8 +120,9 @@ def macd_signal(self) -> pd.Series:
Returns:
pandas.Series: New feature generated.
"""
macd_diff = self.check_fillna(self._macd_signal, value=0)
return pd.Series(macd_diff, name=f'MACD_sign_{self._n_fast}_{self._n_slow}')

macd_signal = self.check_fillna(self._macd_signal, value=0)
return pd.Series(macd_signal, name=f'MACD_sign_{self._n_fast}_{self._n_slow}')

def macd_diff(self) -> pd.Series:
"""MACD Histogram
Expand Down

0 comments on commit 46491bf

Please sign in to comment.