예제 #1
0
        private async void UpdateBalancesAsync()
        {
            if (this.updateBalancesCancellation != null)
            {
                this.updateBalancesCancellation.Cancel();
            }

            this.updateBalancesCancellation = new CancellationTokenSource();

            HitError error = null;

            await Task.Factory.StartNew(() => this.UpdateBalances(this.updateBalancesCancellation.Token, out error))
            .ContinueWith((t) =>
            {
                if (error == null)
                {
                    this.balancesCache.ForEach(b => this.PushMessage(b));
                }
                else
                {
                    var dealTicket = DealTicketGenerator.CreateRefuseDealTicket(error.ToString());

                    this.PushMessage(dealTicket);
                }
            });
        }
예제 #2
0
        public static string Format(this HitError hitError)
        {
            var sb = new StringBuilder();

            if (!string.IsNullOrEmpty(hitError.Message))
            {
                sb.Append(hitError.Message.TrimEnd('.')).Append(". ");
            }

            if (!string.IsNullOrEmpty(hitError.Description))
            {
                sb.Append(hitError.Description);
            }

            if (sb.Length == 0)
            {
                sb.Append(hitError.Code);
            }

            if (sb.Length == 0)
            {
                sb.Append("Unformatted error");
            }

            return(sb.ToString());
        }
예제 #3
0
        protected T CheckHitResponse <T>(HitResponse <T> hitResponse, out HitError hitError, bool pushDealTicketOnError = false)
        {
            T result = hitResponse.Result;

            hitError = hitResponse.Error;

            if (hitError != null && pushDealTicketOnError)
            {
                var dealTicket = DealTicketGenerator.CreateRefuseDealTicket(hitError.Format());

                this.PushMessage(dealTicket);
            }

            return(result);
        }
예제 #4
0
 public static string Format(this HitError hitError) => $"{hitError.Code}. {hitError.Message}. {hitError.Description}";
예제 #5
0
        private void UpdateBalances(CancellationToken token, out HitError hitError)
        {
            // Trading balances
            var tradingBalances = this.CheckHitResponse(this.socketApi.GetTradingBalanceAsync().Result, out hitError, true)?
                                  .Where(b => b.Available + b.Reserved > 0);

            if (tradingBalances == null || hitError != null || token.IsCancellationRequested)
            {
                return;
            }

            // Account balances
            var accountBalances = this.CheckHitResponse(this.restApi.GetAccountBalancesAsync().Result, out hitError, true)?
                                  .Where(b => b.Available + b.Reserved > 0);

            if (accountBalances == null || hitError != null || token.IsCancellationRequested)
            {
                return;
            }

            // Tickers
            var tickers = this.CheckHitResponse(this.restApi.GetTickersAsync().Result, out hitError, true)?
                          .ToDictionary(t => t.Symbol);

            if (tickers == null || hitError != null || token.IsCancellationRequested)
            {
                return;
            }

            var totalBalances = new Dictionary <string, (decimal total, decimal available, decimal reserved)>();

            foreach (var balance in tradingBalances)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                if (totalBalances.TryGetValue(balance.Currency, out var value))
                {
                    totalBalances[balance.Currency] = (value.total + balance.Available + balance.Reserved, value.available + balance.Available, value.reserved + balance.Reserved);
                }
                else
                {
                    totalBalances[balance.Currency] = (balance.Available + balance.Reserved, balance.Available, balance.Reserved);
                }
            }

            foreach (var balance in accountBalances)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                if (totalBalances.TryGetValue(balance.Currency, out var value))
                {
                    totalBalances[balance.Currency] = (value.total + balance.Available + balance.Reserved, value.available, value.reserved + balance.Reserved);
                }
                else
                {
                    totalBalances[balance.Currency] = (balance.Available + balance.Reserved, 0, balance.Reserved);
                }
            }

            foreach (var item in this.currenciesCache)
            {
                if (totalBalances.ContainsKey(item.Key))
                {
                    continue;
                }

                totalBalances[item.Key] = (0, 0, 0);
            }

            var messages = new List <MessageCryptoAssetBalances>();

            foreach (var item in totalBalances)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                string  currency         = item.Key;
                decimal totalBalance     = item.Value.total;
                decimal availableBalance = item.Value.available;
                decimal reservedBalance  = item.Value.reserved;

                decimal estimatedBTC = 0;
                decimal estimatedUSD = 0;

                if (currency == "BTC")
                {
                    estimatedBTC = totalBalance;
                }
                else
                {
                    if (tickers.TryGetValue($"{currency}BTC", out var ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedBTC = totalBalance * ticker.Last.Value;
                        }
                    }
                    else if (tickers.TryGetValue($"BTC{currency}", out ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedBTC = totalBalance / ticker.Last.Value;
                        }
                    }
                }

                if (currency == "USD")
                {
                    estimatedUSD = totalBalance;
                }
                else
                {
                    if (tickers.TryGetValue($"{currency}USDT", out var ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedUSD = totalBalance * ticker.Last.Value;
                        }
                    }
                    else if (tickers.TryGetValue($"USDT{currency}", out ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedUSD = totalBalance / ticker.Last.Value;
                        }
                    }
                    else if (tickers.TryGetValue($"{currency}USD", out ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedUSD = totalBalance * ticker.Last.Value;
                        }
                    }
                    else if (tickers.TryGetValue($"USD{currency}", out ticker))
                    {
                        if (ticker.Last.HasValue)
                        {
                            estimatedUSD = totalBalance / ticker.Last.Value;
                        }
                    }
                }

                messages.Add(new MessageCryptoAssetBalances
                {
                    AccountId        = ACCOUNT,
                    AssetId          = currency,
                    AvailableBalance = (double)availableBalance,
                    ReservedBalance  = (double)reservedBalance,
                    TotalBalance     = (double)totalBalance,
                    TotalInBTC       = (double)estimatedBTC,
                    TotalInUSD       = (double)estimatedUSD
                });
            }

            this.balancesCache = messages;
        }