Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
wukan1986 committed Dec 4, 2024
1 parent 8c22a3f commit 72d5af7
Show file tree
Hide file tree
Showing 29 changed files with 310 additions and 100 deletions.
2 changes: 1 addition & 1 deletion docs/talib/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
直接调用`TA-Lib`
直接调用`TA-Lib`, 多输出时返回`struct`, 可以使用`.struct[0]`取到对应字段的`Series`.

::: polars_ta.talib
1 change: 1 addition & 0 deletions docs/tdx/choice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.choice
1 change: 1 addition & 0 deletions docs/tdx/energy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.energy
1 change: 1 addition & 0 deletions docs/tdx/logical.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.logical
1 change: 1 addition & 0 deletions docs/tdx/moving_average.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.moving_average
1 change: 1 addition & 0 deletions docs/tdx/over_bought_over_sold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.over_bought_over_sold
1 change: 1 addition & 0 deletions docs/tdx/pressure_support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.pressure_support
1 change: 1 addition & 0 deletions docs/tdx/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.reference
1 change: 1 addition & 0 deletions docs/tdx/statistic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.statistic
1 change: 1 addition & 0 deletions docs/tdx/trend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.trend
1 change: 1 addition & 0 deletions docs/tdx/volume.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: polars_ta.tdx.volume
10 changes: 10 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ nav:
- ta/volume.md
- polars_ta.tdx:
- tdx/arithmetic.md
- tdx/choice.md
- tdx/energy.md
- tdx/logical.md
- tdx/moving_average.md
- tdx/over_bought_over_sold.md
- tdx/pressure_support.md
- tdx/reference.md
- tdx/statistic.md
- tdx/trend.md
- tdx/volume.md
- polars_ta.talib: talib/index.md

theme:
Expand Down
2 changes: 1 addition & 1 deletion polars_ta/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.1"
__version__ = "0.4.0"
9 changes: 9 additions & 0 deletions polars_ta/prefix/ta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
from polars_ta.ta.momentum import STOCHF_fastd as ts_STOCHF_fastd # noqa
from polars_ta.ta.momentum import TRIX as ts_TRIX # noqa
from polars_ta.ta.momentum import WILLR as ts_WILLR # noqa
from polars_ta.ta.operators import ADD # noqa
from polars_ta.ta.operators import DIV # noqa
from polars_ta.ta.operators import MAX as ts_MAX # noqa
from polars_ta.ta.operators import MAXINDEX as ts_MAXINDEX # noqa
from polars_ta.ta.operators import MIN as ts_MIN # noqa
from polars_ta.ta.operators import MININDEX as ts_MININDEX # noqa
from polars_ta.ta.operators import MUL # noqa
from polars_ta.ta.operators import SUB # noqa
from polars_ta.ta.operators import SUM as ts_SUM # noqa
from polars_ta.ta.overlap import BBANDS_upperband as ts_BBANDS_upperband # noqa
from polars_ta.ta.overlap import DEMA as ts_DEMA # noqa
from polars_ta.ta.overlap import EMA as ts_EMA # noqa
Expand Down
1 change: 0 additions & 1 deletion polars_ta/prefix/tdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from polars_ta.tdx.arithmetic import EXP # noqa
from polars_ta.tdx.arithmetic import FLOOR # noqa
from polars_ta.tdx.arithmetic import FRACPART # noqa
from polars_ta.tdx.arithmetic import INTPART # noqa
from polars_ta.tdx.arithmetic import LN # noqa
from polars_ta.tdx.arithmetic import LOG # noqa
from polars_ta.tdx.arithmetic import MAX # noqa
Expand Down
57 changes: 29 additions & 28 deletions polars_ta/ta/momentum.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
"""
```python
```
"""
from polars import Expr
from polars import Expr, when

from polars_ta import TA_EPSILON
from polars_ta.ta.operators import MAX
Expand Down Expand Up @@ -149,50 +143,57 @@ def RSI(close: Expr, timeperiod: int = 14) -> Expr:
return RMA(max_(dif, 0), timeperiod) / (RMA(dif.abs(), timeperiod) + TA_EPSILON) # * 100


def STOCHF_fastd(high: Expr, low: Expr, close: Expr, fastk_period: int = 5, fastd_period: int = 3) -> Expr:
return SMA(RSV(high, low, close, fastk_period), fastd_period)


def TRIX(close: Expr, timeperiod: int = 30) -> Expr:
EMA1 = EMA(close, timeperiod)
EMA2 = EMA(EMA1, timeperiod)
EMA3 = EMA(EMA2, timeperiod)
return ROCP(EMA3, 1)


