public List <Balances> GetPortfolio() { lock (_lock) { try { var result = CreateQuery("GET", "/api/2/trading/balance", _pubKey, _secKey, true); List <Balances> balances = JsonConvert.DeserializeAnonymousType(result, new List <Balances>()); BalanceInfo balanceinfo = new BalanceInfo(); balanceinfo.Balances = balances; balanceinfo.Name = _portfolioName; if (NewPortfolio != null) { NewPortfolio(balanceinfo); } return(balances); } catch (Exception exception) { if (LogMessageEvent != null) { SendLogMessage(exception.ToString(), LogMessageType.Error); } return(null); } } }
private void _client_NewPortfolio(BalanceInfo balance) { try { if (balance == null) { return; } if (_portfolios == null) { _portfolios = new List <Portfolio>(); } var cryptoPortfolio = new Portfolio(); cryptoPortfolio.Number = balance.Name; var positionsOnBoard = new List <PositionOnBoard>(); foreach (var portfolio in balance.Balances) { var needPos = positionsOnBoard.Find(p => p.SecurityNameCode == portfolio.currency); decimal avail = portfolio.available.ToDecimal(); decimal reser = portfolio.reserved.ToDecimal(); if (needPos == null) { needPos = new PositionOnBoard(); needPos.SecurityNameCode = portfolio.currency; needPos.ValueCurrent = avail; needPos.ValueBlocked = reser; if (needPos.ValueCurrent != 0 || needPos.ValueBlocked != 0) { positionsOnBoard.Add(needPos); } } else { if (avail != 0) { needPos.ValueCurrent = avail; } else if (reser != 0) { needPos.ValueBlocked = reser; } } } foreach (var pos in positionsOnBoard) { cryptoPortfolio.SetNewPosition(pos); } _portfolios.Add(cryptoPortfolio); PortfolioEvent?.Invoke(_portfolios); } catch (Exception e) { if (LogMessageEvent != null) { SendLogMessage(e.ToString(), LogMessageType.Error); } } }