add crt_sys_strategy,补充示例

This commit is contained in:
fasiondog 2024-08-25 22:58:17 +08:00
parent 03cb9e63c3
commit 0288e77644
6 changed files with 53 additions and 11 deletions

View File

@ -2,6 +2,11 @@
# -*- coding: utf8 -*-
# cp936
#
# 警告Hikyuu 为量化研究工具,本身不包含程序化交易接口。此部分仅为策略调度运行时示例,
# 供自行实现程序化交易时参考,请自行负责程序化交易可能造成的损失。
#
from hikyuu import Strategy, Query, Datetime, Seconds, Minutes
from hikyuu import sm

View File

@ -1,13 +1,16 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
import easytrader
from hikyuu import *
#
# 警告Hikyuu 为量化研究工具,本身不包含程序化交易接口。此部分仅为策略调度运行时示例,
# 供自行实现程序化交易时参考,请自行负责程序化交易可能造成的损失。
#
# 创建 easytrade 订单代理(示例仅支持华泰)可自行参照 EasyTraderOrderBroker 修改
# buy|sell 中已屏蔽实际通过easytrade下单防止调试误操作请自行根据需要打开
# buy|sell 中已屏蔽实际通过easytrade下单防止调试误操作
user = easytrader.use('ht_client')
user.connect(r'D:\htwt\xiadan.exe')
easy_ob = EasyTraderOrderBroker(user)
@ -22,6 +25,8 @@ sys.set_param("sell_delay", False)
# 执行策略主体
def my_func():
# 这里示例使用的是 TC_Zero() 零成本算法,但实际建议使用接近实际的成本算法
# 因为 sys 不依赖于成交单中的实际成本,而是使用成本算法预算需要买入的数量
run_in_strategy(sys, sm['sz000001'], Query(Datetime(20240101)), broker, TC_Zero())
@ -38,3 +43,5 @@ if __name__ == '__main__':
# 每交易日 14点55分 执行
s.run_daily_at(my_func, TimeDelta(0, 14, 55))
s.start()
# 上述,也可以参见 strategy_demo3.py 中使用 crt_sys_strategy 快捷创建 strategy

View File

@ -0,0 +1,24 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
#
# 警告Hikyuu 为量化研究工具,本身不包含程序化交易接口。此部分仅为策略调度运行时示例,
# 供自行实现程序化交易时参考,请自行负责程序化交易可能造成的损失。
#
if __name__ == '__main__':
import easytrader
from hikyuu import *
# 创建 easytrade 订单代理(示例仅支持华泰)可自行参照 EasyTraderOrderBroker 修改
user = easytrader.use('ht_client')
user.connect(r'D:\htwt\xiadan.exe')
easy_ob = EasyTraderOrderBroker(user)
broker = crtOB(easy_ob)
sys = get_part("default.sys.趋势双均线")
# 直接使用 sys 创建 strategy 示例,如果为日线,则自动每日 14点55分 执行
# 如果 query 为日线以下分钟线、5分钟线则自动按对应间隔分钟、5分钟循环执行
stg = crt_sys_strategy(sys, "sz000001", Query(Datetime(20240101), ktype=Query.DAY), broker, TC_Zero(), "demo3")
stg.start()

View File

@ -60,21 +60,22 @@ void RunSystemInStrategy::run(const Stock& stock) {
}
}
StrategyPtr HKU_API crtSysStrategy(const SYSPtr& sys, const Stock& stk, const KQuery& query,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const string& name, const string& config_file) {
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 string& config_file) {
std::shared_ptr<RunSystemInStrategy> runner =
std::make_shared<RunSystemInStrategy>(sys, broker, query, costfunc);
std::function<void()> func = [=]() { runner->run(stk); };
std::function<void()> func = [=]() { runner->run(getStock(stk_market_code)); };
KQuery::KType ktype = query.kType();
StrategyPtr stg = std::make_shared<Strategy>(vector<string>{stk.market_code()},
StrategyPtr stg = std::make_shared<Strategy>(vector<string>{stk_market_code, "SH000001"},
vector<KQuery::KType>{ktype}, name, config_file);
int32_t m = KQuery::getKTypeInMin(ktype);
if (m < KQuery::getKTypeInMin(KQuery::DAY)) {
stg->runDaily(std::move(func), Minutes(m), stk.market());
stg->runDaily(std::move(func), Minutes(m), "SH");
} else {
stg->runDailyAt(std::move(func), TimeDelta(0, 14, 50));
}

View File

@ -29,9 +29,9 @@ private:
TradeRequest m_sellRequest;
};
StrategyPtr HKU_API crtSysStrategy(const SYSPtr& sys, const Stock& stk, const KQuery& query,
const OrderBrokerPtr& broker, const TradeCostPtr& costfunc,
const string& name = "SYSStrategy",
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 string& config_file = "");
} // namespace hku

View File

@ -7,6 +7,7 @@
#include <hikyuu/strategy/Strategy.h>
#include <hikyuu/strategy/BrokerTradeManager.h>
#include <hikyuu/strategy/RunSystemInStrategy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
@ -110,4 +111,8 @@ void export_Strategy(py::module& m) {
const TradeCostPtr&>(runInStrategy),
py::arg("pf"), py::arg("query"), py::arg("adjust_cycle"), py::arg("broker"),
py::arg("cost_func"));
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") = "");
}