Beispiel #1
0
        public Instrument GetInstument(InstrumentField field)
        {
            var inst = new Instrument((InstrumentType)field.Type, field.Symbol);

            inst.AltId.Add(new AltId(_provider.Id, field.InstrumentID, field.ExchangeID));
            inst.PutCall     = (PutCall)field.OptionsType;
            inst.Strike      = field.StrikePrice;
            inst.Exchange    = field.ExchangeID;
            inst.CurrencyId  = CurrencyId.CNY;
            inst.TickSize    = field.PriceTick;
            inst.Factor      = field.VolumeMultiple;
            inst.PriceFormat = "F" + QBHelper.GetPrecision(field.PriceTick);
            inst.Maturity    = field.ExpireDate();
            if (!string.IsNullOrEmpty(field.UnderlyingInstrID) &&
                field.UnderlyingInstrID != field.ProductID &&
                !field.InstrumentID.EndsWith("efp"))
            {
                var underlying = _provider.InstrumentManager.Get(field.UnderlyingInstrID);
                if (underlying == null)
                {
                    //_provider.Logger.Warn($"没有找到合约标的物{field.UnderlyingInstrID},请先导入合约标的物");
                }
                else
                {
                    OpenQuant.Helper.AddLeg(inst, new Leg(underlying));
                }
            }
            return(inst);
        }
Beispiel #2
0
 internal void OnMessage(InstrumentField field, bool completed)
 {
     if (QryInstrumentCompleted)
     {
         return;
     }
     if (field != null)
     {
         var inst = _convertor.GetInstument(field);
         switch (inst.Type)
         {
         case InstrumentType.Stock:
         case InstrumentType.Future:
         case InstrumentType.Option:
         case InstrumentType.FutureOption:
         case InstrumentType.MultiLeg:
             _instruments.Add(inst);
             break;
         }
     }
     if (completed)
     {
         _instruments.Sort((x, y) => string.Compare(x.Symbol, y.Symbol, StringComparison.Ordinal));
         QryInstrumentCompleted = true;
         Logger.Info($"合约查询完毕,收到{_instruments.Count}个合约");
         StartTimerTask();
     }
 }
