# 策略 Demo2:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <kungfu/yijinjing/common.h>
#include <kungfu/yijinjing/log/setup.h>
#include <kungfu/wingchun/strategy/context.h>
#include <kungfu/wingchun/strategy/strategy.h>
namespace py = pybind11;
using namespace kungfu;
using namespace kungfu::wingchun;
using namespace kungfu::wingchun::strategy;
#define ACCOUNT_ID "15003941"
class DemoStrategy : public StrategyWithBook
{
public:
DemoStrategy(yijinjing::data::location_ptr home):StrategyWithBook(home), count_(0)
{
yijinjing::log::copy_log_settings(home, home->name);
};
void test_timer(yijinjing::event_ptr event, Context_ptr context)
{
return;
}
void test_time_interval(Context_ptr context, yijinjing::event_ptr event)
{
//注意:book和position数据,是异步更新,而非实时更新
auto book1 = get_book();
SPDLOG_INFO("Strategy available cash:{}", book1->get_available_cash());
SPDLOG_INFO("Strategy Book: {}", book1->event()->as_dict().dump());
auto pos_uids1 = book1->get_position_uids();
SPDLOG_INFO("Strategy position as follow :");
for(auto& pos_id: pos_uids1)
{
auto pos = book1->get_position(pos_id);
SPDLOG_INFO("instrument_id:{}, volume:{}, sellable volume:{}, avg_open_price :{}", pos->instrument_id_ , pos->volume_ - pos->frozen_total_, pos->yesterday_volume_ - pos->frozen_yesterday_, pos->avg_open_price_);
SPDLOG_INFO("Strategy Position: {}", pos->event()->as_dict().dump());
}
auto book2 = get_account_book(SOURCE_XTP, ACCOUNT_ID);
SPDLOG_INFO("Account available cash:{}", book2->get_available_cash());
SPDLOG_INFO("Account Book: {}", book2->event()->as_dict().dump());
auto pos_uids2 = book2->get_position_uids();
SPDLOG_INFO("Account position as follow :");
for(auto& pos_id: pos_uids2)
{
auto pos = book2->get_position(pos_id);
SPDLOG_INFO("instrument_id:{}, volume:{}, sellable volume:{}, avg_open_price :{}", pos->instrument_id_ , pos->volume_ - pos->frozen_total_, pos->yesterday_volume_ - pos->frozen_yesterday_, pos->avg_open_price_);
SPDLOG_INFO("Account Position: {}", pos->event()->as_dict().dump());
}
return;
}
void cancel_order(yijinjing::event_ptr event, Context_ptr context, uint64_t order_id)
{
context->cancel_order(order_id);
}
// 启动前
void pre_start(Context_ptr context) override
{
SPDLOG_INFO("cpp demo pre start");
StrategyWithBook::pre_start(context);
bool load_ret = context->load_etf_info();
const std::string empty_str("");
// 加载ETF数据,尽量在TD启动三分钟之后再启动策略,防止TD还没有收集齐ETF数据
SPDLOG_INFO("load etf: {}.", load_ret);
if(load_ret)
{
// 只有加载成功后,才能进行以下操作
// 1、获取全市场的ETF清单,返回的map是以“ETF市场_ETF代码”组合成的字符串作为key,比如:SZE_159646
std::unordered_map<std::string, wingchun::msg::data::ETFBase> bases = context->get_all_etfs();
bool basket_prt = false;
std::string etf_code("");
for(const auto& it : bases)
{
etf_code = it.first;
if(!basket_prt)
{
basket_prt = true;
// 2、获取指定ETF的所有成分股
std::unordered_map<std::string, wingchun::msg::data::ETFBasket> baskets = context->get_etf_all_baskets(etf_code);
for(const auto& basket : baskets)
{
SPDLOG_INFO("etf code: {} basket code: {}.", basket.second.get_class_key(), basket.second.get_major_key());
}
}
SPDLOG_INFO("etf code: {}, num: {}.", etf_code, context->get_etf_all_baskets(etf_code).size());
}
// 3、获取单市场的所有ETF清单,市场不能为空,EXCHANGE_SSE:沪市;EXCHANGE_SZE:深市
std::unordered_map<std::string, wingchun::msg::data::ETFBase> sh_bases = context->get_etf_base(EXCHANGE_SSE, "");
for(const auto& it : sh_bases)
{
etf_code = it.first;
SPDLOG_INFO("all shanghai etf code: {}.", etf_code);
}
SPDLOG_INFO("all shanghai etf total num: {}.", sh_bases.size());
// 4、获取指定某支ETF清单,市场和ETF代码都传即可;若两个参数都正确传值,返回的map大小为1;否则为空
sh_bases = context->get_etf_base(EXCHANGE_SSE, "588060");
for(const auto& it : sh_bases)
{
etf_code = it.first;
// 5、获取指定ETF的某个市场的所有成分股,市场不能为空, 返回的map是以“成分股市场_成分股代码”组合成的字符串作为key,比如:SSE_688772
std::unordered_map<std::string, wingchun::msg::data::ETFBasket> baskets = context->get_etf_baskets(etf_code, EXCHANGE_SSE, "");
SPDLOG_INFO("shanghai etf code: {}, basket num: {}.", etf_code, baskets.size());
for(const auto& basket : baskets)
{
SPDLOG_INFO("etf code: {} shanghai basket code: {}.", basket.second.get_class_key(), basket.second.get_major_key());
}
// 6、获取指定ETF的某个市场的某支成分股;若三个参数都正确传值,返回的map大小为1;否则为空
baskets = context->get_etf_baskets(etf_code, EXCHANGE_SSE, "688772");
for(const auto& basket : baskets)
{
SPDLOG_INFO("etf code: {} 688772 basket code: {}.", basket.second.get_class_key(), basket.second.get_major_key());
}
}
}
// 获取配置文件的路径
std::string path = getParamFileDir();
SPDLOG_INFO("path: {}", path);
std::vector<std::string> tickers;
tickers.push_back("600000");
tickers.push_back("601988");
add_account(context, SOURCE_XTP, ACCOUNT_ID, 100000000.0);
// 查询新发行的债
// 备注:context->query_bondIPO_infolist 函数从2.1.1版本开始支持
context->query_bondIPO_infolist(ACCOUNT_ID);
//请求登录xtp算法交易服务器
//备注:context->login_algo_server()函数从2.0.12版本开始支持
//备注:参数中的"username"和"password",需要根据实际情况填写
//context->login_algo_server(SOURCE_XTP, ACCOUNT_ID, "username", "password")
// context->subscribe(SOURCE_XTP, tickers, EXCHANGE_SSE);
context->subscribe_market_data(SOURCE_XTP, tickers, EXCHANGE_SSE);
// context->subscribe_tick_by_tick(SOURCE_XTP, tickers, EXCHANGE_SSE);
// context->subscribe_order_book(SOURCE_XTP, tickers, EXCHANGE_SSE);
// 订阅买一卖一队列行情
// 备注:context->subscribe_bid1_ask1()函数从2.0.11版本开始支持
//context->subscribe_bid1_ask1(SOURCE_XTP, tickers, EXCHANGE_SSE)
auto f1 = std::bind(&DemoStrategy::test_timer, this, std::placeholders::_1, context);
context->add_timer(yijinjing::time::now_in_nano() + yijinjing::time_unit::NANOSECONDS_PER_SECOND * 5, f1);
// 静态行情数量大于零,说明获取成功
// quotes = context->getAllStaticQuote();
// SPDLOG_INFO("load status quote num: {}.", quotes.size());
// 信用账户查询相关示例
// 请求查询信用账户特有信息
// 备注:context->query_credit_fund_info()函数从2.0.8版本开始支持
context->query_credit_fund_info(SOURCE_XTP, ACCOUNT_ID)
// 融资融券业务中请求现金直接还款
// 备注:context->credit_cash_repay()函数从2.0.8版本开始支持
// context->credit_cash_repay(SOURCE_XTP, ACCOUNT_ID, 50000.00)
// 请求查询信用账户可融券头寸信息
// 备注:context->query_credit_ticker_assign_info()函数从2.0.11版本开始支持
//context->query_credit_ticker_assign_info(SOURCE_XTP, ACCOUNT_ID, "600000", EXCHANGE_SSE);
// 请求查询信用账户负债合约信息
// 备注:context->query_credit_debt_info()函数从2.0.11版本开始支持
//context->query_credit_debt_info(SOURCE_XTP, ACCOUNT_ID);
// 请求查询信用账户指定证券负债未还信息
// 备注:context->query_credit_ticker_debt_info()函数从2.0.11版本开始支持
//context->query_credit_ticker_debt_info(SOURCE_XTP, ACCOUNT_ID, "600000", EXCHANGE_SSE);
// 请求查询信用账户待还资金信息
// 备注:context->query_credit_asset_debt_info()函数从2.0.11版本开始支持
//context->query_credit_asset_debt_info(SOURCE_XTP, ACCOUNT_ID);
// 请求债转股信息
// 备注:context->query_bondswapstock_info()函数从2.1.2版本开始支持
// 查询所有的债转股信息
context->query_bondswapstock_info(ACCOUNT_ID);
// 查询单支债转股信息
// context->query_bondswapstock_info(ACCOUNT_ID, "110001", EXCHANGE_SSE);
// 查询用户新股申购额度信息
// 备注:context->query_IPO_quota_info()函数从2.1.8版本开始支持
context->query_IPO_quota_info(SOURCE_XTP, ACCOUNT_ID);
// 查询今日可申购新股信息列表
// /备注:context->query_IPO_info_list()函数从2.1.8版本开始支持
context->query_IPO_info_list(SOURCE_XTP, ACCOUNT_ID);
auto f4= std::bind(&DemoStrategy::test_time_interval, this, context, std::placeholders::_1);
//NANOSECONDS_PER_SECOND是一个系统定义的数值常量,为1000000000,单位是纳秒,也即1秒钟;
//客户可根据实际需要,自行对该值进行替换
context->add_time_interval(yijinjing::time_unit::NANOSECONDS_PER_SECOND*30, f4);
SPDLOG_INFO("account_cash_limit: {}", context->get_account_cash_limit(ACCOUNT_ID));
}
// 启动后
void post_start(Context_ptr context) override
{
SPDLOG_INFO("cpp demo post start");
}
// 策略退出前,可在此做一些回收工作
void pre_stop(Context_ptr context) override
{
SPDLOG_INFO("cpp demo pre stop");
}
// 退出后
void post_stop(Context_ptr context) override
{
SPDLOG_INFO("cpp demo post stop");
}
void on_trading_day(Context_ptr context, int64_t daytime) override
{
SPDLOG_INFO("cpp demo on trading day");
}
// bar 分钟级别行情回调
void on_bar(Context_ptr context, const wingchun::msg::data::Bar &bar) override
{
SPDLOG_INFO("cpp demo on bar");
}
// 行情数据更新回调
void on_quote(Context_ptr context, const wingchun::msg::data::Quote "e) override
{
if(count_>=5)
return;
SPDLOG_INFO("cpp demo on quote");
uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
200, PriceType::Limit, Side::Buy, Offset::Open, HedgeFlag::Speculation);
//信用交易示例
//普通买入
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
// 200, PriceType::Limit, Side::Buy, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//普通卖出
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.bid_price[0],
// 200, PriceType::Limit, Side::Sell, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//融资买入
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.bid_price[0],
// 200, PriceType::Limit, Side::MarginTrade, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//卖券还款
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
// 200, PriceType::Limit, Side::RepayMargin, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//融券卖出
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
// 200, PriceType::Limit, Side::ShortSell, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//买券还券
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
// 200, PriceType::Limit, Side::RepayStock, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
//现券还券
//uint64_t order_id = context->insert_order(quote.instrument_id, quote.exchange_id, ACCOUNT_ID, quote.ask_price[0],
// 200, PriceType::Limit, Side::StockRepayStock, Offset::Open, HedgeFlag::Speculation, BusinessType::MARGIN);
char msg[1024];
sprintf(msg, "insert_order(%s,%s)", quote.instrument_id, quote.exchange_id);
context->send_msg(msg);
if (order_id > 0)
{
auto f = std::bind(&DemoStrategy::cancel_order, this, std::placeholders::_1, context, order_id);
context->add_timer(yijinjing::time::now_in_nano() + yijinjing::time_unit::NANOSECONDS_PER_SECOND, f);
count_++;
}
SPDLOG_INFO("quote received: [time]{} [instrument_id]{} [last_price]{}, msg: {}.",
yijinjing::time::strftime(quote.data_time), quote.instrument_id, quote.last_price,msg);
//策略用户在双中心间进行资金划转(从远端节点转入到本节点200元)。
//ACCOUNT_ID是当前下单用的资金账户;“123456”是账户的密码,需根据实际情况设定
//context->fund_transfer(ACCOUNT_ID, “123456”, FUND_TRANSFER_TYPE::FUND_TRANSFER_IN, 200.0);
//策略用户查询双中心间资金划转情况。
//ACCOUNT_ID是当前下单用的资金账户;“123456”是账户的密码,需根据实际情况设定
//context->query_other_server_fund(ACCOUNT_ID, “123456”, FUND_QUERY_TYPE::FUND_QUERY_INTERNAL) ;
}
//指数通行情更新回调
//@param index_press 指数通的行情数据
void on_index_press(Context_ptr context, const wingchun::msg::data::IndexPress& index_press) override
{
SPDLOG_INFO("index_press received: [instrument_id]{}", index_press.instrument_id);
}
//港股通实时额度更新回调
//@param hkc_rtl 港股通实时额度数据
void on_hkc_realtime_limit(Context_ptr context, const wingchun::msg::data::HKCRealtimeLimit& hkc_rtl) override
{
SPDLOG_INFO("hkc_realtime_limit received: [初始额度]{} [剩余额度]{}", hkc_rtl.threshold_amount, hkc_rtl.pos_amt);
}
// 买一卖一队列行情数据更新回调
// 备注:on_bid1ask1()函数从2.0.11版本开始支持
void on_bid1ask1(Context_ptr context, const msg::data::Bid1Ask1 &b1a1) override
{
SPDLOG_INFO("cpp demo on Bid1Ask1");
}
// 逐笔成交更新回调
void on_transaction(Context_ptr context, const wingchun::msg::data::Transaction &transaction) override
{
SPDLOG_INFO("cpp demo on transaction");
}
// 逐笔委托更新回调
void on_entrust(Context_ptr context, const wingchun::msg::data::Entrust &entrust) override
{
SPDLOG_INFO("cpp demo on entrust");
}
// 订单信息更新回调
void on_order(Context_ptr context, const wingchun::msg::data::Order &order) override
{
SPDLOG_INFO("cpp demo on order");
}
// 订单成交回报回调
void on_trade(Context_ptr context, const wingchun::msg::data::Trade &trade) override
{
SPDLOG_INFO("cpp demo on trade");
}
// 订单操作错误回调
void on_order_action_error(Context_ptr context, const wingchun::msg::data::OrderActionError &error) override
{
SPDLOG_INFO("cpp demo on order action error");
}
// 订单簿数据更新回调
void on_order_book(Context_ptr context, const wingchun::msg::data::OrderBook& order_book) override
{
SPDLOG_INFO("cpp demo on order book");
}
// // 撤单反馈信息回调
// void on_cancelorder_feedback(Context_ptr context, const wingchun::msg::data::CancelOrderFeedBack &cofeedback) override
// {
// SPDLOG_INFO("cpp demo on cancelorder feedback");
// }
// 资金账户响应回调
// 备注:on_asset()函数从2.0.11版本开始支持
void on_asset(Context_ptr context, const msg::data::Asset& asset) override
{
SPDLOG_INFO("cpp demo on asset");
}
// 接收前端发送回来的客户自定义消息
void on_client_msg(Context_ptr context, const std::string &msg) override
{
SPDLOG_INFO("cpp demo on client msg");
}
// 融资融券特有帐户数据回调
// 备注:on_query_credit_fund_info()函数从2.0.8版本开始支持
void on_query_credit_fund_info(Context_ptr context, const msg::data::CreditFundInfo &crdfundinfo) override
{
SPDLOG_INFO("cpp demo on on_query_credit_fund_info");
}
// 融资融券业务中现金直接还款的响应回调
// 备注:on_credit_cash_repay()函数从2.0.8版本开始支持
void on_credit_cash_repay(Context_ptr context, const msg::data::CreditCashRepayRsp& cash_repay_rsp) override
{
SPDLOG_INFO("cpp demo on on_credit_cash_repay");
}
// 请求查询信用账户可融券头寸信息的响应回调
// 备注:on_query_credit_ticker_assign_info()函数从2.0.11版本开始支持
void on_query_credit_ticker_assign_info(Context_ptr context, const msg::data::CrdPositionStkInfo &crd_position_stk_info) override
{
SPDLOG_INFO("cpp demo on query_credit_ticker_assign_info");
}
// 请求查询信用账户负债信息的响应回调
// 备注:on_query_credit_debt_info()函数从2.0.11版本开始支持
void on_query_credit_debt_info(Context_ptr context, const msg::data::CrdDebtInfo &crd_debt_info) override
{
SPDLOG_INFO("cpp demo on query_credit_debt_info");
}
// 请求查询信用账户指定证券负债未还信息响应回调
// 备注:on_query_credit_ticker_debt_info()函数从2.0.11版本开始支持
void on_query_credit_ticker_debt_info(Context_ptr context, const msg::data::CrdDebtStockInfo &crd_debt_stk_info) override
{
SPDLOG_INFO("cpp demo on query_credit_ticker_debt_info");
}
// 请求查询信用账户待还资金的响应回调
// 备注:on_query_credit_asset_debt_info()函数从2.0.11版本开始支持
void on_query_credit_asset_debt_info(Context_ptr context, const double &remain_amount) override
{
SPDLOG_INFO("cpp demo on query_credit_asset_debt_info");
}
// 资金划转结果回调
void on_fund_transfer(Context_ptr context, const msg::data::FundTransferFeedBack &feedback) override
{
SPDLOG_INFO("cpp demo on fund_transfer");
}
// 资金查询结果回调
void on_query_other_server_fund(Context_ptr context, const msg::data::FundQueryFeedBack &feedback) override
{
SPDLOG_INFO("cpp demo on query_other_server_fund");
}
// xtp算法交易策略信息响应回调
// 备注: on_algo_strategy_info()函数从2.0.12版本开始支持
void on_algo_strategy_info(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on algo_strategy_info");
}
// xtp算法交易运行时策略状态结构体响应回调
// 备注: on_algo_strategy_state_report()函数从2.0.12版本开始支持
void on_algo_strategy_state_report(Context_ptr context, const msg::data::AlgoStrategyStateReport& algo_strategy_state_report) override
{
SPDLOG_INFO("cpp demo on algo_strategy_state_report");
}
// xtp算法交易创建策略反馈信息响应回调
// 备注: on_create_algo_strategy()函数从2.0.12版本开始支持
void on_create_algo_strategy(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on create_algo_strategy");
context->start_algo_strategy(ACCOUNT_ID, algo_strategy_info.xtp_strategy_id);
}
// xtp算法交易查询策略反馈信息响应回调
// 备注: on_query_algo_strategy()函数从2.0.12版本开始支持
void on_query_algo_strategy(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on query_algo_strategy");
}
// xtp算法交易开始运行策略反馈信息响应回调
// 备注: on_start_algo_strategy()函数从2.0.12版本开始支持
void on_start_algo_strategy(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on start_algo_strategy");
}
// xtp算法交易停止运行策略反馈信息响应回调
// 备注: on_stop_algo_strategy()函数从2.0.12版本开始支持
void on_stop_algo_strategy(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on stop_algo_strategy");
}
// xtp算法交易销毁策略反馈信息响应回调
// 备注: on_destroy_algo_strategy()函数从2.0.12版本开始支持
void on_destroy_algo_strategy(Context_ptr context, const msg::data::AlgoStrategyInfo& algo_strategy_info) override
{
SPDLOG_INFO("cpp demo on destroy_algo_strategy");
}
// 登录xtp算法交易服务器反馈信息响应回调
// 备注: on_login_algo_server()函数从2.0.12版本开始支持
void on_login_algo_server(Context_ptr context, bool success) override
{
SPDLOG_INFO("cpp demo on login_algo_server");
if(success)
{
context->create_algo_strategy(ACCOUNT_ID, 2001, 202010140939, "{\"buyDateTime\":[\"09:50\",\"10:00\"],\"stop_selling_while_buy_overflow\":false,\"cancel_overflow_order_after_clear_time\":true,\"start_time\":\"09:50:00\",\"end_time\":\"10:00:00\",\"trade_list\":[{\"market\":\"SZ\",\"clientStrategyId\":\"202010140939\",\"quantity\":20000,\"side\":\"BUY\",\"ticker\":\"002513\"},{\"market\":\"SZ\",\"clientStrategyId\":\"202010140939\",\"quantity\":20000,\"side\":\"BUY\",\"ticker\":\"001914\"}],\"business_type\":\"CASH\"}");
}
}
// 查询新发行的债信息响应回调
// 备注: on_query_bondIPO_info函数从2.1.1版本开始支持
void on_query_bondIPO_info(Context_ptr context, const wingchun::msg::data::IPOTickerInfo &IPOInfo) override
{
if(0 == IPOInfo.error_id)
{
SPDLOG_INFO("bond ipo info: {}", wingchun::msg::data::to_string(IPOInfo));
}
else
{
// 查询失败
SPDLOG_WARN("error bond ipo info: {}", wingchun::msg::data::to_string(IPOInfo));
}
}
// 查询新发行的债信息推送结束响应回调
// 备注: on_query_bondIPO_end函数从2.1.1版本开始支持
void on_query_bondIPO_end(Context_ptr context) override
{
// 查询结果推送完成
SPDLOG_INFO("collect finish.");
}
// 查询债转股信息响应回调
// 备注: on_query_bondswapstock_info函数从2.1.2版本开始支持
void on_query_bondswapstock_info(Context_ptr context, const wingchun::msg::data::QueryBondSwapStockRsp &swap_info)
{
if(0 == swap_info.error_id)
{
SPDLOG_INFO("bond swap stock info: {}", wingchun::msg::data::to_string(swap_info));
}
else
{
// 查询失败
SPDLOG_WARN("error bond swap stock info: {}", wingchun::msg::data::to_string(swap_info));
}
}
// 查询债转股信息响应结束回调
// 备注: on_query_bondswapstock_end函数从2.1.2版本开始支持
void on_query_bondswapstock_end(Context_ptr context)
{
// 查询结果推送完成
SPDLOG_INFO("collect finish.");
}
// 接收查询到的用户新股申购额度信息;函数从2.1.8版本开始支持
virtual void on_query_IPO_quota_info(Context_ptr context, const wingchun::msg::data::QueryIPOQuotaRsp "a_info) override
{
if(0 == quota_info.error_id)
{
SPDLOG_INFO("IPO quota info: {}", to_string(quota_info));
}
else
{
SPDLOG_WARN("error IPO quota info: {}", to_string(quota_info));
}
};
// 完成接收查询到的用户新股申购额度信息;函数从2.1.8版本开始支持
virtual void on_query_IPO_quota_info_end(Context_ptr context) override
{
SPDLOG_INFO("IPO quota info collected finish.");
};
// 接收查询到的今日可申购新股信息;函数从2.1.8版本开始支持
virtual void on_query_IPO_info_list(Context_ptr context, const wingchun::msg::data::QueryIPOTickerRsp &ipo_info) override
{
if(0 == ipo_info.error_id)
{
SPDLOG_INFO("IPO ticker info: {}", to_string(ipo_info));
}
else
{
SPDLOG_WARN("error IPO ticker info: {}", to_string(ipo_info));
}
};
// 完成接收查询到的今日可申购新股信息;函数从2.1.8版本开始支持
virtual void on_query_IPO_info_list_end(Context_ptr context) override
{
SPDLOG_INFO("IPO ticker info collected finish.");
};
// 获取编译策略的kungfu版本号,本函数不允许修改。
virtual const std::string getCompileVersion() const override
{
std::ostringstream ver;
#ifdef ALPHAX_VERSION
ver<<ALPHAX_VERSION;
#else
ver<<DEFAULT_COMPILE_VERSION;
#endif
SPDLOG_INFO("Version: {}.", ver.str());
return ver.str();
}
private:
// 静态行情整个交易日都不会变,启动策略时取到所有证券的静态数据后,需要时直接使用
// 而不需要用一次查一次,性能很差(因为需要读写文件)
// add by zz on 20210112
std::unordered_map<std::string, QuoteStaticFullInfo> quotes;
uint64_t count_;
};
PYBIND11_MODULE(PY_MODULE, m)
{
py::class_<DemoStrategy, Strategy, std::shared_ptr<DemoStrategy>>(m, "Strategy")
.def(py::init<yijinjing::data::location_ptr>())
.def("pre_start", &DemoStrategy::pre_start)
.def("post_start", &DemoStrategy::post_start)
.def("pre_stop", &DemoStrategy::pre_stop)
.def("post_stop", &DemoStrategy::post_stop)
.def("on_trading_day", &DemoStrategy::on_trading_day)
.def("on_quote", &DemoStrategy::on_quote)
.def("on_index_press", &DemoStrategy::on_index_press)
.def("on_hkc_realtime_limit", &DemoStrategy::on_hkc_realtime_limit)
.def("on_bid1ask1", &DemoStrategy::on_bid1ask1) // 备注:on_bid1ask1()函数从2.0.11版本开始支持
.def("on_bar", &DemoStrategy::on_bar)
.def("on_entrust", &DemoStrategy::on_entrust)
.def("on_transaction", &DemoStrategy::on_transaction)
.def("on_order", &DemoStrategy::on_order)
.def("on_order_action_error", &DemoStrategy::on_order_action_error)
.def("on_trade", &DemoStrategy::on_trade)
.def("on_order_book", &DemoStrategy::on_order_book)
.def("on_asset", &DemoStrategy::on_asset) // 备注:on_asset()函数从2.0.11版本开始支持
.def("on_client_msg", &DemoStrategy::on_client_msg)
.def("on_query_credit_fund_info", &DemoStrategy::on_query_credit_fund_info) // on_query_credit_fund_info()函数从2.0.8版本开始支持
.def("on_credit_cash_repay", &DemoStrategy::on_credit_cash_repay) // on_credit_cash_repay()函数从2.0.8版本开始支持
.def("on_query_credit_ticker_assign_info", &DemoStrategy::on_query_credit_ticker_assign_info) // 备注:on_query_credit_ticker_assign_info()函数从2.0.11版本开始支持
.def("on_query_credit_debt_info", &DemoStrategy::on_query_credit_debt_info) // 备注:on_query_credit_debt_info()函数从2.0.11版本开始支持
.def("on_query_credit_ticker_debt_info", &DemoStrategy::on_query_credit_ticker_debt_info) // 备注:on_query_credit_ticker_debt_info()函数从2.0.11版本开始支持
.def("on_query_credit_asset_debt_info", &DemoStrategy::on_query_credit_asset_debt_info) // 备注:on_query_credit_asset_debt_info()函数从2.0.11版本开始支持
.def("on_fund_transfer", &DemoStrategy::on_fund_transfer)
.def("on_query_other_server_fund", &DemoStrategy::on_query_other_server_fund)
.def("on_algo_strategy_info", &DemoStrategy::on_algo_strategy_info) // on_algo_strategy_info()函数从2.0.12版本开始支持
.def("on_algo_strategy_state_report", &DemoStrategy::on_algo_strategy_state_report) // 备注: on_algo_strategy_state_report()函数从2.0.12版本开始支持
.def("on_create_algo_strategy", &DemoStrategy::on_create_algo_strategy) // 备注: on_create_algo_strategy()函数从2.0.12版本开始支持
.def("on_query_algo_strategy", &DemoStrategy::on_query_algo_strategy) // 备注: on_query_algo_strategy()函数从2.0.12版本开始支持
.def("on_start_algo_strategy", &DemoStrategy::on_start_algo_strategy) // 备注: on_start_algo_strategy()函数从2.0.12版本开始支持
.def("on_stop_algo_strategy", &DemoStrategy::on_stop_algo_strategy) // 备注: on_stop_algo_strategy()函数从2.0.12版本开始支持
.def("on_destroy_algo_strategy", &DemoStrategy::on_destroy_algo_strategy) // 备注: on_destroy_algo_strategy()函数从2.0.12版本开始支持
.def("on_login_algo_server", &DemoStrategy::on_login_algo_server) // 备注: on_login_algo_server()函数从2.0.12版本开始支持
.def("on_query_bondIPO_info", &DemoStrategy::on_query_bondIPO_info)// 备注: on_query_bondIPO_info()函数从2.1.1版本开始支持
.def("on_query_bondIPO_end", &DemoStrategy::on_query_bondIPO_end)// 备注: on_query_bondIPO_end()函数从2.1.1版本开始支持
.def("on_query_bondswapstock_info", &DemoStrategy::on_query_bondswapstock_info)// 备注: on_query_bondswapstock_info()函数从2.1.2版本开始支持
.def("on_query_bondswapstock_end", &DemoStrategy::on_query_bondswapstock_end)// 备注: on_query_bondswapstock_end()函数从2.1.2版本开始支持
.def("on_query_IPO_quota_info", &DemoStrategy::on_query_IPO_quota_info)// 备注: on_query_IPO_quota_info()函数从2.1.8版本开始支持
.def("on_query_IPO_quota_info_end", &DemoStrategy::on_query_IPO_quota_info_end)// 备注: on_query_IPO_quota_info_end()函数从2.1.8版本开始支持
.def("on_query_IPO_info_list", &DemoStrategy::on_query_IPO_info_list)// 备注: on_query_IPO_info_list()函数从2.1.8版本开始支持
.def("on_query_IPO_info_list_end", &DemoStrategy::on_query_IPO_info_list_end)// 备注: on_query_IPO_info_list_end()函数从2.1.8版本开始支持
.def("getCompileVersion", &DemoStrategy::getCompileVersion);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
备注:“策略 Demo2”,从 2.0.11 版本开始支持