Skip to content

Commit

Permalink
fixed DMA
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Sep 9, 2024
1 parent 754a960 commit 491d176
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
8 changes: 1 addition & 7 deletions hikyuu_cpp/hikyuu/indicator/crt/DMA.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ namespace hku {
* @param a 动态系数
* @ingroup Indicator
*/
Indicator DMA(const Indicator& ind1, const Indicator& a);

inline Indicator DMA(const Indicator& ind1, const Indicator& a) {
Indicator dma = a * ind1 + (1 - a) * REF(ind1, 1);
dma.name("DMA");
return dma;
}
Indicator HKU_API DMA(const Indicator& x, const Indicator& a);

} // namespace hku

Expand Down
59 changes: 59 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-03-10
* Author: fasiondog
*/

#include "hikyuu/indicator/crt/ALIGN.h"
#include "IDma.h"

#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IDma)
#endif

namespace hku {

IDma::IDma() : IndicatorImp("DMA") {}

IDma::IDma(const Indicator& ref_ind) : IndicatorImp("DMA"), m_ref_a(ref_ind) {}

IDma::~IDma() {}

void IDma::_checkParam(const string& name) const {}

IndicatorImpPtr IDma::_clone() {
auto p = make_shared<IDma>();
p->m_ref_a = m_ref_a.clone();
return p;
}

void IDma::_calculate(const Indicator& ind) {
auto k = getContext();
m_ref_a.setContext(k);
Indicator ref = m_ref_a;
if (m_ref_a.size() != ind.size()) {
ref = ALIGN(m_ref_a, ind);
}

size_t total = ind.size();
_readyBuffer(total, 1);
HKU_IF_RETURN(total == 0, void());

m_discard = std::max(ind.discard(), ref.discard());
auto* y = this->data();
const auto* a = ref.data();
const auto* x = ind.data();
y[m_discard] = x[m_discard];
for (size_t i = m_discard + 1; i < total; i++) {
y[i] = a[i] * x[i] + (1 - a[i]) * y[i - 1];
}
}

Indicator HKU_API DMA(const Indicator& x, const Indicator& a) {
auto p = make_shared<IDma>(a);
Indicator result(p);
return result(x);
}

} // namespace hku
47 changes: 47 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/imp/IDma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-09-09
* Author: fasiondog
*/

#pragma once

#include "../Indicator.h"

namespace hku {

/*
* 动态移动平均
* 用法:DMA(X,A),求X的动态移动平均。
* 算法:若Y=DMA(X,A) 则 Y=A*X+(1-A)*Y',其中Y'表示上一周期Y值。
* 例如:DMA(CLOSE,VOL/CAPITAL)表示求以换手率作平滑因子的平均价
*/
class IDma : public IndicatorImp {
public:
IDma();
explicit IDma(const Indicator& ref_a);
virtual ~IDma();

virtual void _checkParam(const string& name) const override;
virtual void _calculate(const Indicator& data) override;
virtual IndicatorImpPtr _clone() override;

private:
Indicator m_ref_a;

//============================================
// 序列化支持
//============================================
#if HKU_SUPPORT_SERIALIZATION
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp);
ar& BOOST_SERIALIZATION_NVP(m_ref_a);
}
#endif
};

}

0 comments on commit 491d176

Please sign in to comment.