Skip to content

Commit e68b43d

Browse files
authored
Merge pull request #440 from fasiondog/feature/factor_ma
优化IndicatorImp; 去除不必要的虚函数
2 parents e279211 + a47f548 commit e68b43d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+490
-154
lines changed

hikyuu/data/mysql_upgrade/0030.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
update `hku_base`.`stocktypeinfo` set `minTradeNumber`=200 where `type`=9 and `description`='科创板';
22
update `hku_base`.`stocktypeinfo` set `minTradeNumber`=100 where `type`=11 and `description`='北交所';
3-
UPDATE `hku_base`.`version` set `version` = 29;
3+
UPDATE `hku_base`.`version` set `version` = 30;

hikyuu_cpp/hikyuu/KData.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ void KData::tocsv(const string& filename) {
9494
}
9595

9696
KData KData::getKData(const Datetime& start, const Datetime& end) const {
97+
KData ret;
9798
const Stock& stk = getStock();
98-
HKU_IF_RETURN(stk.isNull(), KData());
99+
HKU_IF_RETURN(stk.isNull(), ret);
99100

100101
const KQuery& query = getQuery();
101-
return KData(stk, KQueryByDate(start, end, query.kType(), query.recoverType()));
102+
ret = KData(stk, KQueryByDate(start, end, query.kType(), query.recoverType()));
103+
return ret;
102104
}
103105

