public IEnumerable <TradeTransaction> LoadTransactions(Stream stream) { var result = new List <TradeTransaction>(); using (var reader = new StreamReader(stream)) { // Read and ignore first row var line = reader.ReadLine(); if (line != null) { line = reader.ReadLine(); while (line != null) { var tokens = line.Split(','); if (tokens.Length == 12) { var txn = new TradeTransaction(DateTime.Parse(tokens[2]), tokens[4], tokens[3] == "B" ? TradeTypes.Buy : TradeTypes.Sell, Convert.ToInt32(tokens[5]), Convert.ToDecimal(tokens[7]), Convert.ToDecimal(tokens[8])); result.Add(txn); } line = reader.ReadLine(); } } } return(result); }
public void Should_UpdatePriceLevel_When_Price_Crosses_PriceLevel() { var mockHttpClient = new Mock <IHttpPriceLevelRepository>(); var mockLogger = new Mock <ILogger <PriceTradeExecutor> >(); var mockCache = new Mock <ICacheManager>(); var mockTradeProfile = new Mock <ITradeProfileFactory>(); var p = new TradeParameters() { BuySell = "" }; var trans = new TradeTransaction(p) { Closed = false }; //mockTradeProfile.Setup(a=>a.GetTradeGenerationService(It.IsAny<string>()).Generate(It.IsAny<PriceDto>(),It.IsAny<string>())).Returns(trans); var priceLevels = new List <PriceLevelDto>() { new PriceLevelDto() { BuySell = "BUY", AskPrice = 1.3000m }, new PriceLevelDto() { BuySell = "SELL", BidPrice = 1.2000m } }; var lastPrice = new PriceDto() { Ask = 1.3001m, Bid = 1.1999m }; mockCache.Setup(x => x.GetAsync <List <PriceLevelDto> >("price-levels")).ReturnsAsync(priceLevels); mockCache.Setup(x => x.GetAsync <PriceDto>("price")) .ReturnsAsync(lastPrice); var mockPublisher = new Mock <IPriceTradePublisher>(); var subject = new PriceTradeExecutor(mockLogger.Object, mockCache.Object, mockPublisher.Object); var price = new PriceDto() { Ask = 1.2999m, Bid = 1.2001m }; subject.Execute(price, 0.0000m); mockHttpClient.Verify(a => a.UpdatePriceLevel(It.IsAny <PriceLevelDto>()), Times.Exactly(2)); }
public void Reset() { Result = TradeDialogResult.Pending; Transaction = null; EnvoyColumns.Reconstruct(Envoy.Resources, new List <ResourceAmount>(), (int)Envoy.Money); PlayerColumns.Reconstruct(Player.Resources, new List <ResourceAmount>(), (int)Player.Money); UpdateBottomDisplays(); Layout(); }
public void GetTotalPurchasePrice_Use3DecimalPlaceSharePrice_ExpectAccurateResult() { // Arrange decimal purchaseSharePrice = 21.792M; int numShares = 432; TradeTransaction transaction = new TradeTransaction(null, 1, TransactionType.Buy, DateTime.Now.AddMonths(-4), numShares, purchaseSharePrice); // Act decimal totalPrice = transaction.GetTotalPurchasePrice(); // Assert Assert.AreEqual(totalPrice, purchaseSharePrice * numShares); }
public void GetCostBasisPerShare_TypicalValues_ExpectCorrectAnswer() { // Arrange decimal originalSharePrice = 125.44M; int numShares = 44; InvestmentProduct investmentProduct = new InvestmentProduct(1, "ABC", 1223.67M, DateTime.Now); TradeTransaction purchaseTransaction = new TradeTransaction(_nLogger, 1, TransactionType.Buy, DateTime.Now, numShares, originalSharePrice); UserInvestment userInvestment = new UserInvestment(_nLogger, investmentProduct, purchaseTransaction); // Act decimal basisCost = userInvestment.GetCostPerBasisShare(); // Assert Assert.AreEqual(originalSharePrice * numShares, basisCost); }
public void GetTerm_GreaterThan365_expectLong() { // Arrange decimal originalSharePrice = 125.44M; decimal currentPrice = 99.33M; int numShares = 44; DateTime currentDate = new DateTime(2020, 5, 12); DateTime purchaseDate = new DateTime(2018, 1, 15); InvestmentProduct investmentProduct = new InvestmentProduct(1, "ABC", currentPrice, currentDate); TradeTransaction purchaseTransaction = new TradeTransaction(_nLogger, 1, TransactionType.Buy, purchaseDate, numShares, originalSharePrice); UserInvestment userInvestment = new UserInvestment(_nLogger, investmentProduct, purchaseTransaction); // Act InvestmentTerm actualTerm = userInvestment.GetTerm(); // Assert Assert.AreEqual(InvestmentTerm.Long, actualTerm); }
public void GetTotalGainLoss_PriceDropped_ExpectCorrectNegativeAnswer() { // Arrange decimal originalSharePrice = 125.44M; decimal currentPrice = 99.33M; int numShares = 44; InvestmentProduct investmentProduct = new InvestmentProduct(1, "ABC", currentPrice, DateTime.Now); TradeTransaction purchaseTransaction = new TradeTransaction(_nLogger, 1, TransactionType.Buy, DateTime.Now.AddMonths(-4), numShares, originalSharePrice); UserInvestment userInvestment = new UserInvestment(_nLogger, investmentProduct, purchaseTransaction); // Act decimal loss = userInvestment.GetTotalGainLoss(); // Assert decimal expectedLoss = (currentPrice - originalSharePrice) * numShares; Assert.AreEqual(expectedLoss, loss); }
// trade shares public int TradeShares(TradeTransaction trade, int parentCorpShares) { int maxShares; int shares; maxShares = trade.Player.Shares(trade.MergedCorp); if (parentCorpShares < trade.Player.Shares(trade.MergedCorp)) { maxShares = parentCorpShares; } if (maxShares == 0 || maxShares == 1) { return(0); } shares = rand.Next() % maxShares; // only return an even number return((shares % 2) == 0? shares: shares - 1); }
private void DisplayInvestmentDetails(UserInvestment selectedInvestment) { try { InvestmentProduct product = selectedInvestment.GetInvestmentProduct(); TradeTransaction transaction = selectedInvestment.GetPurchaseTransaction(); investmentName.InnerText = product.Name; numShares.InnerText = transaction.NumShares.ToString();; costBasisPerShare.InnerText = selectedInvestment.GetCostPerBasisShare().ToString("C3"); currentValue.InnerText = selectedInvestment.GetCurrentValue().ToString("C3"); currentPrice.InnerText = product.CurrentPrice.ToString("C3"); term.InnerText = selectedInvestment.GetTerm().ToString(); totalGainLoss.InnerText = selectedInvestment.GetTotalGainLoss().ToString("C3"); _nLogger.Info($"Displaying details for {product.Name}"); } catch (Exception ex) { errorLabel.InnerText = "Error displaying investment details. Please try again later"; _nLogger.Error(ex, "Exception displaying investment details"); } }
// decision tree // // Cash - buy 0 shares -> TRADE_HALF // | // buy 1 share // \|/ // TRADE_ALL // trade shares public int TradeShares(TradeTransaction trade, int parentCorpShares) { int lowestprice = Int32.MaxValue; int shares = 0; // find the cheapest share price foreach (CorpStats corp in Corporations) { if (corp.Size > 0 && corp.Shares > 0 && corp.Price < lowestprice) { lowestprice = corp.Price; } } // calculate the max number of shares which can be traded shares = (Player.Shares(trade.MergedCorp) > (parentCorpShares * 2)) ? (parentCorpShares * 2) : Player.Shares(trade.MergedCorp); // ensure they are even if (shares % 2 != 0) { shares--; } if (Player.Cash > lowestprice) { // trade max return(shares); } else { // trade half shares = shares / 2; if (shares % 2 != 0) { shares--; } return(shares); } }
public IEnumerable <UserInvestment> GetAllUserInvestments(int userId) { List <UserInvestment> products = new List <UserInvestment>(); InvestmentProduct productAbc = new InvestmentProduct(1, "Grogu Heavy Lifting", 1.23M, DateTime.Now); TradeTransaction transactionAbc = new TradeTransaction(_nLogger, 1, TransactionType.Buy, new DateTime(2018, 4, 15), 100, 0.55M); UserInvestment investmentAbc = new UserInvestment(_nLogger, productAbc, transactionAbc); products.Add(investmentAbc); InvestmentProduct productBcd = new InvestmentProduct(2, "Guild Enterprises", 44.78M, DateTime.Now); TradeTransaction transactionBcd = new TradeTransaction(_nLogger, 1, TransactionType.Buy, new DateTime(2020, 11, 15), 1500, 71.00M); UserInvestment investmentBcd = new UserInvestment(_nLogger, productBcd, transactionBcd); products.Add(investmentBcd); InvestmentProduct productCde = new InvestmentProduct(3, "Karga Holdings", 1434.78M, DateTime.Now); TradeTransaction transactionCde = new TradeTransaction(_nLogger, 1, TransactionType.Buy, new DateTime(2019, 2, 1), 50, 1042.55M); UserInvestment investmentCde = new UserInvestment(_nLogger, productCde, transactionCde); products.Add(investmentCde); return(products); }
public override void Construct() { Transaction = null; Result = TradeDialogResult.Pending; Border = "border-fancy"; Balance = AddChild(new Balance() { AutoLayout = AutoLayout.DockBottom, MinimumSize = new Point(32 * 3, 64), }) as Balance; var bottomRow = AddChild(new Widget { AutoLayout = AutoLayout.DockBottom, MinimumSize = new Point(0, 32) }); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Propose Trade", AutoLayout = AutoLayout.DockRight, OnClick = (sender, args) => { if (EnvoyColumns.Valid && PlayerColumns.Valid) { var net = ComputeNetValue(); var envoyOut = Envoy.ComputeValue(EnvoyColumns.SelectedResources) + EnvoyColumns.TradeMoney; var tradeTarget = 1.0m; if (PlayerColumns.SelectedResources.Count == 0 && EnvoyColumns.SelectedResources.Count == 0 && EnvoyColumns.TradeMoney == 0 && PlayerColumns.TradeMoney == 0) { Root.ShowTooltip(Root.MousePosition, "You've selected nothing to trade."); } else if (net >= tradeTarget) { Result = TradeDialogResult.Propose; Transaction = new TradeTransaction { EnvoyEntity = Envoy, EnvoyItems = EnvoyColumns.SelectedResources, EnvoyMoney = EnvoyColumns.TradeMoney, PlayerEntity = Player, PlayerItems = PlayerColumns.SelectedResources, PlayerMoney = PlayerColumns.TradeMoney }; Root.SafeCall(OnPlayerAction, this); } else { Result = TradeDialogResult.RejectProfit; Root.SafeCall(OnPlayerAction, this); } } else { Root.ShowTooltip(Root.MousePosition, "Trade is invalid"); } } }); var autoButton = bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Make this work", Tooltip = "What will make this work?", AutoLayout = AutoLayout.DockRight, OnUpdate = (sender, gameTime) => { DwarfBux net, tradeTarget; CalculateTradeAmount(out net, out tradeTarget); (sender as Gui.Widgets.Button).Enabled = net < tradeTarget; sender.Invalidate(); }, OnClick = (sender, args) => { if ((sender as Gui.Widgets.Button).Enabled) { EqualizeColumns(); } } }); Root.RegisterForUpdate(autoButton); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Clear", Tooltip = "Clear trade", AutoLayout = AutoLayout.DockRight, OnClick = (sender, args) => { EnvoyColumns.Reconstruct(Envoy.Resources, new List <ResourceAmount>(), (int)Envoy.Money); PlayerColumns.Reconstruct(Player.Resources, new List <ResourceAmount>(), (int)Player.Money); UpdateBottomDisplays(); Layout(); } }); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Stop", Tooltip = "Stop trading.", AutoLayout = AutoLayout.DockRight, //OnLayout = (sender) => sender.Rect.X -= 16, OnClick = (sender, args) => { Result = TradeDialogResult.Cancel; Root.SafeCall(OnPlayerAction, this); //this.Close(); } }); TotalDisplay = Balance.AddChild(new Widget { MinimumSize = new Point(128, 64), AutoLayout = AutoLayout.DockBottom, Font = "font10", TextColor = new Vector4(0, 0, 0, 1), TextVerticalAlign = VerticalAlign.Bottom, TextHorizontalAlign = HorizontalAlign.Center }); SpaceDisplay = bottomRow.AddChild(new Widget { //MinimumSize = new Point(128, 0), AutoLayout = AutoLayout.DockFill, Font = "font10", TextColor = new Vector4(0, 0, 0, 1), TextVerticalAlign = VerticalAlign.Center, MaximumSize = new Point(256, 32), Tag = "trade_stocks" }); var mainPanel = AddChild(new Columns { AutoLayout = AutoLayout.DockFill }); PlayerColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Player, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, ReverseColumnOrder = true, LeftHeader = "Our Items", RightHeader = "We Offer", MoneyLabel = "Our money", OnTotalSelectedChanged = (s) => UpdateBottomDisplays(), Tag = "trade_money" }) as ResourceColumns; EnvoyColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Envoy, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, LeftHeader = "Their Items", RightHeader = "They Offer", MoneyLabel = "Their money", OnTotalSelectedChanged = (s) => UpdateBottomDisplays() }) as ResourceColumns; UpdateBottomDisplays(); }
// trade shares public int TradeShares(TradeTransaction trade, int parentCorpShares) { Setup(); return(comp.TradeShares(trade, parentCorpShares)); }
public override void Construct() { Transaction = null; Result = TradeDialogResult.Pending; Border = "border-fancy"; var bottomRow = AddChild(new Widget { AutoLayout = AutoLayout.DockBottom, MinimumSize = new Point(0, 32) }); TotalDisplay = bottomRow.AddChild(new Widget { MinimumSize = new Point(128, 0), AutoLayout = AutoLayout.DockLeft, Font = "font10", TextColor = new Vector4(0, 0, 0, 1), TextVerticalAlign = VerticalAlign.Center }); SpaceDisplay = bottomRow.AddChild(new Widget { MinimumSize = new Point(128, 0), AutoLayout = AutoLayout.DockLeft, Font = "font10", TextColor = new Vector4(0, 0, 0, 1), TextVerticalAlign = VerticalAlign.Center }); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Propose Trade", AutoLayout = AutoLayout.DockRight, OnClick = (sender, args) => { if (EnvoyColumns.Valid && PlayerColumns.Valid) { var net = ComputeNetValue(); var envoyOut = Envoy.ComputeValue(EnvoyColumns.SelectedResources) + EnvoyColumns.TradeMoney; var tradeTarget = envoyOut * 0.25; if (PlayerColumns.SelectedResources.Count == 0 && EnvoyColumns.SelectedResources.Count == 0 && EnvoyColumns.TradeMoney == 0 && PlayerColumns.TradeMoney == 0) { Result = TradeDialogResult.Cancel; this.Close(); } else if (net >= tradeTarget) { Result = TradeDialogResult.Propose; Transaction = new TradeTransaction { EnvoyEntity = Envoy, EnvoyItems = EnvoyColumns.SelectedResources, EnvoyMoney = EnvoyColumns.TradeMoney, PlayerEntity = Player, PlayerItems = PlayerColumns.SelectedResources, PlayerMoney = PlayerColumns.TradeMoney }; this.Close(); } else { Result = TradeDialogResult.RejectProfit; this.Close(); } } else { Root.ShowTooltip(Root.MousePosition, "Trade is invalid"); } } }); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "What will make this work?", AutoLayout = AutoLayout.DockRight, OnClick = (sender, args) => EqualizeColumns() }); bottomRow.AddChild(new Gui.Widgets.Button { Font = "font10", Border = "border-button", TextColor = new Vector4(0, 0, 0, 1), Text = "Cancel", AutoLayout = AutoLayout.DockRight, OnLayout = (sender) => sender.Rect.X -= 16, OnClick = (sender, args) => { Result = TradeDialogResult.Cancel; this.Close(); } }); var mainPanel = AddChild(new Columns { AutoLayout = AutoLayout.DockFill }); EnvoyColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Envoy, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, LeftHeader = "Their Items", RightHeader = "They Offer", MoneyLabel = "Their money", OnTotalSelectedChanged = (s) => UpdateBottomDisplays() }) as ResourceColumns; PlayerColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Player, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, ReverseColumnOrder = true, LeftHeader = "Our Items", RightHeader = "We Offer", MoneyLabel = "Our money", OnTotalSelectedChanged = (s) => UpdateBottomDisplays() }) as ResourceColumns; UpdateBottomDisplays(); }
public override void Construct() { Transaction = null; Result = TradeDialogResult.Pending; Border = "border-fancy"; var bottomRow = AddChild(new Widget { AutoLayout = AutoLayout.DockBottom, MinimumSize = new Point(0, 32) }); TotalDisplay = bottomRow.AddChild(new Widget { MinimumSize = new Point(128, 0), AutoLayout = AutoLayout.DockLeft, Font = "outline-font", TextColor = new Vector4(1, 1, 1, 1), TextVerticalAlign = VerticalAlign.Center }); SpaceDisplay = bottomRow.AddChild(new Widget { MinimumSize = new Point(128, 0), AutoLayout = AutoLayout.DockLeft, Font = "outline-font", TextColor = new Vector4(1, 1, 1, 1), TextVerticalAlign = VerticalAlign.Center }); bottomRow.AddChild(new Widget { Font = "outline-font", Border = "border-button", TextColor = new Vector4(1, 1, 1, 1), Text = "Propose Trade", AutoLayout = AutoLayout.DockRight, OnClick = (sender, args) => { if (EnvoyColumns.Valid && PlayerColumns.Valid) { Result = TradeDialogResult.Propose; Transaction = new TradeTransaction { EnvoyEntity = Envoy, EnvoyItems = EnvoyColumns.SelectedResources, EnvoyMoney = EnvoyColumns.TradeMoney, PlayerEntity = Player, PlayerItems = PlayerColumns.SelectedResources, PlayerMoney = PlayerColumns.TradeMoney }; this.Close(); } else { Root.ShowTooltip(Root.MousePosition, "Trade is invalid"); } } }); bottomRow.AddChild(new Widget { Font = "outline-font", Border = "border-button", TextColor = new Vector4(1, 1, 1, 1), Text = "Cancel", AutoLayout = AutoLayout.DockRight, Padding = new Margin(0, 0, 0, 16), OnClick = (sender, args) => { Result = TradeDialogResult.Cancel; this.Close(); } }); var mainPanel = AddChild(new TwoColumns { AutoLayout = AutoLayout.DockFill }); EnvoyColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Envoy, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, LeftHeader = "Their Items", RightHeader = "They Offer", OnTotalSelectedChanged = (s) => UpdateBottomDisplays() }) as ResourceColumns; PlayerColumns = mainPanel.AddChild(new ResourceColumns { TradeEntity = Player, ValueSourceEntity = Envoy, AutoLayout = AutoLayout.DockFill, ReverseColumnOrder = true, LeftHeader = "Our Items", RightHeader = "We Offer", OnTotalSelectedChanged = (s) => UpdateBottomDisplays() }) as ResourceColumns; UpdateBottomDisplays(); }