forked from wukan1986/expr_codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_polars.py
153 lines (125 loc) · 4.83 KB
/
output_polars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# this code is auto generated by the expr_codegen
# https://github.com/wukan1986/expr_codegen
import re
import numpy as np
import polars as pl
import polars.selectors as cs
from loguru import logger
# TODO: load data
df = pl.DataFrame()
def expr_rank_pct(expr: pl.Expr) -> pl.Expr:
"""rank(pct=True)"""
return expr.rank() / (expr.len() - expr.null_count())
def func_0_cs__date(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
# x_6 = cs_rank(OPEN)
x_6=(expr_rank_pct(pl.col("OPEN"))),
)
return df
def func_0_gp__date__sw_l1(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
# x_5 = gp_rank(sw_l1, CLOSE)
x_5=(expr_rank_pct(pl.col("CLOSE"))),
)
return df
def func_0_ts__asset__date(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
# x_0 = ts_mean(OPEN, 10)
x_0=(pl.col("OPEN").rolling_mean(10)),
# expr_6 = ts_delta(OPEN, 10)
expr_6=(pl.col("OPEN").diff(10)),
# expr_7 = ts_delta(OPEN + 1, 10)
expr_7=((pl.col("OPEN") + 1).diff(10)),
# x_1 = ts_mean(CLOSE, 10)
x_1=(pl.col("CLOSE").rolling_mean(10)),
# expr_5 = -ts_corr(OPEN, CLOSE, 10)
expr_5=(-pl.rolling_corr(pl.col("OPEN"), pl.col("CLOSE"), window_size=10)),
)
# ========================================
df = df.with_columns(
# x_7 = ts_mean(x_6, 10)
x_7=(pl.col("x_6").rolling_mean(10)),
)
# ========================================
df = df.with_columns(
# x_4 = abs(log(x_1))
x_4=(pl.col("x_1").log().abs()),
)
return df
def func_1_cs__date(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
# x_2 = cs_rank(x_0)
x_2=(expr_rank_pct(pl.col("x_0"))),
# x_3 = cs_rank(x_1)
x_3=(expr_rank_pct(pl.col("x_1"))),
)
# ========================================
df = df.with_columns(
# expr_4 = cs_rank(x_7)
expr_4=(expr_rank_pct(pl.col("x_7"))),
)
# ========================================
df = df.with_columns(
# expr_2 = x_2 - x_4 + x_5
expr_2=(pl.col("x_2") - pl.col("x_4") + pl.col("x_5")),
)
return df
def func_2_ts__asset__date(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
# expr_3 = ts_mean(x_2, 10)
expr_3=(pl.col("x_2").rolling_mean(10)),
# expr_1 = -ts_corr(x_2, x_3, 10)
expr_1=(-pl.rolling_corr(pl.col("x_2"), pl.col("x_3"), window_size=10)),
)
return df
logger.info("start...")
df = df.sort(by=["date"]).groupby(by=["date"], maintain_order=False).apply(func_0_cs__date)
df = df.sort(by=["date", "sw_l1"]).groupby(by=["date", "sw_l1"], maintain_order=False).apply(func_0_gp__date__sw_l1)
df = df.sort(by=["asset", "date"]).groupby(by=["asset"], maintain_order=True).apply(func_0_ts__asset__date)
df = df.sort(by=["date"]).groupby(by=["date"], maintain_order=False).apply(func_1_cs__date)
df = df.sort(by=["asset", "date"]).groupby(by=["asset"], maintain_order=True).apply(func_2_ts__asset__date)
# #========================================func_0_cs__date
# x_6 = cs_rank(OPEN)
# #========================================func_0_gp__date__sw_l1
# x_5 = gp_rank(sw_l1, CLOSE)
# #========================================func_0_ts__asset__date
# x_0 = ts_mean(OPEN, 10)
# expr_6 = ts_delta(OPEN, 10)
# expr_7 = ts_delta(OPEN + 1, 10)
# x_1 = ts_mean(CLOSE, 10)
# expr_5 = -ts_corr(OPEN, CLOSE, 10)
# #========================================func_0_ts__asset__date
# x_7 = ts_mean(x_6, 10)
# #========================================func_0_ts__asset__date
# x_4 = abs(log(x_1))
# #========================================func_1_cs__date
# x_2 = cs_rank(x_0)
# x_3 = cs_rank(x_1)
# #========================================func_1_cs__date
# expr_4 = cs_rank(x_7)
# #========================================func_1_cs__date
# expr_2 = x_2 - x_4 + x_5
# #========================================func_2_ts__asset__date
# expr_3 = ts_mean(x_2, 10)
# expr_1 = -ts_corr(x_2, x_3, 10)
# expr_1 = -ts_corr(cs_rank(ts_mean(OPEN, 10)), cs_rank(ts_mean(CLOSE, 10)), 10)
# expr_2 = -abs(log(ts_mean(CLOSE, 10))) + cs_rank(ts_mean(OPEN, 10)) + gp_rank(sw_l1, CLOSE)
# expr_3 = ts_mean(cs_rank(ts_mean(OPEN, 10)), 10)
# expr_4 = cs_rank(ts_mean(cs_rank(OPEN), 10))
# expr_5 = -ts_corr(OPEN, CLOSE, 10)
# expr_6 = ts_delta(OPEN, 10)
# expr_7 = ts_delta(OPEN + 1, 10)
# drop intermediate columns
df = df.drop(columns=filter(lambda x: re.search(r"^x_\d+", x), df.columns))
# shrink
df = df.select(cs.all().shrink_dtype())
df = df.shrink_to_fit()
logger.info("done")
# save
# df.write_parquet('output.parquet', compression='zstd')
print(df.tail(5))