private void Cancel(SingleOrder order)
        {
            if (order == null)
            {
                return;
            }

            if (!_bTdConnected)
            {
                EmitError(-1, -1, "交易服务器没有连接,无法撤单");
                tdlog.Error("交易服务器没有连接,无法撤单");
                return;
            }

            DFITCOrderRspDataRtnField pOrderRtn;

            if (_Orders4Cancel.TryGetValue(order, out pOrderRtn))
            {
                Instrument inst      = InstrumentManager.Instruments[order.Symbol];
                string     altSymbol = inst.GetSymbol(Name);

                int localOrderID = pOrderRtn.localOrderID;
                int spdOrderID   = pOrderRtn.spdOrderID;
                if (spdOrderID >= 0)
                {
                    localOrderID = -1;
                }
                else
                {
                    spdOrderID = -1;
                }

                TraderApi.TD_CancelOrder(m_pTdApi, altSymbol, localOrderID, spdOrderID);
            }
        }
Exemple #2
0
 public void Disconnect()
 {
     lock (locker)
     {
         TraderApi.TD_Disconnect();
     }
 }
 void timerPonstion_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     if (_bTdConnected)
     {
         TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, "");
     }
 }
Exemple #4
0
        private void Cancel(SingleOrder order)
        {
            if (null == order)
            {
                return;
            }

            if (!_bTdConnected)
            {
                EmitError(-1, -1, "交易服务器没有连接,无法撤单");
                tdlog.Error("交易服务器没有连接,无法撤单");
                return;
            }

            GenericOrderItem item;

            if (orderMap.TryGetValue(order, out item))
            {
                CThostFtdcOrderField _order;
                if (orderMap.TryGetValue(item, out _order))
                {
                    // 标记下正在撤单
                    orderMap.Order_OrdStatus[order] = order.OrdStatus;
                    EmitExecutionReport(order, OrdStatus.PendingCancel);

                    TraderApi.TD_CancelOrder(m_pTdApi, ref _order);
                }
            }
        }
Exemple #5
0
        private void Cancel(SingleOrder order)
        {
            if (null == order)
            {
                return;
            }

            if (!_bTdConnected)
            {
                EmitError(-1, -1, "交易服务器没有连接,无法撤单");
                tdlog.Error("交易服务器没有连接,无法撤单");
                return;
            }

            Dictionary <string, CThostFtdcOrderField> _Ref2Action;

            if (_Orders4Cancel.TryGetValue(order, out _Ref2Action))
            {
                lock (_Ref2Action)
                {
                    CThostFtdcOrderField __Order;
                    foreach (CThostFtdcOrderField _Order in _Ref2Action.Values)
                    {
                        __Order = _Order;
                        //这地方要是过滤下就好了
                        TraderApi.TD_CancelOrder(m_pTdApi, ref __Order);
                    }
                }
            }
        }
Exemple #6
0
 public void CTP_RegTD()
 {
     CommApi.CTP_RegTDOnConnect(OnConnect_2);
     CommApi.CTP_RegTDOnDisconnect(OnDisconnect_2);
     CommApi.CTP_RegTDOnRspError(OnRspError_2);
     TraderApi.CTP_RegOnErrRtnOrderAction(OnErrRtnOrderAction_2);
     TraderApi.CTP_RegOnErrRtnOrderInsert(OnErrRtnOrderInsert_2);
     TraderApi.CTP_RegOnErrRtnQuoteAction(OnErrRtnQuoteAction_2);
     TraderApi.CTP_RegOnErrRtnQuoteInsert(OnErrRtnQuoteInsert_2);
     TraderApi.CTP_RegOnRspOrderAction(OnRspOrderAction_2);
     TraderApi.CTP_RegOnRspOrderInsert(OnRspOrderInsert_2);
     TraderApi.CTP_RegOnRspQuoteAction(OnRspQuoteAction_2);
     TraderApi.CTP_RegOnRspQuoteInsert(OnRspQuoteInsert_2);
     TraderApi.CTP_RegOnRspQryDepthMarketData(OnRspQryDepthMarketData_2);
     TraderApi.CTP_RegOnRspQryInstrument(OnRspQryInstrument_2);
     TraderApi.CTP_RegOnRspQryInstrumentCommissionRate(OnRspQryInstrumentCommissionRate_2);
     TraderApi.CTP_RegOnRspQryInstrumentMarginRate(OnRspQryInstrumentMarginRate_2);
     TraderApi.CTP_RegOnRspQryInvestorPosition(OnRspQryInvestorPosition_2);
     TraderApi.CTP_RegOnRspQryInvestorPositionDetail(OnRspQryInvestorPositionDetail_2);
     TraderApi.CTP_RegOnRspQryOrder(OnRspQryOrder_2);
     TraderApi.CTP_RegOnRspQryTrade(OnRspQryTrade_2);
     TraderApi.CTP_RegOnRspQrySettlementInfo(OnRspQrySettlementInfo_2);
     TraderApi.CTP_RegOnRspQryTradingAccount(OnRspQryTradingAccount_2);
     TraderApi.CTP_RegOnRtnInstrumentStatus(OnRtnInstrumentStatus_2);
     TraderApi.CTP_RegOnRtnOrder(OnRtnOrder_2);
     TraderApi.CTP_RegOnRtnQuote(OnRtnQuote_2);
     TraderApi.CTP_RegOnRtnTrade(OnRtnTrade_2);
 }
