示例#1
0
 private Position ToCommonPosition(ApiPosition position)
 {
     return(new Position
     {
         AccountId = AccountInfo.ID,
         BrokerName = Name,
         DataFeedName = Name,
         Symbol = position.CurrencyPair.ToString(),
         Quantity = position.Size,
         Profit = position.PL,
         PositionSide = ToCommonPositionSide(position.Side),
         CurrentPrice = position.BasePrice, //price per coin
         Price = position.Total             //position value (price * quantity)
     });
 }
示例#2
0
        private async Task UpdatePositions()
        {
            bool anyPosUpdated   = false;
            var  closedPositions = new List <CurrencyPair>();

            if (_isMarginAccount == true)
            {
                List <ApiPosition> positions = null;
                try { positions = await _api.Trading.GetMarginPositions(); }
                catch (Exception e)
                {
                    var error = $"Failed to retrieve positions from {Name}: {e.Message}";
                    System.Diagnostics.Trace.TraceError(error);  //OnError(error);
                }

                _previousUpdates[UpdateDataType.Positions] = DateTime.UtcNow;
                if (positions != null && positions.Count > 0)
                {
                    //add or update existing positions
                    foreach (var pos in positions)
                    {
                        if (!_positions.ContainsKey(pos.CurrencyPair) ||
                            _positions[pos.CurrencyPair].Size != pos.Size)
                        {
                            _positions[pos.CurrencyPair] = pos;
                            anyPosUpdated = true;
                        }
                    }

                    //mark closed positions
                    foreach (var item in _positions)
                    {
                        if (!positions.Any(p => p.CurrencyPair == item.Key))
                        {
                            closedPositions.Add(item.Key);
                        }
                    }
                }
            }
            else  //optional: simply retrieve currency balances and process them as positions
            {
                List <PoloniexAPI.WalletTools.Balance> balances = null;
                try { balances = await _api.Wallet.GetBalances(); }
                catch { }

                if (balances != null && balances.Count > 0)
                {
                    var baseCur = AccountInfo?.Currency ?? Currency;
                    //add or update existing positions
                    foreach (var b in balances)
                    {
                        if (baseCur == b.Currency)  //that would be 'balance'
                        {
                            AccountInfo.Balance = b.QuoteAvailable;
                            AccountInfo.Equity  = 0;
                            AccountInfo.Margin  = 0;
                            AccountInfo.Profit  = 0;
                            OnAccountStateChanged();
                            continue;
                        }

                        if (b.QuoteAvailable <= 0M)
                        {
                            continue;
                        }

                        var key = new CurrencyPair(baseCur, b.Currency);
                        if (!_positions.ContainsKey(key) || _positions[key].Size != b.QuoteAvailable)
                        {
                            _positions[key] = new ApiPosition
                            {
                                CurrencyPair = key,
                                Side         = PositionSide.Long,
                                Size         = b.QuoteAvailable
                            };
                            anyPosUpdated = true;
                        }
                    }

                    //mark closed positions
                    foreach (var item in _positions)
                    {
                        var b = balances.FirstOrDefault(i => i.Currency == item.Key.QuoteCurrency);
                        if (b == null || b.QuoteAvailable <= 0M)
                        {
                            closedPositions.Add(item.Key);
                        }
                    }
                }
            }

            //remove closed positions
            foreach (var symbol in closedPositions)
            {
                _positions.Remove(symbol);
            }

            if (anyPosUpdated || closedPositions.Count > 0)
            {
                OnPositionsChanged(_positions.Select(p => ToCommonPosition(p.Value)).ToList());
            }
        }