public void netposion(int tok, int lotsize, int Tlot, double b_s, double rate) { try { int t_1 = 0; query1 = Global.Instance.OrdetTable.AsEnumerable().Where(p => p.Field <string>("Status") == orderStatus.Traded.ToString() && Convert.ToInt32(p.Field <string>("TokenNo")) == tok).GroupBy(r => Convert.ToInt32(r.Field <string>("TokenNo"))) .Select(store => new SelectListItem { TredingSymbol = System.Text.ASCIIEncoding.ASCII.GetString(csv.CSV_Class.cimlist.First(tkn => tkn.Token == Convert.ToInt32(store.Key)).Name), TokenNo = Convert.ToInt32(store.Key), // InstrumentName = store.First().Field<string>("InstrumentName"), BuyQty = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))), BuyAvg = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => ((Convert.ToInt32(p.Field <string>("Volume"))) * Convert.ToDouble(p.Field <string>("FillPrice")))) / ((store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))) == 0 ? 1 : (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p8 => Convert.ToInt32(p8.Field <string>("Volume")))))), SellQty = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))), SellAvg = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => (Convert.ToInt32(p.Field <string>("Volume"))) * Convert.ToDouble(p.Field <string>("FillPrice"))) / ((store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))) == 0 ? 1 : (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p8 => Convert.ToInt32(p8.Field <string>("Volume")))))), NetQty = ((store.Where(a1 => a1.Field <String>("Buy_SellIndicator") == "BUY").Sum(p1 => Convert.ToInt32(p1.Field <string>("Volume"))))) - ((store.Where(a2 => a2.Field <String>("Buy_SellIndicator") == "SELL").Sum(p2 => Convert.ToInt32(p2.Field <string>("Volume"))))), }).ToList(); //if(b_s==1) //{ // query1.FirstOrDefault().buy_rate = rate; //} //else //{ // query1.FirstOrDefault().Sell_rate = rate; //} var TempList = csv.CSV_Class.cimlist.Where(a => a.Token == tok).ToList(); var checktok = TempList.First().EGMAGM; if (Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field <string>("TredingSymbol") == checktok).Count() > 0) { //var rows = Global.Instance.NetBookTable.AsEnumerable().ToList(); //var fg = (rows.FindIndex(a => Convert.ToString(a.Field<string>("Symbol")) == "Total")); //if (fg >= 0) //{ // Global.Instance.NetBookTable.Rows[fg].Delete(); //} var rowlist = Global.Instance.NetBookTable.AsEnumerable().Where(p => p.Field <string>("TredingSymbol") == checktok).ToList(); try { foreach (var i in rowlist) { i["Token"] = query1.FirstOrDefault().TokenNo; i["TredingSymbol"] = query1.FirstOrDefault().TredingSymbol; i["BuyQty"] = query1.FirstOrDefault().BuyQty; i["BuyAvg"] = query1.FirstOrDefault().BuyAvg; i["SellQty"] = -query1.FirstOrDefault().SellQty; i["SellAvg"] = query1.FirstOrDefault().SellAvg; //========================================================================== // i["NetQty"] = query1.FirstOrDefault().BuyQty + -query1.FirstOrDefault().SellQty; // i["NetValue"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)), 3) + Math.Round(Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty)), 3); //------------------------------------------------------------------------------ i["InstrumentName"] = TempList.FirstOrDefault().InstrumentName; i["Symbol"] = TempList.FirstOrDefault().Symbol; i["OptionType"] = TempList.FirstOrDefault().OptionType; i["StrikePrice"] = Convert.ToDouble(TempList.FirstOrDefault().StrikePrice / 100); i["ExpiryDate"] = LogicClass.ConvertFromTimestamp(TempList.FirstOrDefault().ExpiryDate); i["LTP"] = Global.Instance.ltp[tok].LTP; i["Delta"] = Math.Round(Convert.ToDouble(Global.Instance.ltp[tok].Delta), 3); i["NetQtyDelta"] = Math.Round(Convert.ToDouble((query1.FirstOrDefault().NetQty) * (Global.Instance.ltp[tok].Delta)), 3); // i["BUYRATE"] = Math.Round(Convert.ToDouble(query1.FirstOrDefault().buy_rate),3); // i["SELLRATE"] = Math.Round(Convert.ToDouble(query1.FirstOrDefault().Sell_rate),3); i["BUYVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty)), 3); i["SELLVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)), 3); i["MTM"] = Math.Round((Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)) + Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty))) - ((Global.Instance.ltp[tok].LTP) * (-query1.FirstOrDefault().BuyQty + -query1.FirstOrDefault().SellQty)), 3); // //========================================= // Global.Instance.NetBookTable.Rows.Add("Total", "-", Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("StrikePrice")), "-", "-", Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("BuyQty")), //Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BuyAvg")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BUYVALUE")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("SellQty")), // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SellAvg")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SELLVALUE")), // 0, 0, // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("MTM")), // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("LTP")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("Delta")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("NetQtyDelta")), 0, 0); // //============= } } catch (Exception Ex) { MessageBox.Show(" Exception " + Ex.StackTrace.ToString()); } } else { //================================== === == == == == == == var rows = Global.Instance.NetBookTable.AsEnumerable().ToList(); //var fg = (rows.FindIndex(a => Convert.ToString(a.Field<string>("Symbol")) == "Total")); //if (fg >= 0) //{ // Global.Instance.NetBookTable.Rows[fg].Delete(); //} DataRow i = Global.Instance.NetBookTable.NewRow(); i["Token"] = query1.FirstOrDefault().TokenNo; i["TredingSymbol"] = query1.FirstOrDefault().TredingSymbol; i["BuyQty"] = query1.FirstOrDefault().BuyQty; i["BuyAvg"] = query1.FirstOrDefault().BuyAvg; i["SellQty"] = -query1.FirstOrDefault().SellQty; i["SellAvg"] = query1.FirstOrDefault().SellAvg; i["LTP"] = Global.Instance.ltp[tok].LTP; i["Delta"] = Math.Round(Convert.ToDouble(Global.Instance.ltp[tok].Delta), 3); i["NetQtyDelta"] = Math.Round(Convert.ToDouble((Convert.ToDouble(query1.FirstOrDefault().NetQty) * (Global.Instance.ltp[tok].Delta))), 3); i["InstrumentName"] = TempList.FirstOrDefault().InstrumentName; i["Symbol"] = TempList.FirstOrDefault().Symbol; i["OptionType"] = TempList.FirstOrDefault().OptionType; i["StrikePrice"] = Convert.ToDouble(TempList.FirstOrDefault().StrikePrice / 100); i["ExpiryDate"] = LogicClass.ConvertFromTimestamp(TempList.FirstOrDefault().ExpiryDate); i["BUYVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty)), 3); i["SELLVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)), 3); i["MTM"] = Math.Round((Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)) + Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty))) - ((Global.Instance.ltp[tok].LTP) * (-query1.FirstOrDefault().BuyQty + -query1.FirstOrDefault().SellQty)), 3); // i["MTM"] = Math.Round(Convert.ToDouble( (query1.FirstOrDefault().NetValue) - ((Global.Instance.ltp[tok].LTP) * (-query1.FirstOrDefault().NetQty))),3); Global.Instance.NetBookTable.Rows.Add(i); //Global.Instance.NetBookTable.Rows.Add("Total", "-", Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("StrikePrice")), "-", "-", Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("BuyQty")), // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BuyAvg")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BUYVALUE")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("SellQty")), // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SellAvg")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SELLVALUE")), // 0, 0, // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("MTM")), // Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("LTP")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("Delta")), Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("NetQtyDelta")), 0, 0); } //DataRow ii = Global.Instance.NetBookTable.NewRow(); //ii["InstrumentName"] = "Total"; //Global.Instance.NetBookTable.Rows.Add(ii); DGV.DataSource = Global.Instance.NetBookTable; } catch (Exception ex) { Task.Factory.StartNew(() => LogWriterClass.logwritercls.logs("tradeerror", ex.StackTrace.ToString())); } if (this.InvokeRequired) { MethodInvoker del = delegate { DGV.Refresh(); }; this.Invoke(del); return; } }
public void netposion2(int tok, int lotsize, int Tlot, double b_s, double rate) { try { int t_1 = 0; query1 = Global.Instance.OrdetTable.AsEnumerable().Where(p => p.Field <string>("Status") == orderStatus.Traded.ToString()).GroupBy(r => Convert.ToInt32(r.Field <string>("TokenNo"))) .Select(store => new SelectListItem { TredingSymbol = System.Text.ASCIIEncoding.ASCII.GetString(csv.CSV_Class.cimlist.First(tkn => tkn.Token == Convert.ToInt32(store.Key)).Name), TokenNo = Convert.ToInt32(store.Key), BuyQty = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))), BuyAvg = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => ((Convert.ToInt32(p.Field <string>("Volume"))) * Convert.ToDouble(p.Field <string>("FillPrice")))) / ((store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))) == 0 ? 1 : (store.Where(a => a.Field <String>("Buy_SellIndicator") == "BUY").Sum(p8 => Convert.ToInt32(p8.Field <string>("Volume")))))), SellQty = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))), SellAvg = (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => (Convert.ToInt32(p.Field <string>("Volume"))) * Convert.ToDouble(p.Field <string>("FillPrice"))) / ((store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p => Convert.ToInt32(p.Field <string>("Volume")))) == 0 ? 1 : (store.Where(a => a.Field <String>("Buy_SellIndicator") == "SELL").Sum(p8 => Convert.ToInt32(p8.Field <string>("Volume")))))), NetQty = ((store.Where(a1 => a1.Field <String>("Buy_SellIndicator") == "BUY").Sum(p1 => Convert.ToInt32(p1.Field <string>("Volume"))))) - ((store.Where(a2 => a2.Field <String>("Buy_SellIndicator") == "SELL").Sum(p2 => Convert.ToInt32(p2.Field <string>("Volume"))))), }).ToList(); if (b_s == 1) { query1.FirstOrDefault().buy_rate = rate; } else { query1.FirstOrDefault().Sell_rate = rate; } //================================== === == == == == == == == == == == for (int j = 0; j < query1.Count(); j++) { var TempList = csv.CSV_Class.cimlist.Where(a => a.Token == query1[j].TokenNo).ToList(); var checktok = TempList.First().EGMAGM; DataRow i = Global.Instance.NetBookTable.NewRow(); i["Token"] = query1[j].TokenNo; i["TredingSymbol"] = query1[j].TredingSymbol; i["BuyQty"] = query1[j].BuyQty; i["BuyAvg"] = query1[j].BuyAvg; i["SellQty"] = query1[j].SellQty; i["SellAvg"] = query1[j].SellAvg; i["NetQty"] = query1[j].BuyAvg - query1[j].SellQty; // i["NetQty"] = query1[j].NetQty; // i["NetQty"] = query1[j].NetQty; // i["BEP"] = query1[j].BEP; // i["NetValue"] = query1[j].NetValue; i["NetValue"] = Math.Round(Convert.ToDouble((query1[j].Sell_rate) * (query1[j].SellQty)), 3) + Math.Round(Convert.ToDouble((query1[j].buy_rate) * (query1[j].BuyQty)), 3); i["InstrumentName"] = TempList.FirstOrDefault().InstrumentName; i["Symbol"] = TempList.FirstOrDefault().Symbol; i["OptionType"] = TempList.FirstOrDefault().OptionType; i["StrikePrice"] = Convert.ToDouble(TempList.FirstOrDefault().StrikePrice / 100); i["ExpiryDate"] = LogicClass.ConvertFromTimestamp(TempList.FirstOrDefault().ExpiryDate); i["LTP"] = 0; i["Delta"] = 0; i["NetQtyDelta"] = 0; i["BUYVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().BuyAvg) * (query1.FirstOrDefault().BuyQty)), 3); i["SELLVALUE"] = Math.Round(Convert.ToDouble((-query1.FirstOrDefault().SellAvg) * (-query1.FirstOrDefault().SellQty)), 3); //i["BUYVALUE"] = Math.Round(Convert.ToDouble((query1[j].buy_rate) * (query1[j].BuyQty)), 3); //i["SELLVALUE"] = Math.Round(Convert.ToDouble((query1[j].Sell_rate) * (query1[j].SellQty)), 3); i["MTM"] = 0; //======= ========== ========= ============= ========== =========== ======================= ============ ========== // lblbq.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("BuyQty")).ToString(); // lblsq.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("SellQty")).ToString(); // lvlNetQt.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("NetQty")).ToString(); // // lvelNetVal.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("NetValue")).ToString(); // lblstrike.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<int>("StrikePrice")).ToString(); // lvlLTP.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("LTP")).ToString(); // lvlDelta.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("Delta")).ToString(); // lvlNetQD.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("NetQtyDelta")).ToString(); // lbllblBR.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BUYRATE")).ToString(); // lvlSealRate.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SELLRATE")).ToString(); // lblBV.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("BUYVALUE")).ToString(); // lvlSealval.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("SELLVALUE")).ToString(); //// lvlMtm.Text = Global.Instance.NetBookTable.AsEnumerable().Sum(x => x.Field<double>("MTM")).ToString(); //============ ============ ========== ========= ======= ======= ================== ==================== ============= ======== ======= Global.Instance.NetBookTable.Rows.Add(i); } DGV.DataSource = Global.Instance.NetBookTable; } catch (Exception ex) { Task.Factory.StartNew(() => LogWriterClass.logwritercls.logs("tradeerror", ex.StackTrace.ToString())); } if (this.InvokeRequired) { MethodInvoker del = delegate { DGV.Refresh(); }; this.Invoke(del); return; } }