private void LoadMorningPositions()
        {
            //changed this since nthBizday takes holidays into account, but positions table doesnt
            DateTime dtcDate = Utils.GetNthBusinessDay(ProcessDate, -1);
            DateTime g1Date  = DateTime.Today; // Utils.GetLastWeekDay(ProcessDate);

            StartingPositionParam g1p = new StartingPositionParam();

            g1p.FileDate_Date.AddParamValue(g1Date);
            g1p.ClearingNo.AddParamValue(Settings.Account);

            //load all the morning positions
            //g1pFact.LoadMinimal(G1BodPositions, g1p);
            g1pFact.Load(G1BodPositions, g1p);

            //make sure there are some positions
            if (G1BodPositions.Count < 50)
            {
                //                throw new Exception("Problem loading G1 positions. Only found " + G1BodPositions.Count);
            }

            //get the non NPB data
            List <StartingPositionObject> tmp = G1BodPositions.FindAll(p => p.TradeCategory != "NPB");

            foreach (StartingPositionObject g in tmp)
            {
                if (G1NonNpbPositions.ContainsKey(g.Cusip))
                {
                    G1NonNpbPositions[g.Cusip].Add(g);
                }
                else
                {
                    List <StartingPositionObject> t = new List <StartingPositionObject>();
                    t.Add(g);
                    G1NonNpbPositions.Add(g.Cusip, t);
                }
            }

            //get the NPB data
            G1NpbPositions = G1BodPositions.FindAll(p => p.TradeCategory == "NPB");

            //Group NPB Data together by cusip
            G1NpbPositions = GroupNpb(G1NpbPositions);

            //load the DTC starting position
            DtcPositionViewParam dp = new DtcPositionViewParam();

            dp.DateofData_Date.AddParamValue(dtcDate);
            dp.ParticipantID.AddParamValue(Settings.Account.ToInt());

            dvFact.Load(DtcBodPositions, dp);

            //make sure there are some positions
            if (DtcBodPositions.Count < 1)
            {
                //                throw new Exception("Problem loading DTC positions. Only found " + DtcBodPositions.Count);
            }

            //load occ positions
            PositionParam occp = new PositionParam();

            occp.ActivityDate_Date.AddParamValue(dtcDate);
            pf.Load(OccBodPositions, occp);



            //now load all the starting positions from G1
            foreach (List <StartingPositionObject> g1 in G1NonNpbPositions.Values)
            {
                foreach (StartingPositionObject g in g1)
                {
                    if (!string.IsNullOrEmpty(g.Cusip) && !DtcBodPositions.ContainsKey(g.Cusip.ToUpper()))
                    {
                        //no dtc position for cusip, add it
                        DtcBodPositions.Add(g.Cusip, new DtcPositionViewObject(dtcDate, g.Cusip, 0, 0, 0, 0, Settings.Account.ToInt(), dtcDate, g.Ticker));
                    }
                }
            }

            // ------------------------------Alandias------------------------------
            //If account is 269, it accounts for overnight deliveries for our 269 account
            //if (Settings.Account == "269")
            {
                List <tblDTFPARTObject> DTFPO    = new List <tblDTFPARTObject>();
                tblDTFPARTFactory       DTFPFact = new tblDTFPARTFactory();
                tblDTFPARTParam         DTFParam = new tblDTFPARTParam();
                DTFParam.DateofData.AddParamValue(DateTime.Today);
                DTFParam.TransOrigSource.AddParamValue("CFSD", "!=");   //Alandias 20130301
                //DTFParam.ParticipantNum.AddParamValue("0269");
                DTFParam.ParticipantNum.AddParamValue(Settings.Account.PadLeft(4, '0'));
                DTFParam.SubFunction.AddParamValue("DTFPDQ");
                DTFParam.StatusCode.AddParamValue("m");
                DTFPFact.Load(DTFPO, DTFParam);

                List <tblDTFPARTObject> ItemsInDTFPOButNotInDtcBodPositions = new List <tblDTFPARTObject>();

                foreach (tblDTFPARTObject t in DTFPO)
                {
                    if (DtcBodPositions.ContainsKey(t.CUSIP.ToUpper()) || DtcBodPositions.ContainsKey(t.CUSIP))
                    {
                        switch (t.TransTypeNew.Trim())
                        {
                        case "026":     //026 is deliver
                            DtcBodPositions[t.CUSIP.ToUpper()].UnPledgedQuantity -= Convert.ToInt32(t.ShareQuantity);
                            break;

                        case "027":     //027 is receive
                            DtcBodPositions[t.CUSIP.ToUpper()].UnPledgedQuantity += Convert.ToInt32(t.ShareQuantity);
                            break;
                        }
                    }
                    else
                    {
                        ItemsInDTFPOButNotInDtcBodPositions.Add(t);
                    }
                }


                foreach (tblDTFPARTObject t in ItemsInDTFPOButNotInDtcBodPositions)
                {
                    string tempticker = "";
                    if (Stocks.ContainsKey(t.CUSIP.ToUpper()))
                    {
                        StockViewObject s = null;
                        Stocks.TryGetValue(t.CUSIP.ToUpper(), out s);

                        if (s != null)
                        {
                            tempticker = s.Ticker;
                        }

                        else
                        {
                            tempticker = "zzzzUNKNOWN";
                        }
                    }

                    Int32 qty = 0;
                    switch (t.TransTypeNew.Trim())
                    {
                    case "026":     //026 is deliver
                        qty -= Convert.ToInt32(t.ShareQuantity);
                        break;

                    case "027":     //027 is receive
                        qty += Convert.ToInt32(t.ShareQuantity);
                        break;
                    }

                    if (!DtcBodPositions.ContainsKey(t.CUSIP.ToUpper()))
                    {
                        DtcBodPositions.Add(t.CUSIP.ToUpper(), new DtcPositionViewObject(dtcDate, t.CUSIP.ToUpper(), 0, 0, 0, qty, Settings.Account.ToInt(), dtcDate, tempticker));
                    }
                    else
                    {
                        DtcBodPositions[t.CUSIP.ToUpper()].UnPledgedQuantity += qty;
                    }

                    Int32 t1 = DtcBodPositions[t.CUSIP.ToUpper()].UnPledgedQuantity;
                }

                foreach (var temp in DtcBodPositions)
                {
                    temp.Value.ShareQuantity = temp.Value.UnPledgedQuantity;
                }
            }
            // -----------END-------------------Alandias---------------------------


            a = "1";
        }
        private void ServerForm_Load(object sender, EventArgs e)
        {
            try
            {
                //load NSCC projected needs
                ProjectedNeedFactory       pnf = new ProjectedNeedFactory();
                ProjectedNeedParam         pnp = new ProjectedNeedParam();
                List <ProjectedNeedObject> pn  = new List <ProjectedNeedObject>();

                pnp.DateOfData_Date.AddParamValue(DateTime.Today);
                pnp.Todays_Current_Net_Position.AddParamValue(0, "<");
                pnp.Participant_Clearing_Number.AddParamValue(Settings.DatabaseName);

                pnf.Load(pn, pnp);

                //load up todays projected needs
                foreach (ProjectedNeedObject p in pn)
                {
                    ProjectedNeedsSummaryObject ps = new ProjectedNeedsSummaryObject(p);
                    ps.Ticker = calc.UpdateStockInfo(p.CUSIP);
                    ProjectedNeeds.Add(ps);
                }



                //load up todays balance orders
                BalanceOrderViewFactory       bof      = new BalanceOrderViewFactory();
                BalanceOrderViewParam         bop      = new BalanceOrderViewParam();
                List <BalanceOrderViewObject> bol      = new List <BalanceOrderViewObject>();
                List <BalanceOrderViewObject> combined = new List <BalanceOrderViewObject>();

                bop.DateOfData_Date.AddParamValue(Utils.GetNthBusinessDay(DateTime.Today, -1));
                bop.Participant_Clearing_Number.AddParamValue(Settings.Account.PadLeft(4, '0'));

                bof.Load(bol, bop);

                foreach (BalanceOrderViewObject b in bol)
                {
                    BalanceOrderViewObject f = combined.Find(c => c.CUSIP == b.CUSIP);

                    if (f == null)
                    {
                        combined.Add(b);
                    }
                    else
                    {
                        f.Quantity += b.Quantity;
                    }
                }


                foreach (BalanceOrderViewObject b in combined)
                {
                    if (ProjectedNeeds.Any(x => x.Cusip != null && b.CUSIP != null && x.Cusip.ToLower() == b.CUSIP.ToLower()))
                    {
                        var item = ProjectedNeeds.SingleOrDefault(x => x.Cusip.ToLower() == b.CUSIP.ToLower());
                        item.BeginningNeed += Math.Abs(b.Quantity.Value);
                    }

                    else
                    {
                        ProjectedNeedsSummaryObject ps = new ProjectedNeedsSummaryObject(b);
                        ps.Ticker = calc.UpdateStockInfo(b.CUSIP);
                        ProjectedNeeds.Add(ps);
                    }
                }


                //ALANDIAS--------Balance orders weren't updating for overnight deliveries------------------------------
                if (combined.Count > 0)
                {
                    List <tblDTFPARTObject> DTFPO    = new List <tblDTFPARTObject>();
                    tblDTFPARTFactory       DTFPFact = new tblDTFPARTFactory();
                    tblDTFPARTParam         DTFParam = new tblDTFPARTParam();
                    DTFParam.DateofData.AddParamValue(DateTime.Today);
                    DTFParam.TransOrigSource.AddParamValue("CFSD", "!=");
                    DTFParam.ParticipantNum.AddParamValue(Settings.Account.PadLeft(4, '0'));
                    DTFParam.SubFunction.AddParamValue("DTFPDQ");
                    DTFParam.StatusCode.AddParamValue("m");
                    DTFParam.TransTypeNew.AddParamValue("026");
                    DTFParam.ContraParticipantNum.AddParamValue("0888");
                    DTFPFact.Load(DTFPO, DTFParam);

                    var BOinPN = ProjectedNeeds.Where(x => x.NeedType != null && x.NeedType.ToLower() == "balanceorder");
                    foreach (var b in BOinPN)
                    {
                        var overNightDelivery = DTFPO.SingleOrDefault(x => x.CUSIP.ToLower() == b.Cusip.ToLower());
                        if (overNightDelivery != null)
                        {
                            b.DeliveredToCns += Convert.ToInt32(overNightDelivery.ShareQuantity);
                        }
                    }
                }

                //------------------------------------------------------------------------------------------------------


                //ALANDIAS NSCCMISCFILE----------------------------------------
                //Please Note
                //Misc import was done incorrectly.  QuantityReceived is actually QuantityDelivered and vice versa
                //Activity '15' is cancel and '02' is new deliver
                //quantitydelivered with activity '15' means we received and it reduces the balance order we have to deliver
                //QuantityReceived with activity '02' means we have to deliver

                string NSCCMISCNewDeliver = "02";
                string NSCCMISCCancel     = "15";

                vNSCCMiscellaneousActivityFactory       MAF    = new vNSCCMiscellaneousActivityFactory();
                vNSCCMiscellaneousActivityParam         MAP    = new vNSCCMiscellaneousActivityParam();
                List <vNSCCMiscellaneousActivityObject> MAList = new List <vNSCCMiscellaneousActivityObject>();

                string Acct = Settings.Account.PadLeft(4, '0');

                MAP.DateOfData.AddParamValue(DateTime.Today);

                MAP.ParticipantClearingNumber.AddParamValue(Acct);



                MAP.Activity.AddParamValue(NSCCMISCNewDeliver);
                MAP.Activity.AddParamValue(NSCCMISCCancel);

                MAF.Load(MAList, MAP);


                var WhatWeHaveToDeliver = MAList.Where(x => (x.Activity == NSCCMISCNewDeliver) && (x.QuantityReceived != 0));
                var ReduceBalanceOrder  = MAList.Where(x => (x.Activity == NSCCMISCCancel) && (x.QuantityDelivered != 0));


                foreach (vNSCCMiscellaneousActivityObject MAItem in WhatWeHaveToDeliver)
                {
                    ProjectedNeedsSummaryObject ps = new ProjectedNeedsSummaryObject(MAItem);
                    ps.Ticker = calc.UpdateStockInfo(MAItem.CUSIP);
                    ProjectedNeeds.Add(ps);
                }


                foreach (vNSCCMiscellaneousActivityObject rbo in ReduceBalanceOrder)
                {
                    if (ProjectedNeeds.Any(x => x.Cusip != null && rbo.CUSIP != null && x.Cusip.ToLower() == rbo.CUSIP.ToLower()))
                    {
                        var item = ProjectedNeeds.FirstOrDefault(x => x.Cusip.ToLower() == rbo.CUSIP.ToLower());
                        item.BeginningNeed -= Math.Abs(Convert.ToInt32(rbo.QuantityDelivered.Value));
                    }

                    /*
                     * else
                     * {
                     *  ProjectedNeedsSummaryObject ps = new ProjectedNeedsSummaryObject(b);
                     *  ps.Ticker = calc.UpdateStockInfo(b.CUSIP);
                     *  ProjectedNeeds.Add(ps);
                     * }
                     */
                }


                //--------------------------------------------------------------------------------



                /*IncomingDeliveryOrderObject o = calc.AllDtcActivity.Find(p => p.ReasonCode == "620");
                 *
                 * int a = 5;*/

                //load the data from the table first
                foreach (IncomingDeliveryOrderObject ido in calc.AllDtcActivity)
                {
                    if (ido.Receiver == "00000888" && ido.Deliverer == Settings.Account.Padded())
                    {
                        int i = ProjectedNeeds.Find("Cusip", ido.Cusip);

                        if (i != -1)
                        {
                            ProjectedNeeds[i].DeliveredToCns += ido.ShareQuantity.Value;
                        }
                    }

                    if (ido.Deliverer == "00000888" && ido.Receiver == Settings.Account.Padded())
                    {
                        int i = ProjectedNeeds.Find("Cusip", ido.Cusip);

                        if (i != -1)
                        {
                            ProjectedNeeds[i].Received += ido.ShareQuantity.Value;
                        }
                    }

                    if (ido.ReasonCode == "620" /*"030"*/ && ido.Deliverer == Settings.Account.Padded())
                    {
                        int i = ProjectedNeeds.Find("Cusip", ido.Cusip);

                        if (i != -1 && ProjectedNeeds[i].NeedType == "BalanceOrder")
                        {
                            ProjectedNeeds[i].DeliveredToCns += ido.ShareQuantity.Value;
                            CalcSummary();
                            ProjectedNeeds.Sort("OpenNeed", ListSortDirection.Descending);
                        }
                    }

                    //ALANDIAS added for NSCCMISC
                    if (ido.ReasonCode == "620" /*"030"*/ && ido.Deliverer == Settings.Account.Padded())
                    {
                        int i = ProjectedNeeds.Find("Cusip", ido.Cusip);

                        if (i != -1 && ProjectedNeeds[i].NeedType == "MiscellaneousFile")
                        {
                            ProjectedNeeds[i].DeliveredToCns += ido.ShareQuantity.Value;
                            CalcSummary();
                            ProjectedNeeds.Sort("OpenNeed", ListSortDirection.Descending);
                        }
                    }
                }

                ProjectedNeeds.Sort("OpenNeed", ListSortDirection.Descending);

                foreach (DataGridViewColumn c in dgvRealTimePosition.Columns)
                {
                    c.ReadOnly = true;
                }
                //dgvRealTimePosition.Columns["Ticker"].ReadOnly = false;

                ColorRows();

                //ETB stuff
                EtbStockViewFactory       ef   = new EtbStockViewFactory();
                EtbStockViewParam         ep   = new EtbStockViewParam();
                List <EtbStockViewObject> temp = new List <EtbStockViewObject>();

                ep.DateOfData_Date.AddParamValue(DateTime.Today);
                ef.Load(temp, ep);

                etbList = temp.ToDictionary(t => t.Symbol, t => t);

                ep   = new EtbStockViewParam();
                temp = new List <EtbStockViewObject>();

                ep.DateOfData_Date.AddParamValue(Utils.GetNthBusinessDay(DateTime.Today, -3));
                ef.Load(temp, ep);

                etbT3List = temp.ToDictionary(t => t.Symbol, t => t);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex, TraceEnum.LoggedError);
                MessageBox.Show("Error starting app: \r\n" + ex.ToString());
            }

            foreach (DataGridViewColumn c in dgvRealTimePosition.Columns)
            {
                c.ReadOnly = true;
            }

            CalcSummary();
        }