JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.05k stars 429 forks source link

Problem with GetAccountInfo Balances #23

Closed kingchenc closed 6 years ago

kingchenc commented 6 years ago

Hello, im not the best c# programmer, im very new here... i cant "read" the full Balance, or the Balance from a single Coin in BTC/XY Coin... i can only get the Count or Capacity..? With accountInfo.Data.Balances.Count/Capacity, but how //BTC/Xy Coin? thank you :)

JKorf commented 6 years ago

Hi, The accountInfo.Data.Balances is a list of all your holdings. You can for example loop through the results:

foreach(var balance in accountInfo.Data.Balances)
{
    Console.WriteLine($"Balance of {balance.Asset}: {balance.Total}");
}

or search for a single asset using LINQ (add using System.Linq; to the top of your file)

var balance = accountInfo.Data.Balances.SingleOrDefault(x => x.Asset == "BTCETH"); // Or some other asset
kingchenc commented 6 years ago

Thanks Jkorf for your help and great work, but can you give me please some Code snipet for a console/writeline application for search a single asset using linq? (with"foreach(var balance in accountInfo.Data.Balances)" i have no problems but the other code....) Its rly hard to understand, when you learning fresh a "language", so not rly understand how to work with "var balance = accountInfo.Data.Balances.SingleOrDefault(x => x.Asset == "BTCETH"); // Or some other asset" thank you :)

JKorf commented 6 years ago

Here is a Console application which will tell you the balance of something when you type it:

using Binance.Net;
using Binance.Net.Objects;
using System;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string apiKey = "YOUR API KEY";
            string apiSecret = "YOUR API SECRET";
            BinanceAccountInfo accountInfo;

            using (var client = new BinanceClient(apiKey, apiSecret))
            {
                client.AutoTimestamp = true;
                var infoResult = client.GetAccountInfo();
                if (!infoResult.Success)
                {
                    Console.WriteLine("Couldn't get account info! " + infoResult.Error.Message);
                    Console.ReadLine();
                }
                accountInfo = infoResult.Data;
            }

            Console.WriteLine("Insert asset to check balance for");
            while (true)
            {
                var input = Console.ReadLine();
                var asset = accountInfo.Balances.SingleOrDefault(balance => balance.Asset.ToLower() == input.ToLower());
                if (asset == null)
                    Console.WriteLine("No balance found for " + input);
                else
                    Console.WriteLine("Balance for " + input + ": " + asset.Total);
            }
        }
    }
}

Remember to change the apikey/secret.

In my previous reaction I was talking about "BTCETH" where I should have said "BTC" or "ETH". Sorry for that. Anyway, if every thing goes okay when you type in "BTC" in this console application it should give you the amount of BTC you have.

Hope this helps!

kingchenc commented 6 years ago

Thank you very much and for your patience Jkorf, now i understand how this works with your Code sniped, thats helped my! <3

0000day commented 3 years ago

I cant find "GetAccountInfo" on the BinanceClient!?

MohamadRezaSafari commented 3 years ago

why are you dont answer ?