Skip to content

Commit

Permalink
KQuery/KRecord 相等与不等比较导出至 python
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Oct 29, 2023
1 parent 2c23644 commit e51f2bf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
54 changes: 33 additions & 21 deletions hikyuu_cpp/hikyuu/KQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ const string KQuery::HOUR12("HOUR12");
// const string KQuery::INVALID_KTYPE("Z");

static vector<string> g_all_ktype{KQuery::MIN, KQuery::MIN5, KQuery::MIN15, KQuery::MIN30,
KQuery::MIN60, KQuery::DAY, KQuery::WEEK, KQuery::MONTH,
KQuery::QUARTER, KQuery::HALFYEAR, KQuery::YEAR, KQuery::MIN3,
KQuery::HOUR2, KQuery::HOUR4, KQuery::HOUR6, KQuery::HOUR12};
KQuery::MIN60, KQuery::DAY, KQuery::WEEK, KQuery::MONTH,
KQuery::QUARTER, KQuery::HALFYEAR, KQuery::YEAR, KQuery::MIN3,
KQuery::HOUR2, KQuery::HOUR4, KQuery::HOUR6, KQuery::HOUR12};

static const unordered_map<string, int32_t> g_ktype2min{
{KQuery::MIN, 1},
{KQuery::MIN3, 3},

{KQuery::MIN5, 5},
{KQuery::MIN15, 15},
{KQuery::MIN30, 30},
{KQuery::MIN60, 60},
{KQuery::HOUR2, 60 * 2},
{KQuery::HOUR4, 60 * 4},
{KQuery::HOUR6, 60 * 6},
{KQuery::HOUR12, 60 * 12},

{KQuery::DAY, 60 * 24},
{KQuery::WEEK, 60 * 24 * 7},
{KQuery::MONTH, 60 * 24 * 30},
{KQuery::QUARTER, 60 * 24 * 30 * 3},
{KQuery::HALFYEAR, 60 * 24 * 30 * 6},
{KQuery::YEAR, 60 * 24 * 365},
{KQuery::MIN, 1},
{KQuery::MIN3, 3},

{KQuery::MIN5, 5},
{KQuery::MIN15, 15},
{KQuery::MIN30, 30},
{KQuery::MIN60, 60},
{KQuery::HOUR2, 60 * 2},
{KQuery::HOUR4, 60 * 4},
{KQuery::HOUR6, 60 * 6},
{KQuery::HOUR12, 60 * 12},

{KQuery::DAY, 60 * 24},
{KQuery::WEEK, 60 * 24 * 7},
{KQuery::MONTH, 60 * 24 * 30},
{KQuery::QUARTER, 60 * 24 * 30 * 3},
{KQuery::HALFYEAR, 60 * 24 * 30 * 6},
{KQuery::YEAR, 60 * 24 * 365},
};

// 获取所有的 KType
Expand Down Expand Up @@ -163,4 +163,16 @@ HKU_API std::ostream& operator<<(std::ostream& os, const KQuery& query) {
return os;
}

bool HKU_API operator!=(const KQuery& q1, const KQuery& q2) {
// cppcheck-suppress [mismatchingContainerExpression]
return q1.start() != q2.start() || q1.end() != q2.end() || q1.queryType() != q2.queryType() ||
q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType();
}

bool HKU_API operator==(const KQuery& q1, const KQuery& q2) {
// cppcheck-suppress [mismatchingContainerExpression]
return q1.start() == q2.start() && q1.end() == q2.end() && q1.queryType() == q2.queryType() &&
q1.kType() == q2.kType() && q1.recoverType() == q2.recoverType();
}

} // namespace hku
14 changes: 2 additions & 12 deletions hikyuu_cpp/hikyuu/KQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,8 @@ HKU_API std::ostream& operator<<(std::ostream& os, const KQuery& query);
// 关系比较函数, 不直接在类中定义是为了支持 Null<>() == d,Null可以放在左边
//
///////////////////////////////////////////////////////////////////////////////
bool operator==(const KQuery&, const KQuery&);
bool operator!=(const KQuery&, const KQuery&);

inline bool operator!=(const KQuery& q1, const KQuery& q2) {
// cppcheck-suppress [mismatchingContainerExpression]
return q1.start() != q2.start() || q1.end() != q2.end() || q1.queryType() != q2.queryType() ||
q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType();
}

inline bool operator==(const KQuery& q1, const KQuery& q2) {
return !(q1 != q2);
}
bool HKU_API operator==(const KQuery&, const KQuery&);
bool HKU_API operator!=(const KQuery&, const KQuery&);

/**
* 提供KQuery的Null值
Expand Down
4 changes: 4 additions & 0 deletions hikyuu_cpp/hikyuu/KRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ bool HKU_API operator==(const KRecord& d1, const KRecord& d2) {
(std::fabs(d1.transCount - d2.transCount) < 0.0001));
}

bool HKU_API operator!=(const KRecord& d1, const KRecord& d2) {
return !(d1 == d2);
}

} // namespace hku
6 changes: 6 additions & 0 deletions hikyuu_cpp/hikyuu/KRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ HKU_API std::ostream& operator<<(std::ostream&, const KRecord&);
*/
bool HKU_API operator==(const KRecord& d1, const KRecord& d2);

/**
* 两个KRecord不等比较
* @ingroup StockManage
*/
bool HKU_API operator!=(const KRecord& d1, const KRecord& d2);

} // namespace hku

#if FMT_VERSION >= 90000
Expand Down
6 changes: 6 additions & 0 deletions hikyuu_pywrap/_KQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using namespace hku;
KQuery::RecoverType (KQuery::*get_recoverType)() const = &KQuery::recoverType;
void (KQuery::*set_recoverType)(KQuery::RecoverType recoverType) = &KQuery::recoverType;

bool (*kquery_eq)(const KQuery&, const KQuery&) = operator==;
bool (*kquery_ne)(const KQuery&, const KQuery&) = operator!=;

void export_KQuery() {
scope in_Query =
class_<KQuery>("Query", "K线数据查询条件", init<>())
Expand All @@ -37,6 +40,9 @@ void export_KQuery() {
.add_property("recover_type", get_recoverType, set_recoverType,
"查询的复权类型 Query.RecoverType")

.def("__eq__", kquery_eq)
.def("__ne__", kquery_ne)

#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(normal_pickle_suite<KQuery>())
#endif
Expand Down
3 changes: 3 additions & 0 deletions hikyuu_pywrap/_KRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace hku;
#endif

bool (*krecord_eq)(const KRecord&, const KRecord&) = operator==;
bool (*krecord_ne)(const KRecord&, const KRecord&) = operator!=;

void export_KReord() {
class_<KRecord>("KRecord", "K线记录,组成K线数据,属性可读写", init<>())
Expand All @@ -35,6 +36,8 @@ void export_KReord() {
.def_readwrite("volume", &KRecord::transCount, "成交量")

.def("__eq__", krecord_eq)
.def("__ne__", krecord_ne)

#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(normal_pickle_suite<KRecord>())
#endif
Expand Down

0 comments on commit e51f2bf

Please sign in to comment.