Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trend_scanning_labels with lookforward false, returns incorrect t1. #537

Open
jinyiabc opened this issue Oct 6, 2023 · 1 comment
Open

Comments

@jinyiabc
Copy link

jinyiabc commented Oct 6, 2023

Describe the bug
trend_scanning_labels with lookforward false, returns incorrect t1.

image

As below funciton implied, t1 should be 2017-07-16 to retrieve "-1.0014031858892483" of tValue.
image

If you can provide email address, I can attach ipynb file.

@jinyiabc
Copy link
Author

jinyiabc commented Oct 6, 2023

import yfinance as yf
import matplotlib.pyplot as plt

Import MlFinLab tools

from mlfinlab.labeling.trend_scanning import trend_scanning_labels

Loading EEM ETF daily close prices during the financial crises

eem_close = yf.download(
tickers="EEM", start="2008-07-01", end="2009-07-01", interval="1d"
)["Adj Close"]

Getting indexes that we want to label

t_events = eem_close.index
tr_scan_labels = trend_scanning_labels(
eem_close, t_events, observation_window=20, look_forward=False, min_sample_length=5
)
tr_scan_labels.dropna(subset=["bin"])

''''''''''''''
t1 t_value ret bin
Date
2008-07-29 2008-07-15 -1.001403 -0.043901 -1.0
2008-07-30 2008-07-08 1.784318 -0.007165 1.0
2008-07-31 2008-07-10 1.106360 0.013099 1.0
2008-08-01 2008-07-22 -1.362230 0.032972 -1.0
2008-08-04 2008-07-11 -8.658784 0.045189 -1.0
... ... ... ... ...
2009-06-24 2009-06-19 -6.849154 0.008768 -1.0
2009-06-25 2009-06-23 -5.874661 -0.049767 -1.0
2009-06-26 2009-06-04 7.476431 0.029371 1.0
2009-06-29 2009-06-08 8.294161 0.008074 1.0
2009-06-30 2009-06-10 5.052513 0.033169 1.0
'''''''''''''
def tValLinR(close):
"""tValue from a linear trend via SNIPPET 5.1 T-VALUE OF A LINEAR TREND

Args:
    close (Series): close prices to search through

Returns:
    float: t-value

Example:
    >>> lookforward = 5
    >>> start = 0
    >>> stop = stop + lookforward
    >>> df1 = close.iloc[start:stop]
    >>> tValLinR(df1.values)

"""
x = np.ones((close.shape[0], 2))
x[:, 1] = np.arange(close.shape[0])
ols = sm1.OLS(close, x).fit()
return ols.tvalues[1]

tValLinR(eem_close.loc['2008-07-16':'2008-07-29'])
'''''''''''''
-1.0014031858892483
'''''''''''''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant