VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 362
声望: 124

CtpGateway仅有的几个交易执行函数

description

CtpGateway继承了BaseGateway,同时包含两个重要的接口:

        self.td_api = CtpTdApi(self)   # 交易接口
        self.md_api = CtpMdApi(self) # 行情接口

另外还有1个向行情服务器订阅行情的订阅函数:

    def subscribe(self, req: SubscribeRequest):
        """"""
        self.md_api.subscribe(req)

4个向交易服务器发送委托请求、取消委托请求、查询账户请求和一个查询持仓请求函数。

    def send_order(self, req: OrderRequest):
        """发送委托请求"""
        if req.type == OrderType.RFQ:
            vt_orderid = self.td_api.send_rfq(req)
        else:
            vt_orderid = self.td_api.send_order(req)
        return vt_orderid

    def cancel_order(self, req: CancelRequest):
        """取消委托请求"""
        self.td_api.cancel_order(req)

    def query_account(self):
        """查询账户请求"""
        self.td_api.query_account()

    def query_position(self):
        """查询持仓请求"""
        self.td_api.query_position()

CtpGateway有账户查询函数,用户持仓函数,可是没有历史委托和历史成交查询函数。

CtpTdApi 提供的主动执行函数:

description

可它仅仅提供了可怜的5个主动执行交易的函数:
send_order()——发委托申请
cancel_order()——取消委托单
send_rfq()
query_account()——查询账户
query_position()——查询持仓

# vnpy.api.generator.ctp_td_source.function.cpp 提供了丰富的接口函数:

int TdApi::reqAuthenticate(const dict &req, int reqid)
int TdApi::reqUserLogin(const dict &req, int reqid)
int TdApi::reqUserLogout(const dict &req, int reqid)
int TdApi::reqUserPasswordUpdate(const dict &req, int reqid)
int TdApi::reqTradingAccountPasswordUpdate(const dict &req, int reqid)
int TdApi::reqUserAuthMethod(const dict &req, int reqid)
int TdApi::reqGenUserCaptcha(const dict &req, int reqid)
int TdApi::reqGenUserText(const dict &req, int reqid)
int TdApi::reqUserLoginWithCaptcha(const dict &req, int reqid)
int TdApi::reqUserLoginWithText(const dict &req, int reqid)
int TdApi::reqUserLoginWithOTP(const dict &req, int reqid)
int TdApi::reqOrderInsert(const dict &req, int reqid)
int TdApi::reqParkedOrderInsert(const dict &req, int reqid)
int TdApi::reqParkedOrderAction(const dict &req, int reqid)
int TdApi::reqOrderAction(const dict &req, int reqid)
int TdApi::reqQueryMaxOrderVolume(const dict &req, int reqid)
int TdApi::reqSettlementInfoConfirm(const dict &req, int reqid)
int TdApi::reqRemoveParkedOrder(const dict &req, int reqid)
int TdApi::reqRemoveParkedOrderAction(const dict &req, int reqid)
int TdApi::reqExecOrderInsert(const dict &req, int reqid)
int TdApi::reqExecOrderAction(const dict &req, int reqid)
int TdApi::reqForQuoteInsert(const dict &req, int reqid)
int TdApi::reqQuoteInsert(const dict &req, int reqid)
int TdApi::reqQuoteAction(const dict &req, int reqid)
int TdApi::reqBatchOrderAction(const dict &req, int reqid)
int TdApi::reqOptionSelfCloseInsert(const dict &req, int reqid)
int TdApi::reqOptionSelfCloseAction(const dict &req, int reqid)
int TdApi::reqCombActionInsert(const dict &req, int reqid)
int TdApi::reqQryOrder(const dict &req, int reqid)
int TdApi::reqQryTrade(const dict &req, int reqid)
int TdApi::reqQryInvestorPosition(const dict &req, int reqid)
int TdApi::reqQryTradingAccount(const dict &req, int reqid)
int TdApi::reqQryInvestor(const dict &req, int reqid)
int TdApi::reqQryTradingCode(const dict &req, int reqid)
int TdApi::reqQryInstrumentMarginRate(const dict &req, int reqid)
int TdApi::reqQryInstrumentCommissionRate(const dict &req, int reqid)
int TdApi::reqQryExchange(const dict &req, int reqid)
int TdApi::reqQryProduct(const dict &req, int reqid)
int TdApi::reqQryInstrument(const dict &req, int reqid)
int TdApi::reqQryDepthMarketData(const dict &req, int reqid)
int TdApi::reqQrySettlementInfo(const dict &req, int reqid)
int TdApi::reqQryTransferBank(const dict &req, int reqid)
int TdApi::reqQryInvestorPositionDetail(const dict &req, int reqid)
int TdApi::reqQryNotice(const dict &req, int reqid)
int TdApi::reqQrySettlementInfoConfirm(const dict &req, int reqid)
int TdApi::reqQryInvestorPositionCombineDetail(const dict &req, int reqid)
int TdApi::reqQryCFMMCTradingAccountKey(const dict &req, int reqid)
int TdApi::reqQryEWarrantOffset(const dict &req, int reqid)
int TdApi::reqQryInvestorProductGroupMargin(const dict &req, int reqid)
int TdApi::reqQryExchangeMarginRate(const dict &req, int reqid)
int TdApi::reqQryExchangeMarginRateAdjust(const dict &req, int reqid)
int TdApi::reqQryExchangeRate(const dict &req, int reqid)
int TdApi::reqQrySecAgentACIDMap(const dict &req, int reqid)
int TdApi::reqQryProductExchRate(const dict &req, int reqid)
int TdApi::reqQryProductGroup(const dict &req, int reqid)
int TdApi::reqQryMMInstrumentCommissionRate(const dict &req, int reqid)
int TdApi::reqQryMMOptionInstrCommRate(const dict &req, int reqid)
int TdApi::reqQryInstrumentOrderCommRate(const dict &req, int reqid)
int TdApi::reqQrySecAgentTradingAccount(const dict &req, int reqid)
int TdApi::reqQrySecAgentCheckMode(const dict &req, int reqid)
int TdApi::reqQrySecAgentTradeInfo(const dict &req, int reqid)
int TdApi::reqQryOptionInstrTradeCost(const dict &req, int reqid)
int TdApi::reqQryOptionInstrCommRate(const dict &req, int reqid)
int TdApi::reqQryExecOrder(const dict &req, int reqid)
int TdApi::reqQryForQuote(const dict &req, int reqid)
int TdApi::reqQryQuote(const dict &req, int reqid)
int TdApi::reqQryOptionSelfClose(const dict &req, int reqid)
int TdApi::reqQryInvestUnit(const dict &req, int reqid)
int TdApi::reqQryCombInstrumentGuard(const dict &req, int reqid)
int TdApi::reqQryCombAction(const dict &req, int reqid)
int TdApi::reqQryTransferSerial(const dict &req, int reqid)
int TdApi::reqQryAccountregister(const dict &req, int reqid)
int TdApi::reqQryContractBank(const dict &req, int reqid)
int TdApi::reqQryParkedOrder(const dict &req, int reqid)
int TdApi::reqQryParkedOrderAction(const dict &req, int reqid)
int TdApi::reqQryTradingNotice(const dict &req, int reqid)
int TdApi::reqQryBrokerTradingParams(const dict &req, int reqid)
int TdApi::reqQryBrokerTradingAlgos(const dict &req, int reqid)
int TdApi::reqQueryCFMMCTradingAccountToken(const dict &req, int reqid)
int TdApi::reqFromBankToFutureByFuture(const dict &req, int reqid)
int TdApi::reqFromFutureToBankByFuture(const dict &req, int reqid)
int TdApi::reqQueryBankAccountMoneyByFuture(const dict &req, int reqid)

其中这两个函数在用户是否可以包含到CtpTdApi接口中?

这些函数中下面两个函数是否可以包含到CtpTdApi接口中,然后封装到CtpGateway中供用户策略中使用。

int TdApi::reqQryOrder(const dict &req, int reqid)
{
    CThostFtdcQryOrderField myreq = CThostFtdcQryOrderField();
    memset(&myreq, 0, sizeof(myreq));
    getString(req, "BrokerID", myreq.BrokerID);
    getString(req, "InvestorID", myreq.InvestorID);
    getString(req, "InstrumentID", myreq.InstrumentID);
    getString(req, "ExchangeID", myreq.ExchangeID);
    getString(req, "OrderSysID", myreq.OrderSysID);
    getString(req, "InsertTimeStart", myreq.InsertTimeStart);   // 开始时间
    getString(req, "InsertTimeEnd", myreq.InsertTimeEnd);     // 结束时间
    getString(req, "InvestUnitID", myreq.InvestUnitID);
    int i = this->api->ReqQryOrder(&myreq, reqid);
    return i;
};

