示例#1
0
        /// <summary>
        /// 定时查询--手续费查询。
        /// </summary>
        /// <param name="queryInfo"></param>
        private void OnQueryFeeExpired(CtpQueryFeeInfo queryInfo)
        {
            try
            {
                InstrumentCommissionRateField field = QueryCommissionRateFieldFromCtp(queryInfo.InstrumentCode);
                m_dataBuffer.UpdateInstrumentFee(field, queryInfo.InstrumentCode);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("OnQueryExpired() query[{0}] fee failed,{1}", queryInfo.InstrumentCode, ex.Message));
                m_logger.WriteError(string.Format("{0}.OnQueryExpired() query[{1}] fee failed,Error:{2}.",
                                                  ToString(), queryInfo.InstrumentCode, ex.Message));
                return;
            }

            try
            {
                USeInstrument       instrument    = m_dataBuffer.GetInstrumnetByCode(queryInfo.InstrumentCode);
                List <USeTradeBook> tradeBookList = m_dataBuffer.GetTradeBook(instrument);
                List <USeOrderBook> orderBookList = m_dataBuffer.GetCheckedOrderBook(instrument);

                if (tradeBookList != null && tradeBookList.Count > 0)
                {
                    foreach (USeTradeBook tradeBook in tradeBookList)
                    {
                        FireTradeBookChanged(tradeBook, false);
                    }
                }
                if (orderBookList != null && orderBookList.Count > 0)
                {
                    foreach (USeOrderBook orderBook in orderBookList)
                    {
                        FireOrderBookChanged(orderBook);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
                m_logger.WriteError(string.Format("{0}.OnQueryFeeExpired() failed,Error:{1}.", ToString(), ex.Message));
            }
        }
        /// <summary>
        /// CTP InstrumentCommissionRateField To USeFee。
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private USeFee CtpInstrumentCommissionRateFieldToUSeFeeInfo(InstrumentCommissionRateField filed)
        {
            USeFee fee = new USeFee();

            try
            {
                fee.OpenRatioByMoney        = filed.OpenRatioByMoney == double.MaxValue ? 0m : Convert.ToDecimal(filed.OpenRatioByMoney);
                fee.CloseRatioByMoney       = filed.CloseRatioByMoney == double.MaxValue ? 0m : Convert.ToDecimal(filed.CloseRatioByMoney);
                fee.CloseTodayRatioByMoney  = filed.CloseTodayRatioByMoney == double.MaxValue ? 0m : Convert.ToDecimal(filed.CloseTodayRatioByMoney);
                fee.OpenRatioByVolume       = filed.OpenRatioByVolume == double.MaxValue ? 0m : Convert.ToDecimal(filed.OpenRatioByVolume);
                fee.CloseRatioByVolume      = filed.CloseRatioByVolume == double.MaxValue ? 0m : Convert.ToDecimal(filed.CloseRatioByVolume);
                fee.CloseTodayRatioByVolume = filed.CloseTodayRatioByVolume == double.MaxValue ? 0m : Convert.ToDecimal(filed.CloseTodayRatioByVolume);
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "CtpInstrumentCommissionRateFieldToUSeFeeInfo() convet failed," + ex.Message);
            }

            return(fee);
        }
        /// <summary>
        /// 从Ctp查询手续费。
        /// </summary>
        /// <param name="instrumentCode">合约代码。</param>
        /// <returns></returns>
        private InstrumentCommissionRateField QueryCommissionRateFieldFromCtp(string instrumentCode)
        {
            List <InstrumentCommissionRateField> commissionFields = new List <InstrumentCommissionRateField>();
            int requestID = m_requetSeqIDCreator.Next();

            try
            {
                USeResetEvent queryEvent = new USeResetEvent(requestID);
                queryEvent.Tag = commissionFields;
                m_eventDic.Add(queryEvent.EventID, queryEvent);

                QryInstrumentCommissionRateField requestFields = new QryInstrumentCommissionRateField();
                requestFields.BrokerID     = m_brokerID;
                requestFields.InvestorID   = m_investorID;
                requestFields.InstrumentID = instrumentCode;

                m_ctpUser.ReqQryInstrumentCommissionRate(ref requestFields, requestID);

                while (true)
                {
                    if (queryEvent.IsError)
                    {
                        Debug.Assert(queryEvent.Tag != null);
                        RspInfoField rspInfo = (RspInfoField)queryEvent.Tag;
                        throw new Exception(string.Format("({0}){1}", rspInfo.ErrorID, rspInfo.ErrorMsg));
                    }

                    if (queryEvent.IsFinish)
                    {
                        break;
                    }

                    if (queryEvent.WaitOne(m_queryTimeOut) == false)
                    {
                        throw new Exception(string.Format("({0}){1}", "99", "time out"));
                    }
                }
            }
            catch (Exception ex)
            {
                m_logger.WriteError(string.Format("{0}.QueryCommissionRateFieldFromCtp() failed,Error:{1}.",
                                                  ToString(), ex.Message));
                throw new Exception(string.Format("Query [{0}] commission rate failed,Error:{1}.", instrumentCode, ex.Message));
            }
            finally
            {
                m_eventDic.Remove(requestID);
            }

            if (commissionFields.Count > 0)
            {
                Debug.Assert(commissionFields.Count == 1);
                return(commissionFields[0]);
            }
            else
            {
                //查询有应答但无值,构造一个空的手续费
                USeInstrumentDetail instrumentDetail = m_dataBuffer.GetInstrumentDetail(instrumentCode);
                Debug.Assert(instrumentDetail != null);
                InstrumentCommissionRateField rateField = new InstrumentCommissionRateField();
                rateField.BrokerID                = m_brokerID;
                rateField.CloseRatioByMoney       = 0d;
                rateField.CloseRatioByVolume      = 0d;
                rateField.CloseTodayRatioByMoney  = 0d;
                rateField.CloseTodayRatioByVolume = 0d;
                rateField.InstrumentID            = instrumentDetail.Varieties;
                rateField.InvestorID              = m_investorID;
                rateField.InvestorRange           = InvestorRangeType.All;
                rateField.OpenRatioByMoney        = 0d;
                rateField.OpenRatioByVolume       = 0d;

                return(rateField);
            }
        }
        /// <summary>
        /// 查询合约手续费。
        /// </summary>
        /// <param name="instrument">合约。</param>
        /// <returns></returns>
        public override USeFee QueryInstrumentFee(USeInstrument instrument)
        {
            if (m_dataBuffer.ExistInstrument(instrument.InstrumentCode) == false)
            {
                throw new Exception(string.Format("Unsupport instrument[{0}]", instrument));
            }

            USeFee fee = m_dataBuffer.GetFee(instrument);

            if (fee != null)
            {
                return(fee);
            }
            else
            {
                m_queryTimer.Change(Timeout.Infinite, Timeout.Infinite); // 先暂停自动查询
                try
                {
                    int index = 3;
                    while (index > 0)
                    {
                        index--;
                        try
                        {
                            InstrumentCommissionRateField field = QueryCommissionRateFieldFromCtp(instrument.InstrumentCode);
                            m_dataBuffer.UpdateInstrumentFee(field, instrument.InstrumentCode);

                            List <USeTradeBook> tradeBookList = m_dataBuffer.GetTradeBook(instrument);
                            List <USeOrderBook> orderBookList = m_dataBuffer.GetCheckedOrderBook(instrument);

                            if (tradeBookList != null && tradeBookList.Count > 0)
                            {
                                foreach (USeTradeBook tradeBook in tradeBookList)
                                {
                                    FireTradeBookChanged(tradeBook, false);
                                }
                            }
                            if (orderBookList != null && orderBookList.Count > 0)
                            {
                                foreach (USeOrderBook orderBook in orderBookList)
                                {
                                    FireOrderBookChanged(orderBook);
                                }
                            }


                            fee = m_dataBuffer.GetFee(instrument);
                            Debug.Assert(fee != null, "QueryInstrumentFee(),fee is null");
                            return(fee);
                        }
                        catch (Exception ex)
                        {
                            m_logger.WriteError(string.Format("{0}.QueryInstrumentFee() failed,Error:{1}.", ToString(), ex.Message));
                        }

                        Thread.Sleep(1000);
                    }

                    Debug.Assert(false, "QueryInstrumentFee(),fee is null");

                    // 如果还查不到则抛出异常
                    throw new Exception(string.Format("Query instrument[{0}] fee failed", instrument));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    m_queryTimer.Change(1000, Timeout.Infinite); // 启动自动查询
                }
            }
        }