Beispiel #3
0
 private void OnRspQryInstrument_callback(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
 {
     if (null != OnRspQryInstrument)
     {
         OnRspQryInstrument(this, new OnRspQryInstrumentEventArgs(ref instrument, size1, bIsLast));
     }
 }
Beispiel #4
0
        /// <summary>
        /// CTP合约数据转换。
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private USeInstrumentDetail InsturmentFiledToUSeInstrumentDetail(InstrumentField field)
        {
            USeMarket market = CtpProtocol.CtpExchangeToUSeMarket(field.ExchangeID);

            USeInstrumentDetail entity = new USeInstrumentDetail();

            entity.Instrument = new USeInstrument(field.InstrumentID,
                                                  field.InstrumentName,
                                                  market);
            entity.OpenDate   = DateTime.ParseExact(field.OpenDate, "yyyyMMdd", null);
            entity.ExpireDate = DateTime.ParseExact(field.ExpireDate, "yyyyMMdd", null);
            if (string.IsNullOrEmpty(field.StartDelivDate) == false)
            {
                entity.StartDelivDate = DateTime.ParseExact(field.StartDelivDate, "yyyyMMdd", null);
            }
            else
            {
                entity.StartDelivDate = entity.ExpireDate.AddDays(1);
            }
            entity.EndDelivDate             = DateTime.ParseExact(field.EndDelivDate, "yyyyMMdd", null);
            entity.VolumeMultiple           = Convert.ToInt32(field.VolumeMultiple);
            entity.IsTrading                = field.IsTrading == IntBoolType.Yes;
            entity.Varieties                = field.ProductID;
            entity.PriceTick                = Convert.ToDecimal(field.PriceTick);
            entity.ProductClass             = CtpProtocol.CtpProductClassToUSeProductClass(field.ProductClass);
            entity.MaxMarketOrderVolume     = Convert.ToInt32(field.MaxMarketOrderVolume);
            entity.MinMarketOrderVolume     = Convert.ToInt32(field.MinMarketOrderVolume);
            entity.MaxLimitOrderVolume      = Convert.ToInt32(field.MaxLimitOrderVolume);
            entity.MinLimitOrderVolume      = Convert.ToInt32(field.MinLimitOrderVolume);
            entity.ExchangeLongMarginRatio  = Convert.ToDecimal(field.LongMarginRatio);
            entity.ExchangeShortMarginRatio = Convert.ToDecimal(field.ShortMarginRatio);

            return(entity);
        }
        public InstrumentField getInstrumentField(string instrumentId)
        {
            InstrumentField instrumentField = new InstrumentField();

            instrumentField.InstrumentID = instrumentId;
            instrumentField.PriceTick    = 1;
            instrumentField.ProductClass = ProductClassType.Futures;

            return(instrumentField);
        }
Beispiel #6
0
        private void OnRspQryInstrument_callback(object sender, [In] ref InstrumentField instrument, int size1, bool bIsLast)
        {
            if (null == OnRspQryInstrument)
            {
                return;
            }

            InstrumentField field = instrument;

            InstrumentClass cls = new InstrumentClass();

            cls.InstrumentName = field.InstrumentName();
            cls.Symbol         = field.Symbol;
            cls.InstrumentID   = field.InstrumentID;
            cls.ExchangeID     = field.ExchangeID;
            cls.ClientID       = field.ClientID;
            cls.AccountID      = field.AccountID;

            cls.ExchangeInstID = field.ExchangeInstID;

            cls.Type        = (int)field.Type;
            cls.Type_String = Enum <XAPI.InstrumentType> .ToString(field.Type);

            cls.VolumeMultiple = field.VolumeMultiple;
            cls.PriceTick      = field.PriceTick;
            cls.ExpireDate     = field.ExpireDate;
            cls.StrikePrice    = field.StrikePrice;

            cls.OptionsType        = (int)field.OptionsType;
            cls.OptionsType_String = Enum <XAPI.PutCall> .ToString(field.OptionsType);

            cls.ProductID         = field.ProductID;
            cls.UnderlyingInstrID = field.UnderlyingInstrID;

            cls.InstLifePhase        = (int)field.InstLifePhase;
            cls.InstLifePhase_String = Enum <XAPI.InstLifePhaseType> .ToString(field.InstLifePhase);

            if (null == OnRspQryInstrument)
            {
                QueueData qd = new QueueData();
                qd.Type        = (int)ResponseType.OnRspQryInstrument;
                qd.Type_String = Enum <XAPI.ResponseType> .ToString(ResponseType.OnRspQryInstrument);

                qd.Sender = this;
                qd.Data1  = cls;
                qd.Data2  = size1;
                qd.Data3  = bIsLast;

                MessageQueue.Enqueue(qd);
            }
            else
            {
                OnRspQryInstrument(this, cls, size1, bIsLast);
            }
        }
Beispiel #7
0
        private void _onRspQryInstrument(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
        {
            //只订阅期货,并且不订阅套利等其他合约
            Regex re = new Regex(@"^[a-zA-Z]+\d+$", RegexOptions.None);

            if (instrument.Type == InstrumentType.Future &&
                re.IsMatch(instrument.InstrumentID))
            {
                mMdApi.Subscribe(instrument.InstrumentID, instrument.ExchangeID);
            }
        }
Beispiel #8
0
        private void _OnRspQryInstrument(IntPtr ptr1, int size1, double double1)
        {
            if (OnRspQryInstrument_ == null)
            {
                return;
            }

            InstrumentField obj = PInvokeUtility.GetObjectFromIntPtr <InstrumentField>(ptr1);

            OnRspQryInstrument_(this, ref obj, size1, double1 != 0);
        }
Beispiel #9
0
 internal static IDataField CreateDataField(IField field, ModuleSchema schema)
 {
     return(field switch
     {
         StringField f => new StringDataField(f),
         BooleanField f => new BooleanDataField(f),
         NumericField f => new NumericDataField(f),
         EnumField f => new EnumDataField(f),
         InstrumentField f => new InstrumentDataField(f, schema),
         OverlayField f => new OverlayDataField(f, schema),
         TempoField f => new TempoDataField(f),
         _ => throw new ArgumentException($"Can't handle {field} yet")
     });
Beispiel #10
0
 private void _onRspQryInstrument(object sender, ref InstrumentField field, int size1, bool bIsLast)
 {
     //只订阅期货,并且不订阅套利等其他合约
     if (field.Type == InstrumentType.Future && RegexUtils.MatchInstrument(field.InstrumentID))
     {
         Instrument inst = new Instrument(field.InstrumentID
                                          , field.ProductID
                                          , ConvertUtil.ConvertExchange(field.ExchangeID)
                                          , field.VolumeMultiple
                                          , field.PriceTick
                                          , 1000);
         mOnInstrument?.Invoke(inst);
     }
 }
Beispiel #11
0
        private void OnRspQryInstrument_callback(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
        {
            if (size1 <= 0)
            {
                (sender as XApi).GetLog().Info("OnRspQryInstrument");
                return;
            }

            _dictInstruments[instrument.Symbol] = instrument;

            if (bIsLast)
            {
                (sender as XApi).GetLog().Info("合约列表已经接收完成,共 {0} 条", _dictInstruments.Count);
            }
        }
Beispiel #12
0
        private void OnRspQryInstrument(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
        {
            if (size1 > 0)
            {
                InstrumentInfoList.Add(new InstrumentInfo()
                {
                    Symbol     = instrument.Symbol,
                    Instrument = instrument.InstrumentID,
                    Exchange   = instrument.ExchangeID,
                    TickSize   = instrument.PriceTick,
                    Factor     = instrument.VolumeMultiple
                });
            }

            this.bIsLast = bIsLast;
        }
 private IEnumerable <KeyValueViewModel> GenerateFields()
 {
     foreach (var field in Model.Container.Fields)
     {
         // TODO: A FieldViewModel?
         string value = field switch
         {
             InstrumentField _ => "Instrument",
             OverlayField f => $"Overlay; {f.FieldLists.Count} lists; {f.NestedFieldCount} nested fields",
             BooleanField _ => "Boolean",
             EnumField f => $"Enum ({f.Values.Count} values)",
             NumericField f => $"Numeric: {f.Min}-{f.Max}",
             StringField f => $"String ({f.Length})",
                  _ => throw new InvalidOperationException($"Unexpected field type: {field?.GetType()}")
         };
         yield return(new KeyValueViewModel(field.Description, value));
     }
 }
        /// <summary>
        /// CTP InstrumentField To USeInstrumentDetail。
        /// </summary>
        /// <param name="filed"></param>
        /// <returns></returns>
        private USeInstrumentDetail CtpInstrumentFieldToUSeInstrumentDetail(InstrumentField filed)
        {
            USeMarket market = CtpProtocol.FtdcExchangeToUSeMarket(filed.ExchangeID);

            Debug.Assert(market != USeMarket.Unknown, "CtpInstrumentFieldToUSeInstrumentDetail(),market is unknown.");

            USeInstrumentDetail detail = new USeInstrumentDetail();

            detail.Instrument = new USeInstrument(filed.InstrumentID,
                                                  filed.InstrumentName,
                                                  market);
            try
            {
                detail.OpenDate   = DateTime.ParseExact(filed.OpenDate, "yyyyMMdd", null);
                detail.ExpireDate = DateTime.ParseExact(filed.ExpireDate, "yyyyMMdd", null);
                if (string.IsNullOrEmpty(filed.StartDelivDate) == false)
                {
                    detail.StartDelivDate = DateTime.ParseExact(filed.StartDelivDate, "yyyyMMdd", null);
                }
                else
                {
                    //[yangming] 有的合约查询不到开始交割日,暂时用到期日下一天
                    detail.StartDelivDate = detail.ExpireDate.AddDays(1);
                }
                detail.EndDelivDate             = DateTime.ParseExact(filed.EndDelivDate, "yyyyMMdd", null);
                detail.VolumeMultiple           = filed.VolumeMultiple;
                detail.IsTrading                = filed.IsTrading == IntBoolType.Yes;
                detail.Varieties                = filed.ProductID;
                detail.PriceTick                = filed.PriceTick.ToDecimal();
                detail.ExchangeLongMarginRatio  = filed.LongMarginRatio.ToDecimal();
                detail.ExchangeShortMarginRatio = filed.ShortMarginRatio.ToDecimal();

                detail.MaxMarketOrderVolume = filed.MaxMarketOrderVolume;
                detail.MinMarketOrderVolume = filed.MinMarketOrderVolume;
                detail.MaxLimitOrderVolume  = filed.MaxLimitOrderVolume;
                detail.MinLimitOrderVolume  = filed.MinLimitOrderVolume;
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "CtpInstrumentFieldToUSeInstrumentDetail() convet failed," + ex.Message);
            }

            return(detail);
        }
Beispiel #15
0
 private IEnumerable <KeyValueViewModel> GenerateFields()
 {
     foreach (var field in Model.Container.Fields)
     {
         // TODO: A FieldViewModel?
         string value = field switch
         {
             InstrumentField _ => "Instrument",
             OverlayField f => $"Overlay; {f.FieldLists.Count} lists; {f.NestedFieldCount} nested fields",
             BooleanField _ => "Boolean",
             EnumField f => $"Enum ({f.Values.Count} values)",
             NumericField f => $"Numeric: {f.Min}-{f.Max}",
             StringField f => $"String ({f.Length})",
             TempoField f => "Tempo",
                  _ => throw new InvalidOperationException($"Unexpected field type: {field?.GetType()}")
         };
         string?valueToolTip = field switch
         {
             EnumField f => string.Join("\r\n", f.Values.Select(value => $"{f.RawNumberByName[value]}: {value}")),
             _ => null
         };
         yield return(new KeyValueViewModel($"{field.Offset}: {field.Description}", value, keyToolTip: null, valueToolTip));
     }
 }
Beispiel #16
0
 void OnRspQryInstrument(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
 {
     Console.WriteLine(instrument.InstrumentName);
 }
Beispiel #17
0
 protected virtual void OnInstrumentReceived(object sender, InstrumentField instrument, bool isLast)
 {
     Provider.OnMessage(instrument, isLast);
 }
Beispiel #18
0
 private void OnRspQryInstrument_callback(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
 {
     if (null != OnRspQryInstrument)
     {
         OnRspQryInstrument(this, new OnRspQryInstrumentEventArgs(ref instrument, size1, bIsLast));
     }
 }
Beispiel #19
0
 public OnRspQryInstrumentEventArgs(ref InstrumentField instrument, int size1, bool bIsLast)
 {
     this.instrument = instrument;
     this.size1      = size1;
     this.bIsLast    = bIsLast;
 }
Beispiel #20
0
 public OnRspQryInstrumentEventArgs(ref InstrumentField instrument, int size1, bool bIsLast)
 {
     this.instrument = instrument;
     this.size1 = size1;
     this.bIsLast = bIsLast;
 }
Beispiel #21
0
        private void OnRspQryInstrument_callback(object sender, ref InstrumentField instrument, int size1, bool bIsLast)
        {
            if (null == OnRspQryInstrument)
                return;

            InstrumentField field = instrument;

            InstrumentClass cls = new InstrumentClass();

            cls.InstrumentName = field.InstrumentName();
            cls.Symbol = field.Symbol;
            cls.InstrumentID = field.InstrumentID;
            cls.ExchangeID = field.ExchangeID;
            cls.ClientID = field.ClientID;
            cls.AccountID = field.AccountID;

            cls.ExchangeInstID = field.ExchangeInstID;

            cls.Type = (int)field.Type;
            cls.Type_String = Enum<XAPI.InstrumentType>.ToString(field.Type);

            cls.VolumeMultiple = field.VolumeMultiple;
            cls.PriceTick = field.PriceTick;
            cls.ExpireDate = field.ExpireDate;
            cls.StrikePrice = field.StrikePrice;

            cls.OptionsType = (int)field.OptionsType;
            cls.OptionsType_String = Enum<XAPI.PutCall>.ToString(field.OptionsType);

            cls.ProductID = field.ProductID;
            cls.UnderlyingInstrID = field.UnderlyingInstrID;

            cls.InstLifePhase = (int)field.InstLifePhase;
            cls.InstLifePhase_String = Enum<XAPI.InstLifePhaseType>.ToString(field.InstLifePhase);

            if (null == OnRspQryInstrument)
            {
                QueueData qd = new QueueData();
                qd.Type = (int)ResponeType.OnRspQryInstrument;
                qd.Type_String = Enum<XAPI.ResponeType>.ToString(ResponeType.OnRspQryInstrument);
                qd.Sender = this;
                qd.Data1 = cls;
                qd.Data2 = size1;
                qd.Data3 = bIsLast;

                MessageQueue.Enqueue(qd);
            }
            else
            {
                OnRspQryInstrument(this, ref cls, size1, bIsLast);
            }
        }