add Condition operator add

This commit is contained in:
fasiondog 2024-02-23 16:12:07 +08:00
parent d489e90c0f
commit c48719b6e2
6 changed files with 121 additions and 2 deletions

View File

@ -27,4 +27,6 @@ HKU_API ConditionPtr operator&(const ConditionPtr& cond1, const ConditionPtr& co
*/
HKU_API ConditionPtr operator|(const ConditionPtr& cond1, const ConditionPtr& cond2);
HKU_API ConditionPtr operator+(const ConditionPtr& cond1, const ConditionPtr& cond2);
} // namespace hku

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2019~2023, hikyuu.org
*
* History:
* 1. 20240223 added by fasiondog
*/
#include "AddCondition.h"
namespace hku {
AddCondition::AddCondition() : ConditionBase("CN_Add") {}
AddCondition::AddCondition(const ConditionPtr& cond1, const ConditionPtr& cond2)
: ConditionBase("CN_Add"), m_cond1(cond1), m_cond2(cond2) {}
AddCondition::~AddCondition() {}
void AddCondition::_calculate() {
HKU_IF_RETURN(!m_cond1 && !m_cond2, void());
if (m_cond1 && !m_cond2) {
price_t* data = m_cond1->data();
for (size_t i = 0, total = m_cond1->size(); i < total; i++) {
m_values[i] = data[i];
}
return;
}
if (!m_cond1 && m_cond2) {
price_t* data = m_cond2->data();
for (size_t i = 0, total = m_cond2->size(); i < total; i++) {
m_values[i] = data[i];
}
return;
}
size_t total = m_kdata.size();
HKU_ASSERT(m_cond1->size() == total && m_cond2->size() == total);
price_t* data1 = m_cond1->data();
price_t* data2 = m_cond2->data();
for (size_t i = 0; i < total; i++) {
m_values[i] = data1[i] + data2[i];
}
}
void AddCondition::_reset() {
if (m_cond1) {
m_cond1->reset();
}
if (m_cond2) {
m_cond2->reset();
}
}
ConditionPtr AddCondition::_clone() {
AddCondition* p = new AddCondition();
if (m_cond1) {
p->m_cond1 = m_cond1->clone();
}
if (m_cond2) {
p->m_cond2 = m_cond2->clone();
}
return ConditionPtr(p);
}
HKU_API ConditionPtr operator+(const ConditionPtr& cond1, const ConditionPtr& cond2) {
return make_shared<AddCondition>(cond1, cond2);
}
} // namespace hku

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019~2023, hikyuu.org
*
* History:
* 1. 20240223 added by fasiondog
*/
#pragma once
#include "../ConditionBase.h"
namespace hku {
class HKU_API AddCondition : public ConditionBase {
public:
AddCondition();
AddCondition(const ConditionPtr& cond1, const ConditionPtr& cond2);
virtual ~AddCondition();
virtual void _calculate() override;
virtual void _reset() override;
virtual ConditionPtr _clone() override;
private:
ConditionPtr m_cond1;
ConditionPtr m_cond2;
//============================================
// 序列化支持
//============================================
#if HKU_SUPPORT_SERIALIZATION
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(ConditionBase);
ar& BOOST_SERIALIZATION_NVP(m_cond1);
ar& BOOST_SERIALIZATION_NVP(m_cond2);
}
#endif
};
} // namespace hku

View File

@ -60,7 +60,7 @@ ConditionPtr AndCondition::_clone() {
}
HKU_API ConditionPtr operator&(const ConditionPtr& cond1, const ConditionPtr& cond2) {
return ConditionPtr(new AndCondition(cond1, cond2));
return make_shared<AndCondition>(cond1, cond2);
}
} // namespace hku

View File

@ -84,7 +84,7 @@ ConditionPtr OrCondition::_clone() {
}
HKU_API ConditionPtr operator|(const ConditionPtr& cond1, const ConditionPtr& cond2) {
return ConditionPtr(new OrCondition(cond1, cond2));
return make_shared<OrCondition>(cond1, cond2);
}
} // namespace hku

View File

@ -112,6 +112,9 @@ void export_Condition(py::module& m) {
.def("__or__",
[](const ConditionPtr& self, const ConditionPtr& other) { return self | other; })
.def("__add__",
[](const ConditionPtr& self, const ConditionPtr& other) { return self + other; })
DEF_PICKLE(ConditionPtr);
m.def("CN_OPLine", CN_OPLine, R"(CN_OPLine(ind)