Exemple #7
0
 public void Connect()
 {
     lock (locker)
     {
         TraderApi.TD_Connect();
     }
 }
Exemple #8
0
        //建立交易
        private void Connect_TD()
        {
            lock (_lockTd)
            {
                if (_bWantTdConnect &&
                    (null == m_pTdApi || IntPtr.Zero == m_pTdApi))
                {
                    m_pTdApi = TraderApi.TD_CreateTdApi();
                    TraderApi.XSpeed_RegOnRspCancelOrder(m_pMsgQueue, _fnOnRspCancelOrder_Holder);
                    TraderApi.XSpeed_RegOnRspInsertOrder(m_pMsgQueue, _fnOnRspInsertOrder_Holder);
                    TraderApi.XSpeed_RegOnRspArbitrageInstrument(m_pMsgQueue, _fnOnRspArbitrageInstrument_Holder);
                    TraderApi.XSpeed_RegOnRspQryExchangeInstrument(m_pMsgQueue, _fnOnRspQryExchangeInstrument_Holder);
                    TraderApi.XSpeed_RegOnRtnCancelOrder(m_pMsgQueue, _fnOnRtnCancelOrder_Holder);
                    TraderApi.XSpeed_RegOnRtnInstrumentStatus(m_pMsgQueue, _fnOnRtnInstrumentStatus_Holder);
                    TraderApi.XSpeed_RegOnRtnMatchedInfo(m_pMsgQueue, _fnOnRtnMatchedInfo_Holder);
                    TraderApi.XSpeed_RegOnRtnOrder(m_pMsgQueue, _fnOnRtnOrder_Holder);
                    TraderApi.XSpeed_RegOnRspQuoteSubscribe(m_pMsgQueue, _fnOnRspQuoteSubscribe_Holder);
                    TraderApi.XSpeed_RegOnRtnQuoteSubscribe(m_pMsgQueue, _fnOnRtnQuoteSubscribe_Holder);
                    TraderApi.TD_RegMsgQueue2TdApi(m_pTdApi, m_pMsgQueue);
                    TraderApi.TD_Connect(m_pTdApi, string.Join(";", server.Trading.ToArray()), account.InvestorId, account.Password, 0);

                    //向单例对象中注入操作用句柄
                    XSpeedAPI.GetInstance().__RegTdApi(m_pTdApi);
                }
            }
        }
