mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-01 03:18:18 +08:00
实现IF指标运算(未完)
This commit is contained in:
parent
b3aa700a04
commit
3b44d16e72
@ -329,5 +329,20 @@ HKU_API Indicator operator|(price_t val, const Indicator& ind) {
|
||||
return CVAL(ind, val) | ind;
|
||||
}
|
||||
|
||||
Indicator HKU_API IF(const Indicator& ind1,
|
||||
const Indicator& ind2, const Indicator& ind3) {
|
||||
if (!ind1.getImp()) {
|
||||
HKU_ERROR("condition indicator is None! [IF]");
|
||||
return Indicator();
|
||||
}
|
||||
|
||||
Indicator new_ind2 = ind2.getImp() ? ind2: CVAL(Null<price_t>());
|
||||
Indicator new_ind3 = ind3.getImp() ? ind3: CVAL(Null<price_t>());
|
||||
|
||||
IndicatorImpPtr p = make_shared<IndicatorImp>();
|
||||
p->add_if(ind1.getImp(), new_ind2.getImp(), new_ind3.getImp());
|
||||
return p->calculate();
|
||||
}
|
||||
|
||||
|
||||
} /* namespace hku */
|
||||
|
@ -196,5 +196,12 @@ HKU_API Indicator operator&(price_t, const Indicator&);
|
||||
HKU_API Indicator operator|(const Indicator&, price_t);
|
||||
HKU_API Indicator operator|(price_t, const Indicator&);
|
||||
|
||||
/**
|
||||
* IF 操作
|
||||
* @param
|
||||
* @ingroup Indicator
|
||||
*/
|
||||
Indicator HKU_API IF(const Indicator&, const Indicator&, const Indicator&);
|
||||
|
||||
} /* namespace hku */
|
||||
#endif /* INDICATOR_H_ */
|
||||
|
@ -306,15 +306,19 @@ void IndicatorImp::add(OPType op, IndicatorImpPtr left, IndicatorImpPtr right) {
|
||||
return;
|
||||
}
|
||||
|
||||
IndicatorImpPtr new_right;
|
||||
if (right->isNeedContext()) {
|
||||
new_right = right->getSameNameNeedContextLeaf(right->name());
|
||||
}
|
||||
|
||||
m_need_calculate = true;
|
||||
m_optype = op;
|
||||
m_left = left ? left->clone() : left;
|
||||
m_right = new_right ? new_right : right->clone();
|
||||
m_right = right->clone();
|
||||
}
|
||||
|
||||
void IndicatorImp::
|
||||
add_if(IndicatorImpPtr cond, IndicatorImpPtr left, IndicatorImpPtr right) {
|
||||
m_need_calculate = true;
|
||||
m_optype = IndicatorImp::IF;
|
||||
m_three = cond->clone();
|
||||
m_left = left->clone();
|
||||
m_right = right->clone();
|
||||
}
|
||||
|
||||
Indicator IndicatorImp::calculate() {
|
||||
@ -397,7 +401,13 @@ Indicator IndicatorImp::calculate() {
|
||||
execute_weave();
|
||||
break;
|
||||
|
||||
case IF:
|
||||
execute_if();
|
||||
break;
|
||||
|
||||
default:
|
||||
HKU_ERROR("Unkown Indicator::OPType! "
|
||||
<< m_optype << " [IndicatorImp::calculate]");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -924,4 +934,46 @@ void IndicatorImp::execute_or() {
|
||||
}
|
||||
}
|
||||
|
||||
void IndicatorImp::execute_if() {
|
||||
m_three->calculate();
|
||||
m_right->calculate();
|
||||
m_left->calculate();
|
||||
|
||||
IndicatorImp *maxp, *minp;
|
||||
if (m_right->size() > m_left->size()) {
|
||||
maxp = m_right.get();
|
||||
minp = m_left.get();
|
||||
} else {
|
||||
maxp = m_left.get();
|
||||
minp = m_right.get();
|
||||
}
|
||||
|
||||
size_t total = maxp->size();
|
||||
size_t discard = maxp->size() - minp->size() + minp->discard();
|
||||
if (discard < maxp->discard()) {
|
||||
discard = maxp->discard();
|
||||
}
|
||||
|
||||
size_t diff = maxp->size() - minp->size();
|
||||
if (m_three->size() >= maxp->size()) {
|
||||
total = m_three->size();
|
||||
discard = total + discard - maxp->size();
|
||||
}
|
||||
|
||||
size_t result_number = std::min(minp->getResultNumber(), maxp->getResultNumber());
|
||||
//size_t diff = maxp->size() - minp->size();
|
||||
_readyBuffer(total, result_number);
|
||||
setDiscard(discard);
|
||||
for (size_t r = 0; r < result_number; ++r) {
|
||||
for (size_t i = discard; i < total; ++i) {
|
||||
if (maxp->get(i, r) >= IND_EQ_THRESHOLD
|
||||
|| minp->get(i-diff, r) >= IND_EQ_THRESHOLD) {
|
||||
_set(1, i, r);
|
||||
} else {
|
||||
_set(0, i, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace hku */
|
||||
|
@ -51,6 +51,7 @@ public:
|
||||
AND, ///<与
|
||||
OR, ///<或
|
||||
WEAVE, ///<特殊的,需要两个指标作为参数的指标
|
||||
IF, ///if操作
|
||||
INVALID
|
||||
};
|
||||
|
||||
@ -141,6 +142,8 @@ public:
|
||||
|
||||
void add(OPType, IndicatorImpPtr left, IndicatorImpPtr right);
|
||||
|
||||
void add_if(IndicatorImpPtr cond, IndicatorImpPtr left, IndicatorImpPtr right);
|
||||
|
||||
IndicatorImpPtr clone();
|
||||
|
||||
// ===================
|
||||
@ -170,6 +173,7 @@ private:
|
||||
void execute_and();
|
||||
void execute_or();
|
||||
void execute_weave();
|
||||
void execute_if();
|
||||
|
||||
protected:
|
||||
string m_name;
|
||||
@ -181,6 +185,7 @@ protected:
|
||||
OPType m_optype;
|
||||
IndicatorImpPtr m_left;
|
||||
IndicatorImpPtr m_right;
|
||||
IndicatorImpPtr m_three;
|
||||
|
||||
#if HKU_SUPPORT_SERIALIZATION
|
||||
private:
|
||||
@ -197,6 +202,7 @@ private:
|
||||
ar & BOOST_SERIALIZATION_NVP(m_optype);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_left);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_right);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_three);
|
||||
size_t act_result_num = 0;
|
||||
size_t i = 0;
|
||||
while (i < m_result_num) {
|
||||
@ -223,6 +229,7 @@ private:
|
||||
ar & BOOST_SERIALIZATION_NVP(m_optype);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_left);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_right);
|
||||
ar & BOOST_SERIALIZATION_NVP(m_three);
|
||||
size_t act_result_num = 0;
|
||||
ar & BOOST_SERIALIZATION_NVP(act_result_num);
|
||||
for (size_t i = 0; i < act_result_num; ++i) {
|
||||
|
Loading…
Reference in New Issue
Block a user