hikyuu2/hikyuu_cpp/hikyuu/indicator/Indicator.cpp

356 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 {
HKU_API std::ostream & operator<<(std::ostream& os, const Indicator& indicator) {
os << indicator.m_imp;
return os;
}
Indicator::Indicator(const IndicatorImpPtr& imp): m_imp(imp) {
}
Indicator::Indicator(const Indicator& indicator) {
m_imp = indicator.m_imp;
}
Indicator::~Indicator() {
}
2019-03-14 01:53:13 +08:00
string Indicator::formula() const {
return m_imp ? m_imp->formula() : "Indicator";
}
2019-03-15 23:35:09 +08:00
void Indicator::setContext(const Stock& stock, const KQuery& query) {
if (m_imp) m_imp->setContext(stock, query);
}
2015-01-07 01:26:14 +08:00
Indicator& Indicator::operator=(const Indicator& indicator) {
if (this == &indicator)
return *this;
if (m_imp != indicator.m_imp)
m_imp = indicator.m_imp;
return *this;
}
string Indicator::name() const {
return m_imp ? m_imp->name() : "IndicatorImp";
}
2016-04-03 00:08:31 +08:00
void Indicator::name(const string& name) {
if (m_imp) {
m_imp->name(name);
}
}
2015-01-07 01:26:14 +08:00
string Indicator::long_name() const {
return m_imp ? m_imp->long_name() : "IndicatorImp()";
}
size_t Indicator::discard() const {
return m_imp ? m_imp->discard() : 0 ;
}
2016-04-03 00:08:31 +08:00
void Indicator::setDiscard(size_t discard) {
if (m_imp)
m_imp->setDiscard(discard);
}
2015-01-07 01:26:14 +08:00
size_t Indicator::getResultNumber() const {
return m_imp ? m_imp->getResultNumber() : 0;
}
bool Indicator::empty() const {
return (!m_imp || m_imp->size() == 0) ? true : false;
}
size_t Indicator::size() const {
return m_imp ? m_imp->size() : 0;
}
2019-03-23 18:54:10 +08:00
Indicator Indicator::clone() const {
return m_imp ? Indicator(m_imp->clone()) : Indicator();
}
2019-03-24 00:17:11 +08:00
PriceList Indicator::getResultAsPriceList(size_t num) const {
if (!m_imp) {
HKU_WARN("indicator imptr is null! [Indicator::getResultAsPriceList]");
return PriceList();
}
return m_imp->getResultAsPriceList(num);
}
Indicator Indicator::getResult(size_t num) const {
if (!m_imp) {
HKU_WARN("indicator imptr is null! [Indicator::getResult]");
return Indicator();
}
return m_imp->getResult(num);
}
2019-03-17 23:07:30 +08:00
Indicator Indicator::operator()(const Indicator& ind) {
if (!m_imp)
return Indicator();
if (!ind.getImp())
return Indicator(m_imp);
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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-03-22 23:12:28 +08:00
HKU_API Indicator operator==(const Indicator& ind1, const Indicator& ind2) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-17 23:07:30 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2015-01-07 01:26:14 +08:00
return 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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-18 03:00:42 +08:00
return Indicator();
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
2019-03-18 03:00:42 +08:00
return Indicator();
}
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;
}
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) {
if (!ind1.getImp() || !ind2.getImp()) {
HKU_ERROR("ind1 or ind2 is Null Indicator! [WEAVE]");
return Indicator();
}
IndicatorImpPtr p = make_shared<IndicatorImp>();
p->add(IndicatorImp::WEAVE, ind1.getImp(), ind2.getImp());
return p->calculate();
}
2019-03-24 23:27:48 +08:00
Indicator HKU_API IF(const Indicator& ind1,
const Indicator& ind2, const Indicator& ind3) {
if (!ind1.getImp() || !ind2.getImp() || !ind3.getImp()) {
2019-03-25 00:55:45 +08:00
HKU_ERROR("Exists null indicator! [IF]");
2019-03-24 23:27:48 +08:00
return Indicator();
}
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-03-23 02:27:28 +08:00
2015-01-07 01:26:14 +08:00
} /* namespace hku */