예제 #1
0
        //todo: any other place I can use this?
        public Holding GetHolding(string symbol)
        {
            Holding holding = null;

            for (int i = 0; i < m_list.Count; i++)
            {
                if (m_list[i].Symbol == symbol)
                {
                    holding = m_list[i];
                    break;
                }
            }

            return(holding);
        }
예제 #2
0
        private void SavetoFile(string path)
        {
            StreamWriter writer = new StreamWriter(path);

            try
            {
                writer.WriteLine("Symbol,Name,Last Price,Shares,Buy Price,Cost Basis,Current Value,$ - Gain/Loss,% - Gain/Loss,% - Weight,Date Purchased");

                for (int i = 0; i < m_portfolio.HoldingCount; i++)
                {
                    Holding holding = m_portfolio.GetHolding(i);

                    for (int j = 0; j < holding.PositionCount; j++)
                    {
                        Position pos = holding.GetPosition(j);

                        writer.WriteLine(holding.Symbol + "," +
                                         holding.Name + "," +
                                         holding.CurrentPrice + "," +
                                         pos.Shares.ToString() + "," +
                                         pos.BuyPrice + "," +
                                         pos.CostBasis + "," +
                                         pos.CurrentValue + "," +
                                         pos.GainLoss + "," +
                                         Math.Round(100 * (pos.GainLoss / pos.CostBasis), 2) + "," +
                                         Math.Round(100 * (pos.CurrentValue / m_portfolio.CurrentValue), 2) + "," +
                                         pos.BuyDate);
                    } //for
                }     //for

                writer.WriteLine("-,-,-,-,Total," + Math.Round(m_portfolio.CostBasis, 2) + "," +
                                 Math.Round(m_portfolio.CurrentValue, 2) + "," +
                                 Math.Round(m_portfolio.GainLoss, 2) + "," +
                                 Math.Round(100 * (m_portfolio.GainLoss / m_portfolio.CurrentValue), 2) + "," +
                                 "100" + ",-");
            }
            catch (Exception ex)
            {
                writer.Close();
                throw ex;
            }

            m_modified = false;
            writer.Close();
        }
예제 #3
0
        void UpdateDataGridView()
        {
            int subPosAddIndex = 0;

            m_updatingGrid = true;
            dataGridView.Rows.Clear();

            for (int i = 0; i < m_portfolio.HoldingCount; i++)
            {
                Holding currHolding = m_portfolio.GetHolding(i);
                string  dateStr     = currHolding.GetPosition(0).BuyDate;
                string  buyPriceStr = currHolding.GetPosition(0).BuyPrice.ToString();

                //if this holding's sub-positions are expanded
                if (m_expandedRows.LastIndexOf(currHolding.Symbol) >= 0)
                {
                    AddAllSubPosToUI(currHolding.Symbol, i + subPosAddIndex);
                    subPosAddIndex += currHolding.PositionCount - 1;
                    continue;
                }

                if (currHolding.PositionCount > 1)
                {
                    dateStr     = "Various";
                    buyPriceStr = "Various";
                }

                this.dataGridView.Rows.Add(currHolding.Symbol,
                                           currHolding.Name,
                                           currHolding.CurrentPrice,
                                           currHolding.Shares,
                                           buyPriceStr,
                                           Math.Round(currHolding.CostBasis, 2),
                                           Math.Round(currHolding.CurrentValue, 2),
                                           Math.Round(currHolding.GainLoss, 2),
                                           Math.Round(100 * (currHolding.GainLoss / currHolding.CostBasis), 2),
                                           Math.Round(100 * (currHolding.CurrentValue / m_portfolio.CurrentValue), 2),
                                           dateStr);
            }//for

            AddLastRow();
            m_updatingGrid = false;
        }
예제 #4
0
        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utils.GetInstance().ClearPriceCache();

            for (int i = 0; i < m_portfolio.HoldingCount; i++)
            {
                Holding currHolding = m_portfolio.GetHolding(i);

                try
                {
                    currHolding.CurrentPrice = Utils.GetInstance().GetRealtimePrice(currHolding.Symbol);
                }
                catch (System.Net.WebException)
                {
                    Utils.GetInstance().ShowNoInternetDialog();
                    return;
                }
            }

            UpdateDataGridView();
        }
예제 #5
0
        public void AddHolding(Holding holding)
        {
            if (m_list.Count == 0)
            {
                m_list.Add(holding);

                m_currVal   += holding.CurrentValue;
                m_costBasis += holding.CostBasis;
                return;
            }

            bool dup          = false;
            int  holdingCount = m_list.Count;

            for (int i = 0; i < holdingCount; i++)
            {
                if (holding.Symbol == m_list[i].Symbol)
                {
                    int posCount = holding.PositionCount;

                    for (int j = 0; j < posCount; j++)
                    {
                        m_list[i].AddPosition(holding.GetPosition(j));
                        m_currVal   += holding.GetPosition(j).CurrentValue;
                        m_costBasis += holding.GetPosition(j).CostBasis;

                        dup = true;
                    }
                }
            }//for

            if (!dup)
            {
                m_list.Add(holding);
                m_currVal   += holding.CurrentValue;
                m_costBasis += holding.CostBasis;
            }
        }