int TdApi::reqQryTrade(const dict &req, int reqid)
{
    CThostFtdcQryTradeField myreq = CThostFtdcQryTradeField();
    memset(&myreq, 0, sizeof(myreq));
    getString(req, "BrokerID", myreq.BrokerID);
    getString(req, "InvestorID", myreq.InvestorID);
    getString(req, "InstrumentID", myreq.InstrumentID);
    getString(req, "ExchangeID", myreq.ExchangeID);
    getString(req, "TradeID", myreq.TradeID);
    getString(req, "TradeTimeStart", myreq.TradeTimeStart);  // 开始时间
    getString(req, "TradeTimeEnd", myreq.TradeTimeEnd);    // 结束时间
    getString(req, "InvestUnitID", myreq.InvestUnitID);
    int i = this->api->ReqQryTrade(&myreq, reqid);
    return i;
};

有必要添加历史委托单和历史成交单查询吗?

无法避免的成交单丢失

交易中,时常会关心上次盈亏和当前算法盈亏状况,这可以通过对开仓以来的所有成交单进行计算得到。
但是因为种种原因,客户端策略可能会丢失部分成交单,造成这种结果的原因很多,如网络故障、电脑宕机、
没有在全交易时段运行策略,都会导致部分成交单不能给被记录。

成交查询可以补齐丢失的成交单

交易所存有完整的成交记录,而且ctp_td_source.function.cpp提供了全面的历史委托查询和历史成交查询函数,
为什么咱们不可以把这些功能多引入一些呢?

有些网关API已经提供了reqQryTrade函数:

vnpy\gateway\da\da_gateway.py(798): self.reqQryTrade(da_req, self.reqid)
vnpy\gateway\tora\td.py(583): err = self._native_api.ReqQryTrade(info, self._get_new_req_id())
vnpy\gateway\uft\uft_gateway.py(489): self.reqQryTrade({}, self.reqid)

Member
avatar
加入于:
帖子: 362
声望: 124

先找到历史委托查询和成交查询的参数字段定义:
description

Member
avatar
加入于:
帖子: 362
声望: 124

C++中ThostFtdcQryOrderField结构

和CThostFtdcQryTradeField结构

它们位于vnpy\api\ctp\include\ctp\ThostFtdcUserApiStruct.h中。

///查询报单
struct CThostFtdcQryOrderField
{
    ///经纪公司代码
    TThostFtdcBrokerIDType  BrokerID;
    ///投资者代码
    TThostFtdcInvestorIDType    InvestorID;
    ///合约代码
    TThostFtdcInstrumentIDType  InstrumentID;
    ///交易所代码
    TThostFtdcExchangeIDType    ExchangeID;
    ///报单编号
    TThostFtdcOrderSysIDType    OrderSysID;
    ///开始时间
    TThostFtdcTimeType  InsertTimeStart;
    ///结束时间
    TThostFtdcTimeType  InsertTimeEnd;
    ///投资单元代码
    TThostFtdcInvestUnitIDType  InvestUnitID;
};

///查询成交
struct CThostFtdcQryTradeField
{
    ///经纪公司代码
    TThostFtdcBrokerIDType  BrokerID;
    ///投资者代码
    TThostFtdcInvestorIDType    InvestorID;
    ///合约代码
    TThostFtdcInstrumentIDType  InstrumentID;
    ///交易所代码
    TThostFtdcExchangeIDType    ExchangeID;
    ///成交编号
    TThostFtdcTradeIDType   TradeID;
    ///开始时间
    TThostFtdcTimeType  TradeTimeStart;
    ///结束时间
    TThostFtdcTimeType  TradeTimeEnd;
    ///投资单元代码
    TThostFtdcInvestUnitIDType  InvestUnitID;
};
Administrator
avatar
加入于:
帖子: 4491
声望: 310

没必要,因为CTP的私有流采用RESTART模式,会自动在登陆后重传今日所有的委托和成交数据回来,用不着额外查询

Member
avatar
加入于:
帖子: 362
声望: 124

哦,知道了。我明白怎么解决了。

顺便问下:

可以把每次登陆后重传今日所有的委托和成交数据,自己保存一份到磁盘,然后再把实时的委托和交易也做保存,这样本地就有了完整的委托和成交记录了,再查询计算就不难了。这想法靠谱吗?

Administrator
avatar
加入于:
帖子: 4491
声望: 310

可以,资管系统就这么做的

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】