Example #1
0
        public async Task <ExchangeOrderResult> Sell(Bot bot, decimal price)
        {
            _exchangeApi = GetExchange(bot);

            if (bot.CurrentPosition == null || bot.CurrentPosition.Status != Enumerations.PositionStatusEnum.Bought)
            {
                throw new Exception("No open current position on bot");
            }

            decimal quantity = (decimal)Math.Round(bot.CurrentPosition.Quantity, bot.Coin.OrderRoundingExponent, MidpointRounding.AwayFromZero);

            switch (bot.OrderType)
            {
            case Enumerations.OrderTypeEnum.Market:
                return(Sell(bot.BaseCoin, bot.Coin, quantity, null, Enumerations.OrderTypeEnum.Market));

            case Enumerations.OrderTypeEnum.Limit:

                // get the best bid
                ExchangeOrderBook orderBook = null;

                if (_exchangeApi.Simulated)
                {
                    orderBook = SimulateExchangeOrderBook(price);
                }
                else
                {
                    orderBook = _exchangeApi.GetOrderBook(bot.BaseCoin, bot.Coin, 1);
                }

                if (orderBook != null)
                {
                    ExchangeOrderPrice bestBid = orderBook.Bids.First();

                    return(Sell(bot.BaseCoin, bot.Coin, quantity, bestBid.Price, Enumerations.OrderTypeEnum.Market));
                }
                break;

            case Enumerations.OrderTypeEnum.Stop:     // to do
                throw new NotImplementedException();
            }
            return(null);
        }
Example #2
0
        public async Task <ExchangeOrderResult> Buy(Bot bot, decimal price)
        {
            _exchangeApi = GetExchange(bot);


            switch (bot.OrderType)
            {
            case Enumerations.OrderTypeEnum.Market:
                return(Buy(bot.BaseCoin, bot.Coin, bot.Amount, null, Enumerations.OrderTypeEnum.Market));

            case Enumerations.OrderTypeEnum.Limit:


                // get the best ask
                ExchangeOrderBook orderBook = null;

                if (_exchangeApi.Simulated)
                {
                    orderBook = SimulateExchangeOrderBook(price);
                }
                else
                {
                    orderBook = _exchangeApi.GetOrderBook(bot.BaseCoin, bot.Coin, 1);
                }

                if (orderBook != null)
                {
                    ExchangeOrderPrice bestAsk = orderBook.Asks.First();

                    decimal quantity = Math.Round(bot.Amount / bestAsk.Price, bot.Coin.OrderRoundingExponent);

                    return(Buy(bot.BaseCoin, bot.Coin, quantity, bestAsk.Price, Enumerations.OrderTypeEnum.Market));
                }

                throw new Exception("Couldn't place a buy order");

            case Enumerations.OrderTypeEnum.Stop:     // to do
                throw new NotImplementedException();
            }

            return(null);
        }