feat(Events): added EventFilter, EventLabeller (#186)

This commit is contained in:
Mark Aron Szulyovszky
2022-01-26 23:22:43 +01:00
committed by GitHub
parent 1042c82333
commit 42a1bc59cb
65 changed files with 759 additions and 276571 deletions
@@ -1,7 +1,7 @@
from feature_extractors.feature_extractors import feature_lag, feature_mom, feature_ROC, feature_RSI, feature_STOD, feature_STOK, feature_expanding_zscore, feature_vol, feature_day_of_month, feature_day_of_week, feature_month, feature_debug_future_lookahead
from utils.types import FeatureExtractorConfig
from utils.helpers import flatten
from feature_extractors.fractional_differentiation import feature_fractional_differentiation, feature_fractional_differentiation_log
from .types import FeatureExtractorConfig
from .feature_extractors import feature_lag, feature_mom, feature_ROC, feature_RSI, feature_STOD, feature_STOK, feature_expanding_zscore, feature_vol, feature_day_of_month, feature_day_of_week, feature_month, feature_debug_future_lookahead
from .fractional_differentiation import feature_fractional_differentiation, feature_fractional_differentiation_log
__presets = dict(
debug_future_lookahead = [('debug_future', feature_debug_future_lookahead, [1])],
+14 -17
View File
@@ -3,46 +3,43 @@ import numpy as np
from feature_extractors.utils import get_close_low_high
from feature_extractors.utils import apply_log_if_necessary_series
def feature_debug_future_lookahead(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_debug_future_lookahead(df: pd.DataFrame, period: int) -> pd.Series:
return df['returns'].shift(-period)
def feature_lag(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_lag(df: pd.DataFrame, period: int) -> pd.Series:
assert period > 0
return df['returns'].shift(period)
def feature_expanding_zscore(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_expanding_zscore(df: pd.DataFrame, period: int) -> pd.Series:
close = df['close']
return (close - close.expanding(period).mean()) / close.expanding(period).std()
def feature_day_of_week(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.DataFrame:
def feature_day_of_week(df: pd.DataFrame, period: int) -> pd.DataFrame:
return pd.get_dummies(pd.DatetimeIndex(df.index).dayofweek, drop_first=True, prefix="date_day_week").set_index(df.index)
def feature_day_of_month(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.DataFrame:
def feature_day_of_month(df: pd.DataFrame, period: int) -> pd.DataFrame:
return pd.get_dummies(pd.DatetimeIndex(df.index).day, drop_first=True, prefix="date_day_month").set_index(df.index)
def feature_month(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.DataFrame:
def feature_month(df: pd.DataFrame, period: int) -> pd.DataFrame:
return pd.get_dummies(pd.DatetimeIndex(df.index).month, drop_first=True, prefix="date_month").set_index(df.index)
def feature_vol(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_vol(df: pd.DataFrame, period: int) -> pd.Series:
return df['returns'].rolling(period).std() * (252**0.5)
def feature_mom(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
if is_log_return:
return np.log(df['close']).diff(period)
else:
return df['close'].pct_change(period)
def feature_mom(df: pd.DataFrame, period: int) -> pd.Series:
return np.log(df['close']).diff(period)
def feature_STOK(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_STOK(df: pd.DataFrame, period: int) -> pd.Series:
close, low, high = get_close_low_high(df)
STOK = ((close - low.rolling(period).min()) / (high.rolling(period).max() - low.rolling(period).min())) * 100
return apply_log_if_necessary_series(STOK, "stok")
def feature_STOD(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
stok = feature_STOK(df, period, is_log_return)
def feature_STOD(df: pd.DataFrame, period: int) -> pd.Series:
stok = feature_STOK(df, period)
return stok.rolling(3).mean()
def feature_RSI(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_RSI(df: pd.DataFrame, period: int) -> pd.Series:
returns = df['returns']
delta = returns.diff().dropna()
u=delta*0
@@ -55,7 +52,7 @@ def feature_RSI(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series
d.ewm(com=period-1, adjust=False).mean()
return apply_log_if_necessary_series(100-100/(1+rs), "rsi")
def feature_ROC(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_ROC(df: pd.DataFrame, period: int) -> pd.Series:
returns = df['returns']
M = returns.diff(period - 1)
N = returns.shift(period - 1)
@@ -3,13 +3,13 @@ import pandas as pd
import numpy as np
from feature_extractors.utils import apply_log_if_necessary_series
def feature_fractional_differentiation(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_fractional_differentiation(df: pd.DataFrame, period: int) -> pd.Series:
frac_diff = FracdiffStat(window = period)
input_series = df["close"].to_numpy().reshape(-1, 1)
result = frac_diff.fit_transform(input_series)
return pd.Series(result.squeeze(), index = df.index)
def feature_fractional_differentiation_log(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_fractional_differentiation_log(df: pd.DataFrame, period: int) -> pd.Series:
series = feature_fractional_differentiation(df, period, is_log_return)
return apply_log_if_necessary_series(series, "fracdiff")
+1 -1
View File
@@ -2,7 +2,7 @@ import pandas_ta as ta
import pandas as pd
from feature_extractors.utils import get_close_low_high
def feature_EBSW(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
def feature_EBSW(df: pd.DataFrame, period: int) -> pd.Series:
close, low, high = get_close_low_high(df)
return ta.ebsw(close, period)
+9
View File
@@ -0,0 +1,9 @@
from typing import Callable, Union, Literal
import pandas as pd
Period = int
IsLogReturn = bool
FeatureExtractor = Callable[[pd.DataFrame, Period], Union[pd.DataFrame, pd.Series]]
Name = str
FeatureExtractorConfig = tuple[Name, FeatureExtractor, list[Period]]
ScalerTypes = Literal['normalize', 'minmax', 'standardize', 'none']