internal void EmitMarketDepth(object o, LevelIIUpdateMessageEventArgs args)
        {
            if (null != NewMarketDepth)
            {
                /*
                 * Console.WriteLine("EmitMarketDepth {0} {1} {2} {3} {4} {5}",
                 * args.Message.Symbol, args.Message.MMID, args.Message.BidPrice, args.Message.BidSize,
                 * args.Message.AskPrice, args.Message.AskSize);
                 */

                MarketDepth             md;
                string                  Symbol = args.Message.Symbol;
                IQFeedDataRequestRecord rr     = htL2WatchedSymbols[Symbol] as IQFeedDataRequestRecord;

                md = new MarketDepth(
                    rr.GetUniqueTimeStamp(),
                    args.Message.MMID, 0, MDOperation.Undefined, MDSide.Bid, args.Message.BidPrice, args.Message.BidSize);
                NewMarketDepth(this, new MarketDepthEventArgs(md, rr.instrument, this));

                md = new MarketDepth(
                    rr.GetUniqueTimeStamp(),
                    args.Message.MMID, 0, MDOperation.Undefined, MDSide.Ask, args.Message.AskPrice, args.Message.AskSize);
                NewMarketDepth(this, new MarketDepthEventArgs(md, rr.instrument, this));
            }
        }
        internal void EmitQuoteTrade(object sender, UpdateMessageEventArgs args)
        {
            IQFeedDataRequestRecord rr = htL1WatchedSymbols[args.Message.Symbol] as IQFeedDataRequestRecord;

            /*
             * if (SmartQuant.TraceLevel.Verbose == trace) {
             * string s;
             * s = string.Format("  sym='{0}',lst='{1}',vol='{2}',bid='{3}',ask='{4}',bsz='{5}',asz='{6}',typ='{7}'",
             *  args.Message.Symbol, args.Message.Last, args.Message.LastSize,
             *  args.Message.Bid, args.Message.Ask, args.Message.BidSize, args.Message.Type );
             * Console.WriteLine(s);
             * }
             */

            switch (args.Message.Type)
            {
            case "a":
            case "b":
                if (null != NewQuote)
                {
                    QuoteEventArgs q = new QuoteEventArgs(
                        new Quote(
                            rr.GetUniqueTimeStamp(),
                            args.Message.Bid,
                            args.Message.BidSize,
                            args.Message.Ask,
                            args.Message.AskSize),
                        rr.instrument, this);
                    NewQuote(this, q);
                }
                break;

            case "t":
            case "T":
                Trade trade = new Trade(
                    rr.GetUniqueTimeStamp(),
                    args.Message.Last,
                    args.Message.LastSize);
                if (null != NewTrade)
                {
                    TradeEventArgs t = new TradeEventArgs(trade, rr.instrument, this);
                    NewTrade(this, t);
                }
                if (null != factory)
                {
                    factory.OnNewTrade(rr.instrument, trade);
                }
                break;

            case "o":
                break;

            default:
                break;
            }
        }
 internal void EmitFundamental(object o, FundamentalMessageEventArgs args)
 {
     if (null != NewFundamental)
     {
         string sSymbol             = args.Message.items[1];
         IQFeedDataRequestRecord rr = htL1WatchedSymbols[sSymbol] as IQFeedDataRequestRecord;
         FundamentalEventArgs    f  = new FundamentalEventArgs(new Fundamental(), rr.instrument, this);
         try {
             if ("" != args.Message.items[10])
             {
                 f.Fundamental.EarningsPerShare = Convert.ToDouble(args.Message.items[10]);
             }
         }
         catch {
         }
         NewFundamental(this, f);
     }
 }
        internal void EmitTradeSummary(object sender, SummaryMessageEventArgs args)
        {
            IQFeedDataRequestRecord rr = htL1WatchedSymbols[args.Message.Symbol] as IQFeedDataRequestRecord;

            if (null != TradeSummary)
            {
                //if (0 < args.Message.Last) {
                TradeEventArgs t = new TradeEventArgs(new Trade(
                                                          rr.GetUniqueTimeStamp(),
                                                          args.Message.Last,
                                                          args.Message.LastSize
                                                          ),
                                                      rr.instrument, this);
                TradeSummary(this, t);
                //}
                //else {
                //  Console.WriteLine("*** IQFeedProvider tradesummary dLast is {0} ***", args.Message.Last);
                //}
            }
        }
        public void SendMarketDataRequest(FIXMarketDataRequest request)
        {
            // need to request as iqfeed's native symbol type, but return as a generic symbol

            FIXRelatedSymGroup      symgrp;
            IQFeedDataRequestRecord rr;

            bool tradeRequest       = false;
            bool quoteRequest       = false;
            bool marketDepthRequest = false;

            if (request.NoMDEntryTypes > 0)
            {
                FIXMDEntryTypesGroup mdEntryTypesGroup = request.GetMDEntryTypesGroup(0);

                switch (mdEntryTypesGroup.MDEntryType)
                {
                case FIXMDEntryType.Trade: {
                    tradeRequest = true;
                }
                break;

                case FIXMDEntryType.Bid:
                case FIXMDEntryType.Offer: {
                    if (request.MarketDepth == 1)
                    {
                        quoteRequest = true;
                    }
                    else
                    {
                        marketDepthRequest = true;
                    }
                }
                break;
                }
            }

            switch (request.SubscriptionRequestType)
            {
            case DataManager.MARKET_DATA_SUBSCRIBE:
                for (int i = 0; i < request.NoRelatedSym; i++)
                {
                    symgrp = request.GetRelatedSymGroup(i);

                    string     sWatchedSymbol = symgrp.Symbol;
                    Instrument instrument     = InstrumentManager.Instruments[symgrp.Symbol];

                    foreach (FIXSecurityAltIDGroup grp in symgrp.SecurityAltIDGroup)
                    {
                        if (Name == grp.SecurityAltIDSource)
                        {
                            sWatchedSymbol = grp.SecurityAltID;
                        }
                    }

                    if (tradeRequest || quoteRequest)
                    {
                        if (htL1WatchedSymbols.ContainsKey(sWatchedSymbol))
                        {
                            rr = htL1WatchedSymbols[sWatchedSymbol] as IQFeedDataRequestRecord;
                            rr.cnt++;
                            rr = null;
                        }
                        else
                        {
                            rr            = new IQFeedDataRequestRecord();
                            rr.request    = request;
                            rr.instrument = instrument;
                            // Symbol		= instrument.GetSymbol(Name); <== hint for other code

                            rr.sCurrency         = symgrp.Currency;
                            rr.sSecurityType     = symgrp.SecurityType;
                            rr.sSecurityExchange = symgrp.SecurityExchange;

                            htL1WatchedSymbols.Add(sWatchedSymbol, rr);
                            Console.WriteLine("MARKET_DATA_SUBSCRIBE {0}", sWatchedSymbol);
                            if (1 == htL1WatchedSymbols.Count)
                            {
                                iqf.HandleFundamentalMessage += new FundamentalMessageHandler(EmitFundamental);
                                iqf.HandleSummaryMessage     += new SummaryMessageHandler(EmitTradeSummary);
                                iqf.HandleUpdateMessage      += new UpdateMessageHandler(EmitQuoteTrade);
                            }
                            iqf.startWatch(sWatchedSymbol);
                            rr = null;
                        }
                    }

                    if (marketDepthRequest)
                    {
                        if (htL2WatchedSymbols.ContainsKey(sWatchedSymbol))
                        {
                            rr = htL1WatchedSymbols[sWatchedSymbol] as IQFeedDataRequestRecord;
                            rr.cnt++;
                            rr = null;
                        }
                        else
                        {
                            instrument.OrderBook.Clear();

                            rr            = new IQFeedDataRequestRecord();
                            rr.request    = request;
                            rr.instrument = instrument;
                            // Symbol		= instrument.GetSymbol(Name); <== hint for other code

                            rr.sCurrency         = symgrp.Currency;
                            rr.sSecurityType     = symgrp.SecurityType;
                            rr.sSecurityExchange = symgrp.SecurityExchange;

                            htL2WatchedSymbols.Add(sWatchedSymbol, rr);
                            Console.WriteLine("MARKETDEPTH_DATA_SUBSCRIBE {0}", sWatchedSymbol);
                            iqfl2.StartWatch(sWatchedSymbol, new LevelIIUpdateMessageHandler(EmitMarketDepth));
                            rr = null;
                        }
                    }
                }
                break;

            case DataManager.MARKET_DATA_UNSUBSCRIBE:
                for (int i = 0; i < request.NoRelatedSym; i++)
                {
                    symgrp = request.GetRelatedSymGroup(i);

                    string     sWatchedSymbol = symgrp.Symbol;
                    Instrument instrument     = InstrumentManager.Instruments[symgrp.Symbol];

                    foreach (FIXSecurityAltIDGroup grp in symgrp.SecurityAltIDGroup)
                    {
                        if (Name == grp.SecurityAltIDSource)
                        {
                            sWatchedSymbol = grp.SecurityAltID;
                        }
                    }

                    if (tradeRequest || quoteRequest)
                    {
                        if (htL1WatchedSymbols.ContainsKey(sWatchedSymbol))
                        {
                            rr = htL1WatchedSymbols[sWatchedSymbol] as IQFeedDataRequestRecord;
                            rr.cnt--;
                            if (0 == rr.cnt)
                            {
                                Console.WriteLine("MARKET_DATA_UNSUBSCRIBE {0}", sWatchedSymbol);
                                iqf.stopWatch(sWatchedSymbol);
                                if (0 == htL1WatchedSymbols.Count)
                                {
                                    iqf.HandleFundamentalMessage -= new FundamentalMessageHandler(EmitFundamental);
                                    iqf.HandleSummaryMessage     -= new SummaryMessageHandler(EmitTradeSummary);
                                    iqf.HandleUpdateMessage      -= new UpdateMessageHandler(EmitQuoteTrade);
                                }

                                rr.request    = null;
                                rr.instrument = null;
                            }
                            rr = null;
                        }
                        else
                        {
                            throw new ArgumentException("No to stop l1 for symbol " + symgrp.Symbol + "/" + sWatchedSymbol);
                        }
                    }

                    if (marketDepthRequest)
                    {
                        if (htL2WatchedSymbols.ContainsKey(sWatchedSymbol))
                        {
                            rr = htL2WatchedSymbols[sWatchedSymbol] as IQFeedDataRequestRecord;
                            rr.cnt--;
                            if (0 == rr.cnt)
                            {
                                Console.WriteLine("MARKETDEPTH_DATA_UNSUBSCRIBE {0}", sWatchedSymbol);
                                iqfl2.StopWatch(sWatchedSymbol, new LevelIIUpdateMessageHandler(EmitMarketDepth));

                                rr.request    = null;
                                rr.instrument = null;
                            }
                            rr = null;
                        }
                        else
                        {
                            throw new ArgumentException("No to stop l2 for symbol " + symgrp.Symbol + "/" + sWatchedSymbol);
                        }
                    }
                }
                break;

            default:
                throw new ArgumentException("Unknown subscription type: " + request.SubscriptionRequestType.ToString());
            }
        }