This commit is contained in:
fasiondog 2019-03-23 18:54:10 +08:00
parent 5ea070cc1f
commit 1cd2427b69
17 changed files with 60 additions and 16 deletions

View File

@ -79,7 +79,7 @@ def draw(stock, query = Query(-130),
slow_op = EMA(n = 2*n)(fast_op)
sg = SG_Cross(fast_op, slow_op)
sg.plot(axes = ax1, kdata = kdata)
ind = slow_op(kdata)
ind = slow_op(KDATA(kdata))
ind.name = "EMA(CAMA)"
ind.plot(axes = ax1, color = 'm', legend_on = True)

View File

@ -80,6 +80,10 @@ size_t Indicator::size() const {
return m_imp ? m_imp->size() : 0;
}
Indicator Indicator::clone() const {
return m_imp ? Indicator(m_imp->clone()) : Indicator();
}
Indicator Indicator::operator()(const Indicator& ind) {
if (!m_imp)
return Indicator();

View File

@ -56,6 +56,8 @@ public:
/** 返回形如Name(param1_val,param2_val,...) */
string long_name() const;
Indicator clone() const;
void setContext(const Stock&, const KQuery&);
KData getCurrentKData() const {

View File

@ -95,8 +95,12 @@ public:
void _set(price_t val, size_t pos, size_t num = 0) {
#if CHECK_ACCESS_BOUND
if ((m_pBuffer[num] == NULL) || pos>= m_pBuffer[num]->size()) {
throw(std::out_of_range("Try to access value out of bounds! "
+ name() + " [IndicatorImp::_set]"));
std::stringstream err_info;
err_info << "Try to access value out of bounds! "
<< name() << " [IndicatorImp::_set]";
HKU_FATAL(err_info.str());
throw(std::out_of_range(err_info.str()));
return;
}
(*m_pBuffer[num])[pos] = val;
#else

View File

@ -38,12 +38,16 @@ bool Ama::check() {
void Ama::_calculate(const Indicator& data) {
size_t total = data.size();
m_discard = data.discard();
if (m_discard >= total) {
m_discard = total;
return;
}
int n = getParam<int>("n");
int fast_n = getParam<int>("fast_n");
int slow_n = getParam<int>("slow_n");
m_discard = data.discard();
size_t start = m_discard;
price_t fastest = 2.0 / (fast_n + 1);

View File

@ -28,14 +28,13 @@ bool Atr::check() {
void Atr::_calculate(const Indicator& indicator) {
size_t total = indicator.size();
int n = getParam<int>("n");
m_discard = indicator.discard();
if (total <= m_discard) {
if (m_discard >= total) {
m_discard = total;
return;
}
int n = getParam<int>("n");
size_t startPos = discard();
price_t ema = indicator[startPos];
_set(ema, startPos);

View File

@ -26,6 +26,7 @@ void Diff::_calculate(const Indicator& data) {
m_discard = data.discard() + 1;
if (total <= m_discard) {
m_discard = total;
return;
}

View File

@ -29,14 +29,13 @@ bool Ema::check() {
void Ema::_calculate(const Indicator& indicator) {
size_t total = indicator.size();
int n = getParam<int>("n");
m_discard = indicator.discard();
if (total <= m_discard) {
m_discard = total;
return;
}
int n = getParam<int>("n");
size_t startPos = discard();
price_t ema = indicator[startPos];
_set(ema, startPos);

View File

@ -34,6 +34,10 @@ void HighLine::_calculate(const Indicator& data) {
int n = getParam<int>("n");
m_discard = data.discard() + n - 1;
if (m_discard >= total) {
m_discard = total;
return;
}
size_t pos = m_discard + 1 - n;
price_t max = 0;

View File

@ -34,6 +34,10 @@ void LowLine::_calculate(const Indicator& data) {
int n = getParam<int>("n");
m_discard = data.discard() + n - 1;
if (m_discard >= total) {
m_discard = total;
return;
}
size_t pos = m_discard + 1 - n;
price_t min = 0;

View File

@ -32,6 +32,10 @@ bool Macd::check() {
void Macd::_calculate(const Indicator& data) {
size_t total = data.size();
if (total == 0) {
return;
}
_readyBuffer(total, 3);
int n1 = getParam<int>("n1");
@ -40,6 +44,7 @@ void Macd::_calculate(const Indicator& data) {
m_discard = data.discard();
if (total <= m_discard) {
m_discard = total;
return;
}

View File

@ -33,6 +33,10 @@ void RightShift::_calculate(const Indicator& data) {
int n = getParam<int>("n");
m_discard = data.discard() + n;
if (m_discard >= total) {
m_discard = total;
return;
}
for (size_t i = m_discard; i < total; ++i) {
_set(data[i-n], i);
}

View File

@ -39,6 +39,9 @@ bool SaftyLoss::check() {
void SaftyLoss::_calculate(const Indicator& data) {
size_t total = data.size();
if (total == 0) {
return;
}
_readyBuffer(total, 1);
int n1 = getParam<int>("n1");
@ -46,7 +49,8 @@ void SaftyLoss::_calculate(const Indicator& data) {
double p = getParam<double>("p");
m_discard = data.discard() + n1 + n2 - 2;
if (0 == total) {
if (m_discard >= total) {
m_discard = total;
return;
}

View File

@ -29,9 +29,13 @@ bool Sma::check() {
void Sma::_calculate(const Indicator& indicator) {
size_t total = indicator.size();
int n = getParam<int>("n");
m_discard = indicator.discard();
if (m_discard >= total) {
m_discard = total;
return;
}
int n = getParam<int>("n");
size_t startPos = m_discard;
price_t sum = 0.0;
size_t count = 1;

View File

@ -34,6 +34,10 @@ void StdDeviation::_calculate(const Indicator& data) {
int n = getParam<int>("n");
m_discard = data.discard() + n - 1;
if (m_discard >= total) {
m_discard = total;
return;
}
Indicator ma = MA(data, n);
size_t N = n - 1;

View File

@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE( test_SAFTYLOSS ) {
result = SAFTYLOSS(CLOSE(kdata), 2, 1);
BOOST_CHECK(result.empty() == true);
BOOST_CHECK(result.size() == 0);
BOOST_CHECK(result.discard() == 1);
BOOST_CHECK(result.discard() == 0);
/** @arg 参数n1、n2非法 */
query = KQuery(0, 20);

View File

@ -57,7 +57,9 @@ void export_Indicator() {
.def("getParam", &Indicator::getParam<boost::any>)
.def("setParam", &Indicator::setParam<object>)
.def("size", &Indicator::size)
.def("empty", & Indicator::empty)
.def("empty", &Indicator::empty)
.def("clone", &Indicator::clone)
.def("formula", &Indicator::formula)
.def("getResultNumber", &Indicator::getResultNumber)
.def("get", &Indicator::get, get_overloads())
.def("getResult", &Indicator::getResult)