Skip to content

Commit

Permalink
Merge pull request bukosabino#89 from bukosabino/feature/testing-fi
Browse files Browse the repository at this point in the history
Feature/testing fi
  • Loading branch information
bukosabino authored Nov 12, 2019
2 parents 03256cb + ce680b8 commit 16f4bfe
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions ta/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from ta.tests.volatility import TestAverageTrueRange
from ta.tests.volume import (TestAccDistIndexIndicator,
TestEaseOfMovementIndicator,
TestForceIndexIndicator,
TestOnBalanceVolumeIndicator)
31 changes: 31 additions & 0 deletions ta/tests/data/cs-fi.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Date,Close,Volume,FI_1,FI
25/06/2010,14.33,,,
25/06/2011,14.23,45579,-4557.9,-4557.9
25/06/2012,13.98,66285,-16571.25,-6274.092857
25/06/2013,13.96,51761,-1035.22,-5525.682449
25/06/2014,13.93,69341,-2080.23,-5033.474956
25/06/2015,13.84,41631,-3746.79,-4849.66282
25/06/2016,13.99,73499,11024.85,-2581.875274
25/06/2017,14.31,55427,17736.64,320.7697651
25/06/2018,14.51,61082,12216.4,2020.145513
25/06/2019,14.46,33325,-1666.25,1493.517583
25/06/2020,14.61,39191,5878.65,2119.965071
25/06/2021,14.48,51128,-6646.64,867.5929178
25/06/2022,14.53,46505,2325.25,1075.829644
25/06/2023,14.56,44562,1336.86,1113.119695
25/06/2024,14.25,48815,-15132.65,-1207.704547
25/06/2025,14.42,33411,5679.87,-223.7653263
25/06/2026,14.24,48157,-8668.26,-1430.121708
25/06/2027,14.19,43199,-2159.95,-1534.382893
25/06/2028,14.5,45773,14189.63,711.9046633
25/06/2029,14.27,54338,-12497.74,-1175.187431
25/06/2030,14.7,56692,24377.56,2475.205059
25/06/2031,14.95,61971,15492.75,4334.854336
25/06/2032,14.68,41212,-11127.24,2125.983717
25/06/2033,14.77,59785,5380.65,2590.936043
25/06/2034,14.68,44986,-4048.74,1642.410894
25/06/2035,15.15,55929,26286.63,5163.013623
25/06/2036,15.99,162619,136599.96,23939.72025
25/06/2037,16.27,73914,20695.92,23476.32021
25/06/2038,16.02,74485,-18621.25,17462.38161
25/06/2039,16.07,52163,2608.15,15340.34852
25 changes: 22 additions & 3 deletions ta/tests/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from ta.tests.utils import TestIndicator
from ta.volume import (AccDistIndexIndicator, EaseOfMovementIndicator,
OnBalanceVolumeIndicator, acc_dist_index,
ease_of_movement, on_balance_volume,
sma_ease_of_movement)
ForceIndexIndicator, OnBalanceVolumeIndicator,
acc_dist_index, ease_of_movement, force_index,
on_balance_volume, sma_ease_of_movement)


class TestOnBalanceVolumeIndicator(TestIndicator):
Expand All @@ -26,6 +26,25 @@ def test_obv2(self):
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)


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

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

def test_fi(self):
target = 'FI'
result = force_index(close=self._df['Close'], volume=self._df['Volume'], n=13, fillna=False)
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_fi2(self):
target = 'FI'
result = ForceIndexIndicator(
close=self._df['Close'], volume=self._df['Volume'], n=13, fillna=False).force_index()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)


class TestEaseOfMovementIndicator(TestIndicator):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:ease_of_movement_emv
Expand Down
9 changes: 5 additions & 4 deletions ta/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
import pandas as pd

from ta.utils import IndicatorMixin
from ta.utils import IndicatorMixin, ema


class AccDistIndexIndicator(IndicatorMixin):
Expand Down Expand Up @@ -126,7 +126,7 @@ class ForceIndexIndicator(IndicatorMixin):
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:force_index
"""

def __init__(self, close: pd.Series, volume: pd.Series, n: int = 2, fillna: bool = False):
def __init__(self, close: pd.Series, volume: pd.Series, n: int = 13, fillna: bool = False):
"""
Args:
high(pandas.Series): dataset 'High' column.
Expand All @@ -143,7 +143,8 @@ def __init__(self, close: pd.Series, volume: pd.Series, n: int = 2, fillna: bool
self._run()

def _run(self):
self._fi = self._close.diff(self._n) * self._volume.diff(self._n)
fi = (self._close - self._close.shift(1)) * self._volume
self._fi = ema(fi, self._n, fillna=self._fillna)

def force_index(self) -> pd.Series:
fi = self.check_fillna(self._fi, value=0)
Expand Down Expand Up @@ -317,7 +318,7 @@ def chaikin_money_flow(high, low, close, volume, n=20, fillna=False):
high=high, low=low, close=close, volume=volume, n=n, fillna=fillna).chaikin_money_flow()


def force_index(close, volume, n=2, fillna=False):
def force_index(close, volume, n=13, fillna=False):
"""Force Index (FI)
It illustrates how strong the actual buying or selling pressure is. High
Expand Down
2 changes: 1 addition & 1 deletion ta/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add_volume_ta(df: pd.DataFrame, high: str, low: str, close: str, volume: str

# Force Index
df[f'{colprefix}volume_fi'] = ForceIndexIndicator(
close=df[close], volume=df[volume], fillna=fillna).force_index()
close=df[close], volume=df[volume], n=13, fillna=fillna).force_index()

# Ease of Movement
indicator = EaseOfMovementIndicator(high=df[high], low=df[low], volume=df[volume], n=14, fillna=fillna)
Expand Down

0 comments on commit 16f4bfe

Please sign in to comment.