Skip to content

Commit

Permalink
Some code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
havocesp authored and GHPortable committed May 13, 2018
1 parent 7112176 commit 85601ef
Show file tree
Hide file tree
Showing 14 changed files with 2,596 additions and 2,626 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Collection of Technical Analysis Functions

* __Author:__ Daniel J. Umpierrez
* __Version:__ 0.0.4
* __Version:__ 0.0.6
* __License:__ MIT

## Description
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pandas
pandas=>23.0
numpy=>1.14.3
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='techa',
version='v0.0.5',
version='v0.0.6',
requires=['pandas'],
packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
package_dir={'techa': 'techa'},
Expand Down
74 changes: 44 additions & 30 deletions techa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,81 @@
import os
import sys

import cycle
import experimental
import momentum
import overlap
import pattern
import prices
import statistic
import volatility

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(BASE_DIR)
from core import TaLib


__author__ = 'Daniel J. Umpierrez'
__version__ = '0.0.4'
__version__ = '0.0.5'

__all__ = (*cycle.__all__, *momentum.__all__, *overlap.__all__, *volatility.__all__, *pattern.__all__,
*statistic.__all__, *experimental.__all__, *prices.__all__, 'ASI', 'SI')

__all__ = ['TaLib']

def ASI(df, L):
def ASI(data, l):
"""
Accumulation Swing Index (J. Welles Wilder)
source: book: New Concepts in Technical Trading Systems
"""
asi_list = []
si_list = SI(df, L)
si_list = SI(data, l)
i = 0
while i < len(df['close']):
asi = .0
while i < len(data['close']):
if i < 1:
asi = float('NaN')
asi_list.append(asi)
asi = .0
else:
asi = asi + si_list[i]
asi += si_list[i]
asi_list.append(asi)
i += 1
return asi_list

def SI(df, L):

def SI(data, l):
"""
Swing Index (J. Welles Wilder)
source: book: New Concepts in Technical Trading Systems
From "New Concepts in Technical Trading Systems"
:param pd.DataFrame data:
:param l:
:return:
"""
si_list = []
i = 0
while i < len(df['close']):
while i < len(data['close']):
if i < 1:
si = float('NaN')
else:
N = (df['close'][i] - df['close'][i - 1]) + (.5 * (df['close'][i] - df['open'][i])) + (
.25 * (df['close'][i - 1] - df['open'][i - 1]))
R1 = df['high'][i] - df['close'][i - 1]
R2 = df['low'][i] - df['close'][i - 1]
R3 = df['high'][i] - df['low'][i]
if R1 > R2 and R1 > R3:
R = (df['high'][i] - df['close'][i - 1]) - (.5 * (df['low'][i] - df['close'][i - 1])) + (
.25 * (df['close'][i - 1] - df['open'][i - 1]))
if R2 > R1 and R2 > R3:
R = (df['low'][i] - df['close'][i - 1]) - (.5 * (df['high'][i] - df['close'][i - 1])) + (
.25 * (df['close'][i - 1] - df['open'][i - 1]))
if R3 > R1 and R3 > R2:
R = (df['high'][i] - df['low'][i]) + (.25 * (df['close'][i - 1] - df['open'][i - 1]))
K1 = df['high'][i] - df['close'][i - 1]
K2 = df['low'][i] - df['close'][i - 1]
if K1 > K2:
K = K1
n = (data['close'][i] - data['close'][i - 1]) + (.5 * (data['close'][i] - data['open'][i])) + (
.25 * (data['close'][i - 1] - data['open'][i - 1]))
r1 = data['high'][i] - data['close'][i - 1]
r2 = data['low'][i] - data['close'][i - 1]
r3 = data['high'][i] - data['low'][i]
if r1 > r2 and r1 > r3:
r = (data['high'][i] - data['close'][i - 1]) - (.5 * (data['low'][i] - data['close'][i - 1])) + (
.25 * (data['close'][i - 1] - data['open'][i - 1]))
if r2 > r1 and r2 > r3:
r = (data['low'][i] - data['close'][i - 1]) - (.5 * (data['high'][i] - data['close'][i - 1])) + (
.25 * (data['close'][i - 1] - data['open'][i - 1]))
if r3 > r1 and r3 > r2:
r = (data['high'][i] - data['low'][i]) + (.25 * (data['close'][i - 1] - data['open'][i - 1]))
k1 = data['high'][i] - data['close'][i - 1]
k2 = data['low'][i] - data['close'][i - 1]
if k1 > k2:
k = k1
else:
K = K2
si = 50 * (N / R) * (K / L)
k = k2
si = 50 * (n / r) * (k / l)
si_list.append(si)
i += 1
return si_list
109 changes: 57 additions & 52 deletions techa/core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
# -*- coding:utf-8 -*-
from collections import OrderedDict
import talib

