Skip to content

Commit

Permalink
Update doc strings for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Magnuson committed Apr 4, 2017
1 parent 035be8e commit 65849d7
Show file tree
Hide file tree
Showing 40 changed files with 189 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Bollinger Bands
-Lower Bollinger Band
-Bandwidth
-Percent Bandwidth
-Range
Chaikin Money Flow
Chande Momentum Oscillator
Commodity Channel Index
Expand Down
4 changes: 3 additions & 1 deletion py_ti/accumulation_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

def accumulation_distribution(close_data, high_data, low_data, volume):
"""
Accumulation/Distribution
Accumulation/Distribution.
Formula:
A/D = (Ct - Lt) - (Ht - Ct) / (Ht - Lt) * Vt + A/Dt-1
"""
catch_errors.check_for_input_len_diff(
Expand Down
4 changes: 4 additions & 0 deletions py_ti/aroon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
def aroon_up(data, period):
"""
Aroon Up.
Formula:
AROONUP = (((PERIOD) - (PERIODS since PERIOD high)) / (PERIOD)) * 100
"""
catch_errors.check_for_period_error(data, period)
Expand All @@ -25,6 +27,8 @@ def aroon_up(data, period):
def aroon_down(data, period):
"""
Aroon Down.
Formula:
AROONDWN = (((PERIOD) - (PERIODS SINCE PERIOD LOW)) / (PERIOD)) * 100
"""
catch_errors.check_for_period_error(data, period)
Expand Down
2 changes: 2 additions & 0 deletions py_ti/average_true_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
def average_true_range(close_data, period):
"""
Average True Range.
Formula:
ATRt = ATRt-1 * (n - 1) + TRt / n
"""
tr = true_range(close_data, period)
Expand Down
2 changes: 2 additions & 0 deletions py_ti/average_true_range_percent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
def average_true_range_percent(close_data, period):
"""
Average True Range Percent.
Formula:
ATRP = (ATR / CLOSE) * 100
"""
catch_errors.check_for_period_error(close_data, period)
Expand Down
49 changes: 40 additions & 9 deletions py_ti/bollinger_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
)


def upper_bollinger_band(data, period, std=2.0):
def upper_bollinger_band(data, period, std_mult=2.0):
"""
Upper Bollinger Band.
Formula:
u_bb = SMA(t) + STD(SMA(t-n:t)) * std_mult
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
Expand All @@ -15,27 +21,34 @@ def upper_bollinger_band(data, period, std=2.0):
upper_bb = []
for idx in range(len(data) - period + 1):
std_dev = np.std(data[idx:idx + period])
upper_bb.append(simple_ma[idx] + std_dev * std)
upper_bb.append(simple_ma[idx] + std_dev * std_mult)
upper_bb = fill_for_noncomputable_vals(data, upper_bb)

return np.array(upper_bb)


def middle_bollinger_band(data, period, std=2.0):
"""
Middle Bollinger Band.
Formula:
m_bb = sma()
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
simple_ma = sma(data, period)[period-1:]

middle_bb = []
for idx in range(len(data) - period + 1):
middle_bb.append(simple_ma[idx])
middle_bb = fill_for_noncomputable_vals(data, middle_bb)
mid_bb = sma(data, period)

return np.array(middle_bb)
return mid_bb


def lower_bollinger_band(data, period, std=2.0):
"""
Lower Bollinger Band.
Formula:
u_bb = SMA(t) - STD(SMA(t-n:t)) * std_mult
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
Expand All @@ -51,6 +64,12 @@ def lower_bollinger_band(data, period, std=2.0):


def bandwidth(data, period, std=2.0):
"""
Bandwidth.
Formula:
bw = u_bb - l_bb / m_bb
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
Expand All @@ -63,6 +82,12 @@ def bandwidth(data, period, std=2.0):


