Exemplo n.º 1
0
		public AltIDGroup Add(string altSource)
		{
			foreach (FIXSecurityAltIDGroup fIXSecurityAltIDGroup in this.instrument.instrument.SecurityAltIDGroup)
			{
				if (fIXSecurityAltIDGroup.SecurityAltIDSource == altSource)
				{
					return new AltIDGroup(this.instrument, fIXSecurityAltIDGroup);
				}
			}
			FIXSecurityAltIDGroup fIXSecurityAltIDGroup2 = new FIXSecurityAltIDGroup();
			fIXSecurityAltIDGroup2.SecurityAltIDSource = altSource;
			this.instrument.instrument.SecurityAltIDGroup.Add(fIXSecurityAltIDGroup2);
			return new AltIDGroup(this.instrument, fIXSecurityAltIDGroup2);
		}
Exemplo n.º 2
0
        public void SendSecurityDefinitionRequest(FIXSecurityDefinitionRequest request)
        {
            lock (this)
            {
                if (!_bTdConnected)
                {
                    EmitError(-1, -1, "交易没有连接,无法获取合约列表");
                    tdlog.Error("交易没有连接,无法获取合约列表");
                    return;
                }

                string symbol           = request.ContainsField(EFIXField.Symbol) ? request.Symbol : null;
                string securityType     = request.ContainsField(EFIXField.SecurityType) ? request.SecurityType : null;
                string securityExchange = request.ContainsField(EFIXField.SecurityExchange) ? request.SecurityExchange : null;

                #region 过滤
                List <CThostFtdcInstrumentField> list = new List <CThostFtdcInstrumentField>();
                foreach (CThostFtdcInstrumentField inst in _dictInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (securityType == GetSecurityType(inst))
                        {
                            ++flag;
                        }
                    }

                    if (3 == flag)
                    {
                        list.Add(inst);
                    }
                }
                #endregion

                list.Sort(SortCThostFtdcInstrumentField);

                //如果查出的数据为0,应当想法立即返回
                if (0 == list.Count)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID        = request.SecurityReqID,
                        SecurityResponseID   = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym      = 1//有个除0错误的问题
                    };
                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }

                foreach (CThostFtdcInstrumentField inst in list)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym      = list.Count
                    };

                    {
                        string securityType2 = GetSecurityType(inst);
                        definition.AddField(EFIXField.SecurityType, securityType2);
                    }
                    {
                        double x = inst.PriceTick;
                        if (x > 0.00001)
                        {
                            int i = 0;
                            for (; x - (int)x != 0; ++i)
                            {
                                x = x * 10;
                            }
                            definition.AddField(EFIXField.PriceDisplay, string.Format("F{0}", i));
                            definition.AddField(EFIXField.TickSize, inst.PriceTick);
                        }
                    }
#if CTP
                    definition.AddField(EFIXField.Symbol, inst.InstrumentID);
#elif CTPZQ
                    definition.AddField(EFIXField.Symbol, GetYahooSymbol(inst.InstrumentID, inst.ExchangeID));
