hikyuu2/hikyuu_cpp/hikyuu/indicator/Indicator.cpp

296 lines
9.0 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* Indicator.cpp
*
* Created on: 2012-10-15
* Author: fasiondog
*/
#include "Indicator.h"
2019-03-22 23:12:28 +08:00
#include "crt/CVAL.h"
2015-01-07 01:26:14 +08:00
namespace hku {
2019-11-10 03:27:57 +08:00
HKU_API std::ostream& operator<<(std::ostream& os, const Indicator& indicator) {
2015-01-07 01:26:14 +08:00
os << indicator.m_imp;
return os;
}
2019-11-10 03:27:57 +08:00
Indicator::Indicator(const IndicatorImpPtr& imp) : m_imp(imp) {}
2015-01-07 01:26:14 +08:00
2019-11-10 03:27:57 +08:00
Indicator::Indicator(const Indicator& indicator) : m_imp(indicator.m_imp) {}
2015-01-07 01:26:14 +08:00
2019-11-10 03:27:57 +08:00
Indicator::~Indicator() {}
2015-01-07 01:26:14 +08:00
2019-03-14 01:53:13 +08:00
string Indicator::formula() const {
return m_imp ? m_imp->formula() : "Indicator";
}
2019-04-17 23:37:22 +08:00
Indicator Indicator::operator()(const KData& k) {
Indicator result = clone();
result.setContext(k);
return result;
}
2019-03-15 23:35:09 +08:00
void Indicator::setContext(const Stock& stock, const KQuery& query) {
2019-11-10 03:27:57 +08:00
if (m_imp)
m_imp->setContext(stock, query);
2019-03-15 23:35:09 +08:00
}
void Indicator::setContext(const KData& k) {
2019-11-10 03:27:57 +08:00
if (m_imp)
m_imp->setContext(k);
}
2019-03-30 23:13:37 +08:00
KData Indicator::getContext() const {
return m_imp ? m_imp->getContext() : KData();
}
2015-01-07 01:26:14 +08:00
Indicator& Indicator::operator=(const Indicator& indicator) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(this == &indicator, *this);
2019-11-10 19:45:57 +08:00
m_imp = indicator.m_imp;
2015-01-07 01:26:14 +08:00
return *this;
}
2019-03-24 00:17:11 +08:00
PriceList Indicator::getResultAsPriceList(size_t num) const {
2020-11-22 18:34:37 +08:00
HKU_WARN_IF_RETURN(!m_imp, PriceList(), "indicator imptr is null!");
2019-03-24 00:17:11 +08:00
return m_imp->getResultAsPriceList(num);
}
Indicator Indicator::getResult(size_t num) const {
2020-11-22 18:34:37 +08:00
HKU_WARN_IF_RETURN(!m_imp, Indicator(), "indicator imptr is null!");
2019-03-24 00:17:11 +08:00
return m_imp->getResult(num);
}
2019-03-17 23:07:30 +08:00
Indicator Indicator::operator()(const Indicator& ind) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!m_imp, Indicator());
HKU_IF_RETURN(!ind.getImp(), Indicator(m_imp));
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = m_imp->clone();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::OP, IndicatorImpPtr(), ind.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator+(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::ADD, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator-(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::SUB, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator*(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::MUL, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator/(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::DIV, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-05-01 21:26:50 +08:00
HKU_API Indicator operator%(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-05-01 21:26:50 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
p->add(IndicatorImp::MOD, ind1.getImp(), ind2.getImp());
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator==(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::EQ, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator!=(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::NE, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator>(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::GT, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator<(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::LT, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator>=(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-17 23:07:30 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::GE, ind1.getImp(), ind2.getImp());
2019-03-17 23:07:30 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator<=(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-15 01:49:07 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::LE, ind1.getImp(), ind2.getImp());
2019-03-15 01:49:07 +08:00
return p->calculate();
2015-01-07 01:26:14 +08:00
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator&(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-18 03:00:42 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::AND, ind1.getImp(), ind2.getImp());
2019-03-18 03:00:42 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator|(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator());
2019-03-18 03:00:42 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-24 00:17:11 +08:00
p->add(IndicatorImp::OR, ind1.getImp(), ind2.getImp());
2019-03-18 03:00:42 +08:00
return p->calculate();
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator+(const Indicator& ind, price_t val) {
return ind + CVAL(ind, val);
}
HKU_API Indicator operator+(price_t val, const Indicator& ind) {
return CVAL(ind, val) + ind;
}
HKU_API Indicator operator-(const Indicator& ind, price_t val) {
return ind - CVAL(ind, val);
}
HKU_API Indicator operator-(price_t val, const Indicator& ind) {
return CVAL(ind, val) - ind;
}
HKU_API Indicator operator*(const Indicator& ind, price_t val) {
return ind * CVAL(ind, val);
}
HKU_API Indicator operator*(price_t val, const Indicator& ind) {
return CVAL(ind, val) * ind;
}
HKU_API Indicator operator/(const Indicator& ind, price_t val) {
return ind / CVAL(ind, val);
}
HKU_API Indicator operator/(price_t val, const Indicator& ind) {
return CVAL(ind, val) / ind;
}
2019-05-01 21:26:50 +08:00
HKU_API Indicator operator%(const Indicator& ind, price_t val) {
return ind % CVAL(ind, val);
}
HKU_API Indicator operator%(price_t val, const Indicator& ind) {
return CVAL(ind, val) % ind;
}
2019-03-22 23:12:28 +08:00
HKU_API Indicator operator==(const Indicator& ind, price_t val) {
return ind == CVAL(ind, val);
}
HKU_API Indicator operator==(price_t val, const Indicator& ind) {
return CVAL(ind, val) == ind;
}
HKU_API Indicator operator!=(const Indicator& ind, price_t val) {
return ind != CVAL(ind, val);
}
HKU_API Indicator operator!=(price_t val, const Indicator& ind) {
return CVAL(ind, val) != ind;
}
HKU_API Indicator operator>(const Indicator& ind, price_t val) {
return ind > CVAL(ind, val);
}
HKU_API Indicator operator>(price_t val, const Indicator& ind) {
return CVAL(ind, val) > ind;
}
HKU_API Indicator operator<(const Indicator& ind, price_t val) {
return ind < CVAL(ind, val);
}
HKU_API Indicator operator<(price_t val, const Indicator& ind) {
return CVAL(ind, val) < ind;
}
HKU_API Indicator operator>=(const Indicator& ind, price_t val) {
return ind >= CVAL(ind, val);
}
HKU_API Indicator operator>=(price_t val, const Indicator& ind) {
return CVAL(ind, val) >= ind;
}
HKU_API Indicator operator<=(const Indicator& ind, price_t val) {
return ind <= CVAL(ind, val);
}
HKU_API Indicator operator<=(price_t val, const Indicator& ind) {
return CVAL(ind, val) <= ind;
}
2019-03-23 02:27:28 +08:00
HKU_API Indicator operator&(const Indicator& ind, price_t val) {
return ind & CVAL(ind, val);
}
HKU_API Indicator operator&(price_t val, const Indicator& ind) {
return CVAL(ind, val) & ind;
}
HKU_API Indicator operator|(const Indicator& ind, price_t val) {
return ind | CVAL(ind, val);
}
HKU_API Indicator operator|(price_t val, const Indicator& ind) {
return CVAL(ind, val) | ind;
}
Indicator HKU_API WEAVE(const Indicator& ind1, const Indicator& ind2) {
2020-11-22 18:34:37 +08:00
HKU_ERROR_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator(),
"ind1 or ind2 is Null Indicator!");
IndicatorImpPtr p = make_shared<IndicatorImp>();
p->add(IndicatorImp::WEAVE, ind1.getImp(), ind2.getImp());
return p->calculate();
}
2019-11-10 03:27:57 +08:00
Indicator HKU_API IF(const Indicator& ind1, const Indicator& ind2, const Indicator& ind3) {
2020-11-22 18:34:37 +08:00
HKU_ERROR_IF_RETURN(!ind1.getImp() || !ind2.getImp() || !ind3.getImp(), Indicator(),
"Exists null indicator!");
2019-03-24 23:27:48 +08:00
IndicatorImpPtr p = make_shared<IndicatorImp>();
2019-03-25 00:55:45 +08:00
p->add_if(ind1.getImp(), ind2.getImp(), ind3.getImp());
2019-03-24 23:27:48 +08:00
return p->calculate();
}
2019-11-10 03:27:57 +08:00
Indicator HKU_API IF(const Indicator& x, price_t a, const Indicator& b) {
return IF(x, CVAL(b, a), b);
}
Indicator HKU_API IF(const Indicator& x, const Indicator& a, price_t b) {
return IF(x, a, CVAL(a, b));
}
Indicator HKU_API IF(const Indicator& x, price_t a, price_t b) {
return IF(x, CVAL(x, a), CVAL(x, b));
}
2019-03-23 02:27:28 +08:00
2015-01-07 01:26:14 +08:00
} /* namespace hku */