Esempio n. 1
0
        public static void Main()
        {
            // Read settings.
            var apikey = ConfigurationManager.AppSettings["BitsoApikey"];
            var secret = ConfigurationManager.AppSettings["BitsoSecret"];
            var cointrackingCurrency = ConfigurationManager.AppSettings["CointrackingAccountCurrency"].ToUpperInvariant();

            // Connect to Bitso and download books (trading pairs).
            Console.Error.Write("Connecting to Bitso... ");
            var bitsoClient = new Bitso(apikey, secret, true);
            var books       = bitsoClient.PublicAPI.GetAvailableBooks();

            Console.Error.WriteLine("Done");

            // All downloaded transactions will be saved in this list.
            var transactions = new List <Transaction>();

            // Get withdrawals.
            Console.Error.Write("Downloading withdrawals... ");
            var withdrawals = bitsoClient.PrivateAPI.GetWithdrawals(BitsoQueryPageSize);

            transactions.AddRange(
                withdrawals.Select(w => new Transaction
            {
                Type         = TransactionType.Withdrawal,
                SellAmount   = w.AmountAsDecimal,
                SellCurrency = w.Currency.ToUpperInvariant(),
                TxId         = w.Wid,
                Exchange     = BitsoName,
                Date         = DateTime.Parse(w.CreatedAt, CultureInfo.InvariantCulture),
            }));
            Console.Error.WriteLine("Done");

            // Get fundings.
            Console.Error.Write("Downloading fundings... ");
            var currencyConverter = new CurrencyConverter();
            var fundings          = bitsoClient.PrivateAPI.GetFundings(BitsoQueryPageSize);

            transactions.AddRange(
                fundings.Select(w => new Transaction
            {
                Type = w.Currency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) &&
                       !cointrackingCurrency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) ?
                       TransactionType.Income :
                       TransactionType.Deposit,
                BuyAmount          = w.AmountAsDecimal,
                BuyCurrency        = w.Currency.ToUpperInvariant(),
                BuyValueInCurrency = w.Currency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) &&
                                     !cointrackingCurrency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) ?
                                     (decimal?)w.AmountAsDecimal * currencyConverter.GetExchangeRate(Mxn, cointrackingCurrency, DateTime.Parse(w.CreatedAt, CultureInfo.InvariantCulture)).Result :
                                     null,
                TxId     = w.Fid,
                Exchange = BitsoName,
                Date     = DateTime.Parse(w.CreatedAt, CultureInfo.InvariantCulture),
                Comment  = w.Currency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) &&
                           !cointrackingCurrency.Equals(Mxn, StringComparison.OrdinalIgnoreCase) ?
                           "Foreign currency deposit (not income)" :
                           null,
            }));
            Console.Error.WriteLine("Done");

            // BUGBUG: BitsoDotNet doesn't know how to get the next page of data of withdrawals and fundings.
            if (withdrawals.Length == BitsoQueryPageSize)
            {
                throw new NotImplementedException(
                          $"Can't download more than {BitsoQueryPageSize} withdrawals. " +
                          "Consider contributing to BitsoDotNet library and this project.");
            }
            else if (fundings.Length == BitsoQueryPageSize)
            {
                throw new NotImplementedException(
                          $"Can't download more than {BitsoQueryPageSize} fundings. " +
                          "Consider contributing to BitsoDotNet library and this project.");
            }

            // Get trades.
            foreach (var book in books)
            {
                Console.Error.Write($"Downloading trades for {book.Book}...");

                long marker = 0;
                do
                {
                    var userTrades = bitsoClient.PrivateAPI.GetUserTrades(
                        book: book.Book,
                        marker: marker.ToString(CultureInfo.InvariantCulture),
                        limit: BitsoQueryPageSize);

                    marker = 0;
                    foreach (var userTrade in userTrades)
                    {
                        var bookSymbols = userTrade.Book.ToUpperInvariant().Split('_');
                        var transaction = new Transaction
                        {
                            Type        = TransactionType.Trade,
                            Exchange    = BitsoName,
                            Date        = DateTime.Parse(userTrade.CreatedAt, CultureInfo.InvariantCulture),
                            TxId        = userTrade.Tid.ToString(CultureInfo.InvariantCulture),
                            FeeCurrency = userTrade.FeesCurrency.ToUpperInvariant(),
                            Fee         = userTrade.FeesAmountAsDecimal,
                        };

                        if (userTrade.Side.Equals("buy", StringComparison.OrdinalIgnoreCase))
                        {
                            transaction.BuyAmount    = userTrade.MajorAsDecimal - userTrade.FeesAmountAsDecimal;
                            transaction.BuyCurrency  = bookSymbols[0];
                            transaction.SellAmount   = -userTrade.MinorAsDecimal;
                            transaction.SellCurrency = bookSymbols[1];
                        }
                        else
                        {
                            transaction.BuyAmount    = userTrade.MinorAsDecimal - userTrade.FeesAmountAsDecimal;
                            transaction.BuyCurrency  = bookSymbols[1];
                            transaction.SellAmount   = -userTrade.MajorAsDecimal;
                            transaction.SellCurrency = bookSymbols[0];
                        }

                        transactions.Add(transaction);
                        marker = userTrade.Tid;

                        Console.Error.Write(".");
                    }
                }while (marker != 0);

                Console.Error.WriteLine(" Done");
            }

            // Print
            Console.Error.WriteLine("Sorting and outputting data to stdout:");
            Transaction.WriteCsvHeader(Console.Out);
            foreach (var transaction in transactions.OrderByDescending(t => t.Date).ThenByDescending(t => t.TxId))
            {
                transaction.WriteCsvLine(Console.Out);
            }

            Console.Error.WriteLine("Done sorting and outputting data to stdout.");
        }
