扩展 SG_Band, 支持3指标

This commit is contained in:
fasiondog 2024-05-18 12:37:38 +08:00
parent c1795d988e
commit f90fd135db
5 changed files with 135 additions and 4 deletions

View File

@ -13,7 +13,24 @@
namespace hku {
SignalPtr HKU_API SG_Band(const Indicator& sig, price_t lower, price_t upper);
/**
* ,
* @note RSI
* @param ind
* @param lower
* @param upper
* @return SignalPtr
*/
SignalPtr HKU_API SG_Band(const Indicator& ind, price_t lower, price_t upper);
/**
* ,
* @param ind
* @param lower
* @param upper
* @return SignalPtr
*/
SignalPtr HKU_API SG_Band(const Indicator& ind, const Indicator& lower, const Indicator& upper);
} /* namespace hku */

View File

@ -16,7 +16,7 @@ namespace hku {
BandSignal::BandSignal() : SignalBase("SG_Band") {}
BandSignal::BandSignal(const Indicator& ind, price_t lower, price_t upper)
: SignalBase("SG_Band"), m_ind(ind), m_lower(lower), m_upper(upper) {
: SignalBase("SG_Band"), m_ind(ind.clone()), m_lower(lower), m_upper(upper) {
HKU_CHECK(lower > upper, "BandSignal: lower track is greater than upper track");
}

View File

@ -0,0 +1,63 @@
/*
* BandSignal2.cpp
*
* Created on: 20230923
* Author: yangrq1018
*/
#include "../../../indicator/crt/KDATA.h"
#include "BandSignal2.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::BandSignal2)
#endif
namespace hku {
BandSignal2::BandSignal2() : SignalBase("SG_Band") {}
BandSignal2::BandSignal2(const Indicator& ind, const Indicator& lower, const Indicator& upper)
: SignalBase("SG_Band"), m_ind(ind.clone()), m_lower(lower.clone()), m_upper(upper.clone()) {}
BandSignal2::~BandSignal2() {}
SignalPtr BandSignal2::_clone() {
BandSignal2* p = new BandSignal2();
p->m_upper = m_upper.clone();
p->m_lower = m_lower.clone();
p->m_ind = m_ind.clone();
return SignalPtr(p);
}
void BandSignal2::_calculate(const KData& kdata) {
Indicator ind = m_ind(kdata);
Indicator upper = m_upper(kdata);
Indicator lower = m_lower(kdata);
HKU_ASSERT(ind.size() == upper.size() && ind.size() == lower.size());
size_t discard = ind.discard();
if (discard < upper.discard()) {
discard = upper.discard();
}
if (discard < lower.discard()) {
discard = lower.discard();
}
size_t total = ind.size();
auto const* inddata = ind.data();
auto const* upperdata = upper.data();
auto const* lowerdata = lower.data();
auto const* ks = kdata.data();
for (size_t i = discard; i < total; ++i) {
if (inddata[i] > upperdata[i]) {
_addBuySignal(ks[i].datetime);
} else if (inddata[i] < lowerdata[i]) {
_addSellSignal(ks[i].datetime);
}
}
}
SignalPtr HKU_API SG_Band(const Indicator& sig, const Indicator& lower, const Indicator& upper) {
return SignalPtr(new BandSignal2(sig, lower, upper));
}
} // namespace hku

View File

@ -0,0 +1,47 @@
/*
* BandSignal2.h
*
* Created on: 20230923
* Author: yangrq1018
*/
#pragma once
#ifndef TRADE_SYS_SIGNAL_IMP_BANDSIGNAL_H_
#define TRADE_SYS_SIGNAL_IMP_BANDSIGNAL_H_
#include "../../../indicator/Indicator.h"
#include "../SignalBase.h"
namespace hku {
class BandSignal2 : public SignalBase {
public:
BandSignal2();
BandSignal2(const Indicator& sig, const Indicator& lower, const Indicator& upper);
virtual ~BandSignal2();
virtual SignalPtr _clone() override;
virtual void _calculate(const KData& kdata) override;
private:
Indicator m_ind;
Indicator m_lower;
Indicator m_upper;
//============================================
// 序列化支持
//============================================
#if HKU_SUPPORT_SERIALIZATION
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(SignalBase);
ar& BOOST_SERIALIZATION_NVP(m_ind);
ar& BOOST_SERIALIZATION_NVP(m_lower);
ar& BOOST_SERIALIZATION_NVP(m_upper);
}
#endif
};
} // namespace hku
#endif

View File

@ -204,14 +204,18 @@ void export_Signal(py::module& m) {
:param int slow_n: 线EMA周期
:return: )");
m.def("SG_Band", SG_Band, py::arg("ind"), py::arg("lower"), py::arg("upper"),
m.def("SG_Band",
py::overload_cast<const Indicator&, const Indicator&, const Indicator&>(SG_Band),
py::arg("ind"), py::arg("lower"), py::arg("upper"));
m.def("SG_Band", py::overload_cast<const Indicator&, price_t, price_t>(SG_Band), py::arg("ind"),
py::arg("lower"), py::arg("upper"),
R"(SG_Band(ind, lower, upper)
,
::
SG_Band(MA(C, n=10), 100, 200)
)");
SG_Band(CLOSE, MA(LOW), MA(HIGH)))");
m.def("SG_AllwaysBuy", SG_AllwaysBuy, R"(SG_AllwaysBuy()