Skip to content

Commit 85601ef

Browse files
havocespGHPortable
havocesp
authored andcommitted
Some code refactor
1 parent 7112176 commit 85601ef

14 files changed

+2596
-2626
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Collection of Technical Analysis Functions
33

44
* __Author:__ Daniel J. Umpierrez
5-
* __Version:__ 0.0.4
5+
* __Version:__ 0.0.6
66
* __License:__ MIT
77

88
## Description

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pandas
12
pandas=>23.0
23
numpy=>1.14.3

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='techa',
6-
version='v0.0.5',
6+
version='v0.0.6',
77
requires=['pandas'],
88
packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
99
package_dir={'techa': 'techa'},

techa/__init__.py

+44-30
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,81 @@
55
import os
66
import sys
77

8+
import cycle
9+
import experimental
10+
import momentum
11+
import overlap
12+
import pattern
13+
import prices
14+
import statistic
15+
import volatility
816

917
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
1018
sys.path.append(BASE_DIR)
11-
from core import TaLib
12-
1319

1420
__author__ = 'Daniel J. Umpierrez'
15-
__version__ = '0.0.4'
21+
__version__ = '0.0.5'
22+
23+
__all__ = (*cycle.__all__, *momentum.__all__, *overlap.__all__, *volatility.__all__, *pattern.__all__,
24+
*statistic.__all__, *experimental.__all__, *prices.__all__, 'ASI', 'SI')
1625

17-
__all__ = ['TaLib']
1826

19-
def ASI(df, L):
27+
def ASI(data, l):
2028
"""
2129
Accumulation Swing Index (J. Welles Wilder)
2230
source: book: New Concepts in Technical Trading Systems
2331
"""
2432
asi_list = []
25-
si_list = SI(df, L)
33+
si_list = SI(data, l)
2634
i = 0
27-
while i < len(df['close']):
35+
asi = .0
36+
while i < len(data['close']):
2837
if i < 1:
2938
asi = float('NaN')
3039
asi_list.append(asi)
3140
asi = .0
3241
else:
33-
asi = asi + si_list[i]
42+
asi += si_list[i]
3443
asi_list.append(asi)
3544
i += 1
3645
return asi_list
3746

38-
def SI(df, L):
47+
48+
def SI(data, l):
3949
"""
4050
Swing Index (J. Welles Wilder)
41-
source: book: New Concepts in Technical Trading Systems
51+
From "New Concepts in Technical Trading Systems"
52+
53+
:param pd.DataFrame data:
54+
:param l:
55+
:return:
4256
"""
4357
si_list = []
4458
i = 0
45-
while i < len(df['close']):
59+
while i < len(data['close']):
4660
if i < 1:
4761
si = float('NaN')
4862
else:
49-
N = (df['close'][i] - df['close'][i - 1]) + (.5 * (df['close'][i] - df['open'][i])) + (
50-
.25 * (df['close'][i - 1] - df['open'][i - 1]))
51-
R1 = df['high'][i] - df['close'][i - 1]
52-
R2 = df['low'][i] - df['close'][i - 1]
53-
R3 = df['high'][i] - df['low'][i]
54-
if R1 > R2 and R1 > R3:
55-
R = (df['high'][i] - df['close'][i - 1]) - (.5 * (df['low'][i] - df['close'][i - 1])) + (
56-
.25 * (df['close'][i - 1] - df['open'][i - 1]))
57-
if R2 > R1 and R2 > R3:
58-
R = (df['low'][i] - df['close'][i - 1]) - (.5 * (df['high'][i] - df['close'][i - 1])) + (
59-
.25 * (df['close'][i - 1] - df['open'][i - 1]))
60-
if R3 > R1 and R3 > R2:
61-
R = (df['high'][i] - df['low'][i]) + (.25 * (df['close'][i - 1] - df['open'][i - 1]))
62-
K1 = df['high'][i] - df['close'][i - 1]
63-
K2 = df['low'][i] - df['close'][i - 1]
64-
if K1 > K2:
65-
K = K1
63+
n = (data['close'][i] - data['close'][i - 1]) + (.5 * (data['close'][i] - data['open'][i])) + (
64+
.25 * (data['close'][i - 1] - data['open'][i - 1]))
65+
r1 = data['high'][i] - data['close'][i - 1]
66+
r2 = data['low'][i] - data['close'][i - 1]
67+
r3 = data['high'][i] - data['low'][i]
68+
if r1 > r2 and r1 > r3:
69+
r = (data['high'][i] - data['close'][i - 1]) - (.5 * (data['low'][i] - data['close'][i - 1])) + (
70+
.25 * (data['close'][i - 1] - data['open'][i - 1]))
71+
if r2 > r1 and r2 > r3:
72+
r = (data['low'][i] - data['close'][i - 1]) - (.5 * (data['high'][i] - data['close'][i - 1])) + (
73+
.25 * (data['close'][i - 1] - data['open'][i - 1]))
74+
if r3 > r1 and r3 > r2:
75+
r = (data['high'][i] - data['low'][i]) + (.25 * (data['close'][i - 1] - data['open'][i - 1]))
76+
k1 = data['high'][i] - data['close'][i - 1]
77+
k2 = data['low'][i] - data['close'][i - 1]
78+
if k1 > k2:
79+
k = k1
6680
else:
67-
K = K2
68-
si = 50 * (N / R) * (K / L)
81+
k = k2
82+
si = 50 * (n / r) * (k / l)
6983
si_list.append(si)
7084
i += 1
7185
return si_list