def bb_range(data, period, std=2.0):
"""
Range.
Formula:
bb_range = u_bb - l_bb
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
Expand All @@ -73,6 +98,12 @@ def bb_range(data, period, std=2.0):


def percent_bandwidth(data, period, std=2.0):
"""
Percent Bandwidth.
Formula:
%_bw = data() - l_bb() / bb_range()
"""
catch_errors.check_for_period_error(data, period)

period = int(period)
Expand Down
16 changes: 16 additions & 0 deletions py_ti/catch_errors.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@

def check_for_period_error(data, period):
"""
Check for Period Error.
This method checks if the developer is trying to enter a period that is
larger than the data set being entered. If that is the case an exception is
raised with a custom message that informs the developer that their period
is greater than the data set.
"""
period = int(period)
data_len = len(data)
if data_len < period:
raise Exception("Error: data_len < period")


def check_for_input_len_diff(*args):
"""
Check for Input Length Difference.
This method checks if multiple data sets that are inputted are all the same
size. If they are not the same length an error is raised with a custom
message that informs the developer that the data set's lengths are not the
same.
"""
arrays_len = map(lambda arr: len(arr), args)
if not all(a == arrays_len[0] for a in arrays_len):
err_msg = ("Error: mismatched data lengths, check to ensure that all "
Expand Down
2 changes: 2 additions & 0 deletions py_ti/chaikin_money_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
def chaikin_money_flow(close_data, high_data, low_data, volume, period):
"""
Chaikin Money Flow.
Formula:
CMF = SUM[(((Cn - Ln) - (Hn - Cn)) / (Hn - Ln)) * V] / SUM(Vn)
"""
catch_errors.check_for_input_len_diff(
Expand Down
3 changes: 3 additions & 0 deletions py_ti/chande_momentum_oscillator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
def chande_momentum_oscillator(close_data, period):
"""
Chande Momentum Oscillator.
Formula:
cmo = 100 * ((sum_up - sum_down) / (sum_up + sum_down))
"""
catch_errors.check_for_period_error(close_data, period)

Expand Down
2 changes: 2 additions & 0 deletions py_ti/commodity_channel_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
def commodity_channel_index(close_data, high_data, low_data, period):
"""
Commodity Channel Index.
Formula:
CCI = (TP - SMA(TP)) / (0.015 * Mean Deviation)
"""
catch_errors.check_for_input_len_diff(close_data, high_data, low_data)
Expand Down
2 changes: 2 additions & 0 deletions py_ti/detrended_price_oscillator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
def detrended_price_oscillator(data, period):
"""
Detrended Price Oscillator.
Formula:
DPO = DATA[i] - Avg(DATA[period/2 + 1])
"""
catch_errors.check_for_period_error(data, period)
Expand Down
22 changes: 18 additions & 4 deletions py_ti/directional_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
def calculate_up_moves(high_data):
"""
Up Move.
Formula:
UPMOVE = Ht - Ht-1
"""
up_moves = map(
Expand All @@ -23,6 +25,8 @@ def calculate_up_moves(high_data):
def calculate_down_moves(low_data):
"""
Down Move.
Formula:
DWNMOVE = Lt-1 - Lt
"""
down_moves = map(
Expand All @@ -34,7 +38,9 @@ def calculate_down_moves(low_data):

def positive_directional_movement(high_data, low_data):
"""
Positive Directional Movement (+DM)
Positive Directional Movement (+DM).
Formula:
+DM: if UPMOVE > DWNMOVE and UPMOVE > 0 then +DM = UPMOVE else +DM = 0
"""
catch_errors.check_for_input_len_diff(high_data, low_data)
Expand All @@ -53,7 +59,9 @@ def positive_directional_movement(high_data, low_data):

def negative_directional_movement(high_data, low_data):
"""
Negative Directional Movement (-DM)
Negative Directional Movement (-DM).
-DM: if DWNMOVE > UPMOVE and DWNMOVE > 0 then -DM = DWNMOVE else -Dm = 0
"""
catch_errors.check_for_input_len_diff(high_data, low_data)
Expand All @@ -72,7 +80,9 @@ def negative_directional_movement(high_data, low_data):

def positive_directional_index(close_data, high_data, low_data, period):
"""
Positive Directional Index (+DI)
Positive Directional Index (+DI).
Formula:
+DI = 100 * SMMA(+DM) / ATR
"""
catch_errors.check_for_input_len_diff(close_data, high_data, low_data)
Expand All @@ -85,7 +95,9 @@ def positive_directional_index(close_data, high_data, low_data, period):

def negative_directional_index(close_data, high_data, low_data, period):
"""
Negative Directional Index (-DI)
Negative Directional Index (-DI).
Formula:
-DI = 100 * SMMA(-DM) / ATR
"""
catch_errors.check_for_input_len_diff(close_data, high_data, low_data)
Expand All @@ -99,6 +111,8 @@ def negative_directional_index(close_data, high_data, low_data, period):
def average_directional_index(close_data, high_data, low_data, period):
"""
Average Directional Index.
Formula:
ADX = 100 * SMMA(abs((+DI - -DI) / (+DI + -DI)))
"""
avg_di = (abs(
Expand Down
1 change: 1 addition & 0 deletions py_ti/double_exponential_moving_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def double_exponential_moving_average(data, period):
"""
Double Exponential Moving Average.
Formula:
DEMA = 2*EMA - EMA(EMA)
"""
catch_errors.check_for_period_error(data, period)
Expand Down
5 changes: 4 additions & 1 deletion py_ti/double_smoothed_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

def double_smoothed_stochastic(data, period):
"""
Double Smoothed Stochastic
Double Smoothed Stochastic.
Formula:
dss = 100 * EMA(Close - Lowest Low) / EMA(Highest High - Lowest Low)
"""
catch_errors.check_for_period_error(data, period)
lows = map(
Expand Down
5 changes: 3 additions & 2 deletions py_ti/exponential_moving_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ def exponential_moving_average(data, period):
"""
Exponential Moving Average.
Formula: p0 + (1 - w) * p1 + (1 - w)^2 * p2 + (1 + w)^3 * p3 +...
Formula:
p0 + (1 - w) * p1 + (1 - w)^2 * p2 + (1 + w)^3 * p3 +...
/ 1 + (1 - w) + (1 - w)^2 + (1 - w)^3 +...
where: w = 2 / (N + 1)
where: w = 2 / (N + 1)
"""
catch_errors.check_for_period_error(data, period)
emas = map(
Expand Down
2 changes: 2 additions & 0 deletions py_ti/hull_moving_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
def hull_moving_average(data, period):
"""
Hull Moving Average.
Formula:
HMA = WMA(2*WMA(n/2) - WMA(n)), sqrt(n)
"""
catch_errors.check_for_period_error(data, period)
Expand Down
10 changes: 10 additions & 0 deletions py_ti/ichimoku_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def conversion_base_line_helper(data, period):
def tenkansen(data, period=9):
"""
TenkanSen (Conversion Line)
Formula:
(H + L) / 2 :: default period=9
"""
ts = conversion_base_line_helper(data, period)
Expand All @@ -31,6 +33,8 @@ def tenkansen(data, period=9):
def kijunsen(data, period=26):
"""
KijunSen (Base Line)
Formula:
(H + L) / 2 :: default period=26
"""
ks = conversion_base_line_helper(data, period)
Expand All @@ -40,6 +44,8 @@ def kijunsen(data, period=26):
def chiku_span(data):
"""
Chiku Span (Lagging Span)
Formula:
Close shifted back 26 bars
"""
cs = data[25::]
Expand All @@ -49,6 +55,8 @@ def chiku_span(data):
def senkou_a(data):
"""
Senkou A (Leading Span A)
Formula:
(TenkanSen + KijunSen) / 2 :: Shift Forward 26 bars
"""
sa = (tenkansen(data) + kijunsen(data)) / 2
Expand All @@ -61,6 +69,8 @@ def senkou_a(data):
def senkou_b(data, period=52):
"""
Senkou B (Leading Span B)
Formula:
(H + L) / 2 :: default period=52 :: shifted forward 26 bars
"""
sb = conversion_base_line_helper(data, period)
Expand Down
Loading

0 comments on commit 65849d7

Please sign in to comment.