private void DGV1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex <= -1) return;

            if (DGV1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
            {
            DataGridViewCheckBoxCell checkCell =(DataGridViewCheckBoxCell)DGV1.Rows[e.RowIndex].Cells["Enable"];

            Client.Csv_Struct _lotsize = new Csv_Struct();
            if ((bool) checkCell.Value == true)
            {

                 int val = 0;

               // FOPAIRLEG2 v;

                //byte[] buffer = DataPacket.RawSerialize(v=new FOPAIRLEG2()
                //{
                //    //Tok1B_S
                //    PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                //    Token1 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                //    Token2 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                //    Token3 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token3"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token3"].Value),
                //    Token4 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token4"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token4"].Value),
                //    Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token3side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok3B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token4side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok4B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token1Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio1"].Value),
                //    Token2Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio2"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio2"].Value),
                //    Token3Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio3"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio3"].Value),
                //    Token4Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio4"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio4"].Value),

                //    CALCTYPE = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Calc_type"].Value.ToString() == "BaseDiff" ? 2 : 1),

                //});
                 FOPAIR v;
                 byte[] buffer = DataPacket.RawSerialize(v = new FOPAIR()
                 {
                     PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                     TokenFar = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                     Token2Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio1"].Value),
                     Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),

                     TokenNear = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                     Token1Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio2"].Value),
                     Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2)

                 });

                NNFHandler.Instance.Publisher(MessageType.FOPAIR, buffer);

                int _buycount = 0;
                int _sellcount = 0;
                var v2 = Global.Instance.Ratio.Where(a => a.Key == v.TokenNear).Select(b => b.Value).ToList();
                val = Convert.ToInt32(v2.FirstOrDefault().ToString());
                if (Holder._DictLotSize.ContainsKey(v.TokenNear) && v.TokenNear != 0)
                {
                    Holder._DictLotSize[v.TokenNear] = new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.TokenNear).Select(a => a.BoardLotQuantity).First()
                    };
                }
                else if (v.TokenNear != 0)
                {
                    Holder._DictLotSize.TryAdd(v.TokenNear, new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.TokenNear).Select(a => a.BoardLotQuantity).First()
                    }
                    );
                }
                _lotsize = Holder._DictLotSize[v.TokenNear];
                if (Holder._DictLotSize.ContainsKey(v.TokenFar) && v.TokenFar != 0)
                {
                    Holder._DictLotSize[v.TokenFar] = new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.TokenFar).Select(a => a.BoardLotQuantity).First()
                    };
                }
                else if (v.TokenFar != 0)
                {
                    Holder._DictLotSize.TryAdd(v.TokenFar, new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.TokenFar).Select(a => a.BoardLotQuantity).First()
                    }
                    );
                }

                //if (Holder._DictLotSize.ContainsKey(v.Token3) && v.Token3 != 0)
                //{
                //    Holder._DictLotSize[v.Token3] = new Csv_Struct()
                //    {
                //        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.Token3).Select(a => a.BoardLotQuantity).First()
                //    };
                //}
                //else if (v.Token3 != 0)
                //{
                //    Holder._DictLotSize.TryAdd(v.Token3, new Csv_Struct()
                //    {
                //        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.Token3).Select(a => a.BoardLotQuantity).First()
                //    }
                //);
                //}

            }
            else
            {
                DGV1.Rows[e.RowIndex].Cells["PF"].Style.BackColor = Color.Red;
                //FOPAIRLEG2 v;
                //byte[] buffer = DataPacket.RawSerialize(v = new FOPAIRLEG2()
                //{
                //    PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                //    Token1 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                //    Token2 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                //    Token3 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token3"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token3"].Value),
                //    Token4 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token4"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token4"].Value),
                //    Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token3side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok3B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token4side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok4B_S"].Value.ToString() == "Buy" ? 1 : 2),
                //    Token1Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio1"].Value),
                //    Token2Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio2"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio2"].Value),
                //    Token3Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio3"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio3"].Value),
                //    Token4Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio4"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["ratio4"].Value),
                //    CALCTYPE = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Calc_type"].Value.ToString() == "BaseDiff" ? 2 : 1),

                //});
                FOPAIR v;
                byte[] buffer = DataPacket.RawSerialize(v = new FOPAIR()
                {
                    PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                    TokenFar = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                    Token2Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio1"].Value),
                    Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),

                    TokenNear = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                    Token1Ratio = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["ratio2"].Value),
                    Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2)

                });
                NNFHandler.Instance.Publisher(MessageType.FOPAIRUNSUB, buffer);
            }

            }
        }
        //double trp1;
        //double trp11;
        //double trp2;
        //double trp22;
        public void Fillqty_ingrd(int tokenno, int buy_sell,MS_TRADE_CONFIRM_TR obj)
        {
            try
            {

                bool sp;
                string strbuysell = buy_sell == 1 ? "Buy" : "Sell";
                string Token1Side,Token2Side, Token3Side;

                Client.Csv_Struct _lotsize = new Csv_Struct();
                object ob = new object();
                lock (ob)
                {
                   // DataGridViewRow row=null;
                    DataGridViewRow row = DGV1.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["Token1"].Value.ToString().Equals(Convert.ToString(tokenno))).FirstOrDefault();
                    //foreach(DataGridViewRow row1 in DGV1.Rows)
                    //{
                    //    if(row1.Cells["Token1"].Value.ToString()==Convert.ToString(tokenno))
                    //    {
                    //        row = row1;
                    //        break;
                    //    }
                    //}

                    if (row == null)
                    {
                        row = DGV1.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["Token2"].Value.ToString().Equals(Convert.ToString(Convert.ToString(tokenno)))).FirstOrDefault();
                        if (row == null)
                       {
                           row = DGV1.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["Token3"].Value.ToString().Equals(Convert.ToString(Convert.ToString(tokenno)))).FirstOrDefault();
                           if (row == null)
                           {
                               return;
                           }
                           }
            //return;
                    }
                    //var v = Global.Instance.Ratio.Where(a => a.Key == (tokenno)).Select(b => b.Value).ToList();
                    //val = Convert.ToInt32(v.FirstOrDefault().ToString());
                    var pf = Convert.ToInt32(DGV1.Rows[row.Index].Cells["PF"].Value);
                    var val1 = Convert.ToInt32(DGV1.Rows[row.Index].Cells["ratio1"].Value);
                    var val2 = Convert.ToInt32(DGV1.Rows[row.Index].Cells["ratio2"].Value);
                    var val3 = Convert.ToInt32(DGV1.Rows[row.Index].Cells["ratio3"].Value);
                   int near_tok1= Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token1"].Value);
                   int far_tok2 = Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token2"].Value);
                   int _tok3 = Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token3"].Value);
                   //  int buy= Convert.ToInt32(DGV1.Rows[row.Index].Cells["Buy"].Value);
                   //int sell = Convert.ToInt32(DGV1.Rows[row.Index].Cells["Sell"].Value);
                    if (Holder._DictLotSize.ContainsKey(tokenno) == false || tokenno != 0)
                    {
                        Holder._DictLotSize.TryAdd(tokenno, new Csv_Struct()
                        {
                            lotsize = CSV_Class.cimlist.Where(q => q.Token == tokenno).Select(a => a.BoardLotQuantity).First()
                        }
                        );
                    }
                     query1 = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token1"].Value)).Select(a => new SelectListItem
                    {
                        TokenNo = Convert.ToInt32(a.Field<Int32>("Token")),
                        BuyAvg = a.Field<double>("BuyAvg"),
                        SellAvg = a.Field<double>("SellAvg")
                    }).ToList();
                     var foundRow = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token2"].Value)).Count()>0;
                    //if(foundRow != null) {
                    ////You have it ...
                    //     }

                 //   if(Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token2"].Value)))
                    if (foundRow ==true)
                    {
                     query2 = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token2"].Value)).Select(a => new SelectListItem
                    {
                        TokenNo = a.Field<Int32>("Token"),
                        BuyAvg = a.Field<double>("BuyAvg"),
                        SellAvg = a.Field<double>("SellAvg")
                    }).ToList();
                    }

                    var foundRow2 = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token3"].Value)).Count() > 0;
                    //if(foundRow != null) {
                    ////You have it ...
                    //     }

                    //   if(Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token2"].Value)))
                    if (foundRow2 == true)
                    {
                        query3 = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field<Int32>("Token") == Convert.ToInt32(DGV1.Rows[row.Index].Cells["Token3"].Value)).Select(a => new SelectListItem
                        {
                            TokenNo = a.Field<Int32>("Token"),
                            BuyAvg = a.Field<double>("BuyAvg"),
                            SellAvg = a.Field<double>("SellAvg")
                        }).ToList();
                    }

                    _lotsize = Holder._DictLotSize[tokenno];

                    if (near_tok1 == tokenno)
                    {

                         if (strbuysell == Convert.ToString(DGV1.Rows[row.Index].Cells["Tok1B_S"].Value))//create
                        {

                            DGV1.Rows[row.Index].Cells["TRP1"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;
                            sp = true;

                            Token2Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok2B_S"].Value);
                            Token3Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok3B_S"].Value);

                           var ob_o = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), strbuysell.ToUpper()) && (a.Field<string>("Status") == "Traded")).Sum(a => Convert.ToInt32(a.Field<string>("Volume")));
                          // DGV1.Rows[row.Index].Cells["TRDQTY(B)"].Value = Convert.ToInt32(ob_o) / (Convert.ToInt32(_lotsize.lotsize) * (Convert.ToInt32(val1)));

                           double ob1 = Convert.ToDouble(ob_o);
                           double rev_ratio = _lotsize.lotsize * val1;
                           DGV1.Rows[row.Index].Cells["TRDQTY(B)"].Value = Math.Round(ob1 / rev_ratio, 2);

                             //=========================================================================

                          //  int TrdqtyBuy = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), strbuysell.ToUpper()) && (a.Field<string>("Status") == "Traded")).ToList().Count;

                         //   DGV1.Rows[row.Index].Cells["TRDQTY(B)"].Value = TrdqtyBuy;

                             //=======================================================

                            double Result1 = strbuysell == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                            double Result2 = Token2Side == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                             double Result3 =  Token3Side == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                             DGV1.Rows[row.Index].Cells["ATP(B)"].Value =

                           Math.Round(  Result1
                                 +
                                 Result2
                                 +
                                Result3,3);

                        }
                        else //reverse
                        {

                            DGV1.Rows[row.Index].Cells["TRP11"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;

                            var ob_o = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), strbuysell.ToUpper()) && (a.Field<string>("Status") == "Traded")).Sum(a => Convert.ToInt32(a.Field<string>("Volume")));
                            double ob1 = Convert.ToDouble(ob_o);
                            double cr_ratio = _lotsize.lotsize * val2;
                            DGV1.Rows[row.Index].Cells["TRDQTY(S)"].Value = Math.Round( ob1 / cr_ratio,2);
                            // DGV1.Rows[row.Index].Cells["TRDQTY(S)"].Value = Convert.ToInt32(ob_o) / (Convert.ToInt32(_lotsize.lotsize) * (Convert.ToInt32(val2)));

                             //==============================================

                           // int TrdqtySel = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), strbuysell.ToUpper()) && (a.Field<string>("Status") == "Traded")).ToList().Count;

                         //  DGV1.Rows[row.Index].Cells["TRDQTY(S)"].Value = TrdqtySel;
                             //========================================================================

                             Token2Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok2B_S"].Value) =="Buy" ? "Sell" : "Buy";
                             Token3Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok3B_S"].Value)=="Buy" ? "Sell" : "Buy";

                             sp = false;

                            double ResultT1 = strbuysell == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                            double ResultT2 = Token2Side == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                             double ResultT3 = Token3Side == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                             DGV1.Rows[row.Index].Cells["ATP(S)"].Value =
                               Math.Round(  ResultT1

                               +
                               ResultT2
                               +
                               ResultT3,3);

                        }

                        //====== ========  ========  ======  ========= =============  ====== ========= =============  ====== ====== ========  ========  ==== ====  ======= ========== ==  == ==  ==  == ==  ==  ==  ==  ==  ==

                    }
                    if (far_tok2 == tokenno)
                    {
                        if (strbuysell == Convert.ToString(DGV1.Rows[row.Index].Cells["Tok2B_S"].Value))//create
                        {

                         DGV1.Rows[row.Index].Cells["TRP2"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;

                            Token1Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok1B_S"].Value);
                            Token3Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok3B_S"].Value);
                            double res1 = Token1Side == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                            double res2 = strbuysell == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                            double res3 = Token3Side == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                            DGV1.Rows[row.Index].Cells["ATP(B)"].Value =
                              Math.Round(   res1
                               +
                              res2
                               +
                               res3,3);

                            sp = true;

                        }
                        else   //reverse
                        {

                            DGV1.Rows[row.Index].Cells["TRP22"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;

                            Token1Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok1B_S"].Value) == "Buy" ? "Sell" : "Buy";
                            Token3Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok3B_S"].Value) == "Buy" ? "Sell" : "Buy";

                            double resS1 = Token1Side == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                            double resS2 = strbuysell == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                            double resS3 = Token3Side == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                            DGV1.Rows[row.Index].Cells["ATP(S)"].Value =
             Math.Round(  resS1

             +
             resS2
             +

             resS3,3);
                            sp = false;

                        }

                        if (Convert.ToString(DGV1.Rows[row.Index].Cells["Strategy_Type"].Value) == "2_LEG")
                        {

                            if (Global.Instance.TradeTrac_dict.ContainsKey(IPAddress.HostToNetworkOrder(obj.Token)))
                            {

                                double trp1 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP1"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP1"].Value) * Convert.ToInt32(val1);
                                double trp11 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP11"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP11"].Value) * Convert.ToInt32(val1);
                                double trp2 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP2"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP2"].Value) * Convert.ToInt32(val2);
                                double trp22 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP22"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP22"].Value) * Convert.ToInt32(val2);

                                trd_obj = new TradeTrac();
                              //  var a = trp1 + trp2;

                           // double    v =v + a;
                                var creat = trp1 + trp2 ;
                               var Rev = trp11 + trp22;
                               int TrdqtyBuy = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), "BUY") && (a.Field<string>("Status") == "Traded")).ToList().Count;

                               int TrdqtySell = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), "SELL") && (a.Field<string>("Status") == "Traded")).ToList().Count;

                                trd_obj = Global.Instance.TradeTrac_dict[near_tok1];
                                trd_obj.ACTUALPRICE = sp == true ? creat : Rev;
                                trd_obj.B_S = IPAddress.HostToNetworkOrder(obj.Buy_SellIndicator);
                                trd_obj.QTy = IPAddress.HostToNetworkOrder(obj.FillQuantity);

                                trd_obj.SYMBOL = Encoding.ASCII.GetString(obj.Contr_dec_tr_Obj.Symbol);
                                trd_obj.TIME = LogicClass.ConvertFromTimestamp(IPAddress.HostToNetworkOrder(obj.LogTime)).ToString("HH:mm:ss.fff");

                                Global.Instance.TradeTrac_dict.AddOrUpdate(IPAddress.HostToNetworkOrder(obj.Token), trd_obj, (k, v1) => trd_obj);

                                //DataRow[] dr = Global.Instance.TradeTracker.Select("PF_ID  = '" + Convert.ToInt32(trd_obj.PF_ID) + "'");
                                DataRow[] dr = Global.Instance.TradeTracker.Select("Unique_id  = '" + ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() +  (IPAddress.HostToNetworkOrder(obj.Token)).ToString() +"'");

                                if (dr.Length > 0)
                                {
                                    dr[0]["PF_ID"] = Convert.ToString(trd_obj.PF_ID);
                                   // dr[0]["B/S"] = trd_obj.B_S == 1 ? "BUY" : "SELL";
                                    dr[0]["B/S"] = sp == true ? "BUY" : "SELL";
                                    //dr[0]["QTY"] = Convert.ToString(trd_obj.QTy);
                                   // dr[0]["QTY"] = trd_obj.B_S == 1 ? Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRDQTY(B)"].Value) : Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRDQTY(S)"].Value);
                                    double vo = Convert.ToDouble(IPAddress.HostToNetworkOrder(obj.FillQuantity));
                                    double createratio = Convert.ToDouble(_lotsize.lotsize * val2);
                                    dr[0]["QTY"] = vo / createratio >= Convert.ToDouble(1.0) ? "Complete" : "Partial";
                                    dr[0]["QTY"] = dr[0]["QTY"] + " \t" + Convert.ToString(Math.Round(vo / createratio, 2)); //"1";
                                  //  dr[0]["QTY"] = trd_obj.B_S == 1 ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                   // dr[0]["QTY"] =Convert.ToDecimal(IPAddress.HostToNetworkOrder(obj.FillQuantity)/ (Convert.ToInt32(_lotsize.lotsize) * (Convert.ToInt32(val2)))); //"1";
                                  //  dr[0]["QTY"] = sp == true ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                    dr[0]["ACTUALPRICE"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.ACTUALPRICE), 3));

                                    //dr[0]["GIVENPRICEBUY"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3));
                                    //dr[0]["GIVENPRICESELL"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));

                                    dr[0]["GIVENPRICEBUY"] = sp == true ? Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3)) : "0";
                                    dr[0]["GIVENPRICESELL"] = sp == true ? "0" : Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));

                                    dr[0]["SYMBOL"] = Convert.ToString(trd_obj.SYMBOL);
                                    dr[0]["TIME"] = Convert.ToString(trd_obj.TIME);
                                    dr[0]["Unique_id"] = ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() + (IPAddress.HostToNetworkOrder(obj.Token)).ToString();

                                }
                                else
                                {

                                    BeginInvoke((Action)delegate
                                    {
                                        DataRow dr2 = Global.Instance.TradeTracker.NewRow();
                                        //  dr2["Sno2"] = trd_ob.ToString();

                                        dr2["PF_ID"] = Convert.ToString(trd_obj.PF_ID);
                                        //dr2["B/S"] = trd_obj.B_S == 1 ? "BUY" : "SELL";
                                        dr2["B/S"] = sp == true ? "BUY" : "SELL";

                                        // dr2["GIVENPRICEBUY"] = sp == true ? "BUY" : "SELL";
                                        //  dr2["QTY"] = Convert.ToString(trd_obj.QTy);
                                        //  dr2["QTY"] = trd_obj.B_S == 1 ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                        //dr2["QTY"] = sp == true ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);

                                        double vo = Convert.ToDouble(IPAddress.HostToNetworkOrder(obj.FillQuantity));
                                        double createratio = Convert.ToDouble(_lotsize.lotsize * val2);
                                        dr2["QTY"] = vo / createratio >= Convert.ToDouble(1.0) ? "Complete" : "Partial";
                                        dr2["QTY"] = dr2["QTY"] + " \t" + Convert.ToString(Math.Round(vo / createratio, 2)); //"1";
                                        dr2["ACTUALPRICE"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.ACTUALPRICE), 3));

                                        dr2["GIVENPRICEBUY"] = sp == true ? Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3)) : "0";
                                        dr2["GIVENPRICESELL"] = sp == true ? "0" : Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));

                                        dr2["SYMBOL"] = Convert.ToString(trd_obj.SYMBOL);
                                        dr2["TIME"] = Convert.ToString(trd_obj.TIME);
                                        dr2["Unique_id"] = ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() + (IPAddress.HostToNetworkOrder(obj.Token)).ToString();
                                        Global.Instance.TradeTracker.Rows.Add(dr2);
                                    });
                                        //Trade_Tracker.Instance.DGV.ScrollBars = ScrollBars.Vertical;
                                }
            //================== ================= =============== ========== ========== ====== ==== ===== ===== ===== =======  ====== ============== ==== ==      == == ==  == ==  == == ==  ==

                                double WTC_txt1 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["W_T_C"].Value);
                                if(sp == true)  // crea
                                {
                                    if ((Math.Abs((trd_obj.Given_Price_Buy)) + Math.Abs(WTC_txt1)) < Math.Abs(trd_obj.ACTUALPRICE))
                                    {
                                        DGV1.Rows[row.Index].Cells["WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) + 1;
                                    }

                                    DGV1.Rows[row.Index].Cells["_con_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) + 1;

                                    if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == Convert.ToInt32(WTC_txt.Text) && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))
                                      //  if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == 5 && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))

                                    {
                                        DGV1.Rows[row.Index].Cells["WTC"].Value = 0;
                                        DGV1.Rows[row.Index].Cells["_con_WTC"].Value = 0;
                                      //  DGV1.Rows[row.Index].Cells["Enable"].Value = false;
                                    }
                                    if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) != Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value))
                                    {
                                        DGV1.Rows[row.Index].Cells["WTC"].Value = 0;
                                        DGV1.Rows[row.Index].Cells["_con_WTC"].Value = 0;
                                    }

                                // \\\\ ///

                                }
                                else   // rev
                                {

                                    if ((Math.Abs((trd_obj.Given_Price_Sell)) - Math.Abs(WTC_txt1)) > Math.Abs(trd_obj.ACTUALPRICE))
                                    {
                                        DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) + 1;
                                    }
                                  //  var _T_v = (IPAddress.HostToNetworkOrder(obj.FillPrice) / 100) + trd_obj.ACTUALPRICE;
                                    DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value) + 1;
                                    if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) == Convert.ToInt32(WTC_txt.Text) && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value))
                                      //  if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == 5 && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))

                                    {
                                        DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = 0;
                                        DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = 0;
                                      // DGV1.Rows[row.Index].Cells["Enable"].Value = false;
                                    }

                                    if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) != Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value))
                                    {
                                        DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = 0;
                                        DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = 0;
                                    }

                                }

                                //===============================================================================================================

                            }

                        }
                    }

                    //=================================
                    if (_tok3 == tokenno)
                    {

                        if (strbuysell == Convert.ToString(DGV1.Rows[row.Index].Cells["Tok3B_S"].Value))//create
                        {
                            DGV1.Rows[row.Index].Cells["TRP3"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;

                       //  var t   = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;
                        // DGV1.Rows[row.Index].Cells["TRP3"].Value = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP3"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP3"].Value) + t;

                            Token1Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok1B_S"].Value);
                            Token2Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok2B_S"].Value);
                            double res1 = Token1Side == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                                 double res2 =  Token2Side == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                                      double res3 =   strbuysell == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                                      DGV1.Rows[row.Index].Cells["ATP(B)"].Value =
                                         Math.Round(  res1

                                         +
                                        res2
                                         +
                                       res3,3);

                                      sp = true;

                        }
                        else //reverse
                        {
                        //  var t = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;
                        //  DGV1.Rows[row.Index].Cells["TRP33"].Value = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP33"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP33"].Value) + t;
                          DGV1.Rows[row.Index].Cells["TRP33"].Value = strbuysell == "Buy" ? -IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00 : IPAddress.HostToNetworkOrder(obj.FillPrice) / 100.00;
                            // DGV1.Rows[row.Index].Cells["ATP(S)"].Value = query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1) - (query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2) - (query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3);
                            Token1Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok1B_S"].Value) == "Buy" ?"Sell" : "Buy";
                            Token2Side = Convert.ToString(DGV1.Rows[row.Index].Cells["Tok2B_S"].Value) == "Buy" ? "Sell" : "Buy";

                            double resS1 = Token1Side == "Buy" ? -(query1.FirstOrDefault().BuyAvg * Convert.ToInt32(val1)) : (query1.FirstOrDefault().SellAvg * Convert.ToInt32(val1));
                                double resS2 =  Token2Side == "Buy" ? -((query2 == null ? 0 : query2.FirstOrDefault().BuyAvg) * Convert.ToInt32(val2)) : ((query2 == null ? 0 : query2.FirstOrDefault().SellAvg) * Convert.ToInt32(val2));
                                    double resS3 = strbuysell == "Buy" ? -((query3 == null ? 0 : query3.FirstOrDefault().BuyAvg) * Convert.ToInt32(val3)) : ((query3 == null ? 0 : query3.FirstOrDefault().SellAvg) * Convert.ToInt32(val3));

                                    DGV1.Rows[row.Index].Cells["ATP(S)"].Value =
                                        Math.Round( resS1

                                       +
                                      resS2
                                       +
                                       resS3,3);

                                    sp = false;

                        }

                        if (Convert.ToString(DGV1.Rows[row.Index].Cells["Strategy_Type"].Value) == "3_LEG")
                        {
                        if (Global.Instance.TradeTrac_dict.ContainsKey(IPAddress.HostToNetworkOrder(obj.Token)))
                        {
                           // double trp13 = Token1Side == "Buy" ? -Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP1"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP1"].Value) : Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP1"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP1"].Value);
                          //  double trp23 = Token2Side == "Buy" ? -Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP2"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP2"].Value) : Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP2"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP2"].Value);
                         //   double trp33 = strbuysell == "Buy" ? -Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP3"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP3"].Value) : Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP3"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP3"].Value);

                            double trp1 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP1"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP1"].Value) * Convert.ToInt32(val1);
                            double trp11 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP11"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP11"].Value) * Convert.ToInt32(val1);
                            double trp2 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP2"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP2"].Value) * Convert.ToInt32(val2);
                            double trp22 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP22"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP22"].Value) * Convert.ToInt32(val2);

                            double trp3 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP3"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP3"].Value) * Convert.ToInt32(val3);
                            double trp33 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["TRP33"].Value == DBNull.Value ? 0 : DGV1.Rows[row.Index].Cells["TRP33"].Value) * Convert.ToInt32(val3);

                            int TrdqtyBuy = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), "BUY") && (a.Field<string>("Status") == "Traded")).ToList().Count;

                            int TrdqtySell = Global.Instance.OrdetTable.AsEnumerable().Where(a => (Convert.ToInt32(a.Field<string>("TokenNo")) == tokenno) && String.Equals(a.Field<string>("Buy_SellIndicator"), "SELL") && (a.Field<string>("Status") == "Traded")).ToList().Count;

                            trd_obj = new TradeTrac();
                            var crea = trp1 + trp2 + trp3;
                            var rev = trp11 + trp22 + trp33;

                            trd_obj = Global.Instance.TradeTrac_dict[near_tok1];
                            trd_obj.ACTUALPRICE = sp == true ? crea : rev;
                            trd_obj.B_S = IPAddress.HostToNetworkOrder(obj.Buy_SellIndicator);
                            trd_obj.QTy = IPAddress.HostToNetworkOrder(obj.FillQuantity);
                            trd_obj.SYMBOL = Encoding.ASCII.GetString(obj.Contr_dec_tr_Obj.Symbol);
                            trd_obj.TIME = LogicClass.ConvertFromTimestamp(IPAddress.HostToNetworkOrder(obj.LogTime)).ToString("HH:mm:ss.fff");

                            Global.Instance.TradeTrac_dict.AddOrUpdate(IPAddress.HostToNetworkOrder(obj.Token), trd_obj, (k, v1) => trd_obj);

                            DataRow[] dr = Global.Instance.TradeTracker.Select("Unique_id  = '" + ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() + (IPAddress.HostToNetworkOrder(obj.Token)).ToString() + "'");

                            if (dr.Length > 0)
                            {
                                dr[0]["PF_ID"] = Convert.ToString(trd_obj.PF_ID);
                                //dr[0]["B/S"] = trd_obj.B_S == 1 ? "BUY" : "SELL";
                                dr[0]["B/S"] = sp == true ? "BUY" : "SELL";

                              //  dr[0]["QTY"] = Convert.ToString(trd_obj.QTy);
                                double vo = Convert.ToDouble(IPAddress.HostToNetworkOrder(obj.FillQuantity));
                                double createratio = Convert.ToDouble(_lotsize.lotsize * val3);
                                dr[0]["QTY"] = vo / createratio >= Convert.ToDouble(1.0) ? "Complete" : "Partial";
                                dr[0]["QTY"] = dr[0]["QTY"] + " \t" + Convert.ToString(Math.Round(vo / createratio, 2)); //"1";

                               // dr[0]["QTY"]  = trd_obj.B_S == 1 ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                               // dr[0]["QTY"] = Convert.ToDecimal(IPAddress.HostToNetworkOrder(obj.OriginalVolume) / (Convert.ToInt32(_lotsize.lotsize) * (Convert.ToInt32(val3))));
                              //  dr[0]["QTY"] = sp == true ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                dr[0]["ACTUALPRICE"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.ACTUALPRICE), 3));

                                dr[0]["GIVENPRICEBUY"] = sp == true ? Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3)) : "0";
                                dr[0]["GIVENPRICESELL"] = sp == true ? "0" : Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));

                                dr[0]["SYMBOL"] = Convert.ToString(trd_obj.SYMBOL);
                                dr[0]["TIME"] = Convert.ToString(trd_obj.TIME);
                                dr[0]["Unique_id"] = ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() + (IPAddress.HostToNetworkOrder(obj.Token)).ToString();
                              // Trade_Tracker.Instance.DGV.Refresh();

                            }
                            else
                            {
                                BeginInvoke((Action)delegate
                                    {
                                        DataRow dr2 = Global.Instance.TradeTracker.NewRow();
                                        //  dr2["Sno2"] = trd_ob.ToString();
                                        dr2["PF_ID"] = Convert.ToString(trd_obj.PF_ID);
                                        //  dr2["B/S"] = trd_obj.B_S == 1 ? "BUY" : "SELL";
                                        dr2["B/S"] = sp == true ? "BUY" : "SELL";
                                        //  dr2["QTY"] = Convert.ToString(trd_obj.QTy);
                                        double vo = Convert.ToDouble(IPAddress.HostToNetworkOrder(obj.FillQuantity));
                                        double createratio = Convert.ToDouble(_lotsize.lotsize * val3);
                                        dr2["QTY"] = vo / createratio >= Convert.ToDouble(1.0) ? "Complete" : "Partial";
                                        dr2["QTY"] = dr2["QTY"] + " \t" + Convert.ToString(Math.Round(vo / createratio, 2)); //"1";

                                        // dr2["QTY"] = trd_obj.B_S == 1 ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                        // dr[0]["QTY"] =Convert.ToDecimal(IPAddress.HostToNetworkOrder(obj.OriginalVolume) / (Convert.ToInt32(_lotsize.lotsize) * (Convert.ToInt32(val3))));                                //dr2["QTY"] = sp == true ? Convert.ToDouble(TrdqtyBuy) : Convert.ToDouble(TrdqtySell);
                                        dr2["ACTUALPRICE"] = Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.ACTUALPRICE), 3));

                                        // dr2["GIVENPRICEBUY"] = trd_obj.B_S == 1 ? Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3)) : "0";
                                        // dr2["GIVENPRICESELL"] = trd_obj.B_S == 1 ? "0" : Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));

                                        dr2["GIVENPRICEBUY"] = sp == true ? Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Buy), 3)) : "0";
                                        dr2["GIVENPRICESELL"] = sp == true ? "0" : Convert.ToString(Math.Round(Convert.ToDouble(trd_obj.Given_Price_Sell), 3));
                                        dr2["SYMBOL"] = Convert.ToString(trd_obj.SYMBOL);
                                        dr2["TIME"] = Convert.ToString(trd_obj.TIME);
                                        dr2["Unique_id"] = ((long)LogicClass.DoubleEndianChange((obj.ResponseOrderNumber))).ToString() + (IPAddress.HostToNetworkOrder(obj.Token)).ToString();
                                        Global.Instance.TradeTracker.Rows.Add(dr2);
                                        //Trade_Tracker.Instance.DGV.Refresh();

                                    });
                            }

                            //================================================================================================================================

                            double WTC_txt1 = Convert.ToDouble(DGV1.Rows[row.Index].Cells["W_T_C"].Value);
                            if (sp == true)  // crea
                            {
                                if ((Math.Abs((trd_obj.Given_Price_Buy)) + Math.Abs(WTC_txt1)) < Math.Abs(trd_obj.ACTUALPRICE))
                                {
                                    DGV1.Rows[row.Index].Cells["WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) + 1;
                                }

                                DGV1.Rows[row.Index].Cells["_con_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) + 1;

                                if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == Convert.ToInt32(WTC_txt.Text) && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))
                                   // if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == 5 && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))

                                {
                                    DGV1.Rows[row.Index].Cells["WTC"].Value = 0;
                                    DGV1.Rows[row.Index].Cells["_con_WTC"].Value = 0;
                                  //  DGV1.Rows[row.Index].Cells["Enable"].Value = false;
                                }
                                if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) != Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value))
                                {
                                    DGV1.Rows[row.Index].Cells["WTC"].Value = 0;
                                    DGV1.Rows[row.Index].Cells["_con_WTC"].Value = 0;
                                }

                            }
                            else   // rev
                            {

                                if ((Math.Abs((trd_obj.Given_Price_Sell)) - Math.Abs(WTC_txt1)) > Math.Abs(trd_obj.ACTUALPRICE))
                                {
                                    DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) + 1;
                                }
                              //  var _T_v = (IPAddress.HostToNetworkOrder(obj.FillPrice) / 100) + trd_obj.ACTUALPRICE;
                                DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value) + 1;

                                if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) == Convert.ToInt32(WTC_txt.Text) && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value))
                                //    if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value) == 5 && Convert.ToInt32(DGV1.Rows[row.Index].Cells["_con_WTC"].Value) == Convert.ToInt32(DGV1.Rows[row.Index].Cells["WTC"].Value))

                                {
                                    DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = 0;
                                    DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = 0;
                                //  DGV1.Rows[row.Index].Cells["Enable"].Value = false;
                                }

                                if (Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_WTC"].Value) != Convert.ToInt32(DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value))
                                {
                                    DGV1.Rows[row.Index].Cells["_sell_WTC"].Value = 0;
                                    DGV1.Rows[row.Index].Cells["_sell_con_WTC"].Value = 0;
                                }

                            }

                            //=====   ===  ==== ===  === ====  === ===  == === ====  =====  == === ===  ===== ====  ===== =====  ==== ==== =====  ====   === === == === ===== ==== ===== ====

                            }
                        }

                    }

                }

            }
            catch (Exception ex)
            {
                Client.LogWriterClass.logwritercls.logs("ErrorValue_check", "Value Check update in gridview" + ex.Message);

            }
        }
        private void DGV1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex <= -1) return;

            if (DGV1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
            {
            DataGridViewCheckBoxCell checkCell =(DataGridViewCheckBoxCell)DGV1.Rows[e.RowIndex].Cells["Enable"];

            if ((bool) checkCell.Value == true)
            {

                  Client.Csv_Struct _lotsize = new Csv_Struct();
                FOPAIRLEG2 v;
                byte[] buffer = DataPacket.RawSerialize(v=new FOPAIRLEG2()
                {
                    //Tok1B_S
                    PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                    Token1 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                    Token2 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                    Token3 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token3"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token3"].Value),
                   Token4 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token4"].Value== DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token4"].Value),
                    Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token3side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok3B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token4side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok4B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    CALCTYPE = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Calc_type"].Value.ToString() == "BaseDiff" ? 2 : 1),
                });
                NNFHandler.Instance.Publisher(MessageType.IOCPAIR, buffer);

                int _buycount = 0;
                int _sellcount = 0;

                DataRow[] drr = Global.Instance.OrdetTable.Select("status='Traded'  and TokenNo='" + v.Token1 + "'  ");
                if (drr.Length > 0)
                {
                    var _v = drr.AsEnumerable().Where(a => a.Field<string>("Buy_SellIndicator") == "BUY").ToList();
                    _buycount = _v.Count();
                    var _v1 = drr.AsEnumerable().Where(a => a.Field<string>("Buy_SellIndicator") == "SELL").ToList();
                    _sellcount = _v1.Count();

                }

                //DataRow[] drr1 = Global.Instance.OrdetTable.Select("status='Traded'  and TokenNo='" + v.Token2 + "'  ");
                //if (drr1.Length > 0)
                //{
                //    var _v = drr1.Where(a => a.Field<string>("Buy_SellIndicator") == "BUY").ToList();
                //    _buycount = _buycount+ _v.Count();
                //    var _v1 = drr1.Where(a => a.Field<string>("Buy_SellIndicator") == "SELL").ToList();
                //    _sellcount =_sellcount+ _v1.Count();

                //}
                DGV1.Rows[e.RowIndex].Cells["TRDQTY(B)"].Value = _buycount;

                DGV1.Rows[e.RowIndex].Cells["TRDQTY(S)"].Value = _sellcount;

                //_lotsize.lotsize = CSV_Class.cimlist.Where(q => q.Token ==v.Token1 ).Select(a => a.BoardLotQuantity).First();
                if (Holder._DictLotSize.ContainsKey(v.Token1) == false  && v.Token1 != 0 )
                {
                Holder._DictLotSize.TryAdd(v.Token1, new Csv_Struct()
                {
                    lotsize=CSV_Class.cimlist.Where(q => q.Token ==v.Token1 ).Select(a => a.BoardLotQuantity).First()
                }
                );
                }

                if (Holder._DictLotSize.ContainsKey(v.Token2) == false && v.Token2 != 0)
                {
                    Holder._DictLotSize.TryAdd(v.Token2, new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.Token2).Select(a => a.BoardLotQuantity).First()
                    }
                    );
                }

                if (Holder._DictLotSize.ContainsKey(v.Token3) == false && v.Token3 != 0)
                {
                    Holder._DictLotSize.TryAdd(v.Token3, new Csv_Struct()
                    {
                        lotsize = CSV_Class.cimlist.Where(q => q.Token == v.Token3).Select(a => a.BoardLotQuantity).First()
                    }
                    );
                }

            }
            else
            {
                FOPAIRLEG2 v;
                byte[] buffer = DataPacket.RawSerialize(v=new FOPAIRLEG2()
                {
                    PORTFOLIONAME = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["PF"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["PF"].Value),
                    Token1 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token1"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token1"].Value),
                    Token2 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token2"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token2"].Value),
                    Token3 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token3"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token3"].Value),
                    Token4 = Convert.ToInt32(DGV1.Rows[e.RowIndex].Cells["Token4"].Value == DBNull.Value ? "0" : DGV1.Rows[e.RowIndex].Cells["Token4"].Value),
                    Token1side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok1B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token2side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok2B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token3side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok3B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    Token4side = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Tok4B_S"].Value.ToString() == "Buy" ? 1 : 2),
                    CALCTYPE = Convert.ToInt16(DGV1.Rows[e.RowIndex].Cells["Calc_type"].Value.ToString() == "BaseDiff" ? 2 : 1),
                });
                NNFHandler.Instance.Publisher(MessageType.IOCPAIRUNSUB, buffer);

                if (Holder._DictLotSize.ContainsKey(v.Token1) == false || v.Token1 != 0)
                {
                    Csv_Struct o = new Csv_Struct();
                    Holder._DictLotSize.TryRemove(v.Token1, out o);

                }

                if (Holder._DictLotSize.ContainsKey(v.Token2) == false || v.Token2 != 0)
                {
                    Csv_Struct o = new Csv_Struct();
                    Holder._DictLotSize.TryRemove(v.Token2, out o);
                }
            }

            }
        }