Exemple #1
0
 /// <summary>
 /// 定时查询--保证金查询。
 /// </summary>
 /// <param name="queryInfo"></param>
 private void OnQueryMarginExpired(CtpQueryMarginInfo queryInfo)
 {
     try
     {
         InstrumentMarginRateField field = QueryMarginFromCtp(queryInfo.InstrumentCode);
         m_dataBuffer.UpdateInstrumentMagin(field);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(string.Format("OnQueryExpired() query[{0}] margin failed,{1}", queryInfo.InstrumentCode, ex.Message));
         m_logger.WriteError(string.Format("{0}.OnQueryExpired() query[{1}] margin failed,Error:{2}.",
                                           ToString(), queryInfo.InstrumentCode, ex.Message));
     }
 }
        /// <summary>
        /// CTP InstrumentMarginRateField To USeMargin。
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private USeMargin CtpMarginRateFieldToUSeProductMarginInfo(InstrumentMarginRateField filed)
        {
            USeMargin margin = new USeMargin();

            margin.Instrument = m_dataBuffer.GetInstrumnetByCode(filed.InstrumentID);
            try
            {
                margin.BrokerLongMarginRatioByMoney   = filed.LongMarginRatioByMoney == double.MaxValue ? 0m : Convert.ToDecimal(filed.LongMarginRatioByMoney);
                margin.BrokerLongMarginRatioByVolume  = filed.LongMarginRatioByVolume == double.MaxValue ? 0m : Convert.ToDecimal(filed.LongMarginRatioByVolume);
                margin.BrokerShortMarginRatioByMoney  = filed.ShortMarginRatioByMoney == double.MaxValue ? 0m : Convert.ToDecimal(filed.ShortMarginRatioByMoney);
                margin.BrokerShortMarginRatioByVolume = filed.ShortMarginRatioByVolume == double.MaxValue ? 0m : Convert.ToDecimal(filed.ShortMarginRatioByVolume);
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "InstrumentMarginRateFieldToFulcProductMarginInfo() convet failed," + ex.Message);
            }

            return(margin);
        }
        /// <summary>
        /// 查询合约保证金。
        /// </summary>
        /// <param name="instrument"></param>
        /// <returns></returns>
        public override USeMargin QueryInstrumentMargin(USeInstrument instrument)
        {
            if (m_dataBuffer.ExistInstrument(instrument.InstrumentCode) == false)
            {
                throw new Exception(string.Format("Unsupport instrument[{0}]", instrument));
            }

            USeMargin margin = m_dataBuffer.GetMargin(instrument);

            if (margin != null)
            {
                return(margin);
            }
            else
            {
                int index = 3;
                while (index > 0)
                {
                    index--;
                    try
                    {
                        InstrumentMarginRateField field = QueryMarginFromCtp(instrument.InstrumentCode);
                        m_dataBuffer.UpdateInstrumentMagin(field);

                        margin = m_dataBuffer.GetMargin(instrument);
                        Debug.Assert(margin != null, "QueryInstrumentMargin(),margin is null");
                        return(margin);
                    }
                    catch
                    {
                    }

                    Thread.Sleep(1200);
                }
                Debug.Assert(false, "QueryInstrumentMargin(),margin is null");

                // 如果还查不到则抛出异常
                throw new Exception(string.Format("Query instrument[{0}] margin failed", instrument));
            }
        }
        /// <summary>
        /// 从Ctp查询保证金。
        /// </summary>
        /// <param name="instrumentCode"></param>
        /// <returns></returns>
        private InstrumentMarginRateField QueryMarginFromCtp(string instrumentCode)
        {
            List <InstrumentMarginRateField> marginFields = new List <InstrumentMarginRateField>();
            int requestID = m_requetSeqIDCreator.Next();

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

                QryInstrumentMarginRateField requestField = new QryInstrumentMarginRateField();
                requestField.BrokerID     = m_brokerID;
                requestField.InvestorID   = m_investorID;
                requestField.InstrumentID = instrumentCode;
                requestField.HedgeFlag    = HedgeFlagType.Speculation;

                m_ctpUser.ReqQryInstrumentMarginRate(ref requestField, 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 oute"));
                    }
                }
            }
            catch (Exception ex)
            {
                m_logger.WriteError(string.Format("{0}.QueryMarginFromCtp() failed,Error:{1}.",
                                                  ToString(), ex.Message));
                throw new Exception(string.Format("Query [{0}]margin failed,Error:{1}.", instrumentCode, ex.Message));
            }
            finally
            {
                m_eventDic.Remove(requestID);
            }

            if (marginFields.Count > 0)
            {
                Debug.Assert(marginFields.Count == 1);
                return(marginFields[0]);
            }
            else
            {
                //查询有应答但无值,构造一个空的保证金
                InstrumentMarginRateField marginField = new InstrumentMarginRateField();
                marginField.BrokerID                 = m_brokerID;
                marginField.HedgeFlag                = HedgeFlagType.Speculation;
                marginField.InstrumentID             = instrumentCode;
                marginField.InvestorID               = m_investorID;
                marginField.InvestorRange            = InvestorRangeType.All;
                marginField.IsRelative               = IntBoolType.Yes;
                marginField.LongMarginRatioByMoney   = 0d;
                marginField.LongMarginRatioByVolume  = 0d;
                marginField.ShortMarginRatioByMoney  = 0d;
                marginField.ShortMarginRatioByVolume = 0d;

                return(marginField);
            }
        }