from cycle import Cycle
from momentum import Momentum
from overlap import Overlap
from pattern import Pattern
from statistic import Statistic
from volatility import Volatility
from volume import Volume
import cycle
import experimental
import momentum
import overlap
import pattern
import prices
import statistic
import volatility
import volume

# from cycle import *
# from momentum import *
# from overlap import *
# from pattern import *
# from statistic import *
# from volatility import *
# from experimental import *
# from prices import *
# from volume import *

_function_list = [f for f in
(*cycle.__all__, *momentum.__all__, *overlap.__all__, *volatility.__all__, *pattern.__all__,
*statistic.__all__, *experimental.__all__, *prices.__all__) if f[0].isupper()]
__all__ = ['TaLib']


# noinspection SpellCheckingInspection
Expand All @@ -17,23 +34,21 @@ class TaLib:
"""

_groups_ref = {
'Cycle Indicators': 'Cyl',
'Momentum Indicators': 'Mom',
'Overlap studies': 'Ovlap',
'Patter Recognition': 'Pattr',
'Statistic Functions': 'Stat',
'Volume Indicators': 'Vol',
'Volatility Indicators': 'Volat'
'cycle': cycle.__all__,
'momentum': momentum.__all__,
'overlap': overlap.__all__,
'patter': pattern.__all__,
'statistic': statistic.__all__,
'volume': volatility.__all__,
'volatility': experimental.__all__,
'price': prices.__all__,
'experimental': volume.__all__
}

Volat = Volatility
Olap = Overlap
Mom = Momentum
Cycl = Cycle
Vol = Volume
Pattr = Pattern
Stat = Statistic

@classmethod
def calculate_indicator(cls, indicator, *args, **kwargs):
fn = globals().get(indicator)
return fn(*args, **kwargs)

@classmethod
def get_groups(cls):
Expand All @@ -42,11 +57,17 @@ def get_groups(cls):
:return: groups names
"""
groups = OrderedDict().fromkeys(sorted([grp for grp in cls.__dict__ if grp[0].isupper()]))
for grp in groups:
groups.update({grp: [fn for fn in cls.__dict__[grp].__dict__ if fn[0].isupper()]})
return groups

return sorted([*cls._groups_ref])

@classmethod
def get_talib_groups(cls):
"""
Just return groups names
:return: groups names
"""
return sorted([*talib.get_function_groups().keys()])

@classmethod
def get_functions(cls):
Expand All @@ -55,34 +76,18 @@ def get_functions(cls):
:return: all functions supported by this lib
"""
result = list()
for grp in cls.get_groups().values():
result.extend(grp)
return sorted(result)

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

@classmethod
def get_function_group(cls, name, display_name=False):
def get_talib_functions(cls):
"""
Get functions grouped by type
Return all functions supported by this lib
:param str name: function name
:param bool display_name: if True, full names will be show instead shorted ones
:return: functions grouped by type
:return: all functions supported by this lib
"""
name = str(name).upper()
if name in cls.get_functions():
if name in cls.Pattr.__dict__:
return cls.Pattr.__name__ if display_name is True else 'Pattr'
elif name in cls.Mom.__dict__:
return cls.Mom.__name__ if display_name is True else 'Mom'
elif name in cls.Olap.__dict__:
return cls.Olap.__name__ if display_name is True else 'Olap'
elif name in cls.Vol.__dict__:
return cls.Vol.__name__ if display_name is True else 'Vol'
elif name in cls.Cycl.__dict__:
return cls.Cycl.__name__ if display_name is True else 'Cycl'
elif name in cls.Stat.__dict__:
return cls.Stat.__name__ if display_name is True else 'Stat'
elif name in cls.Volat.__dict__:
return cls.Volat.__name__ if display_name is True else 'Volat'
return sorted([*talib.get_function_groups().values()])


if __name__ == '__main__':
print(TaLib.get_functions())
Loading

0 comments on commit 85601ef

Please sign in to comment.