Skip to content

Commit

Permalink
KData.get_pos 调整至 c++ 内实现,调整 Indicator.get_pos 行为和 KData 一致
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Feb 21, 2025
1 parent 81b505e commit 78a7567
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
24 changes: 0 additions & 24 deletions hikyuu/extend.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,8 @@ def KData_iter(kdata):
yield kdata[i]


def KData_getPos(kdata, datetime):
"""
获取指定时间对应的索引位置
:param Datetime datetime: 指定的时间
:return: 对应的索引位置,如果不在数据范围内,则返回 None
"""
pos = kdata._getPos(datetime)
return pos if pos != constant.null_size else None


def KData_getPosInStock(kdata, datetime):
"""
获取指定时间对应的原始K线中的索引位置
:param Datetime datetime: 指定的时间
:return: 对应的索引位置,如果不在数据范围内,则返回 None
"""
pos = kdata._getPosInStock(datetime)
return pos if pos != constant.null_size else None


KData.__getitem__ = KData_getitem
KData.__iter__ = KData_iter
KData.get_pos = KData_getPos
KData.get_pos_in_stock = KData_getPosInStock


# ------------------------------------------------------------------
Expand Down
36 changes: 33 additions & 3 deletions hikyuu_pywrap/_KData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,39 @@ void export_KData(py::module& m) {
:param Datetime datetime: 指定的日期
:rtype: KRecord)")

.def("_getPos", &KData::getPos) // python中需要将Null的情况改写为None

.def("_getPosInStock", &KData::getPosInStock)
.def(
"get_pos",
[](const KData& self, const Datetime& d) {
size_t pos = self.getPos(d);
py::object ret = py::none();
if (pos != Null<size_t>()) {
ret = py::int_(pos);
}
return ret;
},
R"(get_pos(self, datetime)
获取指定时间的K线记录的索引位置, 如果不在数据范围内,则返回 None
:param Datetime datetime: 指定的日期
:rtype: int)")

.def(
"get_pos_in_stock",
[](const KData& self, Datetime datetime) {
size_t pos = self.getPosInStock(datetime);
py::object ret = py::none();
if (pos != Null<size_t>()) {
ret = py::int_(pos);
}
return ret;
},
R"(get_pos_in_stock(self, datetime)
获取指定时间对应的原始K线中的索引位置
:param Datetime datetime: 指定的时间
:return: 对应的索引位置,如果不在数据范围内,则返回 None)")

.def("empty", &KData::empty, R"(empty(self)
Expand Down
14 changes: 12 additions & 2 deletions hikyuu_pywrap/indicator/_Indicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,19 @@ void export_Indicator(py::module& m) {
:param int result_index: 指定的结果集
:rtype: float)")

.def("get_pos", &Indicator::getPos, R"(get_pos(self, date):
.def(
"get_pos",
[](const Indicator& self, const Datetime& d) {
size_t pos = self.getPos(d);
py::object ret = py::none();
if (pos != Null<size_t>()) {
ret = py::int_(pos);
}
return ret;
},
R"(get_pos(self, date):
获取指定日期相应的索引位置
获取指定日期相应的索引位置, 如果没有对应位置返回 None
:param Datetime date: 指定日期
:rtype: int)")
Expand Down

0 comments on commit 78a7567

Please sign in to comment.