Exemple #9
0
        //建立交易
        private void Connect_TD()
        {
            lock (_lockTd)
            {
                if (_bWantTdConnect &&
                    (null == m_pTdApi || IntPtr.Zero == m_pTdApi))
                {
                    m_pTdApi = TraderApi.TD_CreateTdApi();
                    TraderApi.CTP_RegOnErrRtnOrderAction(m_pMsgQueue, _fnOnErrRtnOrderAction_Holder);
                    TraderApi.CTP_RegOnErrRtnOrderInsert(m_pMsgQueue, _fnOnErrRtnOrderInsert_Holder);
                    TraderApi.CTP_RegOnRspOrderAction(m_pMsgQueue, _fnOnRspOrderAction_Holder);
                    TraderApi.CTP_RegOnRspOrderInsert(m_pMsgQueue, _fnOnRspOrderInsert_Holder);
                    TraderApi.CTP_RegOnRspQryDepthMarketData(m_pMsgQueue, _fnOnRspQryDepthMarketData_Holder);
                    TraderApi.CTP_RegOnRspQryInstrument(m_pMsgQueue, _fnOnRspQryInstrument_Holder);
                    TraderApi.CTP_RegOnRspQryInstrumentCommissionRate(m_pMsgQueue, _fnOnRspQryInstrumentCommissionRate_Holder);
                    TraderApi.CTP_RegOnRspQryInstrumentMarginRate(m_pMsgQueue, _fnOnRspQryInstrumentMarginRate_Holder);
                    TraderApi.CTP_RegOnRspQryInvestorPosition(m_pMsgQueue, _fnOnRspQryInvestorPosition_Holder);
                    TraderApi.CTP_RegOnRspQryTradingAccount(m_pMsgQueue, _fnOnRspQryTradingAccount_Holder);
                    TraderApi.CTP_RegOnRtnInstrumentStatus(m_pMsgQueue, _fnOnRtnInstrumentStatus_Holder);
                    TraderApi.CTP_RegOnRtnOrder(m_pMsgQueue, _fnOnRtnOrder_Holder);
                    TraderApi.CTP_RegOnRtnTrade(m_pMsgQueue, _fnOnRtnTrade_Holder);
                    TraderApi.TD_RegMsgQueue2TdApi(m_pTdApi, m_pMsgQueue);
                    TraderApi.TD_Connect(m_pTdApi, _newTempPath, string.Join(";", server.Trading.ToArray()),
                                         server.BrokerID, account.InvestorId, account.Password,
                                         ResumeType,
                                         server.UserProductInfo, server.AuthCode);

                    //向单例对象中注入操作用句柄
                    CTPAPI.GetInstance().__RegTdApi(m_pTdApi);
                }
            }
        }
Exemple #10
0
        public int SendQuote(
            int QuoteRef,
            string szInstrument,
            double AskPrice,
            double BidPrice,
            int AskVolume,
            int BidVolume,
            TThostFtdcOffsetFlagType AskOffsetFlag,
            TThostFtdcOffsetFlagType BidOffsetFlag,
            TThostFtdcHedgeFlagType AskHedgeFlag,
            TThostFtdcHedgeFlagType BidHedgeFlag)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return(0);
            }

            return(TraderApi.TD_SendQuote(
                       IntPtrKey,
                       QuoteRef,
                       szInstrument,
                       AskPrice,
                       BidPrice,
                       AskVolume,
                       BidVolume,
                       AskOffsetFlag,
                       BidOffsetFlag,
                       AskHedgeFlag,
                       BidHedgeFlag));
        }
Exemple #11
0
        public int SendOrder(
            int OrderRef,
            string szInstrument,
            TThostFtdcDirectionType Direction,
            string szCombOffsetFlag,
            string szCombHedgeFlag,
            int VolumeTotalOriginal,
            double LimitPrice,
            TThostFtdcOrderPriceTypeType OrderPriceType,
            TThostFtdcTimeConditionType TimeCondition,
            TThostFtdcContingentConditionType ContingentCondition,
            double StopPrice,
            TThostFtdcVolumeConditionType VolumeCondition)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return(0);
            }

            return(TraderApi.TD_SendOrder(
                       IntPtrKey,
                       OrderRef,
                       szInstrument,
                       Direction,
                       szCombOffsetFlag,
                       szCombHedgeFlag,
                       VolumeTotalOriginal,
                       LimitPrice,
                       OrderPriceType,
                       TimeCondition,
                       ContingentCondition,
                       StopPrice,
                       VolumeCondition));
        }
Exemple #12
0
 //建立交易
 private void Connect_TD()
 {
     lock (_lockTd)
     {
         if (_bWantTdConnect &&
             (null == m_pTdApi || IntPtr.Zero == m_pTdApi))
         {
             m_pTdApi = TraderApi.TD_CreateTdApi();
             TraderApi.CTP_RegOnErrRtnOrderAction(m_pMsgQueue, _fnOnErrRtnOrderAction_Holder);
             TraderApi.CTP_RegOnErrRtnOrderInsert(m_pMsgQueue, _fnOnErrRtnOrderInsert_Holder);
             TraderApi.CTP_RegOnRspOrderAction(m_pMsgQueue, _fnOnRspOrderAction_Holder);
             TraderApi.CTP_RegOnRspOrderInsert(m_pMsgQueue, _fnOnRspOrderInsert_Holder);
             TraderApi.CTP_RegOnRspQryInstrument(m_pMsgQueue, _fnOnRspQryInstrument_Holder);
             TraderApi.CTP_RegOnRspQryInvestorPosition(m_pMsgQueue, _fnOnRspQryInvestorPosition_Holder);
             TraderApi.CTP_RegOnRspQryTradingAccount(m_pMsgQueue, _fnOnRspQryTradingAccount_Holder);
             TraderApi.CTP_RegOnRtnOrder(m_pMsgQueue, _fnOnRtnOrder_Holder);
             TraderApi.CTP_RegOnRtnTrade(m_pMsgQueue, _fnOnRtnTrade_Holder);
             TraderApi.TD_RegMsgQueue2TdApi(m_pTdApi, m_pMsgQueue);
             TraderApi.TD_Connect(m_pTdApi, _newTempPath, string.Join(";", server.Trading.ToArray()),
                                  server.BrokerID, account.InvestorId, account.Password,
                                  ResumeType,
                                  server.UserProductInfo, server.AuthCode);
         }
     }
 }
 void timerAccount_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     if (_bTdConnected)
     {
         TraderApi.TD_ReqQryTradingAccount(m_pTdApi);
     }
 }
Exemple #14
0
 void timerAccount_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     if (_bTdConnected)
     {
         TraderApi.TD_ReqQryTradingAccount(m_pTdApi);
         Console.WriteLine(string.Format("Thread:{0},定时查询资金", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
     }
 }
Exemple #15
0
 void timerPonstion_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     if (_bTdConnected)
     {
         TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, "");
         Console.WriteLine(string.Format("Thread:{0},定时查询持仓", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
     }
 }
Exemple #16
0
 public void ReqQrySettlementInfo(string szTradingDay)
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return;
     }
     TraderApi.TD_ReqQrySettlementInfo(IntPtrKey, szTradingDay);
 }
Exemple #17
0
        //#region 合列列表
        //private Dictionary<string, CThostFtdcInstrumentField> _dictInstruments = null;
        //public void __RegInstrumentDictionary(Dictionary<string, CThostFtdcInstrumentField> dict)
        //{
        //    _dictInstruments = dict;
        //}

        //public delegate void RspQryInstrument(CThostFtdcInstrumentField pInstrument);
        //public event RspQryInstrument OnRspQryInstrument;
        //public void FireOnRspQryInstrument(CThostFtdcInstrumentField pInstrument)
        //{
        //    if (null != OnRspQryInstrument)
        //    {
        //        OnRspQryInstrument(pInstrument);
        //    }
        //}

        //public void ReqQryInstrument(string instrument)
        //{
        //    if (null != _dictInstruments)
        //    {
        //        CThostFtdcInstrumentField value;
        //        if (_dictInstruments.TryGetValue(instrument, out value))
        //        {
        //            FireOnRspQryInstrument(value);
        //            return;
        //        }
        //    }

        //    if (!string.IsNullOrEmpty(instrument)
        //            && null != m_pTdApi
        //            && IntPtr.Zero != m_pTdApi)
        //    {
        //        TraderApi.TD_ReqQryInstrument(m_pTdApi, instrument);
        //    }
        //}
        //#endregion

        //#region 保证金率
        //private Dictionary<string, CThostFtdcInstrumentMarginRateField> _dictMarginRate = null;
        //public void __RegInstrumentMarginRateDictionary(Dictionary<string, CThostFtdcInstrumentMarginRateField> dict)
        //{
        //    _dictMarginRate = dict;
        //}
        //public void ReqQryInstrumentMarginRate(string instrument, TThostFtdcHedgeFlagType HedgeFlag)
        //{
        //    if (null != _dictMarginRate)
        //    {
        //        CThostFtdcInstrumentMarginRateField value;
        //        if (_dictMarginRate.TryGetValue(instrument, out value))
        //        {
        //            FireOnRspQryInstrumentMarginRate(value);
        //            return;
        //        }
        //    }

        //    if (!string.IsNullOrEmpty(instrument)
        //        && null != m_pTdApi
        //        && IntPtr.Zero != m_pTdApi)
        //    {
        //        TraderApi.TD_ReqQryInstrumentMarginRate(m_pTdApi, instrument, HedgeFlag);
        //    }
        //}

        //public delegate void RspQryInstrumentMarginRate(CThostFtdcInstrumentMarginRateField pInstrumentMarginRate);
        //public event RspQryInstrumentMarginRate OnRspQryInstrumentMarginRate;
        //public void FireOnRspQryInstrumentMarginRate(CThostFtdcInstrumentMarginRateField pInstrumentMarginRate)
        //{
        //    if (null != OnRspQryInstrumentMarginRate)
        //    {
        //        OnRspQryInstrumentMarginRate(pInstrumentMarginRate);
        //    }
        //}
        //#endregion

        //#region 手续费率
        //private Dictionary<string, CThostFtdcInstrumentCommissionRateField> _dictCommissionRate = null;
        //public void __RegInstrumentCommissionRateDictionary(Dictionary<string, CThostFtdcInstrumentCommissionRateField> dict)
        //{
        //    _dictCommissionRate = dict;
        //}

        //public void ReqQryInstrumentCommissionRate(string instrument)
        //{
        //    if (null != _dictCommissionRate)
        //    {
        //        CThostFtdcInstrumentCommissionRateField value;
        //        if (_dictCommissionRate.TryGetValue(instrument, out value))
        //        {
        //            FireOnRspQryInstrumentCommissionRate(value);
        //            return;
        //        }
        //    }

        //    if (!string.IsNullOrEmpty(instrument)
        //        && null != m_pTdApi
        //        && IntPtr.Zero != m_pTdApi)
        //    {
        //        TraderApi.TD_ReqQryInstrumentCommissionRate(m_pTdApi, instrument);
        //    }
        //}

        //public delegate void RspQryInstrumentCommissionRate(CThostFtdcInstrumentCommissionRateField pInstrumentCommissionRate);
        //public event RspQryInstrumentCommissionRate OnRspQryInstrumentCommissionRate;
        //public void FireOnRspQryInstrumentCommissionRate(CThostFtdcInstrumentCommissionRateField pInstrumentCommissionRate)
        //{
        //    if (null != OnRspQryInstrumentCommissionRate)
        //    {
        //        OnRspQryInstrumentCommissionRate(pInstrumentCommissionRate);
        //    }
        //}
        //#endregion

        #region 深度行情1
        //private Dictionary<string, DFITCDepthMarketDataField> _dictDepthMarketData = null;
        //public void __RegDepthMarketDataDictionary(Dictionary<string, DFITCDepthMarketDataField> dict)
        //{
        //    _dictDepthMarketData = dict;
        //}

        //public void ReqQryDepthMarketData(string instrument)
        //{
        //    if (null != _dictDepthMarketData)
        //    {
        //        DFITCDepthMarketDataField value;
        //        if (_dictDepthMarketData.TryGetValue(instrument, out value))
        //        {
        //            FireOnRspQryDepthMarketData(value);
        //            return;
        //        }
        //    }

        //    //if (!string.IsNullOrEmpty(instrument)
        //    //    && null != m_pTdApi
        //    //    && IntPtr.Zero != m_pTdApi)
        //    //{
        //    //    TraderApi.TD_ReqQryDepthMarketData(m_pTdApi, instrument);
        //    //}
        //}

        //public delegate void RspQryDepthMarketData(DFITCDepthMarketDataField pDepthMarketData);
        //public event RspQryDepthMarketData OnRspQryDepthMarketData;
        //public void FireOnRspQryDepthMarketData(DFITCDepthMarketDataField pDepthMarketData)
        //{
        //    if (null != OnRspQryDepthMarketData)
        //    {
        //        OnRspQryDepthMarketData(pDepthMarketData);
        //    }
        //}
        #endregion

        //#region 深度行情2
        //public delegate void RtnDepthMarketData(CThostFtdcDepthMarketDataField pDepthMarketData);
        //public event RtnDepthMarketData OnRtnDepthMarketData;
        //public void FireOnRtnDepthMarketData(CThostFtdcDepthMarketDataField pDepthMarketData)
        //{
        //    if (null != OnRtnDepthMarketData)
        //    {
        //        OnRtnDepthMarketData(pDepthMarketData);
        //    }
        //}
        //#endregion

        //#region 交易所状态
        //public delegate void RtnInstrumentStatus(CThostFtdcInstrumentStatusField pInstrumentStatus);
        //public event RtnInstrumentStatus OnRtnInstrumentStatus;
        //public void FireOnRtnInstrumentStatus(CThostFtdcInstrumentStatusField pInstrumentStatus)
        //{
        //    if (null != OnRtnInstrumentStatus)
        //    {
        //        OnRtnInstrumentStatus(pInstrumentStatus);
        //    }
        //}
        //#endregion

        #region 做市商
        public void ReqQuoteSubscribe()
        {
            if (null != m_pTdApi &&
                IntPtr.Zero != m_pTdApi)
            {
                TraderApi.TD_ReqQuoteSubscribe(m_pTdApi);
            }
        }
