bukosabino/ta

Confusion about Force Index

lucasguo opened this issue · 1 comments

As current force index indicator implementation, the result is much depended on the history range. If range adjusted, the result will be changed. Here is a small example(to make it easier, window size is change to 3).

let us say, there is a full data like this.

df_full = pd.DataFrame({'close': [1, 2, 3, 4, 5, 6, 7, 8], 'vol': [10, 20, 30, 40, 50, 60, 70, 80]})

and a sub dataframe start with 4 from last dataframe.

df_partial = pd.DataFrame({'close': [4, 5, 6, 7, 8], 'vol': [40, 50, 60, 70, 80]})

then do the fi for both dataframes.

fi_full = ForceIndexIndicator(close=df_full['close'], volume=df_full['vol'], window=3).force_index()
fi_partial = ForceIndexIndicator(close=df_partial['close'], volume=df_partial['vol'], window=3).force_index()

print(fi_full)
print(fi_partial)

The output from above code is:

0         NaN
1         NaN
2         NaN
3    32.50000
4    41.25000
5    50.62500
6    60.31250
7    70.15625
Name: fi_3, dtype: float64
0      NaN
1      NaN
2      NaN
3    62.50
4    71.25
Name: fi_3, dtype: float64

The result shows the same day have different values. (take the last day with close price 8 as example, df_full return 70.15625, and df_partial return 71.25)

The result will cause this indicator very unstable when I do cross validate on some dataset, because the dataset have to be split to small dataset with different length.
In my option, the data should be cut into small pieces with giving window, and then do the calculation. the implementation may be changed to something like.

def fixed_fi_func(close, vol, window):
    fi_series = (close - close.shift(1)) * vol
    return fi_series.rolling(window).apply(lambda x: _ewm(x, window))


def _ewm(x, window):
    return x.ewm(span=window, min_periods=window, adjust=False).mean().iloc[-1]

I'm not sure whether my understanding is correct, just want to take a discussion. If something unclear, please leave a comment.

According to #22
Current implementation is match with Wikipedia's definition.
So just close the issue.