public void AllocationRuleYes() { Portfolio p = new Portfolio(); Position pos1 = new Position(); pos1.status = positionStatus.Open; pos1.Symbol = new Symbol("SYM1"); pos1.price = 10; pos1.quantity = 1; Position pos2 = new Position(); pos2.status = positionStatus.Open; pos1.Symbol = new Symbol("SYM2"); pos2.price = 10; pos2.quantity = 1; p.Positions = new List<Position>(); p.Positions.Add(pos1); p.Positions.Add(pos2); Trade t = new Trade(); t.type = tradeTypes.Buy; t.Symbol = new Symbol("SYM2"); t.price = 1; t.quantity = 2; AllocationRule r = new AllocationRule(0.6); Assert.IsTrue(r.Apply(p, t)); }
// if this transaction violates any of our portfolio rules, this method will throw an exception public void ApplyRules(Portfolio p, Trade t) { foreach (PortfolioRule r in Rules) { r.Apply(p, t); } }
public void PlaceABuyTrade() { Portfolio port = new Portfolio(); port.Cash = 1000; Position pos = new Position(); pos.PositionId = 3; pos.quantity = 0; pos.price = 0; Trade t = new Trade(); PortfolioManager pm = new PortfolioManager(); // this test might need to initialize the rules pm.ProcessBuyTrade(t, "GOOG", 10, 2.5, pos, port); Assert.AreEqual("GOOG", t.SymbolName); Assert.AreEqual(10, t.quantity); Assert.AreEqual(2.5, t.price); Assert.AreEqual(tradeTypes.Buy, t.type); Assert.AreEqual(3, t.PositionId); Assert.AreEqual(10, pos.quantity); Assert.AreEqual(25, pos.price); Assert.AreEqual(975, port.Cash); }
public void EnoughFundsRuleNo() { Portfolio p = new Portfolio(); p.Cash = 1; Trade t = new Trade(); t.type = tradeTypes.Buy; t.quantity = 2; t.price = 10.25; EnoughFundsRule r = new EnoughFundsRule(); Assert.Throws<InsufficientFunds>(() => r.Apply(p, t)); }
public void EnoughFundsRuleYes() { Portfolio p = new Portfolio(); p.Cash = 5000; Trade t = new Trade(); t.type = tradeTypes.Buy; t.quantity = 2; t.price = 10.25; EnoughFundsRule r = new EnoughFundsRule(); Assert.IsTrue(r.Apply(p, t)); }
protected override void Seed(TraderContext context) { Portfolio portfolio = new Portfolio(); portfolio.Cash = 10000; context.Portfolios.Add(portfolio); context.SaveChanges(); Symbol s = new Symbol("GOOG"); context.Symbols.Add(s); context.SaveChanges(); Position pos1 = new Position(); pos1.price = 100; pos1.quantity = 5; pos1.status = positionStatus.Open; pos1.Symbol = s; pos1.Portfolio = portfolio; context.Positions.Add(pos1); context.SaveChanges(); Trade t1 = new Trade(); t1.Symbol = s; t1.Position = pos1; t1.price = 20; t1.quantity = 5; t1.type = tradeTypes.Buy; t1.TransactionId = Guid.NewGuid().ToString(); t1.timestamp = DateTime.Now; context.Trades.Add(t1); context.SaveChanges(); Quote q1 = new Quote(); q1.Symbol = s; q1.timestamp = DateTime.Now; q1.price = 20.15; context.Quotes.Add(q1); context.SaveChanges(); Alert a1 = new Alert(); a1.AlertId = Guid.NewGuid(); a1.Symbol = s; a1.Timestamp = DateTime.Now; a1.Type = tradeTypes.Buy; a1.Quantity = 100; a1.SentTo = "*****@*****.**"; a1.Price = 10.45; a1.ResponseCode = responseCodes.Pending; context.Alerts.Add(a1); context.SaveChanges(); SystemSetting emailaddress = new SystemSetting(); emailaddress.Module = "UserAgent"; emailaddress.Name = "ALERTS_EMAIL_ADDRESS_TO"; emailaddress.Value = "*****@*****.**"; context.SystemSettings.Add(emailaddress); context.SaveChanges(); //ThreeDuckStrategy context.SystemSettings.Add(new SystemSetting("ThreeDuckStrategy", "FIRST_DUCK_SECONDS", "604800")); context.SystemSettings.Add(new SystemSetting("ThreeDuckStrategy", "SECOND_DUCK_SECONDS", "86400")); context.SystemSettings.Add(new SystemSetting("ThreeDuckStrategy", "THIRD_DUCK_SECONDS", "43200")); context.SystemSettings.Add(new SystemSetting("ThreeDuckStrategy", "MOVING_AVERAGE_WINDOW", "10")); context.SaveChanges(); // Adam's stuff to fill in db tables string[] symbols = { "AAPL", "VZ", "INTC", "MSFT", "HP", "PANL", "NVDA", "QCOM", "AMD", "FB", "LNKD", "ZNGA" }; string[] watchlists = { "", "Other", "Test List", "Future Purchases" }; Random rand = new Random(); for (int i = 0; i < watchlists.Length; i++) { WatchList w = new WatchList(watchlists[i]); context.WatchLists.Add(w); } context.SaveChanges(); for (int i = 0; i < symbols.Length; i++) { Symbol symbol = new Symbol(symbols[i]); symbol.CompanyName = "Random Company Name, Inc."; context.Symbols.Add(symbol); WatchListItem wli = new WatchListItem(symbol, watchlists[rand.Next(0, watchlists.Length)]); context.WatchListItems.Add(wli); for (int k = 0; k < 10; k++) { Quote quote1 = new Quote(); quote1.price = Math.Round((rand.NextDouble() * (200 - 5) + 5), 2); quote1.timestamp = DateTime.Now.AddDays(-k); quote1.SymbolName = symbol.name; context.Quotes.Add(quote1); } Alert alert1 = new Alert(); alert1.AlertId = Guid.NewGuid(); alert1.Symbol = symbol; alert1.Timestamp = DateTime.Now.AddDays(-2); alert1.Type = tradeTypes.Buy; alert1.Quantity = 100; alert1.SentTo = "*****@*****.**"; alert1.Price = 20.00; alert1.ResponseCode = responseCodes.Pending; context.Alerts.Add(alert1); Alert alert2 = new Alert(); alert2.AlertId = Guid.NewGuid(); alert2.Symbol = symbol; alert2.Timestamp = DateTime.Now; alert2.Type = tradeTypes.Buy; alert2.Quantity = 100; alert2.SentTo = "*****@*****.**"; alert2.Price = 20.00; alert2.ResponseCode = responseCodes.Pending; context.Alerts.Add(alert2); Alert alert3 = new Alert(); alert3.AlertId = Guid.NewGuid(); alert3.Symbol = symbol; alert3.Timestamp = DateTime.Now.AddHours(-1); alert3.Type = tradeTypes.Buy; alert3.Quantity = 100; alert3.SentTo = "*****@*****.**"; alert3.Price = 20.00; alert3.ResponseCode = responseCodes.Pending; context.Alerts.Add(alert3); context.SaveChanges(); Position p = new Position(); p.status = positionStatus.Open; p.Symbol = symbol; p.Portfolio = portfolio; for (int j = 0; j < new Random().Next(1, 10); j++) { Trade t = new Trade(); t.Symbol = symbol; t.Position = p; t.price = Math.Round((rand.NextDouble() * (200 - 5) + 5), 2); t.quantity = rand.Next(1, 20); t.type = tradeTypes.Buy; t.TransactionId = Guid.NewGuid().ToString(); t.timestamp = DateTime.Now.AddSeconds(rand.Next(1, (60 * 60 * 24 * 60) + 1) * -1); // any time between 60 days ago p.price += t.price; context.Trades.Add(t); } p.Recalculate(); context.Positions.Add(p); } context.SaveChanges(); }
// builds a new Trade object to reflect the requested transaction, updates the position and portfolio public void ProcessBuyTrade(Trade t, string symbolName, int quantity, double price, Position pos, Portfolio port) { t.type = tradeTypes.Buy; t.SymbolName = symbolName; t.quantity = quantity; t.timestamp = DateTime.Now; t.price = price; ApplyRules(port, t); t.PositionId = pos.PositionId; pos.quantity += t.quantity; pos.price += t.price * t.quantity; port.Cash -= (t.price * t.quantity) - t.PaidCommission; }