Exemple #18
0
 public void ReqQryInstrumentCommissionRate(string szInstrument)
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return;
     }
     TraderApi.TD_ReqQryInstrumentCommissionRate(IntPtrKey, szInstrument);
 }
Exemple #19
0
 public void ReqQryInstrumentMarginRate(string szInstrument, TThostFtdcHedgeFlagType HedgeFlag)
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return;
     }
     TraderApi.TD_ReqQryInstrumentMarginRate(IntPtrKey, szInstrument, HedgeFlag);
 }
Exemple #20
0
 public void ReqQryTradingAccount()
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return;
     }
     TraderApi.TD_ReqQryTradingAccount(IntPtrKey);
 }
Exemple #21
0
 public void ReqQryInvestorPositionDetail(string szInstrument)
 {
     if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
     {
         return;
     }
     TraderApi.TD_ReqQryInvestorPositionDetail(IntPtrKey, szInstrument);
 }
Exemple #22
0
 public void ReqQryInvestorPosition(string instrument)
 {
     if (null != m_pTdApi &&
         IntPtr.Zero != m_pTdApi)
     {
         TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, instrument);
     }
 }
Exemple #23
0
        public void ReqQryTradingAccount()
        {
            if (m_pTdApi == null || m_pTdApi == IntPtr.Zero)
            {
                return;
            }

            TraderApi.TD_ReqQryTradingAccount(m_pTdApi);
        }
Exemple #24
0
        public void CancelQuote(ref CThostFtdcQuoteField pQuote)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return;
            }

            TraderApi.TD_CancelQuote(IntPtrKey, ref pQuote);
        }
Exemple #25
0
        public void CancelOrder(ref CThostFtdcOrderField pOrder)
        {
            if (null == IntPtrKey || IntPtr.Zero == IntPtrKey)
            {
                return;
            }

            TraderApi.TD_CancelOrder(IntPtrKey, ref pOrder);
        }
Exemple #26
0
        private void OnRtnTrade(IntPtr pTraderApi, ref CThostFtdcTradeField pTrade)
        {
            if (OutputLog)
            {
                Console.WriteLine("时{0},合约{1},方向{2},开平{3},价{4},量{5},引用{6}",
                                  pTrade.TradeTime, pTrade.InstrumentID, pTrade.Direction, pTrade.OffsetFlag, pTrade.Price, pTrade.Volume, pTrade.OrderRef);
            }

            SingleOrder order;

            //找到自己发送的订单,标记成交
            if (_OrderRef2Order.TryGetValue(string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, pTrade.OrderRef), out order))
            {
                if (TThostFtdcTradeTypeType.CombinationDerived == pTrade.TradeType)
                {
                    //组合,得特别处理
                    DbInMemTrade _trade;//用此对象维护组合对
                    if (!_Orders4Combination.TryGetValue(order, out _trade))
                    {
                        _trade = new DbInMemTrade();
                        _Orders4Combination[order] = _trade;
                    }

                    double Price  = 0;
                    int    Volume = 0;
                    //找到成对交易的,得出价差
                    if (_trade.OnTrade(ref order, ref pTrade, ref Price, ref Volume))
                    {
                        EmitFilled(order, Price, Volume);

                        //完成使命了,删除
                        if (_trade.isEmpty())
                        {
                            _Orders4Combination.Remove(order);
                        }
                    }
                }
                else
                {
                    //普通订单,直接通知即可
                    EmitFilled(order, pTrade.Price, pTrade.Volume);
                }
            }

            if (_dbInMemInvestorPosition.UpdateByTrade(pTrade))
            {
            }
            else
            {
                //本地计算更新失败,重查一次
                TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, pTrade.InstrumentID);
            }
        }
