Skip to content

Commit

Permalink
Merge pull request #225 from fasiondog/feature/turnover
Browse files Browse the repository at this point in the history
add TURNOVER 换手率指标
  • Loading branch information
fasiondog committed Apr 6, 2024
2 parents c698cb1 + 100e6d1 commit 81c0916
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
8 changes: 4 additions & 4 deletions hikyuu/indicator/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def indicator_getitem(data, i):
Indicator.__iter__ = indicator_iter


VALUE = PRICELIST

try:
import numpy as np
import pandas as pd
Expand All @@ -89,8 +87,6 @@ def indicator_to_df(indicator):
"you can't use method Inidicator.to_np() and to_df!"
)

VALUE = PRICELIST


def concat_to_df(dates, ind_list, head_stock_code=True, head_ind_name=False):
"""将列表中的指标至合并在一张 pandas DataFrame 中
Expand Down Expand Up @@ -143,3 +139,7 @@ def concat_to_df(dates, ind_list, head_stock_code=True, head_ind_name=False):
LOW = C_LOW()
AMO = C_AMO()
VOL = C_VOL()

# 同名指标
VALUE = PRICELIST
CAPITAL = LIUTONGPANG
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu/indicator/build_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#include "crt/TIME.h"
#include "crt/TIMELINE.h"
#include "crt/TIMELINEVOL.h"
#include "crt/TURNOVER.h"
#include "crt/UPNDAY.h"
#include "crt/VAR.h"
#include "crt/VARP.h"
Expand Down
27 changes: 27 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/crt/TURNOVER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-04-06
* Author: fasiondog
*/

#include "KDATA.h"
#include "SUM.h"
#include "LIUTONGPAN.h"
#include "TURNOVER.h"

namespace hku {

// 不需要乘以 100,成交量已经是手数即100
Indicator HKU_API TURNOVER(int n) {
HKU_ASSERT(n >= 1);
return n == 1 ? (VOL() / LIUTONGPAN()) : (SUM(VOL(), n) / SUM(LIUTONGPAN(), n));
}

Indicator HKU_API TURNOVER(const KData& kdata, int n) {
HKU_ASSERT(n >= 1);
return n == 1 ? (kdata.vol() / LIUTONGPAN(kdata))
: (SUM(kdata.vol(), n) / SUM(LIUTONGPAN(kdata), n));
}

} // namespace hku
21 changes: 21 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/crt/TURNOVER.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-04-06
* Author: fasiondog
*/

#include "../Indicator.h"

namespace hku {

/**
* @brief 换手率=股票成交量/流通股股数×100%
* @param n 窗口周期
* @return Indicator
*/
Indicator HKU_API TURNOVER(int n = 1);

Indicator HKU_API TURNOVER(const KData& kdata, int n = 1);

} // namespace hku
5 changes: 5 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/imp/ISum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ void ISum::_calculate(const Indicator& ind) {
}

m_discard = ind.discard();
if (n == 1) {
memcpy(dst, src, total * sizeof(value_t));
return;
}

price_t sum = 0.0;
for (size_t i = m_discard, len = (m_discard + n) >= total ? total : m_discard + n; i < len;
i++) {
Expand Down
7 changes: 7 additions & 0 deletions hikyuu_pywrap/indicator/_build_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,4 +1808,11 @@ void export_Indicator_build_in(py::module& m) {
:param float nsigma: 剔除极值时使用的 nsigma 倍 sigma,默认 3.0
:param bool recursive: 是否进行递归剔除极值,默认 False
:rtype: Indicator)");

m.def("TURNOVER", py::overload_cast<int>(TURNOVER), py::arg("n") = 1);
m.def("TURNOVER", py::overload_cast<const KData&, int>(TURNOVER), py::arg("kdata"),
py::arg("n") = 1, R"(TURNOVER(data[,n=1])
换手率=股票成交量/流通股股数×100%
:param int n: 时间窗口)");
}

0 comments on commit 81c0916

Please sign in to comment.