polars-ta 0.5.4__py3-none-any.whl → 0.5.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
polars_ta/_version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.5.4"
1
+ __version__ = "0.5.5"
@@ -0,0 +1,17 @@
1
+ # this code is auto generated by tools/prefix_vec.py
2
+
3
+ from polars_ta.wq.vector import vec_avg as cs_vec_avg # noqa
4
+ from polars_ta.wq.vector import vec_choose as cs_vec_choose # noqa
5
+ from polars_ta.wq.vector import vec_count as cs_vec_count # noqa
6
+ from polars_ta.wq.vector import vec_ir as cs_vec_ir # noqa
7
+ from polars_ta.wq.vector import vec_kurtosis as cs_vec_kurtosis # noqa
8
+ from polars_ta.wq.vector import vec_l2_norm as cs_vec_l2_norm # noqa
9
+ from polars_ta.wq.vector import vec_max as cs_vec_max # noqa
10
+ from polars_ta.wq.vector import vec_min as cs_vec_min # noqa
11
+ from polars_ta.wq.vector import vec_norm as cs_vec_norm # noqa
12
+ from polars_ta.wq.vector import vec_percentage as cs_vec_percentage # noqa
13
+ from polars_ta.wq.vector import vec_powersum as cs_vec_powersum # noqa
14
+ from polars_ta.wq.vector import vec_range as cs_vec_range # noqa
15
+ from polars_ta.wq.vector import vec_skewness as cs_vec_skewness # noqa
16
+ from polars_ta.wq.vector import vec_stddev as cs_vec_stddev # noqa
17
+ from polars_ta.wq.vector import vec_sum as cs_vec_sum # noqa
polars_ta/ta/overlap.py CHANGED
@@ -39,7 +39,7 @@ def EMA(close: Expr, timeperiod: int = 30) -> Expr:
39
39
 
40
40
  """
41
41
  # 相当于alpha=2/(1+timeperiod)
42
- return close.ewm_mean(span=timeperiod, adjust=False, min_periods=timeperiod)
42
+ return close.ewm_mean(span=timeperiod, adjust=False, min_samples=timeperiod)
43
43
 
44
44
 
45
45
  def KAMA(close: Expr, timeperiod: int = 30) -> Expr:
@@ -78,7 +78,7 @@ def RMA(close: Expr, timeperiod: int = 30) -> Expr:
78
78
  https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/rma.py
79
79
 
80
80
  """
81
- return close.ewm_mean(alpha=1 / timeperiod, adjust=False, min_periods=timeperiod)
81
+ return close.ewm_mean(alpha=1 / timeperiod, adjust=False, min_samples=timeperiod)
82
82
 
83
83
 
84
84
  def TEMA(close: Expr, timeperiod: int = 30) -> Expr:
@@ -85,7 +85,7 @@ def DMA(close: Expr, alpha: float = 0.5) -> Expr:
85
85
 
86
86
  求X的动态移动平均.
87
87
  算法:Y=A*X+(1-A)*Y',其中Y'表示上一周期Y值,A必须大于0且小于1.A支持变量"""
88
- return close.ewm_mean(alpha=alpha, adjust=False, min_periods=1)
88
+ return close.ewm_mean(alpha=alpha, adjust=False, min_samples=1)
89
89
 
90
90
 
91
91
  def EMA(close: Expr, N: int = 30) -> Expr:
@@ -113,7 +113,7 @@ def EXPMEMA(close: Expr, N: int = 30) -> Expr:
113
113
  """
114
114
  sma = MA(close, N)
115
115
  x = when(close.cum_count() < N).then(sma).otherwise(close)
116
- return x.ewm_mean(span=N, adjust=False, min_periods=1)
116
+ return x.ewm_mean(span=N, adjust=False, min_samples=1)
117
117
 
118
118
 
119
119
  def HOD(close: Expr, N: int = 30) -> Expr:
@@ -156,7 +156,7 @@ def SMA_CN(X: Expr, N: int, M: int) -> Expr:
156
156
 
157
157
  !!!为防止与talib版SMA误用,这里去了默认值1
