-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoutput_polars.py
479 lines (454 loc) · 12.7 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# this code is auto generated by the expr_codegen
# https://github.com/wukan1986/expr_codegen
# 此段代码由 expr_codegen 自动生成,欢迎提交 issue 或 pull request
import numpy as np # noqa
import pandas as pd # noqa
import polars as pl # noqa
import polars.selectors as cs # noqa
from loguru import logger # noqa
# ===================================
# 导入优先级,例如:ts_RSI在ta与talib中都出现了,优先使用ta
# 运行时,后导入覆盖前导入,但IDE智能提示是显示先导入的
_ = 0 # 只要之前出现了语句,之后的import位置不参与调整
# from polars_ta.prefix.talib import * # noqa
from polars_ta.prefix.tdx import * # noqa
from polars_ta.prefix.ta import * # noqa
from polars_ta.prefix.wq import * # noqa
from polars_ta.prefix.cdl import * # noqa
# ===================================
_ = (
"CLOSE",
"HIGH",
"LOW",
"OPEN",
"SMA_005",
"SMA_010",
"SMA_020",
"IMAX_005",
"IMIN_005",
"IMAX_010",
"IMIN_010",
"IMAX_020",
"IMIN_020",
"IMAX_060",
"IMIN_060",
)
(
CLOSE,
HIGH,
LOW,
OPEN,
SMA_005,
SMA_010,
SMA_020,
IMAX_005,
IMIN_005,
IMAX_010,
IMIN_010,
IMAX_020,
IMIN_020,
IMAX_060,
IMIN_060,
) = (pl.col(i) for i in _)
_ = (
"_x_0",
"ROCP_001",
"ROCP_003",
"ROCP_005",
"ROCP_010",
"ROCP_020",
"ROCP_060",
"ROCP_120",
"TS_RANK_005",
"TS_RANK_010",
"TS_RANK_020",
"TS_RANK_060",
"TS_RANK_120",
"TS_SCALE_005",
"TS_SCALE_010",
"TS_SCALE_020",
"TS_SCALE_060",
"TS_SCALE_120",
"RSI_006",
"RSI_012",
"RSI_024",
"PSY_010",
"PSY_020",
"PPO_12_26",
"IMAX_120",
"IMIN_120",
"RSV_005",
"RSV_010",
"RSV_020",
"RSV_060",
"WILLR_006",
"WILLR_010",
"NATR_006",
"NATR_012",
"NATR_024",
"ADX_014",
"ADX_021",
"AR_010",
"AR_020",
"HHV_005",
"HHV_010",
"HHV_020",
"HHV_060",
"HHV_120",
"LLV_005",
"LLV_010",
"LLV_020",
"LLV_060",
"LLV_120",
"SMA_060",
"SMA_120",
"STD_005",
"STD_010",
"STD_020",
"STD_060",
"STD_120",
"IMXD_005",
"IMXD_010",
"IMXD_020",
"IMXD_060",
"SMA_001_005",
"SMA_005_010",
"SMA_010_020",
)
(
_x_0,
ROCP_001,
ROCP_003,
ROCP_005,
ROCP_010,
ROCP_020,
ROCP_060,
ROCP_120,
TS_RANK_005,
TS_RANK_010,
TS_RANK_020,
TS_RANK_060,
TS_RANK_120,
TS_SCALE_005,
TS_SCALE_010,
TS_SCALE_020,
TS_SCALE_060,
TS_SCALE_120,
RSI_006,
RSI_012,
RSI_024,
PSY_010,
PSY_020,
PPO_12_26,
IMAX_120,
IMIN_120,
RSV_005,
RSV_010,
RSV_020,
RSV_060,
WILLR_006,
WILLR_010,
NATR_006,
NATR_012,
NATR_024,
ADX_014,
ADX_021,
AR_010,
AR_020,
HHV_005,
HHV_010,
HHV_020,
HHV_060,
HHV_120,
LLV_005,
LLV_010,
LLV_020,
LLV_060,
LLV_120,
SMA_060,
SMA_120,
STD_005,
STD_010,
STD_020,
STD_060,
STD_120,
IMXD_005,
IMXD_010,
IMXD_020,
IMXD_060,
SMA_001_005,
SMA_005_010,
SMA_010_020,
) = (pl.col(i) for i in _)
_DATE_ = "date"
_ASSET_ = "asset"
def func_0_cl(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
_x_0=1 / CLOSE,
)
return df
def func_0_ts__asset(df: pl.DataFrame) -> pl.DataFrame:
df = df.sort(_DATE_)
# ========================================
df = df.with_columns(
ROCP_001=ts_returns(CLOSE, 1),
ROCP_003=ts_returns(CLOSE, 3),
ROCP_005=ts_returns(CLOSE, 5),
ROCP_010=ts_returns(CLOSE, 10),
ROCP_020=ts_returns(CLOSE, 20),
ROCP_060=ts_returns(CLOSE, 60),
ROCP_120=ts_returns(CLOSE, 120),
TS_RANK_005=ts_rank(CLOSE, 5),
TS_RANK_010=ts_rank(CLOSE, 10),
TS_RANK_020=ts_rank(CLOSE, 20),
TS_RANK_060=ts_rank(CLOSE, 60),
TS_RANK_120=ts_rank(CLOSE, 120),
TS_SCALE_005=ts_scale(CLOSE, 5),
TS_SCALE_010=ts_scale(CLOSE, 10),
TS_SCALE_020=ts_scale(CLOSE, 20),
TS_SCALE_060=ts_scale(CLOSE, 60),
TS_SCALE_120=ts_scale(CLOSE, 120),
RSI_006=ts_RSI(CLOSE, 6),
RSI_012=ts_RSI(CLOSE, 12),
RSI_024=ts_RSI(CLOSE, 24),
PSY_010=ts_PSY(CLOSE, 10),
PSY_020=ts_PSY(CLOSE, 20),
PPO_12_26=ts_PPO(CLOSE, 12, 26),
IMAX_005=ts_arg_max(HIGH, 5) / 5,
IMAX_010=ts_arg_max(HIGH, 10) / 10,
IMAX_020=ts_arg_max(HIGH, 20) / 20,
IMAX_060=ts_arg_max(HIGH, 60) / 60,
IMAX_120=ts_arg_max(HIGH, 120) / 120,
IMIN_005=ts_arg_min(LOW, 5) / 5,
IMIN_010=ts_arg_min(LOW, 10) / 10,
IMIN_020=ts_arg_min(LOW, 20) / 20,
IMIN_060=ts_arg_min(LOW, 60) / 60,
IMIN_120=ts_arg_min(LOW, 120) / 120,
RSV_005=ts_RSV(HIGH, LOW, CLOSE, 5),
RSV_010=ts_RSV(HIGH, LOW, CLOSE, 10),
RSV_020=ts_RSV(HIGH, LOW, CLOSE, 20),
RSV_060=ts_RSV(HIGH, LOW, CLOSE, 60),
WILLR_006=ts_WILLR(HIGH, LOW, CLOSE, 6),
WILLR_010=ts_WILLR(HIGH, LOW, CLOSE, 10),
NATR_006=ts_NATR(HIGH, LOW, CLOSE, 6),
NATR_012=ts_NATR(HIGH, LOW, CLOSE, 12),
NATR_024=ts_NATR(HIGH, LOW, CLOSE, 24),
ADX_014=ts_ADX(HIGH, LOW, CLOSE, 14),
ADX_021=ts_ADX(HIGH, LOW, CLOSE, 21),
AR_010=ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 10),
AR_020=ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 20),
)
# ========================================
df = df.with_columns(
HHV_005=_x_0 * ts_max(HIGH, 5),
HHV_010=_x_0 * ts_max(HIGH, 10),
HHV_020=_x_0 * ts_max(HIGH, 20),
HHV_060=_x_0 * ts_max(HIGH, 60),
HHV_120=_x_0 * ts_max(HIGH, 120),
LLV_005=_x_0 * ts_min(LOW, 5),
LLV_010=_x_0 * ts_min(LOW, 10),
LLV_020=_x_0 * ts_min(LOW, 20),
LLV_060=_x_0 * ts_min(LOW, 60),
LLV_120=_x_0 * ts_min(LOW, 120),
SMA_005=_x_0 * ts_mean(CLOSE, 5),
SMA_010=_x_0 * ts_mean(CLOSE, 10),
SMA_020=_x_0 * ts_mean(CLOSE, 20),
SMA_060=_x_0 * ts_mean(CLOSE, 60),
SMA_120=_x_0 * ts_mean(CLOSE, 120),
STD_005=_x_0 * ts_std_dev(CLOSE, 5),
STD_010=_x_0 * ts_std_dev(CLOSE, 10),
STD_020=_x_0 * ts_std_dev(CLOSE, 20),
STD_060=_x_0 * ts_std_dev(CLOSE, 60),
STD_120=_x_0 * ts_std_dev(CLOSE, 120),
)
return df
def func_1_cl(df: pl.DataFrame) -> pl.DataFrame:
# ========================================
df = df.with_columns(
IMXD_005=IMAX_005 - IMIN_005,
IMXD_010=IMAX_010 - IMIN_010,
IMXD_020=IMAX_020 - IMIN_020,
IMXD_060=IMAX_060 - IMIN_060,
)
# ========================================
df = df.with_columns(
SMA_001_005=CLOSE / SMA_005,
SMA_005_010=SMA_005 / SMA_010,
SMA_010_020=SMA_010 / SMA_020,
)
return df
"""
#========================================func_0_cl
_x_0 = 1/CLOSE
#========================================func_0_ts__asset
ROCP_001 = ts_returns(CLOSE, 1)
ROCP_003 = ts_returns(CLOSE, 3)
ROCP_005 = ts_returns(CLOSE, 5)
ROCP_010 = ts_returns(CLOSE, 10)
ROCP_020 = ts_returns(CLOSE, 20)
ROCP_060 = ts_returns(CLOSE, 60)
ROCP_120 = ts_returns(CLOSE, 120)
TS_RANK_005 = ts_rank(CLOSE, 5)
TS_RANK_010 = ts_rank(CLOSE, 10)
TS_RANK_020 = ts_rank(CLOSE, 20)
TS_RANK_060 = ts_rank(CLOSE, 60)
TS_RANK_120 = ts_rank(CLOSE, 120)
TS_SCALE_005 = ts_scale(CLOSE, 5)
TS_SCALE_010 = ts_scale(CLOSE, 10)
TS_SCALE_020 = ts_scale(CLOSE, 20)
TS_SCALE_060 = ts_scale(CLOSE, 60)
TS_SCALE_120 = ts_scale(CLOSE, 120)
RSI_006 = ts_RSI(CLOSE, 6)
RSI_012 = ts_RSI(CLOSE, 12)
RSI_024 = ts_RSI(CLOSE, 24)
PSY_010 = ts_PSY(CLOSE, 10)
PSY_020 = ts_PSY(CLOSE, 20)
PPO_12_26 = ts_PPO(CLOSE, 12, 26)
IMAX_005 = ts_arg_max(HIGH, 5)/5
IMAX_010 = ts_arg_max(HIGH, 10)/10
IMAX_020 = ts_arg_max(HIGH, 20)/20
IMAX_060 = ts_arg_max(HIGH, 60)/60
IMAX_120 = ts_arg_max(HIGH, 120)/120
IMIN_005 = ts_arg_min(LOW, 5)/5
IMIN_010 = ts_arg_min(LOW, 10)/10
IMIN_020 = ts_arg_min(LOW, 20)/20
IMIN_060 = ts_arg_min(LOW, 60)/60
IMIN_120 = ts_arg_min(LOW, 120)/120
RSV_005 = ts_RSV(HIGH, LOW, CLOSE, 5)
RSV_010 = ts_RSV(HIGH, LOW, CLOSE, 10)
RSV_020 = ts_RSV(HIGH, LOW, CLOSE, 20)
RSV_060 = ts_RSV(HIGH, LOW, CLOSE, 60)
WILLR_006 = ts_WILLR(HIGH, LOW, CLOSE, 6)
WILLR_010 = ts_WILLR(HIGH, LOW, CLOSE, 10)
NATR_006 = ts_NATR(HIGH, LOW, CLOSE, 6)
NATR_012 = ts_NATR(HIGH, LOW, CLOSE, 12)
NATR_024 = ts_NATR(HIGH, LOW, CLOSE, 24)
ADX_014 = ts_ADX(HIGH, LOW, CLOSE, 14)
ADX_021 = ts_ADX(HIGH, LOW, CLOSE, 21)
AR_010 = ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 10)
AR_020 = ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 20)
#========================================func_0_ts__asset
HHV_005 = _x_0*ts_max(HIGH, 5)
HHV_010 = _x_0*ts_max(HIGH, 10)
HHV_020 = _x_0*ts_max(HIGH, 20)
HHV_060 = _x_0*ts_max(HIGH, 60)
HHV_120 = _x_0*ts_max(HIGH, 120)
LLV_005 = _x_0*ts_min(LOW, 5)
LLV_010 = _x_0*ts_min(LOW, 10)
LLV_020 = _x_0*ts_min(LOW, 20)
LLV_060 = _x_0*ts_min(LOW, 60)
LLV_120 = _x_0*ts_min(LOW, 120)
SMA_005 = _x_0*ts_mean(CLOSE, 5)
SMA_010 = _x_0*ts_mean(CLOSE, 10)
SMA_020 = _x_0*ts_mean(CLOSE, 20)
SMA_060 = _x_0*ts_mean(CLOSE, 60)
SMA_120 = _x_0*ts_mean(CLOSE, 120)
STD_005 = _x_0*ts_std_dev(CLOSE, 5)
STD_010 = _x_0*ts_std_dev(CLOSE, 10)
STD_020 = _x_0*ts_std_dev(CLOSE, 20)
STD_060 = _x_0*ts_std_dev(CLOSE, 60)
STD_120 = _x_0*ts_std_dev(CLOSE, 120)
#========================================func_1_cl
IMXD_005 = IMAX_005 - IMIN_005
IMXD_010 = IMAX_010 - IMIN_010
IMXD_020 = IMAX_020 - IMIN_020
IMXD_060 = IMAX_060 - IMIN_060
#========================================func_1_cl
SMA_001_005 = CLOSE/SMA_005
SMA_005_010 = SMA_005/SMA_010
SMA_010_020 = SMA_010/SMA_020
"""
"""
HHV_005 = ts_max(HIGH, 5)/CLOSE
HHV_010 = ts_max(HIGH, 10)/CLOSE
HHV_020 = ts_max(HIGH, 20)/CLOSE
HHV_060 = ts_max(HIGH, 60)/CLOSE
HHV_120 = ts_max(HIGH, 120)/CLOSE
LLV_005 = ts_min(LOW, 5)/CLOSE
LLV_010 = ts_min(LOW, 10)/CLOSE
LLV_020 = ts_min(LOW, 20)/CLOSE
LLV_060 = ts_min(LOW, 60)/CLOSE
LLV_120 = ts_min(LOW, 120)/CLOSE
SMA_005 = ts_mean(CLOSE, 5)/CLOSE
SMA_010 = ts_mean(CLOSE, 10)/CLOSE
SMA_020 = ts_mean(CLOSE, 20)/CLOSE
SMA_060 = ts_mean(CLOSE, 60)/CLOSE
SMA_120 = ts_mean(CLOSE, 120)/CLOSE
STD_005 = ts_std_dev(CLOSE, 5)/CLOSE
STD_010 = ts_std_dev(CLOSE, 10)/CLOSE
STD_020 = ts_std_dev(CLOSE, 20)/CLOSE
STD_060 = ts_std_dev(CLOSE, 60)/CLOSE
STD_120 = ts_std_dev(CLOSE, 120)/CLOSE
ROCP_001 = ts_returns(CLOSE, 1)
ROCP_003 = ts_returns(CLOSE, 3)
ROCP_005 = ts_returns(CLOSE, 5)
ROCP_010 = ts_returns(CLOSE, 10)
ROCP_020 = ts_returns(CLOSE, 20)
ROCP_060 = ts_returns(CLOSE, 60)
ROCP_120 = ts_returns(CLOSE, 120)
TS_RANK_005 = ts_rank(CLOSE, 5)
TS_RANK_010 = ts_rank(CLOSE, 10)
TS_RANK_020 = ts_rank(CLOSE, 20)
TS_RANK_060 = ts_rank(CLOSE, 60)
TS_RANK_120 = ts_rank(CLOSE, 120)
TS_SCALE_005 = ts_scale(CLOSE, 5)
TS_SCALE_010 = ts_scale(CLOSE, 10)
TS_SCALE_020 = ts_scale(CLOSE, 20)
TS_SCALE_060 = ts_scale(CLOSE, 60)
TS_SCALE_120 = ts_scale(CLOSE, 120)
IMAX_005 = ts_arg_max(HIGH, 5)/5
IMAX_010 = ts_arg_max(HIGH, 10)/10
IMAX_020 = ts_arg_max(HIGH, 20)/20
IMAX_060 = ts_arg_max(HIGH, 60)/60
IMAX_120 = ts_arg_max(HIGH, 120)/120
IMIN_005 = ts_arg_min(LOW, 5)/5
IMIN_010 = ts_arg_min(LOW, 10)/10
IMIN_020 = ts_arg_min(LOW, 20)/20
IMIN_060 = ts_arg_min(LOW, 60)/60
IMIN_120 = ts_arg_min(LOW, 120)/120
RSI_006 = ts_RSI(CLOSE, 6)
RSI_012 = ts_RSI(CLOSE, 12)
RSI_024 = ts_RSI(CLOSE, 24)
RSV_005 = ts_RSV(HIGH, LOW, CLOSE, 5)
RSV_010 = ts_RSV(HIGH, LOW, CLOSE, 10)
RSV_020 = ts_RSV(HIGH, LOW, CLOSE, 20)
RSV_060 = ts_RSV(HIGH, LOW, CLOSE, 60)
WILLR_006 = ts_WILLR(HIGH, LOW, CLOSE, 6)
WILLR_010 = ts_WILLR(HIGH, LOW, CLOSE, 10)
NATR_006 = ts_NATR(HIGH, LOW, CLOSE, 6)
NATR_012 = ts_NATR(HIGH, LOW, CLOSE, 12)
NATR_024 = ts_NATR(HIGH, LOW, CLOSE, 24)
ADX_014 = ts_ADX(HIGH, LOW, CLOSE, 14)
ADX_021 = ts_ADX(HIGH, LOW, CLOSE, 21)
AR_010 = ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 10)
AR_020 = ts_BRAR_AR(OPEN, HIGH, LOW, CLOSE, 20)
PSY_010 = ts_PSY(CLOSE, 10)
PSY_020 = ts_PSY(CLOSE, 20)
PPO_12_26 = ts_PPO(CLOSE, 12, 26)
SMA_001_005 = CLOSE/SMA_005
SMA_005_010 = SMA_005/SMA_010
SMA_010_020 = SMA_010/SMA_020
IMXD_005 = IMAX_005 - IMIN_005
IMXD_010 = IMAX_010 - IMIN_010
IMXD_020 = IMAX_020 - IMIN_020
IMXD_060 = IMAX_060 - IMIN_060
"""
def main(df: pl.DataFrame) -> pl.DataFrame:
# logger.info("start...")
df = func_0_cl(df).drop(*[])
df = df.sort(_ASSET_, _DATE_).group_by(_ASSET_).map_groups(func_0_ts__asset).drop(*["_x_0"])
df = func_1_cl(df).drop(*[])
# drop intermediate columns
# df = df.select(pl.exclude(r'^_x_\d+$'))
df = df.select(~cs.starts_with("_"))
# shrink
df = df.select(cs.all().shrink_dtype())
df = df.shrink_to_fit()
# logger.info('done')
# save
# df.write_parquet('output.parquet')
return df
if __name__ in ("__main__", "builtins"):
# TODO: 数据加载或外部传入
df_output = main(df_input)