#endif
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.SecurityDesc, inst.InstrumentName);
                    definition.AddField(EFIXField.Factor, (double)inst.VolumeMultiple);

                    if (inst.ProductClass == TThostFtdcProductClassType.Futures || inst.ProductClass == TThostFtdcProductClassType.Options)
                    {
                        try
                        {
                            definition.AddField(EFIXField.MaturityDate, DateTime.ParseExact(inst.ExpireDate, "yyyyMMdd", CultureInfo.InvariantCulture));
                        }
                        catch (Exception ex)
                        {
                            tdlog.Warn("合约:{0},字段内容:{1},{2}", inst.InstrumentID, inst.ExpireDate, ex.Message);
                        }

                        //if (inst.ProductClass == TThostFtdcProductClassType.Options)
                        //{
                        //    // 支持中金所,大商所,郑商所
                        //    var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                        //    if (match.Success)
                        //    {
                        //        definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                        //        definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                        //    }
                        //}
                    }

                    // 中金所仿真平台下,居然全设的是股票
                    var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                    if (match.Success)
                    {
                        definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                        definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                    }

                    FIXSecurityAltIDGroup group = new FIXSecurityAltIDGroup();
                    group.SecurityAltID       = inst.InstrumentID;
                    group.SecurityAltExchange = inst.ExchangeID;
                    group.SecurityAltIDSource = this.Name;

                    definition.AddGroup(group);

                    //还得补全内容

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
            }
        }
        public void SendSecurityDefinitionRequest(FIXSecurityDefinitionRequest request)
        {
            lock (this)
            {
                if (!_bTdConnected)
                {
                    EmitError(-1, -1, "交易没有连接,无法获取合约列表");
                    tdlog.Error("交易没有连接,无法获取合约列表");
                    return;
                }

                string symbol           = request.ContainsField(EFIXField.Symbol) ? request.Symbol : null;
                string securityType     = request.ContainsField(EFIXField.SecurityType) ? request.SecurityType : null;
                string securityExchange = request.ContainsField(EFIXField.SecurityExchange) ? request.SecurityExchange : null;

                #region 过滤
                List <DFITCExchangeInstrumentRtnField> list = new List <DFITCExchangeInstrumentRtnField>();
                foreach (DFITCExchangeInstrumentRtnField inst in _dictInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (FIXSecurityType.Future == securityType)
                        {
                            if (DFITCInstrumentTypeType.COMM_TYPE == inst.instrumentType)
                            {
                                ++flag;
                            }
                        }
                        //else if (FIXSecurityType.MultiLegInstrument == securityType)//理解上是否有问题
                        //{
                        //    if (TThostFtdcProductClassType.Combination == inst.ProductClass)
                        //    {
                        //        ++flag;
                        //    }
                        //}
                        else if (FIXSecurityType.FutureOption == securityType)
                        {
                            if (DFITCInstrumentTypeType.OPT_TYPE == inst.instrumentType)
                            {
                                ++flag;
                            }
                        }
                    }

                    if (3 == flag)
                    {
                        list.Add(inst);
                    }
                }
                #endregion

                #region 过滤1
                List <DFITCAbiInstrumentRtnField> list1 = new List <DFITCAbiInstrumentRtnField>();
                foreach (DFITCAbiInstrumentRtnField inst in _dictAbiInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (FIXSecurityType.MultiLegInstrument == securityType)
                        {
                            ++flag;
                        }
                    }

                    if (3 == flag)
                    {
                        list1.Add(inst);
                    }
                }
                #endregion

                list.Sort(SortDFITCExchangeInstrumentRtnField);
                list1.Sort(SortDFITCAbiInstrumentRtnField);

                //如果查出的数据为0,应当想法立即返回
                if (0 == list.Count && 0 == list1.Count)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID        = request.SecurityReqID,
                        SecurityResponseID   = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym      = 1//有个除0错误的问题
                    };
                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }

                #region 期货 期权
                foreach (DFITCExchangeInstrumentRtnField inst in list)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym      = list.Count + list1.Count,
                    };

                    {
                        string securityType2;
                        switch (inst.instrumentType)
                        {
                        case DFITCInstrumentTypeType.COMM_TYPE:
                            securityType2 = FIXSecurityType.Future;
                            break;

                        case DFITCInstrumentTypeType.OPT_TYPE:
                            securityType2 = FIXSecurityType.FutureOption;
                            var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                            if (match.Success)
                            {
                                definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                                definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                            }
                            break;

                        default:
                            securityType2 = FIXSecurityType.NoSecurityType;
                            break;
                        }
                        definition.AddField(EFIXField.SecurityType, securityType2);
                    }
                    {
                        double x = inst.minPriceFluctuation;
                        int    i = 0;
                        for (; x - (int)x != 0; ++i)
                        {
                            x = x * 10;
                        }
                        definition.AddField(EFIXField.PriceDisplay, string.Format("F{0}", i));
                    }


                    definition.AddField(EFIXField.Symbol, inst.InstrumentID);
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.TickSize, inst.minPriceFluctuation);
                    definition.AddField(EFIXField.SecurityDesc, inst.VarietyName);
                    definition.AddField(EFIXField.Factor, inst.contractMultiplier);

                    try
                    {
                        definition.AddField(EFIXField.MaturityDate, DateTime.ParseExact(inst.instrumentMaturity, "yyyy.MM.dd", CultureInfo.InvariantCulture));
                    }
                    catch (Exception ex)
                    {
                        tdlog.Warn("合约:{0},字段内容:{1},{2}", inst.InstrumentID, inst.instrumentMaturity, ex.Message);
                    }

                    FIXSecurityAltIDGroup group = new FIXSecurityAltIDGroup();
                    group.SecurityAltID       = inst.InstrumentID;
                    group.SecurityAltExchange = inst.ExchangeID;
                    group.SecurityAltIDSource = this.Name;

                    definition.AddGroup(group);

                    //还得补全内容

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
                #endregion

                #region 组合
                foreach (DFITCAbiInstrumentRtnField inst in list1)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym      = list.Count + list1.Count,
                    };
                    string securityType2 = FIXSecurityType.MultiLegInstrument;
                    definition.AddField(EFIXField.SecurityType, securityType2);

                    definition.AddField(EFIXField.Symbol, inst.InstrumentID); //
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");           //Currency.CNY
                    definition.AddField(EFIXField.SecurityDesc, inst.instrumentName);

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
                #endregion
            }
        }
