Skip to content

Commit

Permalink
添加 concat_to_df, 将指标列表合并为DataFrame, 以便使用其他依赖于 pandas 的工具
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Mar 7, 2024
1 parent 5467245 commit 61ec0d0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/source/indicator/indicator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
:rtype: Indicator


.. py:function:: ALIGN(data, ref[, use_null=True]):
.. py:function:: ALIGN(data, ref[, use_null=True])
按指定的参考日期对齐

Expand Down Expand Up @@ -201,7 +201,7 @@
:rtype: Indicator


.. py::function:: CORR(ind1, ind2, n)
.. py:function:: CORR(ind1, ind2, n)
计算 ind1 和 ind2 的样本相关系数与协方差。返回中存在两个结果,第一个为相关系数,第二个为协方差。

Expand Down
36 changes: 36 additions & 0 deletions docs/source/indicator/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,39 @@
* :py:func:`ROCP` - 变动率指标: (price - prevPrice) / prevPrice
* :py:func:`ROCR` - 变动率指标: (price / prevPrice)
* :py:func:`ROCR100` - 变动率指标: (price / prevPrice) * 100

**其他转换辅助**

* :py:func:`concat_to_df` - 合并指标列表为 DateFrame


.. py:function:: concat_to_df(dates, ind_list[, head_stock_code=True, head_ind_name=False])
将列表中的指标至合并在一张 pandas DataFrame 中

:param DatetimeList dates: 指定的日期列表
:param sequence ind_list: 已计算的指标列表
:param bool head_ind_name: 表标题是否使用指标名称
:param bool head_stock_code: 表标题是否使用证券代码
:return: 合并后的 DataFrame, 以 dates 为 index(注: dates列 为 Datetime 类型)

::

示例:
query = Query(-200)
k_list = [stk.get_kdata(query) for stk in [sm['sz000001'], sm['sz000002']]]
ma_list = [MA(k) for k in k_list]
concat_to_df(sm.get_trading_calendar(query), ma_list, head_stock_code=True, head_ind_name=False)
df

date SZ000001 SZ000002
0 2023-05-12 00:00:00 12.620000 15.060000
1 2023-05-15 00:00:00 12.725000 15.060000
2 2023-05-16 00:00:00 12.690000 15.010000
3 2023-05-17 00:00:00 12.640000 14.952500
4 2023-05-18 00:00:00 12.610000 14.886000
... ... ... ...
195 2024-03-01 00:00:00 9.950455 9.837273
196 2024-03-04 00:00:00 9.995909 9.838182
197 2024-03-05 00:00:00 10.038182 9.816364
198 2024-03-06 00:00:00 10.070455 9.776818
199 2024-03-07 00:00:00 10.101364 9.738182
44 changes: 44 additions & 0 deletions hikyuu/indicator/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from hikyuu.core import *
from hikyuu import Datetime
import pandas as pd


def indicator_iter(indicator):
Expand Down Expand Up @@ -89,3 +90,46 @@ def indicator_to_df(indicator):
)

VALUE = PRICELIST


def concat_to_df(dates, ind_list, head_stock_code=True, head_ind_name=False):
"""将列表中的指标至合并在一张 pandas DataFrame 中

:param DatetimeList dates: 指定的日期列表
:param sequence ind_list: 已计算的指标列表
:param bool head_ind_name: 表标题是否使用指标名称
:param bool head_stock_code: 表标题是否使用证券代码
:return: 合并后的 DataFrame, 以 dates 为 index(注: dates列 为 Datetime 类型)

示例:
query = Query(-200)
k_list = [stk.get_kdata(query) for stk in [sm['sz000001'], sm['sz000002']]]
ma_list = [MA(k) for k in k_list]
concat_to_df(sm.get_trading_calendar(query), ma_list, head_stock_code=True, head_ind_name=False)

输出:
date SZ000001 SZ000002
0 2023-05-12 00:00:00 12.620000 15.060000
1 2023-05-15 00:00:00 12.725000 15.060000
2 2023-05-16 00:00:00 12.690000 15.010000
3 2023-05-17 00:00:00 12.640000 14.952500
4 2023-05-18 00:00:00 12.610000 14.886000
... ... ... ...
195 2024-03-01 00:00:00 9.950455 9.837273
196 2024-03-04 00:00:00 9.995909 9.838182
197 2024-03-05 00:00:00 10.038182 9.816364
198 2024-03-06 00:00:00 10.070455 9.776818
199 2024-03-07 00:00:00 10.101364 9.738182
"""
df = pd.DataFrame(dates, columns=['date'])
for ind in ind_list:
x = ALIGN(ind, dates)
if head_ind_name and head_stock_code:
x.name = f"{ind.name}/{ind.get_context().get_stock().market_code}"
elif head_ind_name:
x.name = ind.name
else:
x.name = ind.get_context().get_stock().market_code
df = pd.concat([df, x.to_df()], axis=1)
df.set_index('date')
return df

0 comments on commit 61ec0d0

Please sign in to comment.