void grdRealData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //将不需要的行情去除掉
            if (dgRealData.SelectedItem != null)
            {
                DisplayRealData selectedRecord = dgRealData.SelectedItem as DisplayRealData;
                LvQuotesPanel.lblCode.Content = selectedRecord.Code;

                //RealData selectedData = new RealData();
                string tempKey = selectedRecord.Code + "_" + CodeSetManager.ExNameToCtp(selectedRecord.Market);
                if (_BackupCodeDic.ContainsKey(tempKey))
                {
                    LvQuotesPanel.SetLevelsQuotesByRealData(_BackupCodeDic[tempKey]);
                }

                //List<DisplayRealData> uselessDatas = new List<DisplayRealData>();
                //foreach (var item in mainWindow.RealDataCollection)
                //{
                //    if (!HQRealData.commObj.RequestingCodes.Contains(item.Code)
                //        && item.Code != mainWindow.uscNewOrderPanel.txtCode.Text.Trim()
                //        && item != selectedRecord)
                //    {
                //        uselessDatas.Add(item);
                //    }
                //}
                //foreach (var item in uselessDatas)
                //{
                //    mainWindow.RealDataCollection.Remove(item);
                //}
            }
        }
 private void dgGroupRealData_PreviewKeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Enter)
     {
         if (DgCell == null)
         {
             return;
         }
         DisplayRealData record = dgGroupRealData.CurrentItem as DisplayRealData;
         GridHangQingData(record);
         _MainWindow.uscNewOrderPanel.NewOrderFocus();
     }
 }
        public void SelectDataByCode(string code)
        {
            DisplayRealData realData = null;

            foreach (var item in _MainWindow.RealDataCollection)
            {
                if (item.Code == code)
                {
                    realData = item;
                    break;
                }
            }
            if (realData != null)
            {
                dgRealData.SelectedItem = null;
                dgRealData.SelectedItem = realData;
                dgRealData.ScrollIntoView(dgRealData.SelectedItem);
            }
        }
        /// <summary>
        /// 将持仓和下单板的合约行情显示出来
        /// </summary>
        /// <param name="externalHq"></param>
        public void AddExternalHqingData(DisplayRealData externalHq)
        {
            if (defaultButton != null && defaultButton.Content.ToString() == "主力合约")
            {
                return;
            }
            if (!HQRealData._CommObj.RequestingCodes.Contains(externalHq.Code))
            {
                DisplayRealData orderData = null;
                foreach (var item in _MainWindow.RealDataCollection)
                {
                    if (item.Code == externalHq.Code)
                    {
                        orderData = item;
                        break;
                    }
                }

                if (orderData == null)
                {
                    //Util.Log("添加其他行情数据:" + externalHq.ToString());
                    _MainWindow.RealDataCollection.Add(externalHq);
                    dgRealData.SelectedItem = externalHq;
                    dgRealData.ScrollIntoView(dgRealData.SelectedItem);
                }
                else
                {
                    orderData.INewPrice        = externalHq.INewPrice;
                    orderData.StBuyPrice       = externalHq.StBuyPrice;
                    orderData.I64Sum           = externalHq.I64Sum;
                    orderData.StBuyCount       = externalHq.StBuyCount;
                    orderData.StSellPrice      = externalHq.StSellPrice;
                    orderData.StSellCount      = externalHq.StSellCount;
                    orderData.ChiCangLiang     = externalHq.ChiCangLiang;
                    orderData.Volumn           = externalHq.Volumn;
                    orderData.CurrentHand      = externalHq.CurrentHand;
                    orderData.Open             = externalHq.Open;
                    orderData.ISettlementPrice = externalHq.ISettlementPrice;        //现结算
                    orderData.IClose           = externalHq.IClose;
                    orderData.UpdateTime       = externalHq.UpdateTime;
                }
            }
        }
        private void grdRealData_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DgCell = CommonUtil.GetClickedCell(e);
            if (DgCell == null)
            {
                return;
            }
            DisplayRealData record = DgCell.DataContext as DisplayRealData;

            GridHangQingData(record);

            DataGrid dg = sender as DataGrid;

            if (dg != null)
            {
                //dg.SelectedItems.Clear();
                //dg.SelectedItems.Add(record);
                dg.SelectedItem = record;
            }
        }
        /// <summary>
        /// 买量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_MouseEnter_Close_Buy(object sender, MouseEventArgs e)
        {
            Grid grid = sender as Grid;

            if (grid == null)
            {
                return;
            }
            TextBlock       tp = grid.ToolTip as TextBlock;
            DisplayRealData displayRealData = grid.DataContext as DisplayRealData;
            //卖出平仓
            TradeDataClient jyDataServer = TradeDataClient.GetClientInstance();
            int             todayPosCount;
            int             lastPosCount;
            string          code = displayRealData.Code;

            //查找多头的单子
            if (jyDataServer.GetPositionCount(code, true, out todayPosCount, out lastPosCount) == false)
            {
                grid.ToolTip = null;
                return;
            }
            if (todayPosCount == 0 && lastPosCount == 0)
            {
                grid.ToolTip = null;
                return;
            }
            if (tp == null)
            {
                grid.ToolTip = new TextBlock();
                tp           = grid.ToolTip as TextBlock;
            }
            tp.Inlines.Clear();
            Run run = new Run();

            run.Text = "双击 ";
            tp.Inlines.Add(run);

            run = new Run();
            Binding binding1 = new Binding();

            binding1.Source = grid.DataContext;
            binding1.Mode   = BindingMode.OneWay;
            binding1.Path   = new PropertyPath("StBuyPrice");
            run.SetBinding(Run.TextProperty, binding1);
            tp.Inlines.Add(run);

            run            = new Run();
            run.Text       = " 卖出";
            run.Foreground = new SolidColorBrush(Color.FromRgb(0x13, 0xaf, 0x13));
            tp.Inlines.Add(run);

            Boolean bHasComma = false;

            if (CodeSetManager.IsCloseTodaySupport(code) && todayPosCount != 0)
            {
                //如果支持平今
                run            = new Run();
                run.Text       = "平今";
                run.FontSize   = 13;
                run.Foreground = new SolidColorBrush(Color.FromRgb(0xff, 0x00, 0xff));
                tp.Inlines.Add(run);

                run            = new Run();
                run.Text       = " " + todayPosCount.ToString() + " ";
                run.Foreground = new SolidColorBrush(Color.FromRgb(0x13, 0xaf, 0x13));
                tp.Inlines.Add(run);

                bHasComma = true;
            }
            else
            {
                lastPosCount += todayPosCount;
            }
            if (lastPosCount != 0)
            {
                if (bHasComma)
                {
                    run      = new Run();
                    run.Text = ",";
                    tp.Inlines.Add(run);
                }

                run            = new Run();
                run.Text       = " 平仓";
                run.FontSize   = 13;
                run.Foreground = new SolidColorBrush(Color.FromRgb(0xff, 0x00, 0xff));
                tp.Inlines.Add(run);

                run            = new Run();
                run.Text       = " " + lastPosCount.ToString() + " ";
                run.Foreground = new SolidColorBrush(Color.FromRgb(0x13, 0xaf, 0x13));
                tp.Inlines.Add(run);
            }

            run           = new Run();
            run.Text      = "手" + code;
            tp.FontWeight = FontWeights.Bold;
            tp.Inlines.Add(run);
        }
        private void GridHangQingData(DisplayRealData record)
        {
            string  buySell   = "";
            Boolean isBuySell = false;

            if (DgCell.Column == colBuyPrice || DgCell.Column == colBuyCount || DgCell.Column == colGroupBuyPrice || DgCell.Column == colGroupBuyCount)
            {
                buySell   = "卖";
                isBuySell = true;
            }
            else if (DgCell.Column == colSellPrice || DgCell.Column == colSellCount || DgCell.Column == colGroupSellPrice || DgCell.Column == colGroupSellCount)
            {
                buySell   = "买";
                isBuySell = true;
            }

            //点击最新价
            double sNewPrice = 0;

            if (DgCell.Column == colINewPrice || DgCell.Column == colGroupNewPrice)
            {
                sNewPrice = record.INewPrice;
            }

            //点击涨停价和跌停价时使用指定价下单
            double fixPrice = 0;

            if (DgCell.Column == colMaxPrice || DgCell.Column == colGroupMaxPrice)
            {
                buySell  = "买";
                fixPrice = record.UpStopPrice;
            }
            else if (DgCell.Column == colMinPrice || DgCell.Column == colGroupMinPrice)
            {
                buySell  = "卖";
                fixPrice = record.DownStopPrice;
            }
            //今开盘和昨结算
            double sJkpPrice = 0;
            double sZjsPrice = 0;

            if (DgCell.Column == colJkpPrice || DgCell.Column == colGroupJkpPrice)
            {
                sJkpPrice = record.Open;
            }
            else if (DgCell.Column == colZjsPrice || DgCell.Column == colGroupZjsPrice)
            {
                sZjsPrice = record.PrevSettleMent;
            }
            //点击最高价和最低价显示指定价
            double sHighPrice = 0;
            double sLowPrice  = 0;

            if (DgCell.Column == colHightPrice || DgCell.Column == colGroupHightPrice)
            {
                sHighPrice = record.IMaxPrice;
            }
            else if (DgCell.Column == colLowPrice || DgCell.Column == colGroupLowPrice)
            {
                sLowPrice = record.IMinPrice;
            }
            //点击昨收盘和今收盘
            double sZspPrice = 0;
            double sJspPrice = 0;

            if (DgCell.Column == colZspPrice || DgCell.Column == colGroupZspPrice)
            {
                sZspPrice = record.PrevSettleMent;
            }
            else if (DgCell.Column == colJspPrice || DgCell.Column == colGroupJspPrice)
            {
                sJspPrice = record.IClose;
            }
            //点击结算价
            double sJsjPrice  = 0;
            double sDrjjPrice = 0;

            if (DgCell.Column == colJsjPrice || DgCell.Column == colGroupJsjPrice)
            {
                sJsjPrice = record.ISettlementPrice;
            }
            else if (DgCell.Column == colDrjjPrice || DgCell.Column == colGroupDrjjPrice)
            {
                sDrjjPrice = record.AvgPrice;
            }

            RealData realRecord = new RealData();

            if (RealDataMouseLeftButtonDown != null && realRecord.UpdateRealProperties(record))
            {
                RealDataMouseLeftButtonDown(buySell, realRecord, fixPrice, sNewPrice, sJkpPrice, sZjsPrice, sHighPrice, sLowPrice, sZspPrice, sJspPrice, sJsjPrice, sDrjjPrice, isBuySell);
            }
        }
        private void RequestRealData(BackgroundDataServer hqRealData)
        {
            ObservableCollection <UserCodeSet> lstUserCodeSet = UserCodeSetInstance.GetUserCodeSetList();
            //先删除合约
            bool bChanged = false;

            _DelCodeList.Clear();
            foreach (Contract temp in hqRealData.m_oldCodeArray)
            {
                if (!hqRealData.m_codeArray.Contains(temp))
                {
                    _DelCodeList.Add(temp);
                }
            }
            if (_DelCodeList.Count > 0)
            {
                bChanged = true;
                hqRealData.AddContract(_DelCodeList.ToArray());
                hqRealData.UnRequest();
                try
                {
                    RealData tempData = null;
                    foreach (Contract temp in _DelCodeList)
                    {
                        string tempKey = temp.Code + "_" + temp.ExchCode;
                        if (_BackupCodeDic.ContainsKey(tempKey))
                        {
                            tempData = _BackupCodeDic[tempKey];
                            if (tempData != null)
                            {
                                DisplayRealData tempDisplayData = UpdateDisplayProperties(tempData);
                                if (hqRealData == HQRealData)
                                {
                                    //Util.Log("<<:" + tempData.ToString());
                                    _MainWindow.RealDataCollection.Remove(tempDisplayData);
                                }
                                else if (hqRealData == GroupHQRealData)
                                {
                                    _MainWindow.RealDataArbitrageCollection.Remove(tempDisplayData);
                                }
                                _BackupCodeDic.Remove(tempKey);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Util.Log_Error("exception: " + ex.Message);
                    Util.Log_Error("exception: " + ex.StackTrace);
                }
            }
            //在请求新增的合约
            _AddCodeList.Clear();
            foreach (Contract temp in hqRealData.m_codeArray)
            {
                if (!hqRealData.m_oldCodeArray.Contains(temp))
                {
                    _AddCodeList.Add(temp);
                }
            }

            if (_AddCodeList.Count > 0)
            {
                foreach (Contract temp in _AddCodeList)
                {
                    if (temp == null)
                    {
                        continue;
                    }
                    string tempKey = temp.Code + "_" + temp.ExchCode;
                    if (_BackupCodeDic.ContainsKey(tempKey) == false)
                    {
                        RealData tempData2 = new RealData();
                        //Contract tempContract = CodeSetManager.GetContractInfo(temp.Code);
                        tempData2.CodeInfo = CodeSetManager.GetContractInfo(temp.Code, temp.ExchCode);
                        if (tempData2.CodeInfo == null)
                        {
                            tempData2.CodeInfo = new Contract(temp.Code);
                        }
                        //tempData2.Name = tempContract.Name;
                        //decimal fluct = 0.00M;
                        //CodeSetManager.GetHycsAndFluct(tempData2.codeInfo.Code, out (double)tempData2.m_hycs, out fluct);
                        _BackupCodeDic.Add(tempKey, tempData2);
                    }
                }
                bChanged = true;
                hqRealData.AddContract(_AddCodeList.ToArray());
                hqRealData.GetRealData();
                hqRealData.Request();
            }

            DeepCopyInfo(hqRealData.m_codeArray, hqRealData.m_oldCodeArray);

            if (bChanged == false)
            {
                RealData tempData2 = null;
                _MainWindow.RealDataCollection.Clear();
                _MainWindow.RealDataArbitrageCollection.Clear();
                foreach (Contract temp in hqRealData.m_oldCodeArray)
                {
                    //Util.Log(this.defaultButton.Content + " 添加内容 Old-----");
                    string tempKey = temp.Code + "_" + temp.ExchCode;
                    if (_BackupCodeDic.ContainsKey(tempKey))
                    {
                        tempData2 = _BackupCodeDic[tempKey];
                        if (tempData2 != null)
                        {
                            DisplayRealData tempDisplayData = UpdateDisplayProperties(tempData2);
                            if (hqRealData == HQRealData)
                            {
                                //Util.Log(">>:" + tempData2.ToString());
                                _MainWindow.RealDataCollection.Add(tempDisplayData);
                            }
                            else if (hqRealData == GroupHQRealData)
                            {
                                if (tempData2.CodeInfo.Code.Contains("&"))
                                {
                                    //string[] tempType = tempData2.codeInfo.Code.Split(' ');
                                    //string[] tempGroup = tempType[1].Split('&');
                                    //tempData2.Code = tempGroup[0];
                                    //tempData2.Code2 = tempGroup[1];
                                }
                                _MainWindow.RealDataArbitrageCollection.Add(UpdateDisplayProperties(tempData2));
                            }
                        }
                    }
                }
            }
            else
            {
                RealData tempData2 = null;
                _MainWindow.RealDataCollection.Clear();
                _MainWindow.RealDataArbitrageCollection.Clear();
                if (hqRealData == HQRealData)
                {
                    //Util.Log(this.defaultButton.Content + " 添加内容-----");
                    foreach (Contract temp in _CodeArray)
                    {
                        if (temp != null)
                        {
                            string tempKey = temp.Code + "_" + temp.ExchCode;
                            if (_BackupCodeDic.ContainsKey(tempKey) == false)
                            {
                                tempData2 = new RealData();
                                //Contract tempContract = CodeSetManager.GetContractInfo(temp.Code);
                                tempData2.CodeInfo = CodeSetManager.GetContractInfo(temp.Code, temp.ExchCode);
                                if (tempData2.CodeInfo == null)
                                {
                                    tempData2.CodeInfo = new Contract(temp.Code);
                                }
                                //tempData2.Name = tempContract.Name;
                                //double fluct = 0;
                                //CodeSetManager.GetHycsAndFluct(tempData2.Code, out tempData2.m_hycs, out fluct);
                                _BackupCodeDic.Add(tempKey, tempData2);
                            }
                            else
                            {
                                tempData2 = _BackupCodeDic[tempKey];
                            }
                            DisplayRealData tempDisplayData = UpdateDisplayProperties(tempData2);
                            //Util.Log(">>:"+tempData2.ToString());
                            _MainWindow.RealDataCollection.Add(tempDisplayData);
                            //Util.Log("mainWindow.RealDataCollection.count=" + mainWindow.RealDataCollection.Count);
                        }
                    }
                }

                else if (hqRealData == GroupHQRealData)
                {
                    foreach (Contract temp in _GroupCodeArray)
                    {
                        if (temp != null)
                        {
                            string tempKey = temp.Code + "_" + temp.ExchCode;
                            if (_BackupCodeDic.ContainsKey(tempKey) == false)
                            {
                                tempData2          = new RealData();
                                tempData2.CodeInfo = CodeSetManager.GetContractInfo(temp.Code, temp.ExchCode);
                                if (tempData2.CodeInfo == null)
                                {
                                    tempData2.CodeInfo = new Contract(tempKey);
                                }
                                //decimal fluct = 0;
                                //CodeSetManager.GetHycsAndFluct(temp.Code, out tempData2.m_hycs, out fluct);
                                _BackupCodeDic.Add(tempKey, tempData2);
                            }
                            else
                            {
                                tempData2 = _BackupCodeDic[tempKey];
                            }
                            if (tempData2.CodeInfo.Code.Contains("&"))
                            {
                                //string[] tempType = tempData2.Code.Split(' ');
                                //string[] tempGroup = tempType[1].Split('&');
                                //tempData2.Code1 = tempGroup[0];
                                //tempData2.Code2 = tempGroup[1];
                            }
                            _MainWindow.RealDataArbitrageCollection.Add(UpdateDisplayProperties(tempData2));
                        }
                    }
                }
            }
            hqRealData.UpdateRealDataList();
        }
        public DisplayRealData UpdateDisplayProperties(RealData realData)
        {
            DisplayRealData displayData = new DisplayRealData();

            try
            {
                displayData.Code = realData.CodeInfo.Code;
                if (realData.CodeInfo.ProductType != null && realData.CodeInfo.ProductType == "Combination" || displayData.Code.Contains("&"))
                {
                    if (realData.CodeInfo.Code.Contains(" "))
                    {
                        string[] tempType  = realData.CodeInfo.Code.Split(' ');
                        string[] tempGroup = tempType[1].Split('&');
                        displayData.Code1 = tempGroup[0];
                        displayData.Code2 = tempGroup[1];
                    }
                    else
                    {
                        string[] tempGroup = realData.CodeInfo.Code.Split('&');
                        displayData.Code1 = tempGroup[0];
                        displayData.Code2 = tempGroup[1];
                    }
                }

                displayData.Name          = realData.CodeInfo.Name;
                displayData.ChiCangLiang  = realData.Position;
                displayData.StBuyCount    = realData.BidHand[0];
                displayData.StBuyPrice    = realData.BidPrice[0];
                displayData.StSellCount   = realData.AskHand[0];
                displayData.StSellPrice   = realData.AskPrice[0];
                displayData.DownStopPrice = realData.LowerLimitPrice;
                displayData.I64Sum        = (UInt64)realData.Sum;
                //this.ICurrentSum = realData.ICurrentSum ;
                displayData.ISettlementPrice = realData.SettlmentPrice;
                displayData.PrevSettleMent   = realData.PrevSettlementPrice;
                displayData.UpStopPrice      = realData.UpperLimitPrice;
                displayData.CurrentHand      = realData.Hand;
                //this.I64Outside = realData.I64Outside;
                displayData.IClose = realData.ClosePrice;
                //this.IDealNumber = realData.IDealNumber;
                displayData.IMaxPrice = realData.MaxPrice;
                displayData.IMinPrice = realData.MinPrice;
                displayData.INewPrice = realData.NewPrice;

                displayData.Open = realData.OpenPrice;
                //this.NTime = realData.NTime;
                displayData.PrevClose = realData.PrevClose;
                //this.UsUpdateNumber = realData.UsUpdateNumber;
                displayData.Volumn     = realData.Volumn;
                displayData.Market     = CodeSetManager.CtpToExName(realData.CodeInfo.ExchCode);
                displayData.PreChicang = realData.PrevPosition;
                displayData.PrevClose  = realData.PrevClose;
                displayData.UpdateTime = realData.UpdateTime;
            }
            catch (Exception ex)
            {
                Util.Log_Error("exception: " + ex.Message);
                Util.Log_Error("exception: " + ex.StackTrace);
            }
            return(displayData);
        }