Example #1
0
        private void HandleMarginResponse(MarginResponse response)
        {
            if (response.Data.Count() == 0)
            {
                return;
            }

            try
            {
                MarginStatsMutex.WaitOne();

                if (response.Action == BitmexAction.Partial || response.Action == BitmexAction.Insert || response.Action == BitmexAction.Update)
                {
                    MarginStats ma = null;
                    MarginStats mb = null;

                    foreach (Margin m in response.Data)
                    {
                        if (m.Account == Accounts[MTAccount.A])
                        {
                            _view.AccountAID = m.Account.ToString();

                            _marginStatsHandler.UpdateBalances(ZoneRecoveryAccount.A, m.WalletBalance, m.MarginBalance, m.AvailableMargin);
                            ma = _marginStatsHandler.GetMarginBalances(ZoneRecoveryAccount.A);

                            _view.TotalFundsA     = BitmexConverter.ConvertToBtc("XBt", ma.WalletBalance).ToString();
                            _view.AvailableFundsA = BitmexConverter.ConvertToBtc("XBt", ma.MarginBalance).ToString();
                            _view.MarginBalanceA  = BitmexConverter.ConvertToBtc("XBt", ma.AvailableMargin).ToString();
                        }
                        else if (m.Account == Accounts[MTAccount.B])
                        {
                            _view.AccountBID = m.Account.ToString();

                            _marginStatsHandler.UpdateBalances(ZoneRecoveryAccount.B, m.WalletBalance, m.MarginBalance, m.AvailableMargin);
                            mb = _marginStatsHandler.GetMarginBalances(ZoneRecoveryAccount.B);

                            _view.TotalFundsB     = BitmexConverter.ConvertToBtc("XBt", mb.WalletBalance).ToString();
                            _view.AvailableFundsB = BitmexConverter.ConvertToBtc("XBt", mb.MarginBalance).ToString();
                            _view.MarginBalanceB  = BitmexConverter.ConvertToBtc("XBt", mb.AvailableMargin).ToString();
                        }
                    }

                    var a = string.IsNullOrEmpty(_view.TotalFundsA) ? 0.0 : double.Parse(_view.TotalFundsA);
                    var b = string.IsNullOrEmpty(_view.TotalFundsB) ? 0.0 : double.Parse(_view.TotalFundsB);

                    _view.CashImbalance = "L " + Math.Round((a / (a + b) * 100), 2).ToString() + " - S " + Math.Round((b / (b + a) * 100), 2).ToString();
                }
            }
            catch (Exception exc)
            {
                string message = $"[HandleMargin]: {exc.Message}";
                Log.Error(message);
                Console.WriteLine(message);
            }
            finally
            {
                MarginStatsMutex.ReleaseMutex();
            }
        }
        private double?ConvertToBtc(string currency, long?value)
        {
            if (!value.HasValue)
            {
                return(null);
            }

            return(BitmexConverter.ConvertToBtc(currency, value.Value));
        }