private Position GetPosition(PositionInputModel positionInputModel)
        {
            Position positionToSave;
            if (_positionId != null)
            {
                positionToSave = new Position(_positionId ?? -1);
            }
            else
            {
                positionToSave = new Position();
            }

            positionToSave.Shares = decimal.Parse(positionInputModel.SharesText);
            positionToSave.PricePerShare = decimal.Parse(positionInputModel.PriceText);
            positionToSave.Ticker = positionInputModel.TickerText.ToUpper();
            positionToSave.ContainingPortfolioID = _portfolioId;
            return positionToSave;
        }
        public IList<Position> GetAllPositions(long portfolioId)
        {
            var list = new List<Position>();

            var cursor = Db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " ContainingPortfolioID = " + portfolioId, null, null, null, null);
            if (cursor.Count > 0)
            {
                while (cursor.MoveToNext())
                {
                    var position = new Position(cursor.GetInt(0));
                    position.Ticker = cursor.GetString(1);
                    position.Shares = Convert.ToDecimal(cursor.GetFloat(2));
                    position.PricePerShare = Convert.ToDecimal(cursor.GetFloat(3));
                    list.Add(position);
                }
            }
            if (!cursor.IsClosed) cursor.Close();
            return list;
        }
 public void PopulateForm(Position position)
 {
     this.TickerTextBox.Text = position.Ticker;
     this.PriceTextBox.Text = position.PricePerShare.ToString();
     this.SharesTextBox.Text = position.Shares.ToString();
 }
 private void UpdateExistingPosition(Position position)
 {
     var positionID = position.ID ?? -1;
     Log.Error("UpdateExistingPosition", "Positions updated: " + Db.Update(POSITION_TABLE_NAME, GetPositionContentValues(position), "id = " + positionID, null));
 }
 private void InsertNewPosition(Position position)
 {
     Log.Error("InsertNewPosition", "Positions inserted: " + Db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)));
 }
 private ContentValues GetPositionContentValues(Position position)
 {
     var positionValues = new ContentValues();
     positionValues.Put("PricePerShare", (double)position.PricePerShare);
     positionValues.Put("Ticker", position.Ticker.ToUpper());
     positionValues.Put("Shares", (double)position.Shares);
     positionValues.Put("ContainingPortfolioID", position.ContainingPortfolioID);
     return positionValues;
 }
 public void SavePosition(Position position)
 {
     if (position.ID == null)
     {
         InsertNewPosition(position);
     }
     else
     {
         UpdateExistingPosition(position);
     }
 }
        public Position GetPositionById(long positionId)
        {
            Position position = null;

            var cursor = Db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " id = " + positionId, null, null, null, null);
            if (cursor.Count > 0)
            {
                while (cursor.MoveToNext())
                {
                    position = new Position(cursor.GetInt(0));
                    position.Ticker = cursor.GetString(1);
                    position.Shares = Convert.ToDecimal(cursor.GetFloat(2));
                    position.PricePerShare = Convert.ToDecimal(cursor.GetFloat(3));
                }
            }
            if (!cursor.IsClosed) cursor.Close();
            return position;
        }
 private IDictionary<StockDataItem, string> CalculateItems(IEnumerable<StockDataItem> items, Position position, StockQuote quote)
 {
     var dict = new Dictionary<StockDataItem, string>();
     foreach (var item in items)
     {
         switch (item)
         {
             case StockDataItem.GainLoss:
                 dict.Add(item, CalculateGainLoss(quote, position).ToString());
                 break;
             case StockDataItem.GainLossRealTime:
                 dict.Add(item, CalculateGainLossRealTime(quote, position).ToString());
                 break;
             default:
                 throw new ArgumentException("That StockDataItem type cannot be calculated");
         }
     }
     return dict;
 }
 private static decimal CalculateGainLossRealTime(StockQuote quote, Position position)
 {
     var moneyISpent = position.PricePerShare * position.Shares;
     var moneyItsWorth = position.Shares * quote.RealTimeLastTradePrice;
     return moneyItsWorth - moneyISpent;
 }