public async Task GetBalances(Prices prices) { var myPrices = new List <Tuple <string, double, double> >(); // Get the Bittrex balances. var accountBalances = await _bittrexApi.GetBalances(); // Check if there's an actual balance in there. if (accountBalances.Any(x => x.Balance > 0)) { // Loop actual balances. foreach (var item in accountBalances.Where(x => x.Balance > 0)) { // If the balance is not BTC, we need to get the BTC value from Bittrex. if (item.Currency != "BTC") { try { // Get the current value. var ticker = await _bittrexApi.GetTicker("BTC-" + item.Currency); if (ticker == null) { myPrices.Add(new Tuple <string, double, double>(item.Currency, 0, 0)); } else { myPrices.Add(new Tuple <string, double, double>(item.Currency, item.Balance * ticker.Last * (double)prices.Eur.Last, item.Balance * ticker.Last * (double)prices.Usd.Last)); } } catch (Exception) { myPrices.Add(new Tuple <string, double, double>(item.Currency, 0, 0)); } } else { // BTC can simply be added as is. myPrices.Add(new Tuple <string, double, double>(item.Currency, item.Balance * (double)prices.Eur.Last, item.Balance * (double)prices.Usd.Last)); } } Console.WriteLine(accountBalances.Where(x => x.Balance > 0) .ToStringTable <AccountBalance>( new[] { "Currency", "Available", "Balance", "Estimated EUR Value", "Estimated USD Value" }, x => x.Currency, x => x.Available.ToString("0.00000000"), x => x.Balance.ToString("0.00000000"), x => myPrices.FirstOrDefault(y => y.Item1 == x.Currency)?.Item2.ToString("0.00 EUR"), x => (myPrices.FirstOrDefault(y => y.Item1 == x.Currency)?.Item3.ToString("0.00 USD")))); Console.WriteLine($"\tTOTAL BALANCE: {(myPrices.Sum(x => x.Item2)):0.00} EUR / " + $"{(myPrices.Sum(x => x.Item3)):0.00} USD"); } else { Console.WriteLine("\tNo balances found..."); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "trades")] HttpRequestMessage req, TraceWriter log) { var tradeTable = await ConnectionManager.GetTableConnection(Constants.OrderTableName, Constants.IsDryRunning); var currentTrades = tradeTable.CreateQuery <Trade>().Where(x => x.IsOpen).ToList(); currentTrades = currentTrades.OrderBy(x => x.Market).ToList(); var api = new BittrexApi(); List <TradeDto> trades = new List <TradeDto>(); foreach (var trade in currentTrades) { var currentRate = await api.GetTicker(trade.Market); trades.Add(new TradeDto { Market = trade.Market, CloseRate = trade.CloseRate, OpenRate = trade.OpenRate, CloseProfit = trade.CloseProfitPercentage, IsOpen = trade.IsOpen, CloseDate = trade.CloseDate, StakeAmount = trade.StakeAmount, OpenDate = trade.OpenDate, Quantity = trade.Quantity, CurrentRate = currentRate.Ask, Uuid = trade.RowKey }); } return(req.CreateResponse(HttpStatusCode.OK, trades)); }
private void btnTickers_Click(object sender, RoutedEventArgs e) { TikerResponse response = BittrexApi.GetTicker("btc-trx"); if (response.success) { List <Tiker> list = new List <Tiker>(); list.Add(response.result); coinsgrd.ItemsSource = list; } else { MessageBox.Show(response.message); } }
/// <summary> /// Checks the implemented trading indicator(s), /// if one pair triggers the buy signal a new trade record gets created. /// </summary> /// <param name="trades"></param> /// <param name="amountOfBtcToInvestPerTrader"></param> /// <returns></returns> private async Task <Trade> FindTrade(List <Trade> trades, double amountOfBtcToInvestPerTrader) { // Get our Bitcoin balance from the exchange var currentBtcBalance = await _api.GetBalance("BTC"); // Do we even have enough funds to invest? if (currentBtcBalance < Constants.AmountOfBtcToInvestPerTrader) { throw new Exception("Insufficient BTC funds to perform a trade."); } // Retrieve our current markets var markets = await _api.GetMarketSummaries(); // Check if there are markets matching our volume. markets = markets.Where(x => (x.BaseVolume > Constants.MinimumAmountOfVolume || Constants.AlwaysTradeList.Contains(x.MarketName)) && x.MarketName.StartsWith("BTC-")).ToList(); // Remove existing trades from the list to check. foreach (var trade in trades) { markets.RemoveAll(x => x.MarketName == trade.Market); } // Remove items that are on our blacklist. foreach (var market in Constants.MarketBlackList) { markets.RemoveAll(x => x.MarketName == market); } // Check the buy signal! string pair = null; // Prioritize markets with high volume. foreach (var market in markets.Distinct().OrderByDescending(x => x.BaseVolume).ToList()) { if (await GetBuySignal(market.MarketName)) { // A match was made, buy that please! pair = market.MarketName; break; } } // No pairs found. Return. if (pair == null) { return(null); } var openRate = GetTargetBid(await _api.GetTicker(pair)); var amount = amountOfBtcToInvestPerTrader / openRate; var amountYouGet = (amountOfBtcToInvestPerTrader * (1 - Constants.TransactionFeePercentage)) / openRate; var orderId = await _api.Buy(pair, openRate, amount); return(new Trade() { Market = pair, StakeAmount = Constants.AmountOfBtcToInvestPerTrader, OpenRate = openRate, OpenDate = DateTime.UtcNow, Quantity = amountYouGet, OpenOrderId = orderId, BuyOrderId = orderId, IsOpen = true, StrategyUsed = _strategy.Name, PartitionKey = "TRADE", SellType = SellType.None, RowKey = $"MNT{(DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks):d19}" }); }