Skip to content

Commit 5800c9c

Browse files
authored
Merge pull request #337 from fasiondog/feature/dev
add EV logic; 调整 SG logic
2 parents f6c23a8 + b131b08 commit 5800c9c

29 files changed

+1700
-27
lines changed

hikyuu_cpp/hikyuu/trade_sys/condition/ConditionBase.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ DatetimeList ConditionBase::getDatetimeList() const {
107107
}
108108

109109
Indicator ConditionBase::getValues() const {
110-
return PRICELIST(m_values);
110+
DatetimeList dates;
111+
PriceList values;
112+
for (const auto& d : m_date_index) {
113+
dates.push_back(d.first);
114+
}
115+
116+
values.reserve(dates.size());
117+
for (const auto& d : dates) {
118+
values.push_back(m_values[m_date_index.at(d)]);
119+
}
120+
121+
return PRICELIST(values, dates);
111122
}
112123

113124
} /* namespace hku */

hikyuu_cpp/hikyuu/trade_sys/condition/ConditionBase.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class HKU_API ConditionBase : public enable_shared_from_this<ConditionBase> {
6666
/** 获取系统有效的日期列表,注意:和交易对象不等长 */
6767
DatetimeList getDatetimeList() const;
6868

69-
/** 以指标的形式获取实际值,与交易对象等长,0表示无效,1表示系统有效 */
69+
/**
70+
* 以指标的形式获取实际值,与交易对象等长,<=0表示无效,>0表示系统有效
71+
* @note 带日期的时间序列指标
72+
*/
7073
Indicator getValues() const;
7174

7275
/**

hikyuu_cpp/hikyuu/trade_sys/condition/imp/SubCondition.h hikyuu_cpp/hikyuu/trade_sys/condition/imp/logic/SubCondition.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#pragma once
99

10-
#include "../ConditionBase.h"
10+
#include "hikyuu/trade_sys/condition/ConditionBase.h"
1111

1212
namespace hku {
1313

hikyuu_cpp/hikyuu/trade_sys/environment/EnvironmentBase.cpp

+47-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Author: fasiondog
66
*/
77

8+
#include "hikyuu/indicator/crt/PRICELIST.h"
89
#include "EnvironmentBase.h"
910

1011
namespace hku {
@@ -26,7 +27,11 @@ HKU_API std::ostream& operator<<(std::ostream& os, const EnvironmentPtr& en) {
2627
EnvironmentBase::EnvironmentBase() : m_name("EnvironmentBase") {}
2728

2829
EnvironmentBase::EnvironmentBase(const EnvironmentBase& base)
29-
: m_params(base.m_params), m_name(base.m_name), m_valid(base.m_valid) {}
30+
: m_params(base.m_params),
31+
m_name(base.m_name),
32+
m_query(base.m_query),
33+
m_date_index(base.m_date_index),
34+
m_values(base.m_values) {}
3035

3136
EnvironmentBase::EnvironmentBase(const string& name) : m_name(name) {}
3237

@@ -38,7 +43,8 @@ void EnvironmentBase::paramChanged() {}
3843
void EnvironmentBase::reset() {
3944
std::unique_lock<std::shared_mutex> lock(m_mutex);
4045
m_query = Null<KQuery>();
41-
m_valid.clear();
46+
m_date_index.clear();
47+
m_values.clear();
4248
_reset();
4349
}
4450

@@ -59,27 +65,60 @@ EnvironmentPtr EnvironmentBase::clone() {
5965
p->m_params = m_params;
6066
p->m_name = m_name;
6167
p->m_query = m_query;
62-
p->m_valid = m_valid;
68+
p->m_date_index = m_date_index;
69+
p->m_values = m_values;
6370
return p;
6471
}
6572

6673
void EnvironmentBase::setQuery(const KQuery& query) {
6774
std::unique_lock<std::shared_mutex> lock(m_mutex);
6875
if (m_query != query) {
69-
m_valid.clear();
76+
m_query = Null<KQuery>();
77+
m_date_index.clear();
78+
m_values.clear();
7079
_reset();
7180
m_query = query;
7281
_calculate();
7382
}
7483
}
7584

76-
void EnvironmentBase::_addValid(const Datetime& datetime) {
77-
m_valid.insert(datetime);
85+
void EnvironmentBase::_addValid(const Datetime& datetime, price_t value) {
86+
auto iter = m_date_index.find(datetime);
87+
if (iter == m_date_index.end()) {
88+
m_date_index[datetime] = m_values.size();
89+
m_values.push_back(value);
90+
} else {
91+
m_values[iter->second] += value;
92+
}
93+
}
94+
95+
bool EnvironmentBase::isValid(const Datetime& datetime) const {
96+
std::shared_lock<std::shared_mutex> lock(m_mutex);
97+
auto iter = m_date_index.find(datetime);
98+
HKU_IF_RETURN(iter == m_date_index.end(), false);
99+
return m_values[iter->second] > 0.;
100+
}
101+
102+
price_t EnvironmentBase::getValue(const Datetime& datetime) const {
103+
std::shared_lock<std::shared_mutex> lock(m_mutex);
104+
auto iter = m_date_index.find(datetime);
105+
return iter == m_date_index.end() ? 0. : m_values[iter->second];
78106
}
79107

80-
bool EnvironmentBase::isValid(const Datetime& datetime) {
108+
Indicator EnvironmentBase::getValues() const {
81109
std::shared_lock<std::shared_mutex> lock(m_mutex);
82-
return m_valid.count(datetime) != 0;
110+
DatetimeList dates;
111+
PriceList values;
112+
for (const auto& d : m_date_index) {
113+
dates.emplace_back(d.first);
114+
}
115+
116+
values.reserve(dates.size());
117+
for (const auto& d : dates) {
118+
values.emplace_back(m_values[m_date_index.at(d)]);
119+
}
120+
121+
return PRICELIST(values, dates);
83122
}
84123

85124
} /* namespace hku */

hikyuu_cpp/hikyuu/trade_sys/environment/EnvironmentBase.h

+20-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <shared_mutex>
1414
#include "../../KQuery.h"
1515
#include "../../utilities/Parameter.h"
16+
#include "hikyuu/indicator/Indicator.h"
1617

1718
namespace hku {
1819

@@ -64,15 +65,24 @@ class HKU_API EnvironmentBase : public enable_shared_from_this<EnvironmentBase>
6465
/**
6566
* 加入有效时间,在_calculate中调用
6667
* @param datetime 系统有效日期
68+
* @param value 默认为1.0,大于0表示有效,小于等于0表示无效
6769
*/
68-
void _addValid(const Datetime& datetime);
70+
void _addValid(const Datetime& datetime, price_t value = 1.0);
6971

7072
/**
7173
* 判断指定日期的外部环境是否有效
7274
* @param datetime 指定日期
7375
* @return true 有效 | false 无效
7476
*/
75-
bool isValid(const Datetime& datetime);
77+
bool isValid(const Datetime& datetime) const;
78+
79+
price_t getValue(const Datetime& datetime) const;
80+
81+
/**
82+
* 以指标的形式获取实际值,与交易对象等长,<=0表示无效,>0表示系统有效
83+
* @note 带日期的时间序列指标
84+
*/
85+
Indicator getValues() const;
7686

7787
/** 子类计算接口 */
7888
virtual void _calculate() = 0;
@@ -86,8 +96,9 @@ class HKU_API EnvironmentBase : public enable_shared_from_this<EnvironmentBase>
8696
protected:
8797
string m_name;
8898
KQuery m_query;
89-
std::set<Datetime> m_valid;
90-
std::shared_mutex m_mutex;
99+
map<Datetime, size_t> m_date_index;
100+
vector<price_t> m_values;
101+
mutable std::shared_mutex m_mutex;
91102

92103
//============================================
93104
// 序列化支持
@@ -101,14 +112,17 @@ class HKU_API EnvironmentBase : public enable_shared_from_this<EnvironmentBase>
101112
ar& BOOST_SERIALIZATION_NVP(m_params);
102113
// ev可能多个系统共享,保留m_query可能用于查错
103114
ar& BOOST_SERIALIZATION_NVP(m_query);
104-
ar& BOOST_SERIALIZATION_NVP(m_valid);
115+
ar& BOOST_SERIALIZATION_NVP(m_date_index);
116+
ar& BOOST_SERIALIZATION_NVP(m_values);
105117
}
106118

107119
template <class Archive>
108120
void load(Archive& ar, const unsigned int version) {
109121
ar& BOOST_SERIALIZATION_NVP(m_name);
122+
ar& BOOST_SERIALIZATION_NVP(m_params);
110123
ar& BOOST_SERIALIZATION_NVP(m_query);
111-
ar& BOOST_SERIALIZATION_NVP(m_valid);
124+
ar& BOOST_SERIALIZATION_NVP(m_date_index);
125+
ar& BOOST_SERIALIZATION_NVP(m_values);
112126
}
113127

114128
BOOST_SERIALIZATION_SPLIT_MEMBER()

hikyuu_cpp/hikyuu/trade_sys/environment/build_in.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
#include "crt/EV_TwoLine.h"
1313
#include "crt/EV_Bool.h"
1414
#include "crt/EV_Manual.h"
15+
#include "crt/EV_Logic.h"
1516

1617
#endif /* ENVIRONMENT_BUILD_IN_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2025 hikyuu.org
3+
*
4+
* Created on: 2025-03-06
5+
* Author: fasiondog
6+
*/
7+
8+
#pragma once
9+
10+
#include "hikyuu/trade_sys/environment/EnvironmentBase.h"
11+
12+
namespace hku {
13+
14+
/**
15+
* 两个市场环境判定相与,等效于两者的交集
16+
* @param ev1 市场环境判定1
17+
* @param ev2 市场环境判定2
18+
* @return AndCondition 实例指针
19+
*/
20+
HKU_API EnvironmentPtr operator&(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
21+
22+
/**
23+
* 两个市场环境判定相或,等效于两者的并集
24+
* @param ev1 市场环境判定1
25+
* @param ev2 市场环境判定2
26+
* @return OrCondition 实例指针
27+
*/
28+
HKU_API EnvironmentPtr operator|(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
29+
30+
HKU_API EnvironmentPtr operator+(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
31+
HKU_API EnvironmentPtr operator-(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
32+
HKU_API EnvironmentPtr operator*(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
33+
HKU_API EnvironmentPtr operator/(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
34+
35+
} // namespace hku
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2025 hikyuu.org
3+
*
4+
* Created on: 2025-03-06
5+
* Author: fasiondog
6+
*/
7+
8+
#include "AddEnvironment.h"
9+
10+
#if HKU_SUPPORT_SERIALIZATION
11+
BOOST_CLASS_EXPORT(hku::AddEnvironment)
12+
#endif
13+
14+
namespace hku {
15+
16+
AddEnvironment::AddEnvironment() : EnvironmentBase("EV_Add") {}
17+
18+
AddEnvironment::AddEnvironment(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2)
19+
: EnvironmentBase("EV_Add"), m_ev1(ev1), m_ev2(ev2) {}
20+
21+
AddEnvironment::~AddEnvironment() {}
22+
23+
void AddEnvironment::_calculate() {
24+
HKU_IF_RETURN(!m_ev1 && !m_ev2, void());
25+
26+
if (m_ev1) {
27+
m_ev1->setQuery(m_query);
28+
}
29+
30+
if (m_ev2) {
31+
m_ev2->setQuery(m_query);
32+
}
33+
34+
if (m_ev1 && !m_ev2) {
35+
auto values = m_ev1->getValues();
36+
auto dates = values.getDatetimeList();
37+
size_t total = dates.size();
38+
m_values.resize(total);
39+
for (size_t i = 0; i < total; i++) {
40+
m_date_index[dates[i]] = i;
41+
m_values[i] = values[i];
42+
}
43+
return;
44+
}
45+
46+
if (!m_ev1 && m_ev2) {
47+
auto values = m_ev2->getValues();
48+
auto dates = values.getDatetimeList();
49+
size_t total = dates.size();
50+
m_values.resize(total);
51+
for (size_t i = 0; i < total; i++) {
52+
m_date_index[dates[i]] = i;
53+
m_values[i] = values[i];
54+
}
55+
return;
56+
}
57+
58+
auto values = m_ev1->getValues();
59+
auto dates = values.getDatetimeList();
60+
size_t total = dates.size();
61+
m_values.resize(total);
62+
for (size_t i = 0; i < total; i++) {
63+
m_date_index[dates[i]] = i;
64+
m_values[i] = values[i];
65+
}
66+
67+
values = m_ev2->getValues();
68+
dates = values.getDatetimeList();
69+
total = dates.size();
70+
const auto* src = values.data();
71+
for (size_t i = 0; i < total; i++) {
72+
_addValid(dates[i], src[i]);
73+
}
74+
}
75+
76+
void AddEnvironment::_reset() {
77+
if (m_ev1) {
78+
m_ev1->reset();
79+
}
80+
if (m_ev2) {
81+
m_ev2->reset();
82+
}
83+
}
84+
85+
EnvironmentPtr AddEnvironment::_clone() {
86+
auto p = make_shared<AddEnvironment>();
87+
if (m_ev1) {
88+
p->m_ev1 = m_ev1->clone();
89+
}
90+
if (m_ev2) {
91+
p->m_ev2 = m_ev2->clone();
92+
}
93+
return p;
94+
}
95+
96+
HKU_API EnvironmentPtr operator+(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2) {
97+
return make_shared<AddEnvironment>(ev1, ev2);
98+
}
99+
100+
} // namespace hku
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2025 hikyuu.org
3+
*
4+
* Created on: 2025-03-06
5+
* Author: fasiondog
6+
*/
7+
8+
#pragma once
9+
10+
#include "hikyuu/trade_sys/environment/EnvironmentBase.h"
11+
12+
namespace hku {
13+
14+
class HKU_API AddEnvironment : public EnvironmentBase {
15+
public:
16+
AddEnvironment();
17+
AddEnvironment(const EnvironmentPtr& ev1, const EnvironmentPtr& ev2);
18+
virtual ~AddEnvironment();
19+
20+
virtual void _calculate() override;
21+
virtual void _reset() override;
22+
virtual EnvironmentPtr _clone() override;
23+
24+
private:
25+
EnvironmentPtr m_ev1;
26+
EnvironmentPtr m_ev2;
27+
28+
//============================================
29+
// 序列化支持
30+
//============================================
31+
#if HKU_SUPPORT_SERIALIZATION
32+
friend class boost::serialization::access;
33+
template <class Archive>
34+
void serialize(Archive& ar, const unsigned int version) {
35+
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(EnvironmentBase);
36+
ar& BOOST_SERIALIZATION_NVP(m_ev1);
37+
ar& BOOST_SERIALIZATION_NVP(m_ev2);
38+
}
39+
#endif
40+
};
41+
42+
} // namespace hku

0 commit comments

Comments
 (0)