This commit is contained in:
fasiondog 2019-03-11 01:44:21 +08:00
parent 318175142e
commit 6944fa74b0
4 changed files with 26 additions and 23 deletions

View File

@ -32,18 +32,14 @@ Indicator::~Indicator() {
Indicator Indicator::operator()(const Indicator& ind) {
std::cout << "Indicator Indicator::operator()(const Indicator& ind)" << std::endl;
//return m_imp ? Indicator((*m_imp)(ind)) : Indicator();
IndicatorImpPtr p;
if (!m_imp) {
return Indicator(p);
}
p = m_imp->clone();
if (!ind.getImp()) {
return Indicator(p);
if (!m_imp || !ind.getImp()) {
return Indicator();
}
IndicatorImpPtr p = m_imp->clone();
p->calculate(ind);
p->add(IndicatorImp::OP, IndicatorImpPtr(), ind.getImp()->clone());
//p->calculate();
//p->calculate(ind);
return Indicator(p);
}
@ -93,16 +89,21 @@ size_t Indicator::size() const {
return m_imp ? m_imp->size() : 0;
}
/*
HKU_API Indicator operator+(const Indicator& ind1, const Indicator& ind2) {
if (ind1.size() == 0 || ind1.size() != ind2.size()) {
if (!ind1.getImp() || !ind2.getImp()) {
return Indicator();
}
IndicatorImpPtr imp = make_shared<IndicatorImp>();
imp->add(IndicatorImp::ADD, ind1.getImp()->clone(), ind2.getImp()->clone());
size_t result_number = std::min(ind1.getResultNumber(), ind2.getResultNumber());
size_t total = ind1.size();
size_t total = std::min(ind1.size(), ind2.size());
size_t discard = std::max(ind1.discard(), ind2.discard());
IndicatorImpPtr imp(new IndicatorImp());
if (discard > total)
discard = total;
imp->_readyBuffer(total, result_number);
imp->setDiscard(discard);
for (size_t i = discard; i < total; ++i) {
@ -114,7 +115,7 @@ HKU_API Indicator operator+(const Indicator& ind1, const Indicator& ind2) {
return Indicator(imp);
}
/*
HKU_API Indicator operator+(const Indicator& ind, price_t val) {
if (ind.size() == 0) {
return Indicator();

View File

@ -127,7 +127,7 @@ private:
#endif /* HKU_SUPPORT_SERIALIZATION */
};
#if 0
/**
* Indicator实例相加size必须相等
* @return 1) size必须相等
@ -135,6 +135,7 @@ private:
* @ingroup Indicator
*/
HKU_API Indicator operator+(const Indicator&, const Indicator&);
#if 0
HKU_API Indicator operator+(const Indicator&, price_t);
HKU_API Indicator operator+(price_t, const Indicator&);

View File

@ -180,11 +180,9 @@ void IndicatorImp::add(OPType op, IndicatorImpPtr left, IndicatorImpPtr right) {
m_right = right;
if (m_left) m_left->m_parent = this;
m_right->m_parent = this;
calculate();
}
void IndicatorImp::calculate() {
void IndicatorImp::calculate(const Indicator& ind) {
if (!check()) {
HKU_WARN("Invalid param! " << long_name());
return;
@ -192,15 +190,18 @@ void IndicatorImp::calculate() {
switch (m_optype) {
case LEAF:
_calculate();
_calculate(Indicator());
break;
case OP:
m_right->calculate();
m_right->calculate(ind);
this->calculate(Indicator(m_right));
//m_left->calculate();
break;
case ADD: {
m_right->calculate(Indicator());
m_left->calculate(Indicator());
break;
}

View File

@ -117,7 +117,7 @@ public:
/** 返回形如Name(param1=val,param2=val,...) */
string long_name() const;
void calculate();
void calculate(const Indicator&);
void setContext(const Stock&, const KQuery&);
@ -130,7 +130,7 @@ public:
// ===================
virtual bool check() { return false;}
virtual void _calculate() {}
virtual void _calculate(const Indicator&) {}
virtual IndicatorImpPtr _clone() { return make_shared<IndicatorImp>(); }
@ -217,7 +217,7 @@ BOOST_SERIALIZATION_ASSUME_ABSTRACT(IndicatorImp)
virtual IndicatorImpPtr operator()(const Indicator& ind) { \
IndicatorImpPtr p = make_shared<classname>(); \
p->setParameter(m_params); \
p->calculate(); \
p->calculate(ind); \
return p; \
} \
virtual IndicatorImpPtr _clone() { return make_shared<classname>(); } \