예제 #1
0
 private void OnReminder(ReminderEventArgs args)
 {
     lock (this)
     {
         foreach (KeyValuePair <Instrument, Dictionary <DateTime, Daily> > kvp1 in dailyBars.ToList())
         {
             Instrument instrument = kvp1.Key;
             Dictionary <DateTime, Daily> dailyBox = kvp1.Value;
             foreach (KeyValuePair <DateTime, Daily> kvp2 in dailyBox.ToList())
             {
                 DateTime barDateTime = kvp2.Key;
                 Daily    daily       = kvp2.Value;
                 DZHQuote lastQuote   = lastQuotes[instrument.Symbol];
                 if (lastQuote.Time.Date == barDateTime)
                 {
                     daily.High   = lastQuote.High;
                     daily.Low    = lastQuote.Low;
                     daily.Close  = lastQuote.Price;
                     daily.Volume = (long)lastQuote.TotalVolume;
                     ((DZHBarFactory)this.factory).EmitNewBar(daily, instrument);//必须使用barfactory激活事件
                 }
                 dailyBox.Remove(barDateTime);
             }
             if (dailyBox.Count == 0)
             {
                 dailyBars.Remove(instrument);
             }
         }
         ((DZHBarFactory)this.factory).EmitNewBarSlice(BarSize.Day);
     }
 }
예제 #2
0
        protected DZHQuote ReadARecord(long pos)
        {
            DZHQuote aQuote = new DZHQuote();

            fileStream.Position = pos + 60;
            aQuote.Time         = date19700101.AddSeconds(reader.ReadUInt32());
            fileStream.Position = pos + 68;
            aQuote.LastClose    = reader.ReadSingle();
            aQuote.Open         = reader.ReadSingle();
            aQuote.High         = reader.ReadSingle();
            aQuote.Low          = reader.ReadSingle();;
            aQuote.Price        = reader.ReadSingle();
            aQuote.TotalVolume  = reader.ReadSingle();
            aQuote.TotalAmount  = reader.ReadSingle();
            aQuote.Volume       = reader.ReadSingle();
            fileStream.Position = pos + 100;
            aQuote.Bid1         = reader.ReadSingle();
            aQuote.Bid2         = reader.ReadSingle();
            aQuote.Bid3         = reader.ReadSingle();
            aQuote.Bid4         = reader.ReadSingle();
            aQuote.Bid5         = reader.ReadSingle();
            aQuote.Bid1Vol      = reader.ReadSingle();
            aQuote.Bid2Vol      = reader.ReadSingle();
            aQuote.Bid3Vol      = reader.ReadSingle();
            aQuote.Bid4Vol      = reader.ReadSingle();
            aQuote.Bid5Vol      = reader.ReadSingle();
            aQuote.Ask1         = reader.ReadSingle();
            aQuote.Ask2         = reader.ReadSingle();
            aQuote.Ask3         = reader.ReadSingle();
            aQuote.Ask4         = reader.ReadSingle();
            aQuote.Ask5         = reader.ReadSingle();
            aQuote.Ask1Vol      = reader.ReadSingle();
            aQuote.Ask2Vol      = reader.ReadSingle();
            aQuote.Ask3Vol      = reader.ReadSingle();
            aQuote.Ask4Vol      = reader.ReadSingle();
            aQuote.Ask5Vol      = reader.ReadSingle();
            return(aQuote);
        }
예제 #3
0
        /*定时器执行更新报价*/
        private void tickTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            /*如果只在开市时读数据,在非开市时间直接退出*/
            if (onlyReadInMarket && (DateTime.Now < DateTime.Today.Add(beginTime) || DateTime.Now > DateTime.Today.Add(endTime)))
            {
                return;
            }
            /*当运行到第二天时,清空报价缓存*/
            if (lastDateOfQuote < DateTime.Today)
            {
                lastDateOfQuote = DateTime.Today;
                lastQuotes.Clear();
            }

            if (quoteReader == null)
            {
                quoteReader = new DZHQuoteReader(dzhDataPath);
            }
            try
            {
                foreach (string symbol in subscribedSymbols.ToArray())
                {
                    if (!subscribedSymbols.Contains(symbol))
                    {
                        continue;
                    }
                    Instrument curInstrument = InstrumentManager.Instruments[symbol];
                    if (curInstrument == null)
                    {
                        this.EmitError(-1, -1, "Symbol " + symbol + " was not found in list of requested symbols.");
                    }
                    else
                    {
                        DZHSymbol dzhSymbol = new DZHSymbol(curInstrument.SecurityExchange, curInstrument.SecurityID);
                        DZHQuote  newQuote  = quoteReader.RequestQuote(dzhSymbol);
                        bool      flag1     = false;
                        bool      flag2     = false;
                        bool      first     = false;//是否是请求的第一笔数据
                        if (this.lastQuotes.ContainsKey(symbol))
                        {
                            DZHQuote oldQuote = this.lastQuotes[symbol];

                            /*这里注释掉和下面一样,大智慧发送来的数据存在同一时刻多笔交易的现象
                             * 所以不能用时间来排除
                             */
                            //if (newQuote.Time != oldQuote.Time)
                            //{
                            if ((newQuote.Bid1 != oldQuote.Bid1) || (newQuote.Bid1Vol != oldQuote.Bid1Vol) ||

                                (newQuote.Ask1 != oldQuote.Ask1) || (newQuote.Ask1Vol != oldQuote.Ask1Vol))
                            {
                                flag1 = true;
                            }
                            if ((newQuote.TotalVolume != oldQuote.TotalVolume) || (newQuote.Price != oldQuote.Price))
                            {
                                flag2 = true;
                            }
                            // }
                            this.lastQuotes[symbol] = newQuote;
                        }
                        else
                        {
                            first = true;
                            if ((newQuote.Ask1 > 0.0) || (newQuote.Bid1 > 0.0))
                            {
                                flag1 = true;
                            }
                            if ((newQuote.Price > 0.0) || (newQuote.Volume > 0.0))
                            {
                                flag2 = true;
                            }
                            this.lastQuotes.Add(symbol, newQuote);
                        }

                        /*这里注释掉是,不做时间检查,因为不使用市场数据提供者存储数据,市场数据提供者只用于实时交易
                         *而要获取过去的数据存储起来,使用历史数据提供者
                         */
                        //if ((flag1) && ((curInstrument.GetQuoteArray(newQuote.Time, newQuote.Time)).Count == 0))
                        if (flag1)
                        {
                            this.EmitNewQuote(new Quote(newQuote.Time, newQuote.Bid1, (int)newQuote.Bid1Vol, newQuote.Ask1, (int)newQuote.Ask1Vol), curInstrument);
                        }

                        /*大智慧有可能在同一时刻有多笔交易,QD不允许这样,所以有可能丢失某些笔交易
                         * 如果不作同一时刻的检查,则有可能在数据中保存重复的交易
                         * 这里注释掉是,不做时间检查,因为不使用市场数据提供者存储数据,市场数据提供者只用于实时交易
                         *而要获取过去的数据存储起来,使用历史数据提供者
                         */
                        //if ((flag2) && ((curInstrument.GetTradeArray(newQuote.Time, newQuote.Time)).Count == 0))
                        if (flag2)
                        {
                            this.EmitNewTrade(new Trade(newQuote.Time, newQuote.Price, (int)newQuote.Volume), curInstrument);
                            /*以下代码是为了产生日线*/
                            if (first && buildDailyBar)
                            {
                                lock (this)
                                {
                                    DateTime barDateTime = newQuote.Time.Date;
                                    if (!dailyBars.ContainsKey(curInstrument))
                                    {
                                        dailyBars.Add(curInstrument, new Dictionary <DateTime, Daily>());
                                    }
                                    Dictionary <DateTime, Daily> dailyBox = dailyBars[curInstrument];
                                    if (!dailyBox.ContainsKey(barDateTime))
                                    {
                                        Daily daily = new Daily(barDateTime, newQuote.Open, newQuote.High, newQuote.Low, newQuote.Price, (long)newQuote.TotalVolume);
                                        dailyBox.Add(barDateTime, daily);
                                        ((DZHBarFactory)this.factory).EmitNewBarOpen(daily, curInstrument);
                                    }
                                    DateTime realyEndTime = Clock.Now.Add(this.endTime - newQuote.Time.TimeOfDay);
                                    Clock.AddReminder(new ReminderEventHandler(OnReminder), realyEndTime, null);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.EmitError(-1, -1, exception.ToString());
            }
        }