Skip to content

Commit

Permalink
优化双指标输入指标实现
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Feb 3, 2025
1 parent 9b23820 commit a4b3c57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 116 deletions.
75 changes: 12 additions & 63 deletions hikyuu_cpp/hikyuu/indicator_talib/imp/ta_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand All @@ -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 <class Archive> \
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 <class Archive> \
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 { \
Expand Down
60 changes: 7 additions & 53 deletions hikyuu_cpp/hikyuu/indicator_talib/imp/ta_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,40 +448,16 @@
}

#define TA_IN2_OUT1_IMP(func, func_lookback) \
Cls_##func::Cls_##func() : IndicatorImp(#func, 1) { \
setParam<bool>("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<bool>("fill_null", fill_null); \
} \
: Indicator2InImp(#func, ref_ind, fill_null, 1) {} \
Cls_##func::~Cls_##func() {} \
IndicatorImpPtr Cls_##func::_clone() { \
auto p = make_shared<Cls_##func>(); \
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<bool>("fill_null")); \
} \
\
Indicator ref = prepare(ind); \
int lookback = func_lookback(); \
if (lookback < 0) { \
m_discard = total; \
Expand Down Expand Up @@ -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<int>("n", period); \
setParam<bool>("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<int>("n", n); \
setParam<bool>("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<int>("n", n); \
setParam<bool>("fill_null", fill_null); \
} \
\
Cls_##func::~Cls_##func() {} \
Expand All @@ -542,31 +516,11 @@
} \
} \
\
IndicatorImpPtr Cls_##func::_clone() { \
auto p = make_shared<Cls_##func>(); \
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<bool>("fill_null")); \
} \
Indicator ref = prepare(ind); \
\
int n = getParam<int>("n"); \
int lookback = func_lookback(n); \
Expand Down

0 comments on commit a4b3c57

Please sign in to comment.