Esempio n. 2
0
 internal PrivateAPI(Bitso bitsoClient) : base(bitsoClient)
 {
 }
Esempio n. 3
0
 internal PublicAPI(Bitso bitsoClient) : base(bitsoClient)
 {
 }
Esempio n. 4
0
 protected BitsoAPI(Bitso bistoClient)
 {
     BitsoClient = bistoClient;
 }
Esempio n. 5
0
        static void Main(string[] args)
        {
            try
            {
                //Get your API and secret keys at https://bitso.com/api_setup
                //These are not required if calling public API methods only
                var bitsoClient = new Bitso("[API_KEY]", "[SECRET_KEY]", production: false);

                //PUBLIC API

                Console.WriteLine("Get Available Books:");
                Console.WriteLine("-------------------");

                foreach (var book in bitsoClient.PublicAPI.GetAvailableBooks())
                {
                    Console.WriteLine($"[BOOK] {book.Book}, Max. Amount = {book.MaximumAmount}, Min. Amount = {book.MinimumAmount}");
                }

                Console.WriteLine("\r\nGet Order Book:");
                Console.WriteLine("-------------------");

                var orderBook = bitsoClient.PublicAPI.GetOrderBook();

                Console.WriteLine($"Asks = {orderBook.Asks.Length}, Bids = {orderBook.Bids.Length}, Sequence = {orderBook.Sequence}, UpdatedAt = {orderBook.UpdatedAt}");

                Console.WriteLine("\r\nGet Ticker:");
                Console.WriteLine("-------------------");

                var ticker = bitsoClient.PublicAPI.GetTicker();

                Console.WriteLine($"Book = {ticker.Book}, Last = {ticker.LastTradedPrice}, High = {ticker.PriceHigh}, Low = {ticker.PriceLow}");

                Console.WriteLine("\r\nGet Trades:");
                Console.WriteLine("-------------------");

                foreach (var trade in bitsoClient.PublicAPI.GetTrades())
                {
                    Console.WriteLine($"[TRADE] Tid = {trade.Tid}, Book = {trade.Book}, Price = {trade.Price}, Amount = {trade.Amount}");
                }

                //PRIVATE API

                Console.WriteLine("\r\nGet Account Status:");
                Console.WriteLine("-------------");

                var accountStatus = bitsoClient.PrivateAPI.GetAccountStatus();

                Console.WriteLine($"[ACCOUNT STATUS] {accountStatus.Status}, Daily Limit = {accountStatus.DailyLimit}, Daily Remaining = {accountStatus.DailyRemaining}, Monthly Limit = {accountStatus.MonthlyLimit}, Monthly Remaining = {accountStatus.MonthlyRemaining}");

                Console.WriteLine("\r\nGet Fees:");
                Console.WriteLine("-------------");

                var feeInfo = bitsoClient.PrivateAPI.GetFees();

                foreach (var fee in feeInfo.Fees)
                {
                    Console.WriteLine($"[FEE] {fee.Book}, Fee Decimal = {fee.FeeDecimal}, Fee Percent = {fee.FeePercent}");
                }

                Console.WriteLine("\r\nGet Ledger:");
                Console.WriteLine("-------------");

                var operations = bitsoClient.PrivateAPI.GetLedger();

                foreach (var operation in operations)
                {
                    Console.WriteLine($"[OPERATION] {operation.Eid}, Type = {operation.OperationType}");
                }

                Console.WriteLine("\r\nGet Withdrawals:");
                Console.WriteLine("-------------");

                foreach (var withdrawal in bitsoClient.PrivateAPI.GetWithdrawals())
                {
                    Console.WriteLine($"[WITHDRAWAL] {withdrawal.Wid}, Amount = {withdrawal.Amount} {withdrawal.Currency}");
                }

                Console.WriteLine("\r\nGet Fundings:");
                Console.WriteLine("-------------");

                var fundings = bitsoClient.PrivateAPI.GetFundings();

                foreach (var funding in fundings)
                {
                    Console.WriteLine($"[FUNDING] {funding.Fid}, Amount = {funding.Amount} {funding.Currency}");
                }

                Console.WriteLine("\r\nGet Account Balance:");
                Console.WriteLine("-------------");

                foreach (var balance in bitsoClient.PrivateAPI.GetAccountBalance())
                {
                    Console.WriteLine($"[BALANCE] {balance.Currency}, Total = {balance.Total}, Locked = {balance.Locked}, Available = {balance.Available}");
                }

                Console.WriteLine("\r\nGet User Trades:");
                Console.WriteLine("-------------");

                foreach (var userTrade in bitsoClient.PrivateAPI.GetUserTrades())
                {
                    Console.WriteLine($"[USER TRADE] {userTrade.Tid}, Oid = {userTrade.Oid}, Book = {userTrade.Book}, Side = {userTrade.Side}, Price = {userTrade.Price}");
                }

                Console.WriteLine("\r\nGet Open Orders:");
                Console.WriteLine("-------------");

                foreach (var order in bitsoClient.PrivateAPI.GetOpenOrders())
                {
                    Console.WriteLine($"[ORDER] Oid = {order.Oid}, Book = {order.Book}, Status = {order.Status}, Price = {order.Price}");
                }

                //Place an order to buy 0.5 bitcoin at a price of 1,000.00 mxn per bitcoin (to spend: 500.00 mxn)
                var newOrder = bitsoClient.PrivateAPI.PlaceOrder(
                    book: "btc_mxn", type: "limit", side: "buy", price: 1000.00M, majorAmount: 0.5M);

                //Place an order to sell 0.01 bitcoin at a price of 80,000.00 mxn per bitcoin (to earn: 800.00 mxn)
                var newOrder2 = bitsoClient.PrivateAPI.PlaceOrder(
                    book: "btc_mxn", type: "limit", side: "sell", price: 80000.00M, majorAmount: 0.01M);

                //You can also use CancelAllOpenOrders and CancelOrder
                foreach (var order in bitsoClient.PrivateAPI.CancelOpenOrders(newOrder.Oid, newOrder2.Oid))
                {
                    Console.WriteLine($"[CANCELED ORDER] {order}");
                }

                Console.WriteLine("\r\nGet Funding Destination:");
                Console.WriteLine("-------------------");

                var fundingDestination = bitsoClient.PrivateAPI.GetFundingDestination("BTC");

                Console.WriteLine($"[BTC FUNDING DESTINATION] {fundingDestination.AccountIdentifier} ({fundingDestination.AccountIdentifierName})");

                Console.WriteLine("\r\nGet Bank Codes:");
                Console.WriteLine("-------------");

                var bankCodes = bitsoClient.PrivateAPI.GetMexicanBankCodes();

                foreach (var bankCode in bankCodes)
                {
                    Console.WriteLine($"[BANK CODE] {bankCode.Code} {bankCode.Name}");
                }
            }
            catch (BitsoException bex)
            {
                Console.WriteLine($"[ERROR] Code {bex.ErrorCode}. {bex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[ERROR] {ex.Message}");
            }

            Console.WriteLine("\r\n\r\nPress any key to continue...");
            Console.Read();
        }
Esempio n. 6
0
 internal AccountCreationAPI(Bitso bitsoClient) : base(bitsoClient)
 {
 }