Skip to content

Commit

Permalink
run_in_strategy 等添加其他订单代理参数,以便可以进行其他通知
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Oct 22, 2024
1 parent 0190e93 commit ef40fe7
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 19 deletions.
11 changes: 9 additions & 2 deletions hikyuu_cpp/hikyuu/strategy/BrokerTradeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,15 @@ string BrokerTradeManager::str() const {
}

TradeManagerPtr HKU_API crtBrokerTM(const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const string& name) {
return std::make_shared<BrokerTradeManager>(broker, costfunc, name);
const string& name,
const std::vector<OrderBrokerPtr>& other_brokers) {
auto tm = std::make_shared<BrokerTradeManager>(broker, costfunc, name);
for (const auto& brk : other_brokers) {
if (brk) {
tm->regBroker(brk);
}
}
return tm;
}

} // namespace hku
3 changes: 2 additions & 1 deletion hikyuu_cpp/hikyuu/strategy/BrokerTradeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ class HKU_API BrokerTradeManager : public TradeManagerBase {

TradeManagerPtr HKU_API crtBrokerTM(const OrderBrokerPtr& broker,
const TradeCostPtr& costfunc = TC_Zero(),
const string& name = "SYS");
const string& name = "SYS",
const std::vector<OrderBrokerPtr>& other_brokers = {});

} // namespace hku
11 changes: 10 additions & 1 deletion hikyuu_cpp/hikyuu/strategy/RunPortfolioInStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ void RunPortfolioInStrategy::run() {

StrategyPtr HKU_API crtPFStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const string& name, const string& config_file) {
const string& name,
const std::vector<OrderBrokerPtr>& other_brokers,
const string& config_file) {
std::shared_ptr<RunPortfolioInStrategy> runner =
std::make_shared<RunPortfolioInStrategy>(pf, query, adjust_cycle, broker, costfunc);

auto tm = pf->getTM();
for (const auto& brk : other_brokers) {
if (brk) {
tm->regBroker(brk);
}
}

std::function<void()> func = [=]() { runner->run(); };

vector<string> code_list;
Expand Down
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu/strategy/RunPortfolioInStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HKU_API RunPortfolioInStrategy {
StrategyPtr HKU_API crtPFStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const string& name = "PFStrategy",
const std::vector<OrderBrokerPtr>& other_brokers = {},
const string& config_file = "");

} // namespace hku
8 changes: 8 additions & 0 deletions hikyuu_cpp/hikyuu/strategy/RunSystemInStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ void RunSystemInStrategy::run(const Stock& stock) {
StrategyPtr HKU_API crtSysStrategy(const SYSPtr& sys, const string& stk_market_code,
const KQuery& query, const OrderBrokerPtr& broker,
const TradeCostPtr& costfunc, const string& name,
const std::vector<OrderBrokerPtr>& other_brokers,
const string& config_file) {
std::shared_ptr<RunSystemInStrategy> runner =
std::make_shared<RunSystemInStrategy>(sys, broker, query, costfunc);

auto tm = sys->getTM();
for (const auto& brk : other_brokers) {
if (brk) {
tm->regBroker(brk);
}
}

std::function<void()> func = [=]() { runner->run(getStock(stk_market_code)); };

KQuery::KType ktype = query.kType();
Expand Down
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu/strategy/RunSystemInStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class HKU_API RunSystemInStrategy {
StrategyPtr HKU_API crtSysStrategy(const SYSPtr& sys, const string& stk_market_code,
const KQuery& query, const OrderBrokerPtr& broker,
const TradeCostPtr& costfunc, const string& name = "SYSStrategy",
const std::vector<OrderBrokerPtr>& other_brokers = {},
const string& config_file = "");

} // namespace hku
10 changes: 6 additions & 4 deletions hikyuu_cpp/hikyuu/strategy/Strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,24 @@ void Strategy::_startEventLoop() {
}

void HKU_API runInStrategy(const SYSPtr& sys, const Stock& stk, const KQuery& query,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc) {
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const std::vector<OrderBrokerPtr>& other_brokers) {
HKU_ASSERT(sys && broker && sys->getTM());
HKU_ASSERT(!stk.isNull());
HKU_ASSERT(query != Null<KQuery>());
HKU_CHECK(!sys->getParam<bool>("buy_delay") && !sys->getParam<bool>("sell_delay"),
"Thie method only support buy|sell on close!");

auto tm = crtBrokerTM(broker, costfunc, sys->name());
auto tm = crtBrokerTM(broker, costfunc, sys->name(), other_brokers);
tm->fetchAssetInfoFromBroker(broker);
sys->setTM(tm);
sys->setSP(SlippagePtr()); // 清除移滑价差算法
sys->run(stk, query);
}

void HKU_API runInStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc) {
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const std::vector<OrderBrokerPtr>& other_brokers) {
HKU_ASSERT(pf && broker && pf->getTM());
HKU_ASSERT(query != Null<KQuery>());

Expand All @@ -352,7 +354,7 @@ void HKU_API runInStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycl
"Thie method only support buy|sell on close!");
}