158
158
  """
159
- return X.ewm_mean(alpha=M / N, adjust=False, min_periods=1)
159
+ return X.ewm_mean(alpha=M / N, adjust=False, min_samples=1)
160
160
 
161
161
 
162
162
  def SUMIF(condition: Expr, close: Expr, N: int = 30) -> Expr:
@@ -0,0 +1,44 @@
1
+ import re
2
+
3
+ import polars as pl
4
+
5
+
6
+ def with_industry(df: pl.DataFrame, industry_name: str, drop_first: bool, keep_col: bool) -> pl.DataFrame:
7
+ """添加行业哑元变量
8
+
9
+ Parameters
10
+ ----------
11
+ df
12
+ industry_name:str
13
+ 行业列名
14
+ drop_first
15
+ 丢弃第一列
16
+ keep_col
17
+ 是否保留源列
18
+
19
+ Returns
20
+ -------
21
+ pl.DataFrame
22
+
23
+ """
24
+ df = df.with_columns([
25
+ # 行业处理,由浮点改成整数
26
+ pl.col(industry_name).cast(pl.UInt32),
27
+ ])
28
+
29
+ # TODO 没有行业的也过滤,这会不会有问题?已经退市了,但引入了未来数据
30
+ df = df.filter(
31
+ pl.col(industry_name).is_not_null(),
32
+ )
33
+
34
+ if keep_col:
35
+ df = df.with_columns(df.to_dummies(industry_name, drop_first=False))
36
+ else:
37
+ df = df.to_dummies(industry_name, drop_first=False)
38
+
39
+ if drop_first:
40
+ # drop_first丢弃哪个字段是随机的,非常不友好,只能在行业中性化时动态修改代码
41
+ industry_columns = sorted(filter(lambda x: re.search(rf"^{industry_name}_\d+$", x), df.columns))
42
+ return df.drop(industry_columns[0])
43
+ else:
44
+ return df
@@ -56,7 +56,9 @@ def cs_one_side(x: Expr, is_long: bool = True) -> Expr:
56
56
 
57
57
 
58
58
  def cs_scale(x: Expr, scale_: float = 1, long_scale: float = 1, short_scale: float = 1) -> Expr:
59
- """横截面上,将输入数据进行比例调整。此外,可通过向运算符添加额外参数,将多头头寸和空头头寸分别映射到独立的缩放比例上
59
+ """横截面上,将输入数据进行比例调整
60
+
61
+ 此外,可通过向运算符添加额外参数,将多头头寸和空头头寸分别映射到独立的缩放比例上
60
62
 
61
63
  Scales input to booksize. We can also scale the long positions and short positions to separate scales by mentioning additional parameters to the operator.
62
64
 
@@ -211,6 +213,16 @@ def cs_fill_mean(x: Expr) -> Expr:
211
213
  return x.fill_null(strategy='mean')
212
214
 
213
215
 
216
+ def cs_fill_max(x: Expr) -> Expr:
217
+ """横截面上,填充`null`为最大值"""
218
+ return x.fill_null(strategy='max')
219
+
220
+
221
+ def cs_fill_min(x: Expr) -> Expr:
222
+ """横截面上,填充`null`为最小值"""
223
+ return x.fill_null(strategy='min')
224
+
225
+
214
226
  def cs_fill_null(x: Expr, value: float = 0) -> Expr:
215
227
  """横截面上,填充`null`为`value`"""
216
228
  return x.fill_null(value)
@@ -227,7 +239,7 @@ def cs_regression_proj(y: Expr, x: Expr) -> Expr:
227
239
 
228
240
 
229
241
  def cs_rank(x: Expr, pct: bool = True) -> Expr:
230
- """横截面排名。
242
+ """横截面排名
231
243
 
232
244
  Ranks the input among all the instruments and returns an equally distributed number between 0.0 and 1.0. For precise sort, use the rate as 0.
233
245
 
@@ -1,9 +1,18 @@
1
+ """
2
+ 补空值 → 去极值 → 标准化 → 中性化 → 标准化(可选二次标准化)
3
+
4
+ # 对数市值。去极值
5
+ MC_LOG = cs_quantile(log1p(market_cap), 0.01, 0.99)
6
+ # 对数市值。标准化。供其他因子市值中性化时使用
7
+ MC_NORM = cs_zscore(MC_LOG)
8
+ # 对数市值。行业中性化。直接作为因子使用
9
+ MC_NEUT = cs_zscore(cs_resid(MC_NORM, CS_SW_L1, ONE))
10
+
11
+ """
1
12
  import polars_ols as pls
2
13
  from polars import Expr, when
3
14
  from polars_ols.least_squares import OLSKwargs
4
15
 
5
- from polars_ta.wq.cross_sectional import cs_rank
6
-
7
16
 
8
17
  # ======================
9
18
  # standardize
@@ -23,7 +32,7 @@ def cs_minmax(x: Expr) -> Expr:
23
32
 
24
33
  # ======================
25
34
  # winsorize
26
- def cs_quantile(x: Expr, low_limit: float = 0.025, up_limit: float = 0.995) -> Expr:
35
+ def cs_quantile(x: Expr, low_limit: float = 0.025, up_limit: float = 0.975) -> Expr:
27
36
  """横截面分位数去极值"""
28
37
  a = x.quantile(low_limit)
29
38
  b = x.quantile(up_limit)
@@ -81,6 +90,11 @@ def cs_resid(y: Expr, *more_x: Expr) -> Expr:
81
90
  return pls.compute_least_squares(y, *more_x, mode='residuals', ols_kwargs=_ols_kwargs)
82
91
 
83
92
 
93
+ def cs_zscore_resid(y: Expr, *more_x: Expr) -> Expr:
94
+ """横截面标准化、中性化"""
95
+ return cs_resid(cs_zscore(y), *more_x)
96
+
97
+
84
98
  def cs_mad_zscore(y: Expr) -> Expr:
85
99
  """横截面去极值、标准化"""
86
100
  return cs_zscore(cs_mad(y))
@@ -91,27 +105,6 @@ def cs_mad_zscore_resid(y: Expr, *more_x: Expr) -> Expr:
91
105
  return cs_resid(cs_zscore(cs_mad(y)), *more_x)
92
106
 
93
107
 
94
- def cs_mad_rank(y: Expr) -> Expr:
95
- """横截面去极值、排名"""
96
- return cs_rank(cs_mad(y))
97
-
98
-
99
- def cs_mad_rank2(y: Expr, m: float) -> Expr:
100
- """横截面去极值,排名,移动峰或谷到零点,然后平方。非线性处理
101
-
102
- 适合于分层收益V型或倒V的情况"""
103
- return (cs_rank(cs_mad(y)) - m) ** 2
104
-
105
-
106
- def cs_mad_rank2_resid(y: Expr, m: float, *more_x: Expr) -> Expr:
107
- """横截面去极值,排名,移动峰或谷到零点,然后平方。回归取残差。非线性处理
108
-
109
- 适合于分层收益V型或倒V的情况"""
110
- return cs_resid((cs_rank(cs_mad(y)) - m) ** 2, *more_x)
111
-
112
-
113
- def cs_rank2(y: Expr, m: float) -> Expr:
114
- """横截面移动峰或谷到零点,然后平方。非线性处理
115
-
116
- 适合于分层收益V型或倒V的情况"""
117
- return (cs_rank(y) - m) ** 2
108
+ def cs_mad_zscore_resid_zscore(y: Expr, *more_x: Expr) -> Expr:
109
+ """横截面去极值、标准化、中性化、二次标准化"""
110
+ return cs_zscore(cs_resid(cs_zscore(cs_mad(y)), *more_x))
@@ -12,7 +12,9 @@ from polars_ta.wq._nb import roll_argmax, roll_argmin, roll_co_kurtosis, roll_co
12
12
 
13
13
 
14
14
  def ts_arg_max(x: Expr, d: int = 5, reverse: bool = True, min_samples: Optional[int] = None) -> Expr:
15
- """窗口内最大值出现的相对位置,最近的一天记为第 0 天,最远的一天为第 d-1 天
15
+ """最大值相对位置
16
+
17
+ 最近的一天记为第 0 天,最远的一天为第 d-1 天
16
18
 
17
19
  Returns the relative index of the max value in the time series for the past d days.
18
20
  If the current day has the max value for the past d days, it returns 0.
@@ -63,7 +65,9 @@ def ts_arg_max(x: Expr, d: int = 5, reverse: bool = True, min_samples: Optional[
63
65
 
64
66
 
65
67
  def ts_arg_min(x: Expr, d: int = 5, reverse: bool = True, min_samples: Optional[int] = None) -> Expr:
66
- """窗口内最小值出现的相对位置,最近的一天记为第 0 天,最远的一天为第 d-1 天
68
+ """最小值相对位置
69
+
70
+ 最近的一天记为第 0 天,最远的一天为第 d-1 天
67
71
 
68
72
  Parameters
69
73
  ----------
@@ -753,7 +757,7 @@ def ts_returns(x: Expr, d: int = 1) -> Expr:
753
757
 
754
758
 
755
759
  def ts_scale(x: Expr, d: int = 5, min_samples: Optional[int] = None) -> Expr:
756
- """时序滚动缩放
760
+ """时序滚动缩放。相当于ts_minmax
757
761
 
758
762
  Returns (x – ts_min(x, d)) / (ts_max(x, d) – ts_min(x, d)) + constant
759
763
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polars_ta
3
- Version: 0.5.4
3
+ Version: 0.5.5
4
4
  Summary: polars expressions
5
5
  Author-email: wukan <wu-kan@163.com>
6
6
  License: MIT License
@@ -91,7 +91,7 @@ df = df.with_columns([
91
91
  ])
92
92
  ```
93
93
 
94
- When both `min_periods` and `MIN_SAMPLES` are set, `min_periods` takes precedence. default value is `None`.
94
+ When both `min_samples` and `MIN_SAMPLES` are set, `min_samples` takes precedence. default value is `None`.
95
95
 
96
96
  ```python
97
97
  import polars_ta
@@ -194,7 +194,7 @@ df = df.with_columns([
194
194
  ])
195
195
  ```
196
196
 
197
- 当`min_periods`和`MIN_SAMPLES`都设置时,以`min_periods`为准,默认值为`None`
197
+ 当`min_samples`和`MIN_SAMPLES`都设置时,以`min_samples`为准,默认值为`None`
198
198
 
199
199
  ```python
200
200
  import polars_ta
@@ -1,5 +1,5 @@
1
1
  polars_ta/__init__.py,sha256=ig6f6c1AMSpntwKjqaX3msBzVIwkI7J776IujEmiuvA,123
2
- polars_ta/_version.py,sha256=DITpct-LrdIsTgwx2NgH5Ghx5y8Xgz1YMimy1ZV5RTY,22
2
+ polars_ta/_version.py,sha256=78mfpLewKVki6c9UONSUdlVme_JsN9ZwIfp4Hf4jmG0,22
3
3
  polars_ta/noise.py,sha256=LJHubBqnWlU3Bz84z07N1JB-b-hAMW2rgBF1BT4m0FE,1471
4
4
  polars_ta/candles/__init__.py,sha256=AW68IuFC0gD4_OyjwLP3p22WSzSIYqlSrsS9fW_15xw,141
5
5
  polars_ta/candles/cdl1.py,sha256=RnRu1QzpqEt5y0-1hlfZRUvza1no-Gj4x_dx2QWxr5A,3130
@@ -18,6 +18,7 @@ polars_ta/prefix/reports.py,sha256=B2QgdfX6RbZu9ytgaINv3Aq-ZAh_m3cr2pgbpFyL35A,4
18
18
  polars_ta/prefix/ta.py,sha256=qIb3oRmexNZ6LTQ35V194QTLqj8UgzjaYl5ASwqN5pc,4167
19
19
  polars_ta/prefix/talib.py,sha256=6JsrkgDIUsR2bIA7jVywxgWwxiiFuzsCPvbDeAt4qBA,9392
20
20
  polars_ta/prefix/tdx.py,sha256=EttaExzqoA3Y3lD-YKA32-5GfJ6jgKjt-nRuoeuK0wQ,11429
21
+ polars_ta/prefix/vec.py,sha256=jQ0w3ECn60j5Uefxe_-4bD1d_iQIvwOfXoXpcP1Bp4E,1055
21
22
  polars_ta/prefix/wq.py,sha256=PpSLQQ3vD0Mf3Tiz_AFSi--q8p7CkLg9CHh_ayMfoYo,35
22
23
  polars_ta/reports/__init__.py,sha256=KEATtWxhdaO6XScHYN2XE4WkYbYpQvDOEyXgyc2z22g,45
23
24
  polars_ta/reports/cicc.py,sha256=ZpNzHo5h855Xo9IeKeOzlUjjwE5tR8opb11UoJVxGkg,707
@@ -25,7 +26,7 @@ polars_ta/ta/README.md,sha256=XuDf1sMxNrq9QDEiwBaP8cp-skTK3Jr5VClxhsNiYJ4,647
25
26
  polars_ta/ta/__init__.py,sha256=GRI1suzvmLxfyfXAik4zsxqLaijq5MRCO-fUx3qaCl0,351
26
27
  polars_ta/ta/momentum.py,sha256=-W-wZ1oQE9QiUiVgEx7fvnCEkWJnW6j9CxA7Mc-WMGg,5623
27
28
  polars_ta/ta/operators.py,sha256=H_ORq1SeFKLj58vrOTAIa-X135evZNyo4ZE7gaH_7VM,3711
28
- polars_ta/ta/overlap.py,sha256=Q2A8BSF9tcTdEg269LQ97ia55H0LaY_vxfBYG6j2EQE,3023
29
+ polars_ta/ta/overlap.py,sha256=imoEVrFrv_P1ueg5KbPBTDEnsekNaxunL5U9BzDNxKE,3023
29
30
  polars_ta/ta/price.py,sha256=ME26yPPZk1kf7XDna53mmUrX7P1NDNq3trrZ_sCbZ_A,1013
30
31
  polars_ta/ta/statistic.py,sha256=dS5dPBszbdlroJ9u5aXBniJmKgPQnuh9zE7miYKzICs,968
31
32
  polars_ta/ta/transform.py,sha256=c4s5l6khVqIer50g6oScdZ1EEPWYANeKm64AkBkPrtY,1789
@@ -47,7 +48,7 @@ polars_ta/tdx/over_bought_over_sold.py,sha256=zB5W10TZBpKrMdzZlH9jmiVqLhrkcXdNI0
47
48
  polars_ta/tdx/pattern.py,sha256=jB0a5Ro0SQ25R5B_mkQWl9Hz7aB9n-DUns1OSxPeHxI,1636
48
49
  polars_ta/tdx/pattern_feature.py,sha256=D3_nyntC2UnvYL3smBYbADoxyh1oGkpWAI4E6Uqh8ZM,8746
49
50
  polars_ta/tdx/pressure_support.py,sha256=Lk_6nXImyWLuloP4rBwarp5LEVZjgr6_5ekxIksZPr8,1085
50
- polars_ta/tdx/reference.py,sha256=5F6ezUyjUobTXwd7dniOmDoDJsw3uo5HzaHSS9-R0CI,6298
51
+ polars_ta/tdx/reference.py,sha256=bij16Axl-ICVu4q9NQCQmXfZqcFAYxGCNb8_TSbmy2g,6298
51
52
  polars_ta/tdx/statistic.py,sha256=jdNLx3nV-uN4LrHRGxUUMhNOnlu8MOozjIHvshgMNPg,2588
52
53
  polars_ta/tdx/trend.py,sha256=n5HDHhb1Qak39DJjMFx27hFTYSttbNFeXJD7saRiTMo,3008
53
54
  polars_ta/tdx/trend_feature.py,sha256=uNc4Z46Kyd7Prl0Ftl2ljSAXztTzQtbO28p7gy8iMvM,9086
@@ -57,18 +58,19 @@ polars_ta/utils/helper.py,sha256=rqxBhmA-RhpHd1-qxdYi7OEZloQpPxR30Kz8fJ_hLic,625
57
58
  polars_ta/utils/numba_.py,sha256=P0z-81mabX_9zyJOX0Ib1CFmvPl9-SSatzki3dOciVA,5972
58
59
  polars_ta/utils/pandas_.py,sha256=codb51l45TUMngY55twQaUPUxnffjvj8UEOm35zFpNE,1989
59
60
  polars_ta/utils/pit.py,sha256=YNgUE6mbwhrtA-7wyOL0PCUDdwfTQouXwage-cK6efY,4312
61
+ polars_ta/utils/withs.py,sha256=cMSRHTyoFoG0qN5NZgx8D2HjmSJzqkE1MZRNiylMPtY,1167
60
62
  polars_ta/utils/wrapper.py,sha256=jbR-ZQdzBf5iWvtnJ1HsN9HdepmDbU7fUj4w8Bt2BkU,3422
61
63
  polars_ta/wq/__init__.py,sha256=C3YYJc997XCUI7H_afGT_mj2m4UdHMcql4sboCVvrXU,327
62
64
  polars_ta/wq/_nb.py,sha256=9N7afRiABqdjO1QPnRlP1RIKJ98cQLpv6nF3dsqUA6k,11281
63
65
  polars_ta/wq/_slow.py,sha256=MfWg9FYX8NGNLWN4ezI_awf3pPTqhSq_zo0LOS4nCzw,937
64
66
  polars_ta/wq/arithmetic.py,sha256=NwyX-K8j9ul7XHHSNuGimBq6f3NvcOGQ4le5PQuaRPc,26346
65
- polars_ta/wq/cross_sectional.py,sha256=1unrHaPPDQIZACg0CepWn3CXvd_zmQWjNwbXzqZXke8,14518
67
+ polars_ta/wq/cross_sectional.py,sha256=4HFs9QDx4Vfmxr3XfjmUwGZNc07D5Z2buKZmwD4x1rs,14768
66
68
  polars_ta/wq/logical.py,sha256=PfcPrY3iYoYFDTJ-B1IlCfpab1uTWV7GN1TdRtjauEk,2241
67
- polars_ta/wq/preprocess.py,sha256=Zpjv7zpU690Ugr5PNnq6CNlUn5qD9sH0SQr5q5AFtys,3444
68
- polars_ta/wq/time_series.py,sha256=KGVgFSQWiDAOpN7nW0rPE8UE81Q3Co9WwZiY1DWs3KQ,33164
69
+ polars_ta/wq/preprocess.py,sha256=Wb-n5HccciexrfMmL8-WJvMkMeDXMGYAf0mrurXE3kI,3297
70
+ polars_ta/wq/time_series.py,sha256=nywBEfkVrTsUgyUL7NCSMUSBoLm09e0ToCp9zwrxbvM,33155
69
71
  polars_ta/wq/transformational.py,sha256=HBnxQiS2Pw0fHnRW-aLqbznQVl1SSbZ9OqZG62igiJk,7266
70
72
  polars_ta/wq/vector.py,sha256=Qs-mHC2YsiWXoyMuXZZPzd5W5w_HL1ZgDCj9wRAnqmQ,1902
71
- polars_ta-0.5.4.dist-info/METADATA,sha256=YuA2qBXp9R4_9PbOLyk-ZhjiUdTnNLxXKQmZPvpIjc8,8839
72
- polars_ta-0.5.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
73
- polars_ta-0.5.4.dist-info/licenses/LICENSE,sha256=nREFtfwxWCCYD-ZA1jMzhhxMyTz-wGWFlnkpgg0DCtQ,1062
74
- polars_ta-0.5.4.dist-info/RECORD,,
73
+ polars_ta-0.5.5.dist-info/METADATA,sha256=XXh2IiEyvAH7YE4owO0HxmhM6Vczw1mSSftiCk7HXig,8839
74
+ polars_ta-0.5.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
75
+ polars_ta-0.5.5.dist-info/licenses/LICENSE,sha256=nREFtfwxWCCYD-ZA1jMzhhxMyTz-wGWFlnkpgg0DCtQ,1062
76
+ polars_ta-0.5.5.dist-info/RECORD,,