예제 #6
0
        private void buttonSaveClose_Click(object sender, EventArgs e)
        {
            bool error = false;

            try
            {
                if (textBoxSymbol.Text.Trim() == "")
                {
                    error = true;
                    MessageBox.Show("Invalid symbol input", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (textBoxPrice.Text.Trim() == "" || Convert.ToDouble(textBoxPrice.Text) <= 0)
                {
                    error = true;
                    MessageBox.Show("Invalid price input.", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (textBoxShares.Text.Trim() == "" || Convert.ToDouble(textBoxShares.Text) <= 0)
                {
                    error = true;
                    MessageBox.Show("Invalid shares input.", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (Convert.ToDateTime(dateTimePicker.Text) > DateTime.Today || Convert.ToDateTime(dateTimePicker.Text) < new DateTime(1900, 1, 1))
                {
                    error = true;
                    MessageBox.Show("Invalid date input.", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (FormatException)
            {
                error = true;
                MessageBox.Show("Invalid price/shares input.", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (!error)
            {
                try
                {
                    m_holding  = new Holding(textBoxSymbol.Text);
                    m_position = new Position(m_holding,
                                              Convert.ToDouble(textBoxShares.Text),
                                              Convert.ToDouble(textBoxPrice.Text),
                                              Convert.ToDateTime(dateTimePicker.Text).ToShortDateString());
                }
                catch (WebException)
                {
                    error = true;
                    Utils.GetInstance().ShowNoInternetDialog();
                }
                catch (ArgumentException)
                {
                    error = true;
                    MessageBox.Show("Invalid stock symbol.", "Portfolio Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (!error)
                {
                    DialogResult = DialogResult.OK;

                    m_holding.AddPosition(m_position);
                    this.Close();
                }
            }
        }
예제 #7
0
        private void LoadFromFile(string path)
        {
            StreamReader reader = new StreamReader(path);

            m_portfolio.Clear();

            int i = 0;

            while (!reader.EndOfStream)
            {
                string[] row = reader.ReadLine().Split(',');
                Holding  holding;

                if (row.Length != COL_COUNT)
                {
                    reader.Close();
                    throw new FormatException("Bad file format.");
                }

                //skip header
                if (i == 0)
                {
                    i++;
                    continue;
                }

                //done, no need load the last total line.
                if (row[0] == "-")
                {
                    break;
                }

                try
                {
                    holding = new Holding(row[(int)COL_INDEX.SYMBOL], new Position(null,
                                                                                   Convert.ToDouble(row[(int)COL_INDEX.SHARES]),
                                                                                   Convert.ToDouble(row[(int)COL_INDEX.BUY_PRICE]),
                                                                                   row[(int)COL_INDEX.DATE]));
                }
                catch (FormatException ex)
                {
                    reader.Close();
                    throw ex;
                }

                m_portfolio.AddHolding(holding);
            }//while

            bool flag = false;

            if (m_portfolio.HoldingCount == 0)
            {
                buttonEdit.Enabled               = flag;
                buttonDelete.Enabled             = flag;
                saveToolStripMenuItem.Enabled    = flag;
                refreshToolStripMenuItem.Enabled = flag;
            }
            else
            {
                buttonEdit.Enabled               = !flag;
                buttonDelete.Enabled             = !flag;
                saveToolStripMenuItem.Enabled    = !flag;
                refreshToolStripMenuItem.Enabled = !flag;
            }

            reader.Close();
        }
예제 #8
0
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            string  symbol    = dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.SYMBOL].Value.ToString();
            Holding holding   = m_portfolio.GetHolding(symbol);
            int     occurance = OccuranceInDataGridView(symbol);
            bool    deleted   = false;
            int     rowIndex  = dataGridView.SelectedRows[0].Index;

            if (holding == null)
            {
                throw new NullReferenceException("Null holding.");
            }

            //delete holding w/1 pos.
            if (occurance == 1 && holding.PositionCount == 1)
            {
                DialogResult retVal = MessageBox.Show("Delele this holding?", "Portfolio Manager", MessageBoxButtons.YesNo);

                if (retVal == DialogResult.Yes)
                {
                    if (DeleteAllHolding(dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.SYMBOL].Value.ToString()))
                    {
                        UpdateDataGridView();
                        deleted = true;
                    }
                }
            }
            //delete holding w/multiple pos.
            else if (occurance == 1 && holding.PositionCount > 1)
            {
                DialogResult retVal = MessageBox.Show("Delele this holding with multiple positions?", "Portfolio Manager", MessageBoxButtons.YesNo);

                if (retVal == DialogResult.Yes)
                {
                    if (DeleteAllHolding(dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.SYMBOL].Value.ToString()))
                    {
                        UpdateDataGridView();
                        deleted = true;
                    }
                }
            }
            //delete a sub-pos
            else
            {
                DialogResult retVal = MessageBox.Show("Delele this position?", "Portfolio Manager", MessageBoxButtons.YesNo);

                if (retVal == DialogResult.Yes)
                {
                    double buyPrice = Convert.ToDouble(dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.BUY_PRICE].Value);
                    double shares   = Convert.ToDouble(dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.SHARES].Value);
                    string buyDate  = dataGridView.SelectedRows[0].Cells[(int)COL_INDEX.DATE].Value.ToString();

                    holding.RemovePosition(holding.GetPosition(holding.Symbol, shares, buyPrice, buyDate));

                    if (holding.PositionCount == 1)
                    {
                        m_expandedRows.RemoveAll(item => item == holding.Symbol);
                    }
                    else
                    {
                        m_expandedRows.Remove(holding.Symbol);
                    }

                    UpdateDataGridView();
                    deleted = true;
                }
            }

            if (deleted)
            {
                //-1 because we now have 1 less row
                if (rowIndex != 0)
                {
                    rowIndex--;
                }

                if (m_portfolio.HoldingCount == 0)
                {
                    buttonEdit.Enabled   = false;
                    buttonDelete.Enabled = false;
                }

                dataGridView.Rows[rowIndex].Cells[0].Selected = true;
                m_modified = true;
            }
        }