Exemplo n.º 4
0
		internal AltIDGroup(Instrument instrument, FIXSecurityAltIDGroup group)
		{
			this.group = group;
			this.instrument = instrument;
		}
        public void SendSecurityDefinitionRequest(FIXSecurityDefinitionRequest request)
        {
            lock (this)
            {
                if (!_bTdConnected)
                {
                    EmitError(-1, -1, "交易没有连接,无法获取合约列表");
                    tdlog.Error("交易没有连接,无法获取合约列表");
                    return;
                }

                string symbol = request.ContainsField(EFIXField.Symbol) ? request.Symbol : null;
                string securityType = request.ContainsField(EFIXField.SecurityType) ? request.SecurityType : null;
                string securityExchange = request.ContainsField(EFIXField.SecurityExchange) ? request.SecurityExchange : null;

                #region 过滤
                List<CThostFtdcInstrumentField> list = new List<CThostFtdcInstrumentField>();
                foreach (CThostFtdcInstrumentField inst in _dictInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (securityType == GetSecurityType(inst))
                        {
                            ++flag;
                        }
                    }
                    
                    if (3==flag)
                    {
                        list.Add(inst);
                    }
                }
                #endregion

                list.Sort(SortCThostFtdcInstrumentField);

                //如果查出的数据为0,应当想法立即返回
                if (0==list.Count)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = 1//有个除0错误的问题
                    };
                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }

                foreach (CThostFtdcInstrumentField inst in list)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = list.Count
                    };

                    {
                        string securityType2 = GetSecurityType(inst);
                        definition.AddField(EFIXField.SecurityType, securityType2);
                    }
                    {
                        double x = inst.PriceTick;
                        if (x > 0.00001)
                        {
                            int i = 0;
                            for (; x - (int)x != 0; ++i)
                            {
                                x = x * 10;
                            }
                            definition.AddField(EFIXField.PriceDisplay, string.Format("F{0}", i));
                            definition.AddField(EFIXField.TickSize, inst.PriceTick);
                        }
                    }
#if CTP
                    definition.AddField(EFIXField.Symbol, inst.InstrumentID);
#elif CTPZQ
                    definition.AddField(EFIXField.Symbol, GetYahooSymbol(inst.InstrumentID, inst.ExchangeID));
#endif
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.SecurityDesc, inst.InstrumentName);
                    definition.AddField(EFIXField.Factor, (double)inst.VolumeMultiple);

                    if(inst.ProductClass == TThostFtdcProductClassType.Futures||inst.ProductClass == TThostFtdcProductClassType.Options)
                    {
                        try
                        {
                            definition.AddField(EFIXField.MaturityDate, DateTime.ParseExact(inst.ExpireDate, "yyyyMMdd", CultureInfo.InvariantCulture));
                        }
                        catch (Exception ex)
                        {
                            tdlog.Warn("合约:{0},字段内容:{1},{2}", inst.InstrumentID, inst.ExpireDate, ex.Message);
                        }

                        //if (inst.ProductClass == TThostFtdcProductClassType.Options)
                        //{
                        //    // 支持中金所,大商所,郑商所
                        //    var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                        //    if (match.Success)
                        //    {
                        //        definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                        //        definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                        //    }
                        //}
                    }

                    // 中金所仿真平台下,居然全设的是股票
                    var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                    if (match.Success)
                    {
                        definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                        definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                    }

                    FIXSecurityAltIDGroup group = new FIXSecurityAltIDGroup();
                    group.SecurityAltID = inst.InstrumentID;
                    group.SecurityAltExchange = inst.ExchangeID;
                    group.SecurityAltIDSource = this.Name;

                    definition.AddGroup(group);
                    
                    //还得补全内容

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
            }
        }
Exemplo n.º 6
0
        public InstrumentList Load()
        {
            IDbCommand command1 = this.connection.CreateCommand();

            command1.CommandText = "comdd";
            IDataReader dataReader1 = command1.ExecuteReader();

            while (dataReader1.Read())
            {
                int  int32 = dataReader1.GetInt32(0);
                Type type  = Type.GetType(dataReader1.GetString(1));
                this.aKpdKBGUfP.Add(int32, type);
                this.cVFd9nJe3b.Add(type, int32);
            }
            dataReader1.Close();
            command1.Dispose();
            IDbCommand command2 = this.connection.CreateCommand();

            command2.CommandText = "load";
            IDataReader dataReader2 = command2.ExecuteReader();

            while (dataReader2.Read())
            {
                int  int32 = dataReader2.GetInt32(0);
                Type type  = Type.GetType(dataReader2.GetString(1));
                this.BuYdCExFpF.Add(int32, type);
                this.UXKdMsMyXN.Add(type, int32);
            }
            dataReader2.Close();
            command2.Dispose();
            InstrumentList instrumentList = new InstrumentList();
            IDbCommand     command3       = this.connection.CreateCommand();

            command3.CommandText = "load";
            IDataReader dataReader3 = command3.ExecuteReader();

            while (dataReader3.Read())
            {
                int        int32      = dataReader3.GetInt32(0);
                Type       type1      = this.aKpdKBGUfP[dataReader3.GetInt32(1)];
                Type       type2      = this.BuYdCExFpF[dataReader3.GetInt32(2)];
                string     @string    = dataReader3.GetString(3);
                Instrument instrument = Activator.CreateInstance(type1, true) as Instrument;
                IPricer    pricer     = Activator.CreateInstance(type2) as IPricer;
                instrument.Symbol = @string;
                instrument.Id     = int32;
                instrument.Pricer = pricer;
                if (instrumentList[@string] == null)
                {
                    instrumentList.Add(instrument);
                }
            }
            dataReader3.Close();
            command3.Dispose();
            FIXDbServer.LoadFIXGroups(this.connection, (FIXGroupList)instrumentList, "fsdfs");
            LegList    legList  = new LegList();
            IDbCommand command4 = this.connection.CreateCommand();

            command4.CommandText = "load";
            IDataReader dataReader4 = command4.ExecuteReader();

            while (dataReader4.Read())
            {
                int int32_1 = dataReader4.GetInt32(0);
                int int32_2 = dataReader4.GetInt32(1);
                int int32_3 = dataReader4.GetInt32(2);
                Leg leg     = new Leg(instrumentList.GetById(int32_3));
                leg.Id = int32_1;
                instrumentList.GetById(int32_2).Legs.Add((FIXGroup)leg);
                legList.Add((FIXGroup)leg);
            }
            dataReader4.Close();
            command4.Dispose();
            FIXDbServer.LoadFIXGroups(this.connection, (FIXGroupList)legList, "");
            UnderlyingList underlyingList = new UnderlyingList();
            IDbCommand     command5       = this.connection.CreateCommand();

            command5.CommandText = "load";
            IDataReader dataReader5 = command5.ExecuteReader();

            while (dataReader5.Read())
            {
                int        int32_1    = dataReader5.GetInt32(0);
                int        int32_2    = dataReader5.GetInt32(1);
                int        int32_3    = dataReader5.GetInt32(2);
                Underlying underlying = new Underlying(instrumentList.GetById(int32_3));
                underlying.Id = int32_1;
                instrumentList.GetById(int32_2).Underlyings.Add((FIXGroup)underlying);
                underlyingList.Add((FIXGroup)underlying);
            }
            dataReader5.Close();
            command5.Dispose();
            FIXDbServer.LoadFIXGroups(this.connection, (FIXGroupList)underlyingList, "dss");
            FIXSecurityAltIDGroupList securityAltIdGroupList = new FIXSecurityAltIDGroupList();
            IDbCommand command6 = this.connection.CreateCommand();

            command6.CommandText = "load";
            IDataReader dataReader6 = command6.ExecuteReader();

            while (dataReader6.Read())
            {
                int int32_1 = dataReader6.GetInt32(0);
                int int32_2 = dataReader6.GetInt32(1);
                FIXSecurityAltIDGroup securityAltIdGroup = new FIXSecurityAltIDGroup();
                securityAltIdGroup.Id = int32_1;
                instrumentList.GetById(int32_2).SecurityAltIDGroup.Add((FIXGroup)securityAltIdGroup);
                securityAltIdGroupList.Add((FIXGroup)securityAltIdGroup);
            }
            dataReader6.Close();
            command6.Dispose();
            FIXDbServer.LoadFIXGroups(this.connection, (FIXGroupList)securityAltIdGroupList, "fsfs");
            return(instrumentList);
        }
Exemplo n.º 7
0
        private void startSymbol()
        {
            string     sSymbol;
            Instrument instrument = null;

            if (bSymbolsDone)
            {
                // do nothing
            }
            else
            {
                if (!bAbort && sdr.Read())
                {
                    sSymbol = sdr.GetString(0); // get next symbol in list
                    cntSymbols++;

                    try {
                        instrument                  = new Instrument(sSymbol, st);
                        instrument.Currency         = "USD";
                        instrument.SecurityExchange = "SMART";
                        //instrument.Exchange = "SMART";

                        FIXSecurityAltIDGroup altid;

                        altid = new FIXSecurityAltIDGroup();
                        altid.SecurityAltID       = sSymbol;
                        altid.SecurityAltIDSource = "IB";
                        instrument.AddGroup(altid);
                        altid = null;

                        altid = new FIXSecurityAltIDGroup();
                        altid.SecurityAltID       = sSymbol;
                        altid.SecurityAltIDSource = "IQFeed";
                        instrument.AddGroup(altid);
                        altid = null;

                        instrument.Save();
                    }
                    catch {
                        //Console.WriteLine( "Caught:  Symbol {0} already exists", sSymbol );
                        instrument = InstrumentManager.Instruments[sSymbol];
                    }

                    Console.WriteLine("Starting {0}, {1}", sSymbol, cntSymbols);
                    //Console.WriteLine("Starting {0}, {1} {2} {3}", sSymbol, cntSymbols, hs.intUseCount, hs.intPlaceInQ);

                    if (bTick)
                    {
                        Send(sSymbol, "Trade", instrument, "HT," + sSymbol + "," + cntTick + ";", cntTick);
                    }

                    if (b1min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt1min + ",1;", 60, cnt1min);
                    }

                    if (b3min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt3min + ",3;", 180, cnt3min);
                    }

                    if (b10min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt10min + ",10;", 600, cnt10min);
                    }

                    if (b30min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt30min + ",30;", 1800, cnt30min);
                    }

                    if (b2hr)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt2hr + ",120;", 7200, cnt2hr);
                    }

                    if (bDaily)
                    {
                        //Console.WriteLine( "Starting {0}, {1} {2} {3}", sSymbol, cntSymbols, hs.intUseCount, hs.intPlaceInQ );
                        Send(sSymbol, "Daily", instrument, "HD," + sSymbol + "," + cntDaily + ";", cntDaily);
                    }
                }
                else
                {
                    Console.WriteLine("All {0} symbols requested", cntSymbols);
                    bSymbolsDone = true;
                    sdr.Close();
                    db.Close();
                }
            }
        }
        public void SendSecurityDefinitionRequest(FIXSecurityDefinitionRequest request)
        {
            lock (this)
            {
                if (!_bTdConnected)
                {
                    EmitError(-1, -1, "交易没有连接,无法获取合约列表");
                    tdlog.Error("交易没有连接,无法获取合约列表");
                    return;
                }

                string symbol = request.ContainsField(EFIXField.Symbol) ? request.Symbol : null;
                string securityType = request.ContainsField(EFIXField.SecurityType) ? request.SecurityType : null;
                string securityExchange = request.ContainsField(EFIXField.SecurityExchange) ? request.SecurityExchange : null;

                #region 过滤
                List<DFITCExchangeInstrumentRtnField> list = new List<DFITCExchangeInstrumentRtnField>();
                foreach (DFITCExchangeInstrumentRtnField inst in _dictInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (FIXSecurityType.Future == securityType)
                        {
                            if (DFITCInstrumentTypeType.COMM_TYPE == inst.instrumentType)
                            {
                                ++flag;
                            }
                        }
                        //else if (FIXSecurityType.MultiLegInstrument == securityType)//理解上是否有问题
                        //{
                        //    if (TThostFtdcProductClassType.Combination == inst.ProductClass)
                        //    {
                        //        ++flag;
                        //    }
                        //}
                        else if (FIXSecurityType.FutureOption == securityType)
                        {
                            if (DFITCInstrumentTypeType.OPT_TYPE == inst.instrumentType)
                            {
                                ++flag;
                            }
                        }
                    }

                    if (3 == flag)
                    {
                        list.Add(inst);
                    }
                }
                #endregion

                #region 过滤1
                List<DFITCAbiInstrumentRtnField> list1 = new List<DFITCAbiInstrumentRtnField>();
                foreach (DFITCAbiInstrumentRtnField inst in _dictAbiInstruments.Values)
                {
                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (inst.InstrumentID.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (inst.ExchangeID.ToUpper().StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (FIXSecurityType.MultiLegInstrument == securityType)
                        {
                            ++flag;
                        }
                    }

                    if (3 == flag)
                    {
                        list1.Add(inst);
                    }
                }
                #endregion

                list.Sort(SortDFITCExchangeInstrumentRtnField);
                list1.Sort(SortDFITCAbiInstrumentRtnField);

                //如果查出的数据为0,应当想法立即返回
                if (0 == list.Count&&0==list1.Count)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = 1//有个除0错误的问题
                    };
                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }

                #region 期货 期权
                foreach (DFITCExchangeInstrumentRtnField inst in list)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = list.Count + list1.Count,
                    };

                    {
                        string securityType2;
                        switch (inst.instrumentType)
                        {
                            case DFITCInstrumentTypeType.COMM_TYPE:
                                securityType2 = FIXSecurityType.Future;
                                break;
                            case DFITCInstrumentTypeType.OPT_TYPE:
                                securityType2 = FIXSecurityType.FutureOption;
                                var match = Regex.Match(inst.InstrumentID, @"(\d+)(-?)([CP])(-?)(\d+)");
                                if (match.Success)
                                {
                                    definition.AddField(EFIXField.PutOrCall, match.Groups[3].Value == "C" ? FIXPutOrCall.Call : FIXPutOrCall.Put);
                                    definition.AddField(EFIXField.StrikePrice, double.Parse(match.Groups[5].Value));
                                }                                
                                break;
                            default:
                                securityType2 = FIXSecurityType.NoSecurityType;
                                break;
                        }
                        definition.AddField(EFIXField.SecurityType, securityType2);
                    }
                    {
                        double x = inst.minPriceFluctuation;
                        int i = 0;
                        for (; x - (int)x != 0; ++i)
                        {
                            x = x * 10;
                        }
                        definition.AddField(EFIXField.PriceDisplay, string.Format("F{0}", i));
                    }

                    
                    definition.AddField(EFIXField.Symbol, inst.InstrumentID);
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.TickSize, inst.minPriceFluctuation);
                    definition.AddField(EFIXField.SecurityDesc, inst.VarietyName);
                    definition.AddField(EFIXField.Factor, inst.contractMultiplier);

                    try
                    {
                        definition.AddField(EFIXField.MaturityDate, DateTime.ParseExact(inst.instrumentMaturity, "yyyy.MM.dd", CultureInfo.InvariantCulture));
                    }
                    catch (Exception ex)
                    {
                        tdlog.Warn("合约:{0},字段内容:{1},{2}", inst.InstrumentID, inst.instrumentMaturity, ex.Message);
                    }

                    FIXSecurityAltIDGroup group = new FIXSecurityAltIDGroup();
                    group.SecurityAltID = inst.InstrumentID;
                    group.SecurityAltExchange = inst.ExchangeID;
                    group.SecurityAltIDSource = this.Name;

                    definition.AddGroup(group);

                    //还得补全内容

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
                #endregion

                #region 组合
                foreach (DFITCAbiInstrumentRtnField inst in list1)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = list.Count + list1.Count,
                    };
                    string securityType2 = FIXSecurityType.MultiLegInstrument;
                    definition.AddField(EFIXField.SecurityType, securityType2);

                    definition.AddField(EFIXField.Symbol, inst.InstrumentID);//
                    definition.AddField(EFIXField.SecurityExchange, inst.ExchangeID);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.SecurityDesc, inst.instrumentName);                    

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
                #endregion
            }
        }
        public void SendSecurityDefinitionRequest(FIXSecurityDefinitionRequest request)
        {
            lock (_dictDepthMarketData)
            {
                string symbol = request.ContainsField(EFIXField.Symbol) ? request.Symbol : null;
                string securityType = request.ContainsField(EFIXField.SecurityType) ? request.SecurityType : null;
                string securityExchange = request.ContainsField(EFIXField.SecurityExchange) ? request.SecurityExchange : null;


                #region 过滤
                List<StructRcvReportEx> list = new List<StructRcvReportEx>();
                foreach (StructRcvReport inst in _dictDepthMarketData.Values)
                {
                    StructRcvReportEx ex = new StructRcvReportEx(inst);

                    int flag = 0;
                    if (null == symbol)
                    {
                        ++flag;
                    }
                    else if (ex.newSymbol.ToUpper().StartsWith(symbol.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityExchange)
                    {
                        ++flag;
                    }
                    else if (ex.yahooExchange.StartsWith(securityExchange.ToUpper()))
                    {
                        ++flag;
                    }

                    if (null == securityType)
                    {
                        ++flag;
                    }
                    else
                    {
                        if (securityType == ex.securityType)
                        {
                            ++flag;
                        }
                    }

                    if (3 == flag)
                    {
                        list.Add(ex);
                    }
                }
                #endregion

                list.Sort(SortCThostFtdcInstrumentField);

                //如果查出的数据为0,应当想法立即返回
                if (0 == list.Count)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = 1//有个除0错误的问题
                    };
                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }

                foreach (StructRcvReportEx inst in list)
                {
                    FIXSecurityDefinition definition = new FIXSecurityDefinition
                    {
                        SecurityReqID = request.SecurityReqID,
                        //SecurityResponseID = request.SecurityReqID,
                        SecurityResponseType = request.SecurityRequestType,
                        TotNoRelatedSym = list.Count
                    };

                    {
                        definition.AddField(EFIXField.SecurityType, inst.securityType);
                    }
                    {
                        //double x = inst.PriceTick;
                        //if (x > 0.0001)
                        //{
                        //    int i = 0;
                        //    for (; x - (int)x != 0; ++i)
                        //    {
                        //        x = x * 10;
                        //    }
                        //    definition.AddField(EFIXField.PriceDisplay, string.Format("F{0}", i));
                        //    definition.AddField(EFIXField.TickSize, inst.PriceTick);
                        //}
                    }

                    definition.AddField(EFIXField.Symbol, GetYahooSymbol(inst.newSymbol, inst.yahooExchange));
                    definition.AddField(EFIXField.SecurityExchange, inst.yahooExchange);
                    definition.AddField(EFIXField.Currency, "CNY");//Currency.CNY
                    definition.AddField(EFIXField.SecurityDesc, inst.RcvReport.StockName);

                    FIXSecurityAltIDGroup group = new FIXSecurityAltIDGroup();
                    group.SecurityAltID = inst.RcvReport.StockCode;
                    group.SecurityAltExchange = inst.RcvReport.MarketType;
                    group.SecurityAltIDSource = this.Name;

                    definition.AddGroup(group);

                    //还得补全内容

                    if (SecurityDefinition != null)
                    {
                        SecurityDefinition(this, new SecurityDefinitionEventArgs(definition));
                    }
                }
            }
        }
Exemplo n.º 10
0
 internal AltIDGroup(Instrument instrument, FIXSecurityAltIDGroup group)
 {
     this.group      = group;
     this.instrument = instrument;
 }