def RSV(high: Expr, low: Expr, close: Expr, timeperiod: int = 5) -> Expr:
"""
"""RSV=STOCHF_FASTK
(Today's Close - LowestLow)
FASTK(Kperiod) = --------------------------- * 100
(HighestHigh - LowestLow)
Notes
-----
Also called `STOCHF_fastk`, `talib.STOCHF` is multiplied by 100,
and it is very similar to the `WILLR` indicator
RSV = STOCHF_FASTK。没乘以100
又名STOCHF_fastk, talib.STOCHF版相当于多乘了100,与WILLR指标又很像
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_STOCHF.c#L279
"""
a = MAX(high, timeperiod)
b = MIN(low, timeperiod)

return (close - b) / (a - b + TA_EPSILON)


def STOCHF_fastd(high: Expr, low: Expr, close: Expr, fastk_period: int = 5, fastd_period: int = 3) -> Expr:
return SMA(RSV(high, low, close, fastk_period), fastd_period)


def TRIX(close: Expr, timeperiod: int = 30) -> Expr:
EMA1 = EMA(close, timeperiod)
EMA2 = EMA(EMA1, timeperiod)
EMA3 = EMA(EMA2, timeperiod)
return ROCP(EMA3, 1)
# return (close - b) / (a - b + TA_EPSILON)
return when(a != b).then((close - b) / (a - b)).otherwise(0)


def WILLR(high: Expr, low: Expr, close: Expr, timeperiod: int = 14) -> Expr:
"""
"""威廉指标
Notes
-----
`talib.WILLR` is multiplied by -100, but I think it is unnecessary
talib.WILLR版相当于多乘了-100,但个人认为没有必要
WILLR=1-RSV
References
----------
- https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_WILLR.c#L294
- https://www.investopedia.com/terms/w/williamsr.asp
- https://school.stockcharts.com/doku.php?id=technical_indicators:williams_r
"""
a = MAX(high, timeperiod)
b = MIN(low, timeperiod)

return (a - close) / (a - b + TA_EPSILON)
# return (a - close) / (a - b + TA_EPSILON)
return when(a != b).then((a - close) / (a - b)).otherwise(0)
122 changes: 85 additions & 37 deletions polars_ta/ta/operators.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,109 @@
"""
通过`import`直接导入或更名的函数
```python
from polars_ta.wq.arithmetic import add as ADD # noqa
from polars_ta.wq.arithmetic import divide as DIV # noqa
from polars_ta.wq.arithmetic import multiply as MUL # noqa
from polars_ta.wq.arithmetic import subtract as SUB # noqa
from polars_ta.wq.time_series import ts_max as MAX # noqa
from polars_ta.wq.time_series import ts_min as MIN # noqa
from polars_ta.wq.time_series import ts_sum as SUM # noqa
```
"""
from polars import Expr

from polars_ta.wq.arithmetic import add as ADD # noqa
from polars_ta.wq.arithmetic import divide as DIV # noqa
from polars_ta.wq.arithmetic import multiply as MUL # noqa
from polars_ta.wq.arithmetic import subtract as SUB # noqa
from polars_ta.wq.time_series import ts_arg_max
from polars_ta.wq.time_series import ts_arg_min
from polars_ta.wq.time_series import ts_max
from polars_ta.wq.time_series import ts_min
from polars_ta.wq.time_series import ts_sum
from polars_ta.wq.time_series import ts_max as MAX # noqa
from polars_ta.wq.time_series import ts_min as MIN # noqa
from polars_ta.wq.time_series import ts_sum as SUM # noqa


def ADD(high: Expr, low: Expr) -> Expr:
return high + low


def DIV(high: Expr, low: Expr) -> Expr:
return high / low


def MAX(close: Expr, timeperiod: int = 30) -> Expr:
def MAXINDEX(close: Expr, timeperiod: int = 30) -> Expr:
"""
Notes
-----
It is the maximum value of the time series, not the maximum value of multiple columns (max_horizontal)
时序上窗口最大,不要与多列最大搞混
"""
return ts_max(close, timeperiod)


def MAXINDEX(close: Expr, timeperiod: int = 30) -> Expr:
"""
Comparing to `ts_arg_max` this also marks the abs. position of the max value
与ts_arg_max的区别是,标记了每个区间最大值的绝对位置,可用来画图标记
Examples
--------
```python
from polars_ta.ta import MAXINDEX as ta_MAXINDEX
from polars_ta.talib import MAXINDEX as talib_MAXINDEX
from polars_ta.wq import ts_arg_max
df = pl.DataFrame({
'a': [6, 2, 8, 5, 9, 4],
}).with_columns(
out1=ts_arg_max(pl.col('a'), 3),
out2=ta_MAXINDEX(pl.col('a'), 3),
out3=talib_MAXINDEX(pl.col('a'), 3),
)
shape: (6, 4)
┌─────┬──────┬──────┬──────┐
│ a ┆ out1 ┆ out2 ┆ out3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ u16 ┆ i64 ┆ i32 │
╞═════╪══════╪══════╪══════╡
│ 6 ┆ null ┆ null ┆ 0 │
│ 2 ┆ null ┆ null ┆ 0 │
│ 8 ┆ 0 ┆ 2 ┆ 2 │
│ 5 ┆ 1 ┆ 2 ┆ 2 │
│ 9 ┆ 0 ┆ 4 ┆ 4 │
│ 4 ┆ 1 ┆ 4 ┆ 4 │
└─────┴──────┴──────┴──────┘
```
"""
a = close.cum_count()
b = ts_arg_max(close, timeperiod)
return a - b - 1


def MIN(close: Expr, timeperiod: int = 30) -> Expr:
return ts_min(close, timeperiod)
def MININDEX(close: Expr, timeperiod: int = 30) -> Expr:
"""
def MININDEX(close: Expr, timeperiod: int = 30) -> Expr:
Examples
--------
```python
from polars_ta.ta import MININDEX as ta_MININDEX
from polars_ta.talib import MININDEX as talib_MININDEX
from polars_ta.wq import ts_arg_min
df = pl.DataFrame({
'a': [6, 2, 8, 5, 9, 4],
}).with_columns(
out1=ts_arg_min(pl.col('a'), 3),
out2=ta_MININDEX(pl.col('a'), 3),
out3=talib_MININDEX(pl.col('a'), 3),
)
shape: (6, 4)
┌─────┬──────┬──────┬──────┐
│ a ┆ out1 ┆ out2 ┆ out3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ u16 ┆ i64 ┆ i32 │
╞═════╪══════╪══════╪══════╡
│ 6 ┆ null ┆ null ┆ 0 │
│ 2 ┆ null ┆ null ┆ 0 │
│ 8 ┆ 1 ┆ 1 ┆ 1 │
│ 5 ┆ 2 ┆ 1 ┆ 1 │
│ 9 ┆ 1 ┆ 3 ┆ 3 │
│ 4 ┆ 0 ┆ 5 ┆ 5 │
└─────┴──────┴──────┴──────┘
```
"""
a = close.cum_count()
b = ts_arg_min(close, timeperiod)
return a - b - 1


def MULT(high: Expr, low: Expr) -> Expr:
return high * low


def SUB(high: Expr, low: Expr) -> Expr:
return high - low


def SUM(close: Expr, timeperiod: int = 30) -> Expr:
return ts_sum(close, timeperiod)
14 changes: 14 additions & 0 deletions polars_ta/ta/overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,24 @@ def KAMA(close: Expr, timeperiod: int = 30) -> Expr:


def MIDPOINT(close: Expr, timeperiod: int = 14) -> Expr:
"""MIDPOINT = (Highest Value + Lowest Value)/2
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_MIDPOINT.c#L198
"""
return (MAX(close, timeperiod) + MIN(close, timeperiod)) / 2


def MIDPRICE(high: Expr, low: Expr, timeperiod: int = 14) -> Expr:
"""MIDPRICE = (Highest High + Lowest Low)/2
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_MIDPRICE.c#L202
"""
return (MAX(high, timeperiod) + MIN(low, timeperiod)) / 2


Expand Down
32 changes: 28 additions & 4 deletions polars_ta/ta/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@


def AVGPRICE(open: Expr, high: Expr, low: Expr, close: Expr) -> Expr:
"""(open + high + low + close) / 4"""
"""(open + high + low + close) / 4
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_AVGPRICE.c#L187
"""
return (open + high + low + close) / 4


def MEDPRICE(high: Expr, low: Expr) -> Expr:
"""(high + low) / 2"""
"""(high + low) / 2
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_MEDPRICE.c#L180
"""
return (high + low) / 2


def TYPPRICE(high: Expr, low: Expr, close: Expr) -> Expr:
"""(high + low + close) / 3"""
"""(high + low + close) / 3
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_TYPPRICE.c#L185
"""
return (high + low + close) / 3


def WCLPRICE(high: Expr, low: Expr, close: Expr) -> Expr:
"""(high + low + close * 2) / 4"""
"""(high + low + close * 2) / 4
References
----------
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_WCLPRICE.c#L184
"""
return (high + low + close * 2) / 4
Loading

0 comments on commit 72d5af7

Please sign in to comment.