public async Task ASXSearch(string stockCode, string option = "def", [Remainder] string parameter = "") { if (option != "def") { switch (option) { case "company": case "info": case "i": await ASXCompany(stockCode); return; case "annualreport": case "annual": case "report": case "r": case "ar": await ASXAnnualReport(stockCode); return; case "announce": case "announcements": case "an": case "ann": await ASXAnnouncements(stockCode); return; case "similar": case "sim": case "s": await ASXSimilar(stockCode); return; case "upcomingevents": case "e": case "agm": case "events": await ASXUpcomingEvents(stockCode); return; default: break; } } if (stockCode.ToUpper() == "XAO") { await ASXIndex(); return; } var dataResult = await _asxService.Lookup(stockCode); if (!dataResult.IsSuccess) { await ReplyAsync(dataResult.Message); return; } var data = dataResult.Payload; var embed = ASXSearchViewBuilders.BuildLookupView(data); await ReplyAsync("", embed : embed); }
public async Task <Result <PapertradePortfolioResult> > GetMoney(long userId) { var user = await GetOrCreateUser(userId); if (user.Transactions != null) { var holdings = new List <PapertradePortfolioHolding>(); var transactionsByStock = user.Transactions.GroupBy(x => x.StockCode); foreach (var stock in transactionsByStock) { var code = stock.Key; var buys = stock.Where(x => x.TransactionType == PapertradeTransactionType.BUY).ToList(); var bought = buys.Sum(x => x.Shares); var sold = stock.Where(x => x.TransactionType == PapertradeTransactionType.SELL) .Sum(x => x.Shares); var totalHeld = bought - sold; if (totalHeld == 0) { continue; } var orderedBuys = buys.OrderBy(x => x.Price).ToList(); var numberToReconcile = totalHeld; int smallestBuyIndex = 0; decimal totalSpent = 0; while (numberToReconcile != 0) { var smallestBuy = orderedBuys[smallestBuyIndex]; var reconcilingHere = Math.Min(numberToReconcile, smallestBuy.Shares); totalSpent += (reconcilingHere * smallestBuy.Price); numberToReconcile -= reconcilingHere; smallestBuyIndex++; } var lookup = await _asxService.Lookup(code); if (!lookup.IsSuccess) { return new Result <PapertradePortfolioResult> { IsSuccess = false, Message = lookup.Message } } ; var data = lookup.Payload; if (!data.Last_Price.HasValue) { return new Result <PapertradePortfolioResult>() { IsSuccess = false, Message = "Current price could not be retrieved." } } ; var lastPrice = (decimal)data.Last_Price.Value; var value = lastPrice * totalHeld; var totalGain = (totalHeld * lastPrice) - totalSpent; var holding = new PapertradePortfolioHolding() { StockCode = code, Quantity = totalHeld, PricePaid = totalSpent, Value = $"{value:F2}", LastPrice = lastPrice, TotalGain = $"${totalGain:F2}", TotalGainPercent = $"{(totalGain / totalSpent):P}" }; holdings.Add(holding); } return(new Result <PapertradePortfolioResult>() { IsSuccess = true, Message = String.Empty, Payload = new PapertradePortfolioResult() { Holdings = holdings, Money = user.Money, UserId = user.UserId } }); } return(new Result <PapertradePortfolioResult>() { IsSuccess = false, Message = String.Empty }); }