public async Task <IEnumerable <ExchangePosition> > GetCurrentPositionsAsync()
        {
            var payload = await GetNoncePayloadAsync();

            string url = "/position?";

            url += "&columns=[\"symbol\", \"currentQty\", \"avgEntryPrice\", \"liquidationPrice\", \"leverage\"]";
            JToken token = await MakeJsonRequestAsync <JToken>(url, BaseUrl, payload);

            List <ExchangePosition> positions = new List <ExchangePosition>();

            foreach (var item in token)
            {
                ExchangePosition position = new ExchangePosition
                {
                    MarketSymbol     = item["symbol"].ToStringUpperInvariant(),
                    Amount           = item["currentQty"].ConvertInvariant <decimal>(),
                    AveragePrice     = item["avgEntryPrice"].ConvertInvariant <decimal>(),
                    LiquidationPrice = item["liquidationPrice"].ConvertInvariant <decimal>(),
                    Leverage         = item["leverage"].ConvertInvariant <decimal>(),
                };
                positions.Add(position);
            }
            return(positions);
        }
Example #2
0
        private ExchangePosition ParsePosition(JToken token)
        {
            ExchangePosition result = new ExchangePosition
            {
                MarketSymbol     = token["symbol"].ToStringUpperInvariant(),
                Amount           = token["currentQty"].ConvertInvariant <decimal>(),
                AveragePrice     = token["avgEntryPrice"].ConvertInvariant <decimal>(),
                LiquidationPrice = token["liquidationPrice"].ConvertInvariant <decimal>(),
                Leverage         = token["leverage"].ConvertInvariant <decimal>(),
                LastPrice        = token["lastPrice"].ConvertInvariant <decimal>(),
                TimeStamp        = CryptoUtility.ParseTimestamp(token["currentTimestamp"], TimestampType.Iso8601)
            };

            return(result);
        }
        private ExchangePosition ParsePosition(object position)
        {
            try
            {
                JToken           token  = JToken.FromObject(position);
                ExchangePosition result = null;
                if (token != null)
                {
                    if (token["delta"]["updatedAt"].Type != JTokenType.Null)
                    {
                        DateTime updatedAt = DateTime.Parse(token["delta"]["updatedAt"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);

                        // Convert from order to ExchangePosition.
                        result = new ExchangePosition
                        {
                            OrderId      = token["delta"]["id"].ToStringInvariant(),
                            MarketSymbol = token["delta"]["marketSymbol"].ToStringInvariant(),
                            Amount       = token["delta"]["quantity"].ConvertInvariant <decimal>(),
                            AmountFilled = 0,
                            AveragePrice = token["delta"]["limit"].ConvertInvariant <decimal>(),
                            LastPrice    = token["delta"]["limit"].ConvertInvariant <decimal>(),
                            TimeStamp    = updatedAt
                        };

                        if (token["delta"]["type"] != null && token["delta"]["type"].Type != JTokenType.Null)
                        {
                            if (token["delta"]["type"].ToStringInvariant() == "LIMIT")
                            {
                                result.OrderType = (ExchangeSharp.OrderType)OrderType.Limit;
                            }
                            else if (token["delta"]["type"].ToStringInvariant() == "MARKET")
                            {
                                // TODO: Is this mapping correct?
                                result.OrderType = (ExchangeSharp.OrderType)OrderType.Market;
                            }
                        }

                        if (token["delta"]["fillQuantity"] != null && token["delta"]["fillQuantity"].Type != JTokenType.Null)
                        {
                            result.AmountFilled = token["delta"]["fillQuantity"].ConvertInvariant <decimal>();
                        }

                        if (token["delta"]["status"] != null && token["delta"]["status"].Type != JTokenType.Null)
                        {
                            string status = token["delta"]["status"].ToStringInvariant();
                            if (status == "OPEN" && result.AmountFilled == 0)
                            {
                                result.Status = ExchangeAPIOrderResult.Working;
                            }
                            else if (status == "OPEN" && result.AmountFilled < result.Amount)
                            {
                                result.Status = ExchangeAPIOrderResult.FilledPartially;
                            }
                            else if (status == "CLOSED" && result.AmountFilled == 0)
                            {
                                result.Status = ExchangeAPIOrderResult.Canceled;
                            }
                            else if (status == "CLOSED" && result.AmountFilled < result.Amount)
                            {
                                result.Status = ExchangeAPIOrderResult.FilledPartiallyAndCancelled;
                            }
                            else if (status == "CLOSED" && result.AmountFilled == result.Amount)
                            {
                                result.Status = ExchangeAPIOrderResult.Filled;
                            }
                            else if (status == "CLOSED")
                            {
                                result.Status = ExchangeAPIOrderResult.Canceled;
                            }
                            else
                            {
                                result.Status = ExchangeAPIOrderResult.Unknown;
                            }
                        }
                        else
                        {
                            result.Status = ExchangeAPIOrderResult.Unknown;
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }