string GetKeyFromSTKTRACEDATA(ref STKTRACEDATA pTraceData)
 {
     return string.Format("{0}:{1}", pTraceData.Market, pTraceData.Code);
 }
        private void OnRspTraceData(IntPtr pQuotApi, IntPtr pBuffer, ref STKTRACEDATA pTraceData)
        {
            string key = GetKeyFromSTKTRACEDATA(ref pTraceData);
            string requestId;
            if (historicalDataRecords.TryGetValue(key, out requestId))
            {
                HistoricalDataRequest request;
                if (historicalDataIds.TryGetValue(requestId, out request))
                {
                    int day = -1;
                    float volume = 0;
                    DateTime datetime = DateTime.Now;
                    DateTime updatetime = DateTime.Now;

                    IntPtr ptrHead = (IntPtr)(pBuffer + Marshal.SizeOf(typeof(STKTRACEDATA)));
                    for (int i = 0; i < pTraceData.nCount; ++i)
                    {
                        IntPtr ptr = (IntPtr)(ptrHead + Marshal.SizeOf(typeof(STOCKTRACEDATA)) * i);
                        STOCKTRACEDATA std = (STOCKTRACEDATA)Marshal.PtrToStructure(ptr, typeof(STOCKTRACEDATA));

                        DateTime dt = Convert.ToDateTime(std.time);
                        if (datetime == dt)
                        {
                            updatetime = updatetime.AddMilliseconds(250);
                        }
                        else
                        {
                            updatetime = dt;
                        }
                        if (day != updatetime.Day)
                        {
                            volume = 0;
                        }
                        day = updatetime.Day;
                        volume = std.m_Volume - volume;

                        if (updatetime >= request.BeginDate && updatetime < request.EndDate)
                        {
                            if (request.DataType == HistoricalDataType.Trade)
                            {
                                Trade trade = new Trade(updatetime, std.m_NewPrice, (int)volume);
                                NewHistoricalTrade(this,
                                    new HistoricalTradeEventArgs(trade, request.RequestId, request.Instrument, this, -1));
                            }
                            else
                            {
                                Quote quote = new Quote(updatetime, std.m_BuyPrice, (int)std.m_BuyVol, std.m_SellPrice, (int)std.m_SellVol);
                                NewHistoricalQuote(this,
                                    new HistoricalQuoteEventArgs(quote, request.RequestId, request.Instrument, this, -1));
                            }
                        }

                        datetime = dt;
                        volume = std.m_Volume;
                    }

                    historicalDataIds.Remove(request.RequestId);
                    EmitHistoricalDataCompleted(request);
                }
                historicalDataRecords.Remove(key);
            }
        }
        private void OnRspTraceData(IntPtr pQuotApi, IntPtr pBuffer, ref STKTRACEDATA pTraceData)
        {
            ehlog.Info("<--OnRspTraceData:{0},{1},{2}条", pTraceData.Market, pTraceData.Code, pTraceData.nCount);
            string key = GetKeyFromSTKTRACEDATA(ref pTraceData);
            DataRecord dr;
            if (historicalDataRecords_key.TryGetValue(key, out dr))
            {
                HistoricalDataRequest request = dr.request;

                int day = -1;
                float volume = 0;
                DateTime datetime = DateTime.Now;
                DateTime updatetime = DateTime.Now;

                IntPtr ptrHead = (IntPtr)(pBuffer + Marshal.SizeOf(typeof(STKTRACEDATA)));
                for (int i = 0; i < pTraceData.nCount; ++i)
                {
                    IntPtr ptr = (IntPtr)(ptrHead + Marshal.SizeOf(typeof(STOCKTRACEDATA)) * i);
                    STOCKTRACEDATA std = (STOCKTRACEDATA)Marshal.PtrToStructure(ptr, typeof(STOCKTRACEDATA));

                    DateTime dt = Convert.ToDateTime(std.time);
                    if (datetime == dt)
                    {
                        updatetime = updatetime.AddMilliseconds(250);
                    }
                    else
                    {
                        updatetime = dt;
                    }
                    if (day != updatetime.Day)
                    {
                        volume = 0;
                    }
                    day = updatetime.Day;
                    volume = std.m_Volume - volume;

                    if (updatetime >= request.BeginDate && updatetime < request.EndDate)
                    {
                        if (BothTradeAndQuote)
                        {
                            Trade trade = new Trade(updatetime, std.m_NewPrice, (int)volume);
                            NewHistoricalTrade(this,
                                new HistoricalTradeEventArgs(trade, request.RequestId, request.Instrument, this, -1));

                            if (std.m_BuyPrice == 0 && std.m_BuyVol == 0 
                                &&std.m_SellPrice == 0 && std.m_SellVol == 0)
                            {
                            }
                            else
                            {
                                Quote quote = new Quote(updatetime, std.m_BuyPrice, (int)std.m_BuyVol, std.m_SellPrice, (int)std.m_SellVol);
                                NewHistoricalQuote(this,
                                    new HistoricalQuoteEventArgs(quote, request.RequestId, request.Instrument, this, -1));
                            }
                        }
                        else
                        {
                            if (request.DataType == HistoricalDataType.Trade)
                            {
                                Trade trade = new Trade(updatetime, std.m_NewPrice, (int)volume);
                                NewHistoricalTrade(this,
                                    new HistoricalTradeEventArgs(trade, request.RequestId, request.Instrument, this, -1));
                            }
                            else
                            {
                                if (std.m_BuyPrice == 0 && std.m_BuyVol == 0
                                && std.m_SellPrice == 0 && std.m_SellVol == 0)
                                {
                                }
                                else
                                {
                                    Quote quote = new Quote(updatetime, std.m_BuyPrice, (int)std.m_BuyVol, std.m_SellPrice, (int)std.m_SellVol);
                                    NewHistoricalQuote(this,
                                        new HistoricalQuoteEventArgs(quote, request.RequestId, request.Instrument, this, -1));
                                }
                            }
                        }
                    }

                    datetime = dt;
                    volume = std.m_Volume;
                }

                RemoveRequest(key);
                SendRequest_Tick(key);
            }
        }