104106
KData KData::getKData(int64_t start, int64_t end) const {
@@ -127,19 +129,15 @@ KData KData::getKData(int64_t start, int64_t end) const {
127129
return KData(getStock(), query);
128130
}
129131

130-
KData KData::getKData(const KQuery::KType& ktype) const {
131-
const Stock& stk = getStock();
132-
HKU_IF_RETURN(stk.isNull(), KData());
133-
132+
KQuery KData::getOtherQueryByDate(const Datetime& start_datetime, const Datetime& end_datetime,
133+
const KQuery::KType& ktype) const {
134134
const KQuery& query = getQuery();
135-
HKU_IF_RETURN(query.kType() == ktype, *this);
135+
if (getStock().isNull()) {
136+
return KQueryByDate(Null<Datetime>(), Null<Datetime>(), ktype, query.recoverType());
137+
}
136138

137-
if (empty()) {
138-
if (query.queryType() == KQuery::INDEX) {
139-
return KData(stk, KQuery(0, 0, ktype, query.recoverType()));
140-
}
141-
return KData(
142-
stk, KQuery(query.startDatetime(), query.endDatetime(), ktype, query.recoverType()));
139+
if (empty() && query.queryType() == KQuery::INDEX) {
140+
return KQuery(Null<Datetime>(), Null<Datetime>(), ktype, query.recoverType());
143141
}
144142

145143
Datetime end;
@@ -148,9 +146,15 @@ KData KData::getKData(const KQuery::KType& ktype) const {
148146
end = Null<Datetime>();
149147
} else {
150148
end = this->back().datetime;
149+
if (end_datetime < end) {
150+
end = end_datetime;
151+
}
151152
}
152153

153154
Datetime start = this->front().datetime;
155+
if (start_datetime > start) {
156+
start = start_datetime;
157+
}
154158

155159
auto day_ktype_seconds = KQuery::getKTypeInSeconds(KQuery::DAY);
156160
// 从日线及以上转日线以下
@@ -167,7 +171,15 @@ KData KData::getKData(const KQuery::KType& ktype) const {
167171
start = start.startOfDay();
168172
}
169173

170-
return KData(stk, KQuery(start, end, ktype, query.recoverType()));
174+
return KQuery(start, end, ktype, query.recoverType());
175+
}
176+
177+
KData KData::getKData(const KQuery::KType& ktype) const {
178+
KData ret;
179+
const Stock& stk = getStock();
180+
HKU_IF_RETURN(stk.isNull(), ret);
181+
ret = stk.getKData(getOtherQueryByDate(front().datetime, back().datetime, ktype));
182+
return ret;
171183
}
172184

173185
Indicator KData::open() const {

hikyuu_cpp/hikyuu/KData.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ class HKU_API KData {
8383
*/
8484
KData getKData(int64_t start, int64_t end = Null<int64_t>()) const;
8585

86+
/**
87+
* 特殊用途!谨慎!按当前K线范围,获取指定日期范围的其他类型的按日期查询的 Query 条件
88+
* @note
89+
* 1. 指定日期范围必须在当前K线数据范围内,否则截断在 K 线范围内
90+
* 2. start_datetime/end_datetime 的精度应和当前 KData 一致
91+
* 3. 如果原截止条件为 Null<Datetime>()且未指定end_datetime, 则返回的查询条件为
92+
* Null<Datetime>()
93+
* @param start_datetime
94+
* @param end_datetime
95+
* @param ktype
96+
* @return KData
97+
*/
98+
KQuery getOtherQueryByDate(const Datetime& start_datetime, const Datetime& end_datetime,
99+
const KQuery::KType& ktype) const;
100+
86101
/** 按日期查询对应的索引位置,注:是 KData 中的位置,不是在 Stock 中原始K记录的位置 */
87102
size_t getPos(const Datetime& datetime) const;
88103

@@ -192,7 +207,8 @@ class HKU_API KData {
192207

193208
private:
194209
std::shared_ptr<KDataImp>& get_null_kdata_imp() {
195-
static std::shared_ptr<KDataImp> instance = std::make_shared<KDataImp>(); // 第一次调用时初始化
210+
static std::shared_ptr<KDataImp> instance =
211+
std::make_shared<KDataImp>(); // 第一次调用时初始化
196212
return instance;
197213
}
198214

hikyuu_cpp/hikyuu/data_driver/BlockInfoDriver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ class HKU_API BlockInfoDriver {
9494
*/
9595
virtual void remove(const string& category, const string& name) = 0;
9696

97+
protected:
98+
bool isPythonObject() const {
99+
return m_is_python_object;
100+
}
101+
97102
private:
98103
bool checkType();
99104

100105
protected:
101106
string m_name;
107+
bool m_is_python_object{false};
102108
};
103109

104110
typedef shared_ptr<BlockInfoDriver> BlockInfoDriverPtr;

hikyuu_cpp/hikyuu/data_driver/KDataDriver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ shared_ptr<KDataDriver> KDataDriver::clone() {
3737
shared_ptr<KDataDriver> ptr = _clone();
3838
ptr->m_params = m_params;
3939
ptr->m_name = m_name;
40+
ptr->m_is_python_object = m_is_python_object;
4041
ptr->_init();
4142
return ptr;
4243
}

hikyuu_cpp/hikyuu/data_driver/KDataDriver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ class HKU_API KDataDriver {
134134
}
135135

136136
protected:
137-
virtual bool isPythonObject() const {
138-
return false;
137+
bool isPythonObject() const {
138+
return m_is_python_object;
139139
}
140140

141141
private:
142142
bool checkType();
143143

144-
private:
144+
protected:
145145
string m_name;
146+
bool m_is_python_object{false};
146147
};
147148

148149
typedef shared_ptr<KDataDriver> KDataDriverPtr;

hikyuu_cpp/hikyuu/indicator/Indicator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class HKU_API Indicator {
176176
return m_imp->getParam<ValueType>(name);
177177
}
178178

179-
bool supportIndParam() const;
180179
bool haveIndParam(const string& name) const;
181180
void setIndParam(const string& name, const Indicator& ind);
182181
void setIndParam(const string& name, const IndParam& ind);
@@ -213,6 +212,8 @@ class HKU_API Indicator {
213212

214213
string str() const;
215214

215+
bool isPythonObject() const;
216+
216217
public:
217218
class Iterator {
218219
private:
@@ -387,8 +388,8 @@ inline const IndicatorImpPtr Indicator::getIndParamImp(const string& name) const
387388
return m_imp ? m_imp->getIndParamImp(name) : IndicatorImpPtr();
388389
}
389390

390-
inline bool Indicator::supportIndParam() const {
391-
return m_imp ? m_imp->supportIndParam() : false;
391+
inline bool Indicator::isPythonObject() const {
392+
return m_imp ? m_imp->isPythonObject() : false;
392393
}
393394

394395
//--------------------------------------------------------------

hikyuu_cpp/hikyuu/indicator/Indicator2InImp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ BOOST_CLASS_EXPORT(hku::Indicator2InImp)
1818
namespace hku {
1919

2020
Indicator2InImp::Indicator2InImp() : IndicatorImp("Indicator2InImp") {
21+
m_need_self_alike_compare = true;
2122
setParam<bool>("fill_null", true);
2223
}
2324

2425
Indicator2InImp::Indicator2InImp(const string& name, size_t result_num)
2526
: IndicatorImp(name, result_num) {
27+
m_need_self_alike_compare = true;
2628
setParam<bool>("fill_null", true);
2729
}
2830

2931
Indicator2InImp::Indicator2InImp(const string& name, const Indicator& ref_ind, bool fill_null,
3032
size_t result_num)
3133
: IndicatorImp(name, result_num), m_ref_ind(ref_ind) {
34+
m_need_self_alike_compare = true;
3235
setParam<bool>("fill_null", fill_null);
3336
}
3437

hikyuu_cpp/hikyuu/indicator/Indicator2InImp.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class Indicator2InImp : public IndicatorImp {
2222

2323
virtual IndicatorImpPtr _clone() override;
2424

25-
virtual bool needSelfAlikeCompare() const noexcept override {
26-
return true;
27-
}
28-
2925
virtual bool selfAlike(const IndicatorImp& other) const noexcept override;
3026

3127
virtual void getSelfInnerNodesWithInputConext(vector<IndicatorImpPtr>& nodes) const override;

hikyuu_cpp/hikyuu/indicator/IndicatorImp.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ string IndicatorImp::str() const {
342342
os << "Indicator{\n"
343343
<< " name: " << name() << "\n size: " << size() << "\n discard: " << discard()
344344
<< "\n result sets: " << getResultNumber() << "\n params: " << getParameter()
345-
<< "\n support indicator param: " << (supportIndParam() ? "True" : "False");
346-
if (supportIndParam()) {
345+
<< "\n is python object: " << (isPythonObject() ? "True" : "False");
346+
const auto &ind_params = getIndParams();
347+
if (!ind_params.empty()) {
347348
os << "\n ind params: {";
348-
const auto &ind_params = getIndParams();
349349
for (auto iter = ind_params.begin(); iter != ind_params.end(); ++iter) {
350350
os << iter->first << ": " << iter->second->formula() << ", ";
351351
}
@@ -366,10 +366,25 @@ string IndicatorImp::str() const {
366366
return os.str();
367367
}
368368

369+
void IndicatorImp::swap(IndicatorImp *other) {
370+
HKU_ASSERT(other != nullptr);
371+
HKU_IF_RETURN(this == other, void());
372+
HKU_CHECK(other->m_result_num == m_result_num, "indicator result num not equal!");
373+
HKU_CHECK(other->size() == size(), "indicator size not equal!");
374+
for (size_t r = 0; r < m_result_num; ++r) {
375+
vector<value_t> *tmp = m_pBuffer[r];
376+
m_pBuffer[r] = other->m_pBuffer[r];
377+
other->m_pBuffer[r] = tmp;
378+
}
379+
}
380+
369381
IndicatorImpPtr IndicatorImp::clone() {
370382
IndicatorImpPtr p = _clone();
371383
p->m_params = m_params;
372384
p->m_name = m_name;
385+
p->m_is_python_object = m_is_python_object;
386+
p->m_need_self_alike_compare = m_need_self_alike_compare;
387+
p->m_is_serial = m_is_serial;
373388
p->m_discard = m_discard;
374389
p->m_result_num = m_result_num;
375390
p->m_context = m_context;

0 commit comments

Comments
 (0)