From 5d382872282eebb87da6ece123ead26cbe958690 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 3 Feb 2025 02:06:05 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu_cpp/hikyuu/indicator/crt/WINNER.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hikyuu_cpp/hikyuu/indicator/crt/WINNER.h b/hikyuu_cpp/hikyuu/indicator/crt/WINNER.h index 271374b53..9422d9366 100644 --- a/hikyuu_cpp/hikyuu/indicator/crt/WINNER.h +++ b/hikyuu_cpp/hikyuu/indicator/crt/WINNER.h @@ -14,7 +14,13 @@ namespace hku { /** - * 求绝对值 + * 获利盘比例 + * @details + *
+ * 用法: WINNER(CLOSE) 表示以当前收市价卖出的获利盘比例。
+ * 例如: 返回0.1表示10%获利盘;WINNER(10.5)表示10.5元价格的获利盘比例
+ * 该函数仅对日线分析周期有效。
+ * 
* @ingroup Indicator */ Indicator HKU_API WINNER(); From 90ae0d27d58eff8ee38fc1bfc604fe469538c370 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 3 Feb 2025 02:50:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E4=B8=BA=E5=8F=8C=E6=8C=87=E6=A0=87=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E6=8C=87=E6=A0=87=E8=AE=A1=E7=AE=97=E6=97=B6=E5=AF=B9=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=BA=8F=E5=88=97=E5=AF=B9=E9=BD=90=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp | 2 +- hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp | 5 ++++- hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp | 2 +- hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp | 2 +- hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp index 1155fef0d..1b15a6f3f 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp @@ -58,7 +58,7 @@ void ICorr::_calculate(const Indicator& ind) { } else if (ref.size() < ind.size()) { ref = CVAL(ind, 0.) + ref; } - } else if (m_ref_ind.size() != ind.size()) { + } else if (k != ind.getContext()) { ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); } diff --git a/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp b/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp index 29ac67277..3cd41d7d8 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp @@ -8,6 +8,7 @@ #include "hikyuu/indicator/crt/ALIGN.h" #include "hikyuu/indicator/crt/CVAL.h" #include "hikyuu/indicator/crt/SLICE.h" +#include "hikyuu/indicator/crt/CONTEXT.h" #include "IDma.h" #if HKU_SUPPORT_SERIALIZATION @@ -45,12 +46,14 @@ void IDma::_calculate(const Indicator& ind) { Indicator ref = m_ref_ind; auto dates = ref.getDatetimeList(); if (dates.empty()) { + // 如果不是时间序列,则以 ind 为基准,按右端对齐,不足用 nan 填充, 超长则截断左端 if (ref.size() > ind.size()) { ref = SLICE(ref, ref.size() - ind.size(), ref.size()); } else if (ref.size() < ind.size()) { ref = CVAL(ind, 0.) + ref; } - } else if (m_ref_ind.size() != ind.size()) { + } else if (k != ind.getContext()) { + // 如果是时间序列,当两者的上下文不同,则按日期对齐 ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); } diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp index 9674854c0..105fba14b 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp @@ -92,7 +92,7 @@ void ISpearman::_calculate(const Indicator &ind) { } else if (ref.size() < ind.size()) { ref = CVAL(ind, 0.) + ref; } - } else if (m_ref_ind.size() != ind.size()) { + } else if (k != ind.getContext()) { ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); } diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp index c626a9548..61c7eb8b9 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp @@ -69,7 +69,7 @@ void TaMavp::_calculate(const Indicator& ind) { } else if (ref.size() < ind.size()) { ref = CVAL(ind, 0.) + ref; } - } else if (m_ref_ind.size() != ind.size()) { + } else if (k != ind.getContext()) { ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); } diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h index 774329371..88fa8d920 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h @@ -478,7 +478,7 @@ } else if (ref.size() < ind.size()) { \ ref = CVAL(ind, 0.) + ref; \ } \ - } else if (m_ref_ind.size() != ind.size()) { \ + } else if (k != ind.getContext()) { \ ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); \ } \ \ @@ -564,7 +564,7 @@ } else if (ref.size() < ind.size()) { \ ref = CVAL(ind, 0.) + ref; \ } \ - } else if (m_ref_ind.size() != ind.size()) { \ + } else if (k != ind.getContext()) { \ ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); \ } \ \ From 9b23820aeb61e0cf2d40f77ca357bdab623ddd8b Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 3 Feb 2025 04:25:19 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E8=BE=93=E5=85=A5=E6=8C=87=E6=A0=87=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hikyuu/indicator/Indicator2InImp.cpp | 64 ++++++++++++++++++ hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h | 65 +++++++++++++++++++ hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp | 31 +-------- hikyuu_cpp/hikyuu/indicator/imp/ICorr.h | 27 ++------ hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp | 39 ++--------- hikyuu_cpp/hikyuu/indicator/imp/IDma.h | 29 ++------- hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp | 31 +-------- hikyuu_cpp/hikyuu/indicator/imp/ISpearman.h | 25 ++----- 8 files changed, 156 insertions(+), 155 deletions(-) create mode 100644 hikyuu_cpp/hikyuu/indicator/Indicator2InImp.cpp create mode 100644 hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h diff --git a/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.cpp b/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.cpp new file mode 100644 index 000000000..406b8821d --- /dev/null +++ b/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 hikyuu.org + * + * Created on: 2025-02-03 + * Author: fasiondog + */ + +#include "hikyuu/indicator/crt/ALIGN.h" +#include "hikyuu/indicator/crt/CVAL.h" +#include "hikyuu/indicator/crt/SLICE.h" +#include "hikyuu/indicator/crt/CONTEXT.h" +#include "Indicator2InImp.h" + +#if HKU_SUPPORT_SERIALIZATION +BOOST_CLASS_EXPORT(hku::Indicator2InImp) +#endif + +namespace hku { + +Indicator2InImp::Indicator2InImp() : IndicatorImp("Indicator2InImp") { + setParam("fill_null", true); +} + +Indicator2InImp::Indicator2InImp(const string& name, size_t result_num) +: IndicatorImp(name, result_num) { + setParam("fill_null", true); +} + +Indicator2InImp::Indicator2InImp(const string& name, const Indicator& ref_ind, bool fill_null, + size_t result_num) +: IndicatorImp(name, result_num), m_ref_ind(ref_ind) { + setParam("fill_null", fill_null); +} + +Indicator2InImp::~Indicator2InImp() {} + +IndicatorImpPtr Indicator2InImp::_clone() { + auto p = make_shared(); + p->m_ref_ind = m_ref_ind.clone(); + return p; +} + +Indicator Indicator2InImp::prepare(const Indicator& ind) { + auto k = getContext(); + m_ref_ind.setContext(k); + + Indicator ref = m_ref_ind; + auto dates = ref.getDatetimeList(); + if (dates.empty()) { + // 如果不是时间序列,则以 ind 为基准,按右端对齐,不足用 nan 填充, 超长则截断左端 + if (ref.size() > ind.size()) { + ref = SLICE(ref, ref.size() - ind.size(), ref.size()); + } else if (ref.size() < ind.size()) { + ref = CVAL(ind, 0.) + ref; + } + } else if (k != ind.getContext()) { + // 如果是时间序列,当两者的上下文不同,则按日期对齐 + ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); + } + + return ref; +} + +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h b/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h new file mode 100644 index 000000000..014765547 --- /dev/null +++ b/hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 hikyuu.org + * + * Created on: 2025-02-03 + * Author: fasiondog + */ + +#pragma once + +#include "Indicator.h" + +namespace hku { + +class Indicator2InImp : public IndicatorImp { +public: + Indicator2InImp(); + explicit Indicator2InImp(const string& name, size_t result_num = 1); + Indicator2InImp(const string& name, const Indicator& ref_a, bool fill_null = false, + size_t result_num = 1); + virtual ~Indicator2InImp(); + + virtual IndicatorImpPtr _clone() override; + +protected: + Indicator prepare(const Indicator& ind); + +protected: + Indicator m_ref_ind; + +//============================================ +// 序列化支持 +//============================================ +#if HKU_SUPPORT_SERIALIZATION +private: + friend class boost::serialization::access; + template + void serialize(Archive& ar, const unsigned int version) { + ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); + ar& BOOST_SERIALIZATION_NVP(m_ref_ind); + } +#endif +}; + +#if HKU_SUPPORT_SERIALIZATION +#define INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION \ +private: \ + friend class boost::serialization::access; \ + template \ + void serialize(Archive& ar, const unsigned int version) { \ + ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Indicator2InImp); \ + } +#else +#define INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION +#endif + +#define INDICATOR2IN_IMP(classname) \ +public: \ + virtual void _calculate(const Indicator& data) override; \ + virtual IndicatorImpPtr _clone() override { \ + auto p = make_shared(); \ + p->m_ref_ind = m_ref_ind.clone(); \ + return p; \ + } + +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp index 1b15a6f3f..c27c204c3 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.cpp @@ -5,9 +5,6 @@ * Author: fasiondog */ -#include "hikyuu/indicator/crt/ALIGN.h" -#include "hikyuu/indicator/crt/CVAL.h" -#include "hikyuu/indicator/crt/SLICE.h" #include "ICorr.h" #if HKU_SUPPORT_SERIALIZATION @@ -16,15 +13,13 @@ BOOST_CLASS_EXPORT(hku::ICorr) namespace hku { -ICorr::ICorr() : IndicatorImp("CORR") { +ICorr::ICorr() : Indicator2InImp("CORR") { setParam("n", 10); - setParam("fill_null", true); } ICorr::ICorr(const Indicator& ref_ind, int n, bool fill_null) -: IndicatorImp("CORR"), m_ref_ind(ref_ind) { +: Indicator2InImp("CORR", ref_ind, fill_null, 2) { setParam("n", n); - setParam("fill_null", fill_null); } ICorr::~ICorr() {} @@ -36,31 +31,11 @@ void ICorr::_checkParam(const string& name) const { } } -IndicatorImpPtr ICorr::_clone() { - auto p = make_shared(); - p->m_ref_ind = m_ref_ind.clone(); - return p; -} - void ICorr::_calculate(const Indicator& ind) { size_t total = ind.size(); HKU_IF_RETURN(total == 0, void()); - _readyBuffer(total, 2); - - auto k = getContext(); - m_ref_ind.setContext(k); - Indicator ref = m_ref_ind; - auto dates = ref.getDatetimeList(); - if (dates.empty()) { - if (ref.size() > ind.size()) { - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); - } else if (ref.size() < ind.size()) { - ref = CVAL(ind, 0.) + ref; - } - } else if (k != ind.getContext()) { - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); - } + Indicator ref = prepare(ind); int n = getParam("n"); if (n == 0) { diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.h b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.h index 52a4e68d4..508505471 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ICorr.h +++ b/hikyuu_cpp/hikyuu/indicator/imp/ICorr.h @@ -7,35 +7,20 @@ #pragma once -#include "../Indicator.h" +#include "../Indicator2InImp.h" namespace hku { -class ICorr : public IndicatorImp { +class ICorr : public Indicator2InImp { + INDICATOR2IN_IMP(ICorr) + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION + public: ICorr(); ICorr(const Indicator& ref_ind, int n, bool fill_null); virtual ~ICorr(); virtual void _checkParam(const string& name) const override; - virtual void _calculate(const Indicator& data) override; - virtual IndicatorImpPtr _clone() override; - -private: - Indicator m_ref_ind; - -//============================================ -// 序列化支持 -//============================================ -#if HKU_SUPPORT_SERIALIZATION -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) { - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); - } -#endif }; -} \ No newline at end of file +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp b/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp index 3cd41d7d8..9d40a0a91 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp @@ -5,10 +5,6 @@ * Author: fasiondog */ -#include "hikyuu/indicator/crt/ALIGN.h" -#include "hikyuu/indicator/crt/CVAL.h" -#include "hikyuu/indicator/crt/SLICE.h" -#include "hikyuu/indicator/crt/CONTEXT.h" #include "IDma.h" #if HKU_SUPPORT_SERIALIZATION @@ -17,45 +13,18 @@ BOOST_CLASS_EXPORT(hku::IDma) namespace hku { -IDma::IDma() : IndicatorImp("DMA") { - setParam("fill_null", true); -} +IDma::IDma() : Indicator2InImp("DMA") {} -IDma::IDma(const Indicator& ref_ind, bool fill_null) : IndicatorImp("DMA"), m_ref_ind(ref_ind) { - setParam("fill_null", fill_null); -} +IDma::IDma(const Indicator& ref_ind, bool fill_null) +: Indicator2InImp("DMA", ref_ind, fill_null, 1) {} IDma::~IDma() {} -void IDma::_checkParam(const string& name) const {} - -IndicatorImpPtr IDma::_clone() { - auto p = make_shared(); - p->m_ref_ind = m_ref_ind.clone(); - return p; -} - void IDma::_calculate(const Indicator& ind) { size_t total = ind.size(); HKU_IF_RETURN(total == 0, void()); - _readyBuffer(total, 1); - - auto k = getContext(); - m_ref_ind.setContext(k); - Indicator ref = m_ref_ind; - auto dates = ref.getDatetimeList(); - if (dates.empty()) { - // 如果不是时间序列,则以 ind 为基准,按右端对齐,不足用 nan 填充, 超长则截断左端 - if (ref.size() > ind.size()) { - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); - } else if (ref.size() < ind.size()) { - ref = CVAL(ind, 0.) + ref; - } - } else if (k != ind.getContext()) { - // 如果是时间序列,当两者的上下文不同,则按日期对齐 - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); - } + Indicator ref = prepare(ind); m_discard = std::max(ind.discard(), ref.discard()); auto* y = this->data(); diff --git a/hikyuu_cpp/hikyuu/indicator/imp/IDma.h b/hikyuu_cpp/hikyuu/indicator/imp/IDma.h index 0024d596e..5a7d919bd 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/IDma.h +++ b/hikyuu_cpp/hikyuu/indicator/imp/IDma.h @@ -7,7 +7,7 @@ #pragma once -#include "../Indicator.h" +#include "../Indicator2InImp.h" namespace hku { @@ -17,31 +17,14 @@ namespace hku { * 算法:若Y=DMA(X,A) 则 Y=A*X+(1-A)*Y',其中Y'表示上一周期Y值。 * 例如:DMA(CLOSE,VOL/CAPITAL)表示求以换手率作平滑因子的平均价 */ -class IDma : public IndicatorImp { +class IDma : public Indicator2InImp { + INDICATOR2IN_IMP(IDma) + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION + public: IDma(); explicit IDma(const Indicator& ref_a, bool fill_null); 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_ind; - -//============================================ -// 序列化支持 -//============================================ -#if HKU_SUPPORT_SERIALIZATION -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) { - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); - } -#endif }; -} \ No newline at end of file +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp index 105fba14b..578fb8a11 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp +++ b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.cpp @@ -5,9 +5,6 @@ * Author: fasiondog */ -#include "hikyuu/indicator/crt/ALIGN.h" -#include "hikyuu/indicator/crt/CVAL.h" -#include "hikyuu/indicator/crt/SLICE.h" #include "ISpearman.h" #if HKU_SUPPORT_SERIALIZATION @@ -16,15 +13,13 @@ BOOST_CLASS_EXPORT(hku::ISpearman) namespace hku { -ISpearman::ISpearman() : IndicatorImp("SPEARMAN") { +ISpearman::ISpearman() : Indicator2InImp("SPEARMAN") { setParam("n", 0); - setParam("fill_null", true); } ISpearman::ISpearman(const Indicator &ref_ind, int n, bool fill_null) -: IndicatorImp("SPEARMAN"), m_ref_ind(ref_ind) { +: Indicator2InImp("SPEARMAN", ref_ind, fill_null, 1) { setParam("n", n); - setParam("fill_null", fill_null); } ISpearman::~ISpearman() {} @@ -36,12 +31,6 @@ void ISpearman::_checkParam(const string &name) const { } } -IndicatorImpPtr ISpearman::_clone() { - auto p = make_shared(); - p->m_ref_ind = m_ref_ind.clone(); - return p; -} - static void spearmanLevel(const IndicatorImp::value_t *data, IndicatorImp::value_t *level, size_t total) { std::vector> data_index(total); @@ -80,21 +69,7 @@ void ISpearman::_calculate(const Indicator &ind) { size_t total = ind.size(); HKU_IF_RETURN(total == 0, void()); - _readyBuffer(total, 2); - - auto k = getContext(); - m_ref_ind.setContext(k); - Indicator ref = m_ref_ind; - auto dates = ref.getDatetimeList(); - if (dates.empty()) { - if (ref.size() > ind.size()) { - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); - } else if (ref.size() < ind.size()) { - ref = CVAL(ind, 0.) + ref; - } - } else if (k != ind.getContext()) { - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); - } + Indicator ref = prepare(ind); int n = getParam("n"); if (n == 0) { diff --git a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.h b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.h index 613e24486..b969a6c78 100644 --- a/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.h +++ b/hikyuu_cpp/hikyuu/indicator/imp/ISpearman.h @@ -7,35 +7,20 @@ #pragma once -#include "../Indicator.h" +#include "../Indicator2InImp.h" namespace hku { -class ISpearman : public IndicatorImp { +class ISpearman : public Indicator2InImp { + INDICATOR2IN_IMP(ISpearman) + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION + public: ISpearman(); ISpearman(const Indicator& ref_ind, int n, bool fill_null); virtual ~ISpearman(); virtual void _checkParam(const string& name) const override; - virtual void _calculate(const Indicator& data) override; - virtual IndicatorImpPtr _clone() override; - -private: - Indicator m_ref_ind; - -//============================================ -// 序列化支持 -//============================================ -#if HKU_SUPPORT_SERIALIZATION -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) { - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); - } -#endif }; } // namespace hku \ No newline at end of file From a4b3c57c5ed98e730275438a5b578533fcfae2bb Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 3 Feb 2025 17:51:02 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E8=BE=93=E5=85=A5=E6=8C=87=E6=A0=87=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hikyuu/indicator_talib/imp/ta_defines.h | 75 +++---------------- .../hikyuu/indicator_talib/imp/ta_imp.h | 60 ++------------- 2 files changed, 19 insertions(+), 116 deletions(-) diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_defines.h b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_defines.h index e775a1848..7effc0ead 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_defines.h +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_defines.h @@ -8,6 +8,7 @@ #pragma once #include "hikyuu/indicator/Indicator.h" +#include "hikyuu/indicator/Indicator2InImp.h" #define TA_IN1_OUT_DEF(func) \ class Cls_##func : public IndicatorImp { \ @@ -30,79 +31,27 @@ virtual void _checkParam(const string& name) const override; \ }; -#if HKU_SUPPORT_SERIALIZATION -#define TA_IN2_OUT_DEF(func) \ - class Cls_##func : public IndicatorImp { \ - public: \ - Cls_##func(); \ - Cls_##func(const Indicator& ref_ind, bool fill_null); \ - virtual ~Cls_##func(); \ - virtual void _calculate(const Indicator& data) override; \ - virtual IndicatorImpPtr _clone() override; \ - \ - private: \ - Indicator m_ref_ind; \ - \ - private: \ - friend class boost::serialization::access; \ - template \ - void serialize(Archive& ar, const unsigned int version) { \ - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); \ - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); \ - } \ +#define TA_IN2_OUT_DEF(func) \ + class Cls_##func : public Indicator2InImp { \ + INDICATOR2IN_IMP(Cls_##func) \ + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION \ + public: \ + Cls_##func(); \ + Cls_##func(const Indicator& ref_ind, bool fill_null); \ + virtual ~Cls_##func(); \ }; #define TA_IN2_OUT_N_DEF(func) \ - class Cls_##func : public IndicatorImp { \ + class Cls_##func : public Indicator2InImp { \ + INDICATOR2IN_IMP(Cls_##func) \ + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION \ public: \ Cls_##func(); \ explicit Cls_##func(int n, bool fill_null); \ Cls_##func(const Indicator& ref_ind, int n, bool fill_null); \ virtual ~Cls_##func(); \ virtual void _checkParam(const string& name) const override; \ - virtual void _calculate(const Indicator& data) override; \ - virtual IndicatorImpPtr _clone() override; \ - \ - private: \ - Indicator m_ref_ind; \ - \ - private: \ - friend class boost::serialization::access; \ - template \ - void serialize(Archive& ar, const unsigned int version) { \ - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); \ - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); \ - } \ - }; -#else -#define TA_IN2_OUT_DEF(func) \ - class Cls_##func : public IndicatorImp { \ - public: \ - Cls_##func(); \ - Cls_##func(const Indicator& ref_ind); \ - virtual ~Cls_##func(); \ - virtual void _calculate(const Indicator& data) override; \ - virtual IndicatorImpPtr _clone() override; \ - \ - private: \ - Indicator m_ref_ind; \ - }; - -#define TA_IN2_OUT_N_DEF(func) \ - class Cls_##func : public IndicatorImp { \ - public: \ - Cls_##func(); \ - explicit Cls_##func(int n); \ - Cls_##func(const Indicator& ref_ind, int n); \ - virtual ~Cls_##func(); \ - virtual void _checkParam(const string& name) const override; \ - virtual void _calculate(const Indicator& data) override; \ - virtual IndicatorImpPtr _clone() override; \ - \ - private: \ - Indicator m_ref_ind; \ }; -#endif #define TA_K_OUT_DEF(func) \ class Cls_##func : public IndicatorImp { \ diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h index 88fa8d920..205649a6c 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h @@ -448,40 +448,16 @@ } #define TA_IN2_OUT1_IMP(func, func_lookback) \ - Cls_##func::Cls_##func() : IndicatorImp(#func, 1) { \ - setParam("fill_null", true); \ - } \ + Cls_##func::Cls_##func() : Indicator2InImp(#func, 1) {} \ Cls_##func::Cls_##func(const Indicator &ref_ind, bool fill_null) \ - : IndicatorImp(#func, 1), m_ref_ind(ref_ind) { \ - setParam("fill_null", fill_null); \ - } \ + : Indicator2InImp(#func, ref_ind, fill_null, 1) {} \ Cls_##func::~Cls_##func() {} \ - IndicatorImpPtr Cls_##func::_clone() { \ - auto p = make_shared(); \ - p->m_ref_ind = m_ref_ind.clone(); \ - return p; \ - } \ \ void Cls_##func::_calculate(const Indicator &ind) { \ size_t total = ind.size(); \ HKU_IF_RETURN(total == 0, void()); \ \ - _readyBuffer(total, 1); \ - \ - auto k = getContext(); \ - m_ref_ind.setContext(k); \ - Indicator ref = m_ref_ind; \ - auto dates = ref.getDatetimeList(); \ - if (dates.empty()) { \ - if (ref.size() > ind.size()) { \ - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); \ - } else if (ref.size() < ind.size()) { \ - ref = CVAL(ind, 0.) + ref; \ - } \ - } else if (k != ind.getContext()) { \ - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); \ - } \ - \ + Indicator ref = prepare(ind); \ int lookback = func_lookback(); \ if (lookback < 0) { \ m_discard = total; \ @@ -517,20 +493,18 @@ } #define TA_IN2_OUT1_N_IMP(func, func_lookback, period, period_min, period_max) \ - Cls_##func::Cls_##func() : IndicatorImp(#func, 1) { \ + Cls_##func::Cls_##func() : Indicator2InImp(#func, 1) { \ setParam("n", period); \ - setParam("fill_null", true); \ } \ \ - Cls_##func::Cls_##func(int n, bool fill_null) : IndicatorImp(#func, 1) { \ + Cls_##func::Cls_##func(int n, bool fill_null) : Indicator2InImp(#func, 1) { \ setParam("n", n); \ setParam("fill_null", fill_null); \ } \ \ Cls_##func::Cls_##func(const Indicator &ref_ind, int n, bool fill_null) \ - : IndicatorImp(#func, 1), m_ref_ind(ref_ind) { \ + : Indicator2InImp(#func, ref_ind, fill_null, 1) { \ setParam("n", n); \ - setParam("fill_null", fill_null); \ } \ \ Cls_##func::~Cls_##func() {} \ @@ -542,31 +516,11 @@ } \ } \ \ - IndicatorImpPtr Cls_##func::_clone() { \ - auto p = make_shared(); \ - p->m_ref_ind = m_ref_ind.clone(); \ - return p; \ - } \ - \ void Cls_##func::_calculate(const Indicator &ind) { \ size_t total = ind.size(); \ HKU_IF_RETURN(total == 0, void()); \ \ - _readyBuffer(total, 1); \ - \ - auto k = getContext(); \ - m_ref_ind.setContext(k); \ - Indicator ref = m_ref_ind; \ - auto dates = ref.getDatetimeList(); \ - if (dates.empty()) { \ - if (ref.size() > ind.size()) { \ - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); \ - } else if (ref.size() < ind.size()) { \ - ref = CVAL(ind, 0.) + ref; \ - } \ - } else if (k != ind.getContext()) { \ - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); \ - } \ + Indicator ref = prepare(ind); \ \ int n = getParam("n"); \ int lookback = func_lookback(n); \ From 164b82ba64886c18c37d0a8a67462459263cb91e Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 3 Feb 2025 17:56:21 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E8=BE=93=E5=85=A5=E6=8C=87=E6=A0=87=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hikyuu/indicator_talib/imp/TaMavp.cpp | 34 ++----------------- .../hikyuu/indicator_talib/imp/TaMavp.h | 24 +++---------- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp index 61c7eb8b9..e15dd3ee3 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.cpp @@ -6,12 +6,6 @@ */ #include -#include "hikyuu/indicator/crt/ALIGN.h" -#include "hikyuu/indicator/crt/CVAL.h" -#include "hikyuu/indicator/crt/SLICE.h" -#include "hikyuu/indicator/crt/PRICELIST.h" -#include "hikyuu/indicator/imp/IContext.h" -#include "hikyuu/indicator/crt/CONTEXT.h" #include "TaMavp.h" #if HKU_SUPPORT_SERIALIZATION @@ -20,19 +14,17 @@ BOOST_CLASS_EXPORT(hku::TaMavp) namespace hku { -TaMavp::TaMavp() : IndicatorImp("TA_MAVP", 1) { +TaMavp::TaMavp() : Indicator2InImp("TA_MAVP", 1) { setParam("min_n", 2); setParam("max_n", 30); setParam("matype", 0); - setParam("fill_null", true); } TaMavp::TaMavp(const Indicator& ref_ind, int min_n, int max_n, int matype, bool fill_null) -: IndicatorImp("TA_MAVP", 1), m_ref_ind(ref_ind) { +: Indicator2InImp("TA_MAVP", ref_ind, fill_null, 1) { setParam("min_n", min_n); setParam("max_n", max_n); setParam("matype", matype); - setParam("fill_null", fill_null); } TaMavp::~TaMavp() {} @@ -47,31 +39,11 @@ void TaMavp::_checkParam(const string& name) const { } } -IndicatorImpPtr TaMavp::_clone() { - auto p = make_shared(); - p->m_ref_ind = m_ref_ind.clone(); - return p; -} - void TaMavp::_calculate(const Indicator& ind) { size_t total = ind.size(); HKU_IF_RETURN(total == 0, void()); - _readyBuffer(total, 2); - - auto k = getContext(); - m_ref_ind.setContext(k); - Indicator ref = m_ref_ind; - auto dates = ref.getDatetimeList(); - if (dates.empty()) { - if (ref.size() > ind.size()) { - ref = SLICE(ref, ref.size() - ind.size(), ref.size()); - } else if (ref.size() < ind.size()) { - ref = CVAL(ind, 0.) + ref; - } - } else if (k != ind.getContext()) { - ref = ALIGN(m_ref_ind, ind, getParam("fill_null")); - } + Indicator ref = prepare(ind); int min_n = getParam("min_n"); int max_n = getParam("max_n"); diff --git a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.h b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.h index 189aaf8c9..5bafaef2d 100644 --- a/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.h +++ b/hikyuu_cpp/hikyuu/indicator_talib/imp/TaMavp.h @@ -8,34 +8,20 @@ #pragma once #include "hikyuu/indicator/Indicator.h" +#include "hikyuu/indicator/Indicator2InImp.h" namespace hku { -class TaMavp : public IndicatorImp { +class TaMavp : public Indicator2InImp { + INDICATOR2IN_IMP(TaMavp) + INDICATOR2IN_IMP_NO_PRIVATE_MEMBER_SERIALIZATION + public: TaMavp(); TaMavp(const Indicator& ref_ind, int min_n, int max_n, int matype, bool fill_null); virtual ~TaMavp(); virtual void _checkParam(const string& name) const override; - virtual void _calculate(const Indicator& data) override; - virtual IndicatorImpPtr _clone() override; - -private: - Indicator m_ref_ind; - -//============================================ -// 序列化支持 -//============================================ -#if HKU_SUPPORT_SERIALIZATION -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) { - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(IndicatorImp); - ar& BOOST_SERIALIZATION_NVP(m_ref_ind); - } -#endif }; } // namespace hku \ No newline at end of file