mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-17 21:08:08 +00:00
chore(Linter): reformatted code with black (#211)
* chore(Linter): reformatted code with black * Create black.yaml
This commit is contained in:
@@ -1,35 +1,55 @@
|
||||
|
||||
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
|
||||
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])],
|
||||
single_mom = [('mom', feature_mom, [30])],
|
||||
single_vol = [('vol', feature_vol, [30])],
|
||||
mom = [('mom', feature_mom, [10, 20, 30, 60, 90])],
|
||||
vol = [('vol', feature_vol, [10, 20, 30, 60])],
|
||||
lags_up_to_5 = [('lag', feature_lag, [1,2,3,4,5])],
|
||||
lags_up_to_10 = [('lag', feature_lag, [1,2,3,4,5,6,7,8,9,10])],
|
||||
date_all = [
|
||||
('day_of_week', feature_day_of_week, [0]),
|
||||
('day_of_month', feature_day_of_month, [0]),
|
||||
('month', feature_month, [0])],
|
||||
date_days = [
|
||||
('day_of_week', feature_day_of_week, [0]),
|
||||
('day_of_month', feature_day_of_month, [0]),
|
||||
debug_future_lookahead=[("debug_future", feature_debug_future_lookahead, [1])],
|
||||
single_mom=[("mom", feature_mom, [30])],
|
||||
single_vol=[("vol", feature_vol, [30])],
|
||||
mom=[("mom", feature_mom, [10, 20, 30, 60, 90])],
|
||||
vol=[("vol", feature_vol, [10, 20, 30, 60])],
|
||||
lags_up_to_5=[("lag", feature_lag, [1, 2, 3, 4, 5])],
|
||||
lags_up_to_10=[("lag", feature_lag, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])],
|
||||
date_all=[
|
||||
("day_of_week", feature_day_of_week, [0]),
|
||||
("day_of_month", feature_day_of_month, [0]),
|
||||
("month", feature_month, [0]),
|
||||
],
|
||||
roc = [('roc', feature_ROC, [10, 30])],
|
||||
rsi = [('rsi', feature_ROC, [10, 30, 100])],
|
||||
stod = [('stod', feature_STOD, [10, 30, 200])],
|
||||
stok = [('stok', feature_STOK, [10, 30, 200])],
|
||||
fracdiff = [('fracdiff', feature_fractional_differentiation, [10, 30])],
|
||||
fracdiff_log = [('fracdiff_log', feature_fractional_differentiation_log, [10, 30])],
|
||||
z_score = [('z_score', feature_expanding_zscore, [10])],
|
||||
date_days=[
|
||||
("day_of_week", feature_day_of_week, [0]),
|
||||
("day_of_month", feature_day_of_month, [0]),
|
||||
],
|
||||
roc=[("roc", feature_ROC, [10, 30])],
|
||||
rsi=[("rsi", feature_ROC, [10, 30, 100])],
|
||||
stod=[("stod", feature_STOD, [10, 30, 200])],
|
||||
stok=[("stok", feature_STOK, [10, 30, 200])],
|
||||
fracdiff=[("fracdiff", feature_fractional_differentiation, [10, 30])],
|
||||
fracdiff_log=[("fracdiff_log", feature_fractional_differentiation_log, [10, 30])],
|
||||
z_score=[("z_score", feature_expanding_zscore, [10])],
|
||||
)
|
||||
|
||||
presets = __presets | dict(
|
||||
level_1 = __presets["mom"] + __presets["vol"],
|
||||
level_2 = __presets["mom"] + __presets["vol"] + __presets["roc"] + __presets["rsi"] + __presets["stod"] + __presets["stok"],
|
||||
level_1=__presets["mom"] + __presets["vol"],
|
||||
level_2=__presets["mom"]
|
||||
+ __presets["vol"]
|
||||
+ __presets["roc"]
|
||||
+ __presets["rsi"]
|
||||
+ __presets["stod"]
|
||||
+ __presets["stok"],
|
||||
)
|
||||
|
||||
|
||||
@@ -3,58 +3,85 @@ 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) -> pd.Series:
|
||||
return df['returns'].shift(-period)
|
||||
return df["returns"].shift(-period)
|
||||
|
||||
|
||||
def feature_lag(df: pd.DataFrame, period: int) -> pd.Series:
|
||||
assert period > 0
|
||||
return df['returns'].shift(period)
|
||||
return df["returns"].shift(period)
|
||||
|
||||
|
||||
def feature_expanding_zscore(df: pd.DataFrame, period: int) -> pd.Series:
|
||||
close = df['close']
|
||||
close = df["close"]
|
||||
return (close - close.expanding(period).mean()) / close.expanding(period).std()
|
||||
|
||||
|
||||
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)
|
||||
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) -> pd.DataFrame:
|
||||
return pd.get_dummies(pd.DatetimeIndex(df.index).day, drop_first=True, prefix="date_day_month").set_index(df.index)
|
||||
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) -> pd.DataFrame:
|
||||
return pd.get_dummies(pd.DatetimeIndex(df.index).month, drop_first=True, prefix="date_month").set_index(df.index)
|
||||
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) -> pd.Series:
|
||||
return df['returns'].rolling(period).std() * (252**0.5)
|
||||
return df["returns"].rolling(period).std() * (252**0.5)
|
||||
|
||||
|
||||
def feature_mom(df: pd.DataFrame, period: int) -> pd.Series:
|
||||
return np.log(df['close']).diff(period)
|
||||
return np.log(df["close"]).diff(period)
|
||||
|
||||
|
||||
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
|
||||
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) -> pd.Series:
|
||||
stok = feature_STOK(df, period)
|
||||
return stok.rolling(3).mean()
|
||||
|
||||
|
||||
def feature_RSI(df: pd.DataFrame, period: int) -> pd.Series:
|
||||
returns = df['returns']
|
||||
returns = df["returns"]
|
||||
delta = returns.diff().dropna()
|
||||
u=delta*0
|
||||
u = delta * 0
|
||||
d = u.copy()
|
||||
u[delta > 0] = delta[delta > 0]
|
||||
d[delta < 0] = -delta[delta < 0]
|
||||
u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains u = u.drop(u.index[:(period-1)])
|
||||
d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses d = d.drop(d.index[:(period-1)])
|
||||
rs = u.ewm(com=period-1, adjust=False).mean() / \
|
||||
d.ewm(com=period-1, adjust=False).mean()
|
||||
return apply_log_if_necessary_series(100-100/(1+rs), "rsi")
|
||||
u[u.index[period - 1]] = np.mean(
|
||||
u[:period]
|
||||
) # first value is sum of avg gains u = u.drop(u.index[:(period-1)])
|
||||
d[d.index[period - 1]] = np.mean(
|
||||
d[:period]
|
||||
) # first value is sum of avg losses d = d.drop(d.index[:(period-1)])
|
||||
rs = (
|
||||
u.ewm(com=period - 1, adjust=False).mean()
|
||||
/ 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) -> pd.Series:
|
||||
returns = df['returns']
|
||||
returns = df["returns"]
|
||||
M = returns.diff(period - 1)
|
||||
N = returns.shift(period - 1)
|
||||
roc = pd.Series(((M / N) * 100), name = 'ROC_' + str(period))
|
||||
return apply_log_if_necessary_series(roc, "roc")
|
||||
roc = pd.Series(((M / N) * 100), name="ROC_" + str(period))
|
||||
return apply_log_if_necessary_series(roc, "roc")
|
||||
|
||||
@@ -3,13 +3,14 @@ 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) -> pd.Series:
|
||||
frac_diff = FracdiffStat(window = period)
|
||||
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)
|
||||
return pd.Series(result.squeeze(), index=df.index)
|
||||
|
||||
|
||||
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")
|
||||
|
||||
@@ -2,6 +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) -> pd.Series:
|
||||
close, low, high = get_close_low_high(df)
|
||||
|
||||
|
||||
@@ -6,4 +6,4 @@ 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']
|
||||
ScalerTypes = Literal["normalize", "minmax", "standardize", "none"]
|
||||
|
||||
@@ -2,10 +2,11 @@ import pandas as pd
|
||||
import numpy as np
|
||||
from scipy.stats import shapiro
|
||||
|
||||
|
||||
def get_close_low_high(df: pd.DataFrame) -> tuple[pd.Series, pd.Series, pd.Series]:
|
||||
close = df['close']
|
||||
low = df['low']
|
||||
high = df['high']
|
||||
close = df["close"]
|
||||
low = df["low"]
|
||||
high = df["high"]
|
||||
return close, low, high
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user