public void SellCreatesPLTransaction() { var stock = new OwnedStock("tsla", _userId); stock.Purchase(1, 5, DateTimeOffset.UtcNow); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(7.5, stock.AverageCost); Assert.Equal(15, stock.State.Cost); stock.Sell(1, 6, DateTimeOffset.UtcNow, null); var tx = stock.State.Transactions.Last(); Assert.True(tx.IsPL); Assert.Equal(1.5, tx.Debit); // average cost is 7.5, selling for 6 is 1.5 loss stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(8.75, stock.AverageCost); stock.Sell(2, 10, DateTimeOffset.UtcNow, null); tx = stock.State.Transactions.Last(); Assert.True(tx.IsPL); Assert.Equal(2.5, tx.Credit); // average cost is 8.75, selling 2 x 10 is 2.5 gain }
public void MultipleBuysAverageCostCorrect() { var stock = new OwnedStock("tsla", _userId); stock.Purchase(1, 5, DateTimeOffset.UtcNow); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(7.5m, stock.State.AverageCost); Assert.Equal(15, stock.State.Cost); stock.Sell(1, 6, DateTimeOffset.UtcNow, null); Assert.Equal(7.5m, stock.State.AverageCost); Assert.Equal(7.5m, stock.State.Cost); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(8.75m, stock.State.AverageCost); Assert.Equal(17.5m, stock.State.Cost); stock.Sell(2, 10, DateTimeOffset.UtcNow, null); Assert.Equal(0, stock.State.Owned); Assert.Equal(0, stock.State.AverageCost); Assert.Equal(0, stock.State.Cost); Assert.Single(stock.State.PositionInstances); Assert.Equal(0, stock.State.PositionInstances[0].DaysHeld); Assert.Equal(1, stock.State.PositionInstances[0].Profit); Assert.Equal(0.04m, stock.State.PositionInstances[0].Percentage); }
public void MultipleBuysAverageCostCorrect() { var stock = new OwnedStock("tsla", _userId); stock.Purchase(1, 5, DateTimeOffset.UtcNow); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(7.5, stock.AverageCost); Assert.Equal(15, stock.State.Cost); stock.Sell(1, 6, DateTimeOffset.UtcNow, null); Assert.Equal(7.5, stock.AverageCost); Assert.Equal(7.5, stock.State.Cost); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(8.75, stock.AverageCost); Assert.Equal(17.5, stock.State.Cost); stock.Sell(2, 10, DateTimeOffset.UtcNow, null); Assert.Equal(0, stock.State.Owned); Assert.Equal(0, stock.AverageCost); Assert.Equal(0, stock.State.Cost); }
public async Task OwnedStockStorageAsync() { var stock = new OwnedStock(GenerateTestTicker(), _userId); stock.Purchase(10, 2.1, DateTime.UtcNow); var storage = CreateStorage(); await storage.Save(stock, _userId); var loadedList = await storage.GetStocks(_userId); Assert.NotEmpty(loadedList); var loaded = await storage.GetStock(stock.State.Ticker, _userId); Assert.Equal(loaded.State.Owned, stock.State.Owned); loaded.Purchase(5, 5, DateTime.UtcNow); await storage.Save(loaded, _userId); loaded = await storage.GetStock(loaded.State.Ticker, loaded.State.UserId); Assert.NotEqual(loaded.State.Owned, stock.State.Owned); await storage.Delete(_userId); var afterDelete = await storage.GetStocks(_userId); Assert.Empty(afterDelete); }
public void SellingNotOwnedFails() { var stock = new OwnedStock("TEUM", _userId); stock.Purchase(10, 2.1, DateTime.UtcNow); Assert.ThrowsAny <Exception>(() => stock.Sell(20, 100, DateTime.UtcNow, "sample note")); }
public void ExportStocksHeader() { var stock = new OwnedStock("tsla", Guid.NewGuid()); stock.Purchase(1, 100, DateTime.UtcNow, "some note"); var report = CSVExport.Generate(new[] { stock }); Assert.Contains(CSVExport.STOCK_HEADER, report); Assert.Contains("TSLA", report); }
public void EventCstrReplaysEvents() { var stock = new OwnedStock("tlsa", _userId); stock.Purchase(1, 10, DateTime.UtcNow); var events = stock.Events; var stock2 = new OwnedStock(events); Assert.Equal(stock.State.Owned, stock2.State.Owned); }
// Event handler for save editing button, will update current stock protected void SaveInPopupClicked(object sender, EventArgs e) { popupLayout.IsOpen = false; var newNumShares = float.Parse(editNumOfShares.Value.ToString()); var newPrice = float.Parse(editPurchasePrice.Value.ToString()); var newReturnGoal = float.Parse(editReturnGoal.Value.ToString()); UpdatePurchaseDate(); OwnedStock.EditStock(newNumShares, newPrice, newReturnGoal); ((App)Application.Current).User.Portfolio.UpdatePortfolio(); }
public OwnedStockTest() { test1 = new OwnedStock(); test2 = new OwnedStock(new Stock { CompanyName = "Burger Fuel", CurrentPrice = 42.2f, LastTrade = new DateTime(2019, 09, 09, 0, 0, 0), Shares = 4, Volume = 4, PriceClose = 42.4f }, new System.DateTime(2019, 09, 09, 0, 0, 0), 50.0f, 100, 1000f); test3 = new OwnedStock(new Stock { CompanyName = "Spark", CurrentPrice = 3.24f, LastTrade = new DateTime(2019, 09, 09, 0, 0, 0), Shares = 4, Volume = 4, PriceClose = 3.20f }, new System.DateTime(2019, 09, 09, 0, 0, 0), 3.00f, 150, 500f); test4 = new OwnedStock(new Stock { CompanyName = "A2 Milk", CurrentPrice = 13.23f, LastTrade = new DateTime(2019, 09, 09, 0, 0, 0), Shares = 4, Volume = 4, PriceClose = 13.15f }, new System.DateTime(2019, 09, 09, 0, 0, 0), 14.0f, 300, 1000f); }
public OwnedStockView(OwnedStock o) { Id = o.Id; Category = o.State.Category; Ticker = o.State.Ticker; Owned = o.State.Owned; Description = o.State.Description; AverageCost = o.State.AverageCost; Cost = o.State.Cost; DaysHeld = o.State.DaysHeld; DaysSinceLastTransaction = o.State.DaysSinceLastTransaction; }
public ReviewEntry(OwnedStock s) { Created = null; Ticker = s.Ticker; Description = s.Description; Expiration = null; IsExpired = false; ExpiresSoon = false; DaysLeft = null; Stats = null; Price = new TickerPrice(); IsOption = false; IsNote = false; }
//Adds purchased shares of stock to the users portfolio private void AddInPopupClicked(object sender, EventArgs e) { if (_purchasedPrice <= 0) { DisplayAlert(null, "Please enter purchase price!", "OK"); } else if (_sharesPurchased <= 0) { DisplayAlert(null, "Please enter number of shares purchased!", "OK"); } else { UpdatePurchaseDate(); var os = new OwnedStock(PublicStock, _purchasedDate, _purchasedPrice, _sharesPurchased, _returnGoal); ((App)Application.Current).User.Portfolio.AddAsset(os); StockPortfolioForm.IsOpen = false; } }
public EmailReviewEntry(OwnedStock s) { Created = null; Ticker = s.State.Ticker; Description = s.State.Description; Expiration = null; IsExpired = false; ExpiresSoon = false; DaysLeft = null; Stats = null; IsOption = false; IsAlert = false; AverageCost = s.State.AverageCost; OptionType = null; StrikePrice = 0; OptionType = null; PricePoints = new List <AlertPricePoint>(); }
public void DaysHeldCorrect() { var stock = new OwnedStock("tsla", _userId); stock.Purchase(1, 5, DateTimeOffset.UtcNow.AddDays(-5)); stock.Purchase(1, 10, DateTimeOffset.UtcNow.AddDays(-2)); Assert.Equal(5, stock.State.DaysHeld); Assert.Equal(2, stock.State.DaysSinceLastTransaction); stock.Sell(1, 6, DateTimeOffset.UtcNow, null); Assert.Equal(5, stock.State.DaysHeld); Assert.Equal(0, stock.State.DaysSinceLastTransaction); stock.Sell(1, 10, DateTimeOffset.UtcNow, null); Assert.Equal(0, stock.State.DaysHeld); Assert.Equal(0, stock.State.DaysSinceLastTransaction); }
public void PurchaseWorks() { var stock = new OwnedStock("TEUM", _userId); stock.Purchase(10, 2.1m, DateTime.UtcNow); Assert.Equal("TEUM", stock.State.Ticker); Assert.Equal(_userId, stock.State.UserId); Assert.Equal(10, stock.State.Owned); Assert.Equal(21, stock.State.Cost); stock.Purchase(5, 2, DateTime.UtcNow); Assert.Equal(15, stock.State.Owned); Assert.Equal(31, stock.State.Cost, 0); stock.Sell(5, 20, DateTime.UtcNow, "sample note"); Assert.Equal(10, stock.State.Owned); }
public void MultipleBuysDeletingTransactions() { var stock = new OwnedStock("tsla", _userId); stock.Purchase(1, 5, DateTimeOffset.UtcNow); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(7.5m, stock.State.AverageCost); Assert.Equal(15, stock.State.Cost); stock.Sell(1, 6, DateTimeOffset.UtcNow, null); Assert.Equal(7.5m, stock.State.AverageCost); Assert.Equal(7.5m, stock.State.Cost); stock.Purchase(1, 10, DateTimeOffset.UtcNow); Assert.Equal(8.75m, stock.State.AverageCost); Assert.Equal(17.5m, stock.State.Cost); stock.Sell(2, 10, DateTimeOffset.UtcNow, null); Assert.Equal(0, stock.State.Owned); Assert.Equal(0, stock.State.AverageCost); Assert.Equal(0, stock.State.Cost); var last = stock.State.Transactions.Where(t => !t.IsPL).Last(); stock.DeleteTransaction(last.EventId); Assert.Equal(2, stock.State.Owned); Assert.Equal(8.75m, stock.State.AverageCost); Assert.Equal(17.5m, stock.State.Cost); stock.Delete(); Assert.Equal(0, stock.State.Owned); Assert.Equal(0, stock.State.AverageCost); Assert.Equal(0, stock.State.AverageCost); Assert.Empty(stock.State.Transactions); }
public OwnedStockDetailsPage(OwnedStock ownedStock) { OwnedStock = ownedStock; OwnedStock.UpdateOwnedAsset(); BindingContext = this; InitializeComponent(); editNumOfShares = new SfNumericTextBox { Value = 0 }; editNumOfShares.ValueChanged += Handle_NumSharesChanged; editPurchasePrice = new SfNumericTextBox { Value = 0 }; editPurchasePrice.ValueChanged += Handle_PriceChanged; editReturnGoal = new SfNumericTextBox { Value = 0 }; editReturnGoal.ValueChanged += Handle_ReturnGoalChanged; }
internal static object ToOwnedView(OwnedStock o, TickerPrice price) { var equity = o.State.Owned * price.Amount; var cost = o.State.Cost; var profits = equity - cost; var profitsPct = profits / (1.0 * cost); return(new { id = o.Id, currentPrice = price.Amount, ticker = o.State.Ticker, owned = o.State.Owned, equity = equity, description = o.State.Description, averageCost = o.State.AverageCost, cost = cost, profits = profits, profitsPct = profitsPct, transactions = new TransactionList(o.State.Transactions, null, null) }); }
public void BuyingForZeroThrows() { var stock = new OwnedStock("tlsa", _userId); Assert.Throws <InvalidOperationException>(() => stock.Purchase(10, 0, DateTime.UtcNow)); }
public Task Save(OwnedStock stock, Guid userId) { return(Save(stock, _stock_entity, userId)); }
public Task Save(OwnedStock stock, Guid userId) { throw new NotImplementedException(); }
public void BuyingWithBadDateThrows() { var stock = new OwnedStock("tlsa", _userId); Assert.Throws <InvalidOperationException>(() => stock.Purchase(10, 0, DateTime.MinValue)); }
public Task <int> SaveOwnedStockAsync(OwnedStock ownedStock) { return(_database.InsertOrReplaceAsync(ownedStock)); }
public void PurchaseWithDateNotProvidedThrows() { var stock = new OwnedStock("tlsa", _userId); Assert.Throws <InvalidOperationException>(() => stock.Purchase(1, 20, DateTime.MinValue)); }