Exemple #27
0
        private void OnConnect(IntPtr pApi, ref CThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
        {
            if (m_pMdApi == pApi)//行情
            {
                _bMdConnected = false;
                if (ConnectionStatus.E_logined == result)
                {
                    _bMdConnected = true;
                }
                //这也有个时间,但取出的时间无效
                Console.WriteLine(string.Format("MdApi:{0},{1},{2}", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), result, pRspUserLogin.LoginTime));
            }
            else if (m_pTdApi == pApi)//交易
            {
                _bTdConnected = false;

                if (ConnectionStatus.E_logined == result)
                {
                    _RspUserLogin = pRspUserLogin;

                    //用于行情记算时简化时间解码
                    _yyyyMMdd = int.Parse(pRspUserLogin.TradingDay);
                    _yyyy     = _yyyyMMdd / 10000;
                    _MM       = (_yyyyMMdd % 10000) / 100;
                    _dd       = _yyyyMMdd % 100;
                }
                else if (ConnectionStatus.E_confirmed == result)
                {
                    _bTdConnected = true;
                    //请求查询资金
                    TraderApi.TD_ReqQryTradingAccount(m_pTdApi);

                    //请求查询全部持仓
                    TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, null);

                    //请求查询合约
                    _dictInstruments.Clear();
                    TraderApi.TD_ReqQryInstrument(m_pTdApi, null);
                }
                Console.WriteLine(string.Format("TdApi:{0},{1},{2}", Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), result, pRspUserLogin.LoginTime));
            }

            if (
                (_bMdConnected && _bTdConnected) ||//都连上
                (!_bWantMdConnect && _bTdConnected) ||//只用分析交易连上
                (!_bWantTdConnect && _bMdConnected)   //只用分析行情连上
                )
            {
                ChangeStatus(ProviderStatus.LoggedIn);
                EmitConnectedEvent();
            }
        }
Exemple #28
0
 static void Main(string[] args)
 {
     _tdapi = TraderApi.CreateTraderApi("tmp/");
     _tdapi.OnFrontConnected  += OnFrontConnected;
     _tdapi.OnRspAuthenticate += OnRspAuthenticate;
     _tdapi.OnRspUserLogin    += OnRspUserLogin;
     _tdapi.SubscribePublicTopic(RESUME_TYPE.RESTART);
     _tdapi.SubscribePublicTopic(RESUME_TYPE.RESTART);
     _tdapi.RegisterFront("tcp://180.168.146.187:10130");
     _tdapi.Init();
     Console.ReadLine();
     _tdapi.Release();
 }
Exemple #29
0
 private void Disconnect_TD()
 {
     lock (_lockTd)
     {
         if (null != m_pTdApi && IntPtr.Zero != m_pTdApi)
         {
             TraderApi.TD_RegMsgQueue2TdApi(m_pTdApi, IntPtr.Zero);
             TraderApi.TD_ReleaseTdApi(m_pTdApi);
             m_pTdApi = IntPtr.Zero;
         }
         _bTdConnected = false;
     }
 }
Exemple #30
0
        public override void Disconnect()
        {
            lock (this)
            {
                if (null != IntPtrKey && IntPtr.Zero != IntPtrKey)
                {
                    TraderApi.TD_RegMsgQueue2TdApi(IntPtrKey, IntPtr.Zero);
                    TraderApi.TD_ReleaseTdApi(IntPtrKey);
                    IntPtrKey = IntPtr.Zero;
                }

                base.Disconnect();
            }
        }