Skip to content

Commit

Permalink
add EV_Bool
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Feb 3, 2024
1 parent bf50d10 commit 7896300
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/source/trade_sys/environment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
:param Indicator slow: 慢线指标
:param string market: 市场名称

.. py:function:: EV_Bool(ind[, market = 'SH'])
布尔信号指标市场环境

:param Indicator ind: bool类型的指标, 指标中相应位置大于0则代表市场有效, 否则无效
:param str market: 指定的市场,用于获取相应的交易日历


自定义市场环境判定策略
----------------------
Expand Down
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu/trade_sys/environment/build_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#define ENVIRONMENT_BUILD_IN_H_

#include "crt/EV_TwoLine.h"
#include "crt/EV_Bool.h"

#endif /* ENVIRONMENT_BUILD_IN_H */
23 changes: 23 additions & 0 deletions hikyuu_cpp/hikyuu/trade_sys/environment/crt/EV_Bool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-02-03
* Author: fasiondog
*/

#pragma once

#include "hikyuu/indicator/Indicator.h"
#include "hikyuu/trade_sys/environment/EnvironmentBase.h"

namespace hku {

/**
* 布尔信号指标市场环境
* @param ind bool类型的指标,指标中相应位置>0则代表市场有效,否则无效
* @param market 指定的市场,用于获取相应的交易日历
* @return
*/
EVPtr HKU_API EV_Bool(const Indicator& ind, const string& market = "SH");

}
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/trade_sys/environment/crt/EV_TwoLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace hku {
* @param market 市场名称,默认为"SH"
* @return
*/
EVPtr HKU_API EV_TwoLine(const Indicator& fast, const Indicator& slow, const string& market);
EVPtr HKU_API EV_TwoLine(const Indicator& fast, const Indicator& slow, const string& market = "SH");

} /* namespace hku */

Expand Down
56 changes: 56 additions & 0 deletions hikyuu_cpp/hikyuu/trade_sys/environment/imp/BoolEnvironment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-02-03
* Author: fasiondog
*/

#include "hikyuu/StockManager.h"
#include "BoolEnvironment.h"

#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::BoolEnvironment)
#endif

namespace hku {

BoolEnvironment::BoolEnvironment() : EnvironmentBase("EV_Bool") {
setParam<string>("market", "SH");
}

BoolEnvironment::BoolEnvironment(const Indicator& ind) : EnvironmentBase("EV_Bool"), m_ind(ind) {
setParam<string>("market", "SH");
}

BoolEnvironment::~BoolEnvironment() {}

EnvironmentPtr BoolEnvironment::_clone() {
return make_shared<BoolEnvironment>(m_ind.clone());
}

void BoolEnvironment::_calculate() {
string market = getParam<string>("market");
const StockManager& sm = StockManager::instance();
MarketInfo market_info = sm.getMarketInfo(market);
HKU_ERROR_IF_RETURN(market_info == Null<MarketInfo>(), void(), "Can't find maket({}) info!",
market);

Stock stock = sm.getStock(market + market_info.code());
KData kdata = stock.getKData(m_query);

auto ds = kdata.getDatetimeList();
m_ind.setContext(kdata);
for (size_t i = m_ind.discard(), len = m_ind.size(); i < len; i++) {
if (!std::isnan(m_ind[i]) && m_ind[i] > 0.) {
_addValid(ds[i]);
}
}
}

EVPtr HKU_API EV_Bool(const Indicator& ind, const string& market) {
BoolEnvironment* p = new BoolEnvironment(ind);
p->setParam<string>("market", market);
return EVPtr(p);
}

} // namespace hku
40 changes: 40 additions & 0 deletions hikyuu_cpp/hikyuu/trade_sys/environment/imp/BoolEnvironment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-02-03
* Author: fasiondog
*/

#pragma once

#include "hikyuu/indicator/Indicator.h"
#include "hikyuu/trade_sys/environment/EnvironmentBase.h"

namespace hku {

class BoolEnvironment : public EnvironmentBase {
public:
BoolEnvironment();
BoolEnvironment(const Indicator& ind);
virtual ~BoolEnvironment();

virtual void _calculate() override;
virtual EnvironmentPtr _clone() override;

private:
Indicator m_ind;

//============================================
// 序列化支持
//============================================
#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(EnvironmentBase);
ar& BOOST_SERIALIZATION_NVP(m_ind);
}
#endif
};

} // namespace hku
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ BOOST_CLASS_EXPORT(hku::TwoLineEnvironment)

namespace hku {

TwoLineEnvironment::TwoLineEnvironment() : EnvironmentBase("TwoLine") {
TwoLineEnvironment::TwoLineEnvironment() : EnvironmentBase("EV_TwoLine") {
setParam<string>("market", "SH");
}

TwoLineEnvironment::TwoLineEnvironment(const Indicator& fast, const Indicator& slow)
: EnvironmentBase("TwoLine"), m_fast(fast), m_slow(slow) {
: EnvironmentBase("EV_TwoLine"), m_fast(fast), m_slow(slow) {
setParam<string>("market", "SH");
}

Expand Down
13 changes: 11 additions & 2 deletions hikyuu_pywrap/trade_sys/_Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class PyEnvironmentBase : public EnvironmentBase {
};

void export_Environment(py::module& m) {
py::class_<EnvironmentBase, EnvironmentPtr, PyEnvironmentBase>(m, "EnvironmentBase",
R"(市场环境判定策略基类
py::class_<EnvironmentBase, EnvironmentPtr, PyEnvironmentBase>(
m, "EnvironmentBase",
R"(市场环境判定策略基类
自定义市场环境判定策略接口:
Expand Down Expand Up @@ -101,4 +102,12 @@ void export_Environment(py::module& m) {
:param Indicator fast: 快线指标
:param Indicator slow: 慢线指标
:param string market: 市场名称)");

m.def("EV_Bool", EV_Bool, py::arg("ind"), py::arg("market") = "SH",
R"(EV_Bool(ind, market='SH')
布尔信号指标市场环境
:param Indicator ind: bool类型的指标,指标中相应位置>0则代表市场有效,否则无效
:param str market: 指定的市场,用于获取相应的交易日历)");
}

0 comments on commit 7896300

Please sign in to comment.