auto tm = crtBrokerTM(broker, costfunc, pf->name());
auto tm = crtBrokerTM(broker, costfunc, pf->name(), other_brokers);
tm->fetchAssetInfoFromBroker(broker);
pf->setTM(tm);
pf->run(query, adjust_cycle, true);
Expand Down
12 changes: 8 additions & 4 deletions hikyuu_cpp/hikyuu/strategy/Strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,27 @@ typedef shared_ptr<Strategy> StrategyPtr;
* @param sys 交易系统
* @param stk 交易对象
* @param query 查询条件
* @param broker 订单代理
* @param broker 订单代理(专用与和账户资产同步的订单代理)
* @param costfunc 成本函数
* @param other_brokers 其他的订单代理
*/
void HKU_API runInStrategy(const SYSPtr& sys, const Stock& stk, const KQuery& query,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc);
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const std::vector<OrderBrokerPtr>& other_brokers = {});

/**
* @brief 在策略运行时中执行组合策略 PF
* @note 目前仅支持 buy_delay| sell_delay 均为 false 的系统,即 close 时执行交易
* @param pf 资产组合
* @param query 查询条件
* @param adjust_cycle 调仓周期
* @param broker 订单代理
* @param broker 订单代理(专用与和账户资产同步的订单代理)
* @param costfunc 成本函数
* @param other_brokers 其他的订单代理
*/
void HKU_API runInStrategy(const PFPtr& pf, const KQuery& query, int adjust_cycle,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc);
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const std::vector<OrderBrokerPtr>& other_brokers = {});

/**
* 从 hikyuu 数据缓存服务器拉取更新最新的缓存数据
Expand Down
42 changes: 36 additions & 6 deletions hikyuu_pywrap/strategy/_Strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <hikyuu/strategy/Strategy.h>
#include <hikyuu/strategy/BrokerTradeManager.h>
#include <hikyuu/strategy/RunSystemInStrategy.h>
#include <hikyuu/strategy/RunPortfolioInStrategy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

Expand Down Expand Up @@ -152,22 +153,51 @@ void export_Strategy(py::module& m) {
:param ignore_holiday: 节假日不执行)");

m.def("crtBrokerTM", crtBrokerTM, py::arg("broker"), py::arg("cost_func") = TC_Zero(),
py::arg("name") = "SYS");
py::arg("name") = "SYS", py::arg("other_brokers") = std::vector<OrderBrokerPtr>());

m.def("run_in_strategy",
py::overload_cast<const SYSPtr&, const Stock&, const KQuery&, const OrderBrokerPtr&,
const TradeCostPtr&>(runInStrategy),
const TradeCostPtr&, const std::vector<OrderBrokerPtr>&>(runInStrategy),
py::arg("sys"), py::arg("stock"), py::arg("query"), py::arg("broker"),
py::arg("cost_func"));
py::arg("cost_func"), py::arg("other_brokers") = std::vector<OrderBrokerPtr>(),
R"(run_in_strategy(sys, stk, query, broker, costfunc, [other_brokers=[]])
在策略运行时中执行系统交易 SYS
目前仅支持 buy_delay| sell_delay 均为 false 的系统,即 close 时执行交易
:param sys: 交易系统
:param stk: 交易对象
:param query: 查询条件
:param broker: 订单代理(专用与和账户资产同步的订单代理)
:param costfunc: 成本函数
:param other_brokers: 其他的订单代理)");

m.def("run_in_strategy",
py::overload_cast<const PFPtr&, const KQuery&, int, const OrderBrokerPtr&,
const TradeCostPtr&>(runInStrategy),
const TradeCostPtr&, const std::vector<OrderBrokerPtr>&>(runInStrategy),
py::arg("pf"), py::arg("query"), py::arg("adjust_cycle"), py::arg("broker"),
py::arg("cost_func"));
py::arg("cost_func"), py::arg("other_brokers") = std::vector<OrderBrokerPtr>(),
R"(run_in_strategy(pf, query, adjust_cycle, broker, costfunc, [other_brokers=[]])
在策略运行时中执行组合策略 PF
目前仅支持 buy_delay| sell_delay 均为 false 的系统,即 close 时执行交易
:param Portfolio pf: 资产组合
:param Query query: 查询条件
:param int adjust_cycle: 调仓周期
:param broker: 订单代理(专用与和账户资产同步的订单代理)
:param costfunc: 成本函数
:param other_brokers: 其他的订单代理)");

m.def("crt_sys_strategy", crtSysStrategy, py::arg("sys"), py::arg("stk_market_code"),
py::arg("query"), py::arg("broker"), py::arg("cost_func"),
py::arg("name") = "SYSStrategy", py::arg("config") = "");
py::arg("other_brokers") = std::vector<OrderBrokerPtr>(), py::arg("name") = "SYSStrategy",
py::arg("config") = "");

m.def("crt_pf_strategy", crtPFStrategy, py::arg("pf"), py::arg("query"),
py::arg("adjust_cycle"), py::arg("broker"), py::arg("cost_func"),
py::arg("name") = "PFStrategy", py::arg("other_brokers") = std::vector<OrderBrokerPtr>(),
py::arg("config") = "");

m.def("get_data_from_buffer_server", getDataFromBufferServer);
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ Hikyuu Quant Framework是一款基于C++/Python的开源量化交易研究框架

![weixin](docs/source/_static/weixin.jpg)

## QQ交流群(逐渐废弃):114910869, 或扫码加入
## QQ交流群:114910869, 或扫码加入

![img](docs/source/_static/10003-qq.png)

0 comments on commit ef40fe7

Please sign in to comment.