Skip to content

Commit

Permalink
add comment to multi_freq_handler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zhupr committed Sep 30, 2021
1 parent e8ae975 commit f58282e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
45 changes: 35 additions & 10 deletions examples/benchmarks/LightGBM/multi_freq_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,53 @@ def __init__(

def loader_config(self):

# features day
# Results for dataset: df: pd.DataFrame
# len(df.columns) == 6 + 6 * 16, len(df.index.get_level_values(level="datetime").unique()) == T
# df.columns: close0, close1, ..., close16, open0, ..., open16, ..., vwap16
# freq == day:
# close0, open0, low0, high0, volume0, vwap0
# freq == 1min:
# close1, ..., close16, ..., vwap1, ..., vwap16
# df.index.name == ["datetime", "instrument"]: pd.MultiIndex
# Example:
# feature ... label
# close0 open0 low0 ... vwap1 vwap16 LABEL0
# datetime instrument ...
# 2020-10-09 SH600000 11.794546 11.819587 11.769505 ... NaN NaN -0.005214
# 2020-10-15 SH600000 12.044961 11.944795 11.932274 ... NaN NaN -0.007202
# ... ... ... ... ... ... ... ...
# 2021-05-28 SZ300676 6.369684 6.495406 6.306568 ... NaN NaN -0.001321
# 2021-05-31 SZ300676 6.601626 6.465643 6.465130 ... NaN NaN -0.023428

# features day: len(columns) == 6
fields = ["$close", "$open", "$low", "$high", "$volume", "$vwap"]
# names: close0, open0, ..., vwap0
names = list(map(lambda x: x.strip("$") + "0", fields))

config = {"feature_day": (fields, names)}

# NOTE:
# Mean($close, 15) ==> df.rolling(15, min_periods=1).mean()
# Ref(Mean($close, 15), 15) ==> df.rolling(15, min_periods=1).mean().shift(15)
#
# features 15min:
# features 15min: len(columns) == 6 * 16
# time: 09:00 --> 09:14, ..., 14:45 --> 14:59
# fields: Ref(Mean($close, 15), 225), ..., Mean($close, 15)
# name: close1, ..., close16

# Expression description: take close as an example
# Mean($close, 15) ==> df["$close"].rolling(15, min_periods=1).mean()
# Ref(Mean($close, 15), 15) ==> df["$close"].rolling(15, min_periods=1).mean().shift(15)

# NOTE: The last data of each trading day, which is the average of the i-th 15 minutes

# Average:
# Average of the i-th 15-minute period of each trading day: 1 <= i <= 250 // 16
# Avg(15minutes): Ref(Mean($close, 15), 240 - i * 15)
# Average of the first 15 minutes of each trading day
# Avg(09:00 --> 09:14), df["09:14"]: Ref(Mean($close, 15), 240- i * 15) ==> Ref(Mean($close, 15), 225)
# Average of the last 15 minutes of each trading day
# Avg(14:45 --> 14:59), df["14:59"]: Ref(Mean($close, 15), 240 - 16 * 15) ==> Ref(Mean($close, 15), 0) ==> Mean($close, 15)
#
# Average of the first 15 minutes of each trading day; i = 1
# Avg(09:00 --> 09:14), df.index.loc["09:14"]: Ref(Mean($close, 15), 240- 1 * 15) ==> Ref(Mean($close, 15), 225)
# Average of the last 15 minutes of each trading day; i = 16
# Avg(14:45 --> 14:59), df.index.loc["14:59"]: Ref(Mean($close, 15), 240 - 16 * 15) ==> Ref(Mean($close, 15), 0) ==> Mean($close, 15)

# 15min resample to day
# df.resample("1d").last()
tmp_fields = []
tmp_names = []
for i, _f in enumerate(fields):
Expand Down
3 changes: 2 additions & 1 deletion qlib/workflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ def run():


if __name__ == "__main__":
run()
workflow(r"E:\code\python\microsoft\qlib_latest\qlib\examples\benchmarks\LightGBM\workflow_config_lightgbm_Alpha158_multi_freq_debug.yaml")
# run()

0 comments on commit f58282e

Please sign in to comment.