Skip to content

Commit 5f637bb

Browse files
committed
Indicator 增加 exist_nan 方法,判断有效数据中是否存在 nan 值
1 parent e36045d commit 5f637bb

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

hikyuu_cpp/hikyuu/indicator/Indicator.h

+7
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class HKU_API Indicator {
138138
*/
139139
PriceList getResultAsPriceList(size_t num) const;
140140

141+
/** 指定结果集中是否包含 nan 值 */
142+
bool existNan(size_t result_idx) const;
143+
141144
/**
142145
* 获取 DatetimeList
143146
*/
@@ -259,6 +262,10 @@ inline DatetimeList Indicator::getDatetimeList() const {
259262
return m_imp ? m_imp->getDatetimeList() : DatetimeList();
260263
}
261264

265+
inline bool Indicator::existNan(size_t result_idx) const {
266+
return m_imp ? m_imp->existNan(result_idx) : false;
267+
}
268+
262269
inline Indicator::value_t Indicator::getByDate(Datetime date, size_t num) const {
263270
return m_imp ? m_imp->getByDate(date, num) : Null<Indicator::value_t>();
264271
}

hikyuu_cpp/hikyuu/indicator/IndicatorImp.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,17 @@ size_t IndicatorImp::getPos(Datetime date) const {
471471
return getContext().getPos(date);
472472
}
473473

474+
bool IndicatorImp::existNan(size_t result_idx) const {
475+
HKU_CHECK(result_idx < m_result_num, "result_idx: {}", result_idx);
476+
const value_t *src = data(result_idx);
477+
for (size_t i = m_discard, total = size(); i < total; i++) {
478+
if (std::isnan(src[i])) {
479+
return true;
480+
}
481+
}
482+
return false;
483+
}
484+
474485
string IndicatorImp::formula() const {
475486
std::stringstream buf;
476487

hikyuu_cpp/hikyuu/indicator/IndicatorImp.h

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class HKU_API IndicatorImp : public enable_shared_from_this<IndicatorImp> {
109109
*/
110110
void _readyBuffer(size_t len, size_t result_num);
111111

112+
/** 数据中是否包含 nan 值 */
113+
bool existNan(size_t result_idx = 0) const;
114+
112115
const string& name() const;
113116
void name(const string& name);
114117

hikyuu_pywrap/indicator/_Indicator.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ void export_Indicator(py::module& m) {
147147
148148
:rtype: DatetimeList)")
149149

150+
.def("exist_nan", &Indicator::existNan, py::arg("result_idx=0"),
151+
R"(exist_nan(self, result_index)
152+
153+
判断是否存在NaN值
154+
155+
:param int result_index: 指定的结果集
156+
:rtype: bool)")
157+
150158
.def("set_context", setContext_1)
151159
.def("set_context", setContext_2, R"(set_context(self, kdata)
152160

0 commit comments

Comments
 (0)