private Order FromBittrexOrder(Pair pair, MarketSide marketSide, BittrexOrder b)
 {
     return(new Order(
                pair,
                b.Rate,
                b.Quantity,
                ExchangeName.Bittrex,
                marketSide,
                DateTime.UtcNow,
                OrderType.Limit,
                SourceSystemCode.ExternalExchange));
 }
        public override async Task <AcceptedOrder> SellLimit(string marketName, decimal quantity, decimal rate)
        {
            var currentRate = (await GetTicker(marketName)).Last;

            var acceptedOrderId = Guid.NewGuid().ToString();

            if (currentRate >= rate)
            {
                var order = new BittrexOrder
                {
                    Closed       = DateTime.Now,
                    Exchange     = marketName,
                    IsOpen       = false,
                    Limit        = rate,
                    Opened       = DateTime.Now,
                    OrderUuid    = acceptedOrderId,
                    Price        = -quantity * rate,
                    PricePerUnit = rate,
                    Quantity     = -quantity
                };
                simulatedFinishedOrders.Add(order);

                var currency = Helper.GetTargetCurrencyFromMarketName(marketName);
                removeBalance(currency, quantity);
            }
            else
            {
                var order = new OpenOrder
                {
                    Closed       = DateTime.Now,
                    Exchange     = marketName,
                    Limit        = rate,
                    Opened       = DateTime.Now,
                    OrderUuid    = acceptedOrderId,
                    Price        = -quantity * rate,
                    PricePerUnit = rate,
                    Quantity     = -quantity
                };
                simulatedOpenOrders.Add(order);
            }

            return(new AcceptedOrder
            {
                Uuid = acceptedOrderId
            });
        }
        public bool PlaceOrder(UserTradeOrder aOrder, bool aUseProxy = true)
        {
            if (!IsCredentialsSet)
            {
                throw new Exception("No Credentials were set");
            }

            if (aOrder == null)
            {
                throw new ArgumentNullException(nameof(aOrder), $"Invalid order{aOrder.ID}");
            }

            if (aOrder.Status != OrderStatus.Waiting)
            {
                return(false);
            }

            BittrexClientOptions lBittrexClientOptions = new BittrexClientOptions()
            {
                Proxy          = PandoraProxy.GetApiProxy(),
                ApiCredentials = new ApiCredentials(FUserCredentials.Item1, FUserCredentials.Item2)
            };

            using (BittrexClient lClient = aUseProxy ? new BittrexClient(lBittrexClientOptions) : new BittrexClient())
            {
                if (!aOrder.Market.TryCastToLocalMarket(out ExchangeMarket lExchangeMarket))
                {
                    throw new ArgumentException(nameof(aOrder.Market), "Invalid Market");
                }
                CallResult <BittrexOrder> lResponse;
                if (aOrder.Market.MarketDirection == MarketDirection.Sell)
                {
                    lResponse = lClient.PlaceOrderAsync(lExchangeMarket.MarketPairID, OrderSide.Sell, OrderType.Limit, TimeInForce.GoodTillCancelled, aOrder.SentQuantity, limit: aOrder.Rate).Result;
                }
                else
                {
                    lResponse = lClient.PlaceOrderAsync(lExchangeMarket.MarketPairID, OrderSide.Buy, OrderType.Limit, TimeInForce.GoodTillCancelled, aOrder.SentQuantity / aOrder.Rate, limit: aOrder.Rate).Result;
                }
                if (!lResponse.Success)
                {
                    throw new Exception("Bittrex Error. Message: " + lResponse.Error.Message);
                }

                string lUuid = lResponse.Data.Id;
                CallResult <BittrexOrder> lResponse2 = lClient.GetOrderAsync(lUuid).Result;
                if (!lResponse.Success)
                {
                    throw new Exception("Failed to verify order with server");
                }

                BittrexOrder lReceivedOrder = lResponse2.Data;

                aOrder.ID = lUuid;

                aOrder.OpenTime  = lReceivedOrder.CreatedAt;
                aOrder.Cancelled = lReceivedOrder.OrderToCancel != null;
                aOrder.Completed = lReceivedOrder.ClosedAt.HasValue;

                return(true);
            }
        }