-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
|
||
} |