private void UpdatePositions(RealTimePositionObject pos)
        {
            try
            {
                if (pos != null)
                {
                    //find the position
                    int i = AllRealTimePositions.Find("Cusip", pos.Cusip);

                    if (i != -1)
                    {
                        //position already exists, remove it and add new one
                        pos.Notes = AllRealTimePositions[i].Notes;

                        AllRealTimePositions.RemoveAt(i);
                        RealTimeOccPositionObject r = new RealTimeOccPositionObject(pos);
                        AllRealTimePositions.Add(r);
                    }
                    else
                    {
                        //new position. add it
                        RealTimeOccPositionObject r = new RealTimeOccPositionObject(pos);
                        AllRealTimePositions.Add(r);
                    }
                    AllRealTimePositions.Sort("SortField", ListSortDirection.Descending);
                    ColorRows();

                    dgvRealTimePosition.Refresh();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void ServerForm_Load(object sender, EventArgs e)
        {
            try
            {
                //load the data from the table first
                foreach (RealTimePositionObject pos in calc.RealTimePositions.Values)
                {
                    if (pos.DtcActivity.Exists(d => IsHedge(d.ReasonCode)))
                    {
                        RealTimeOccPositionObject r = new RealTimeOccPositionObject(pos);
                        AllRealTimePositions.Add(r);
                    }
                }

                AllRealTimePositions.Sort("SortField", ListSortDirection.Descending);

                ColorRows();
            }
            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;
            }
            dgvRealTimePosition.Columns["Notes"].ReadOnly = false;
        }
        private void CalculateHedge(RealTimeOccPositionObject pos)
        {
            pos.HedgeLoanValue = 0;
            pos.HedgeQuantity  = 0;

            foreach (IncomingDeliveryOrderObject d in pos.DtcActivity)
            {
                if (IsHedge(d.ReasonCode))
                {
                    //TODO: Differentiate pending and made
                }
            }
        }
        private void ColorRows()
        {
            try
            {
                foreach (DataGridViewRow row in dgvRealTimePosition.Rows)
                {
                    RealTimeOccPositionObject r = (RealTimeOccPositionObject)row.DataBoundItem;

                    if (r.Cusip == selectedCusip)
                    {
                        row.DefaultCellStyle.BackColor = selectedColor;
                    }

                    else
                    {
                        if (r.OnRecall == "On Recall")
                        {
                            row.DefaultCellStyle.BackColor = txtOnRecall.BackColor;
                        }

                        else
                        {
                            row.DefaultCellStyle.BackColor = Color.White;
                        }


                        if (r.Price == 0)
                        {
                            row.Cells["Exposure"].Style.BackColor = txtMissingPrice.BackColor;
                        }

                        else if (r.NumBorrows == 0)
                        {
                            row.Cells["Exposure"].Style.BackColor = txtMissingBorrows.BackColor;
                        }

                        else
                        {
                            row.Cells["Exposure"].Style.BackColor = row.Cells["RealTimePosition"].Style.BackColor;
                        }
                    }
                }
                dgvRealTimePosition.Refresh();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex, TraceEnum.LoggedError);
            }
        }
        private void dgvRealTimePosition_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex != -1)
                {
                    RealTimeOccPositionObject d = (RealTimeOccPositionObject)dgvRealTimePosition.Rows[e.RowIndex].DataBoundItem;
                    DtcChannel.Instance.CusipSelected(d.Cusip);

                    selectedCusip = d.Cusip;

                    //color rows
                    ColorRows();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex, TraceEnum.LoggedError);
                MessageBox.Show(ex.ToString(), "Error");
            }
        }