techa/core.py

+57-52
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
# -*- coding:utf-8 -*-
2-
from collections import OrderedDict
2+
import talib
33

4-
from cycle import Cycle
5-
from momentum import Momentum
6-
from overlap import Overlap
7-
from pattern import Pattern
8-
from statistic import Statistic
9-
from volatility import Volatility
10-
from volume import Volume
4+
import cycle
5+
import experimental
6+
import momentum
7+
import overlap
8+
import pattern
9+
import prices
10+
import statistic
11+
import volatility
12+
import volume
13+
14+
# from cycle import *
15+
# from momentum import *
16+
# from overlap import *
17+
# from pattern import *
18+
# from statistic import *
19+
# from volatility import *
20+
# from experimental import *
21+
# from prices import *
22+
# from volume import *
23+
24+
_function_list = [f for f in
25+
(*cycle.__all__, *momentum.__all__, *overlap.__all__, *volatility.__all__, *pattern.__all__,
26+
*statistic.__all__, *experimental.__all__, *prices.__all__) if f[0].isupper()]
27+
__all__ = ['TaLib']
1128

1229

1330
# noinspection SpellCheckingInspection
@@ -17,23 +34,21 @@ class TaLib:
1734
"""
1835

1936
_groups_ref = {
20-
'Cycle Indicators': 'Cyl',
21-
'Momentum Indicators': 'Mom',
22-
'Overlap studies': 'Ovlap',
23-
'Patter Recognition': 'Pattr',
24-
'Statistic Functions': 'Stat',
25-
'Volume Indicators': 'Vol',
26-
'Volatility Indicators': 'Volat'
37+
'cycle': cycle.__all__,
38+
'momentum': momentum.__all__,
39+
'overlap': overlap.__all__,
40+
'patter': pattern.__all__,
41+
'statistic': statistic.__all__,
42+
'volume': volatility.__all__,
43+
'volatility': experimental.__all__,
44+
'price': prices.__all__,
45+
'experimental': volume.__all__
2746
}
2847

29-
Volat = Volatility
30-
Olap = Overlap
31-
Mom = Momentum
32-
Cycl = Cycle
33-
Vol = Volume
34-
Pattr = Pattern
35-
Stat = Statistic
36-
48+
@classmethod
49+
def calculate_indicator(cls, indicator, *args, **kwargs):
50+
fn = globals().get(indicator)
51+
return fn(*args, **kwargs)
3752

3853
@classmethod
3954
def get_groups(cls):
@@ -42,11 +57,17 @@ def get_groups(cls):
4257
4358
:return: groups names
4459
"""
45-
groups = OrderedDict().fromkeys(sorted([grp for grp in cls.__dict__ if grp[0].isupper()]))
46-
for grp in groups:
47-
groups.update({grp: [fn for fn in cls.__dict__[grp].__dict__ if fn[0].isupper()]})
48-
return groups
4960

61+
return sorted([*cls._groups_ref])
62+
63+
@classmethod
64+
def get_talib_groups(cls):
65+
"""
66+
Just return groups names
67+
68+
:return: groups names
69+
"""
70+
return sorted([*talib.get_function_groups().keys()])
5071

5172
@classmethod
5273
def get_functions(cls):
@@ -55,34 +76,18 @@ def get_functions(cls):
5576
5677
:return: all functions supported by this lib
5778
"""
58-
result = list()
59-
for grp in cls.get_groups().values():
60-
result.extend(grp)
61-
return sorted(result)
6279

80+
return sorted(sum(cls._groups_ref.values(), []))
6381

6482
@classmethod
65-
def get_function_group(cls, name, display_name=False):
83+
def get_talib_functions(cls):
6684
"""
67-
Get functions grouped by type
85+
Return all functions supported by this lib
6886
69-
:param str name: function name
70-
:param bool display_name: if True, full names will be show instead shorted ones
71-
:return: functions grouped by type
87+
:return: all functions supported by this lib
7288
"""
73-
name = str(name).upper()
74-
if name in cls.get_functions():
75-
if name in cls.Pattr.__dict__:
76-
return cls.Pattr.__name__ if display_name is True else 'Pattr'
77-
elif name in cls.Mom.__dict__:
78-
return cls.Mom.__name__ if display_name is True else 'Mom'
79-
elif name in cls.Olap.__dict__:
80-
return cls.Olap.__name__ if display_name is True else 'Olap'
81-
elif name in cls.Vol.__dict__:
82-
return cls.Vol.__name__ if display_name is True else 'Vol'
83-
elif name in cls.Cycl.__dict__:
84-
return cls.Cycl.__name__ if display_name is True else 'Cycl'
85-
elif name in cls.Stat.__dict__:
86-
return cls.Stat.__name__ if display_name is True else 'Stat'
87-
elif name in cls.Volat.__dict__:
88-
return cls.Volat.__name__ if display_name is True else 'Volat'
89+
return sorted([*talib.get_function_groups().values()])
90+
91+
92+
if __name__ == '__main__':
93+
print(TaLib.get_functions())

0 commit comments

Comments
 (0)