Beispiel #1
0
        public async Task <IActionResult> StockDataTest(Stock stock)
        {
            List <Stock> stockList = StockList.GetStockList();

            var TradingBot = new TradingBot(_sentimentProvider);
            var trade      = TradingBot.TradeEvaluation(stock);

            var tradeOrder = new
            {
                trade = trade.ToString()
            };

            return(Ok(tradeOrder));
        }
Beispiel #2
0
        public void Trade_InvalidParameters_Returns_()
        {
            Mock <ISentimentProvider> mockSentimentProvider = new Mock <ISentimentProvider>();

            mockSentimentProvider.Setup(x => x.GetSentiment(It.Is <string>(s => s == "Apple"))).Returns(Sentiment.neutral);

            var tradingBot = new TradingBot(mockSentimentProvider.Object);

            var Stock    = new Stock {
            };
            var decision = tradingBot.TradeEvaluation(Stock);

            Assert.Equal(TradeDecision.error, decision);
        }
Beispiel #3
0
 public IResponse CreateBot(JObject stuff)
 {
     try
     {
         var bot  = stuff["bot"].ToObject <TradingBot>();
         var id   = stuff["id"].ToObject <int>();
         var user = _context.Users.Where(r => r.Id == id).SingleOrDefault();
         if (user == null)
         {
             return new Response {
                        IsSuccess = false, Message = "Пользователь не найден"
             }
         }
         ;
         ;
         if (bot == null)
         {
             return new Response {
                        IsSuccess = false, Message = "Объект робота пуст"
             }
         }
         ;
         ;
         var newTradingBot = new TradingBot
         {
             Id   = new Guid(),
             User = user,
             Name = bot.Name,
             //Type = bot.Type,
             Sum      = bot.Sum,
             Profit   = 0,
             Strategy = bot.Strategy,
             //Assets = bot.Assets,
             //TimeFrame = bot.TimeFrame,
             //FinancialInstrument = bot.FinancialInstrument,
             CreatedDate = DateTime.Now,
             ESValue     = bot?.ESValue != 0 ? bot.ESValue : 10
         };
         _context.TradingBots.Add(newTradingBot);
         _context.SaveChanges();
         return(new Response {
             IsSuccess = true
         });
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Beispiel #4
0
        public void Trade_LowLiquidity_Returns_NoTrade()
        {
            Mock <ISentimentProvider> mockSentimentProvider = new Mock <ISentimentProvider>();

            var tradingBot = new TradingBot(mockSentimentProvider.Object);

            var Stock = new Stock
            {
                Liquidity          = 0.6,
                Name               = "Alphabet",
                Price              = 100,
                Price200DayAverage = 67
            };
            var decision = tradingBot.TradeEvaluation(Stock);

            Assert.Equal(TradeDecision.noTrade, decision);
        }
Beispiel #5
0
 public IActionResult DeleteBot(TradingBot bot)
 {
     try
     {
         var botForDelete = _context.TradingBots.Where(r => r.Id == bot.Id).SingleOrDefault();
         if (botForDelete == null)
         {
             throw new Exception("Bot not found");
         }
         ;
         _context.TradingBots.Remove(botForDelete);
         _context.SaveChanges();
         return(Ok(bot));
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Beispiel #6
0
        public void Trade_HighPrice_NegativeSentiment_Returns_StrongShortSell()
        {
            Mock <ISentimentProvider> mockSentimentProvider = new Mock <ISentimentProvider>();

            mockSentimentProvider.Setup(x => x.GetSentiment(It.Is <string>(s => s == "Apple"))).Returns(Sentiment.negative);

            var tradingBot = new TradingBot(mockSentimentProvider.Object);

            var Stock = new Stock
            {
                Name               = "Apple",
                Liquidity          = 1.1,
                Price              = 11,
                Price200DayAverage = 10
            };
            var decision = tradingBot.TradeEvaluation(Stock);

            Assert.Equal(TradeDecision.strongShortSell, decision);
        }
Beispiel #7
0
        public void Trade_LowPrice_NeutralSentiment_Returns_Buy()
        {
            Mock <ISentimentProvider> mockSentimentProvider = new Mock <ISentimentProvider>();

            mockSentimentProvider.Setup(x => x.GetSentiment(It.Is <string>(s => s == "Apple"))).Returns(Sentiment.neutral);

            var tradingBot = new TradingBot(mockSentimentProvider.Object);

            var Stock = new Stock
            {
                Name               = "Apple",
                Liquidity          = 1.1,
                Price              = 9,
                Price200DayAverage = 10
            };
            var decision = tradingBot.TradeEvaluation(Stock);

            Assert.Equal(TradeDecision.buy, decision);
        }
Beispiel #8
0
        public void LowLiquidity_Returns_NoTrade()
        {
            Mock <ISentimentProvider> mockSentimentProvider = new Mock <ISentimentProvider>();
            Mock <IStockProvider>     mockStockProvider     = new Mock <IStockProvider>();
            APIController             APIController         = new APIController(mockSentimentProvider.Object, mockStockProvider.Object);

            mockSentimentProvider.Setup(x => x.GetSentiment(It.Is <string>(s => s == "Microsoft"))).Returns(Sentiment.positive);

            Stock stock = new Stock
            {
                Id                 = 1,
                Name               = "Microsoft",
                Liquidity          = 0.8,
                Price              = 100,
                Price200DayAverage = 87
            };

            var tradingBot = new TradingBot(mockSentimentProvider.Object);
            var decision   = tradingBot.TradeEvaluation(stock);
        }
Beispiel #9
0
 public IActionResult UpdateBot(TradingBot bot)
 {
     try
     {
         var oldBot = _context.TradingBots.Where(r => r.Id == bot.Id).SingleOrDefault();
         if (oldBot == null)
         {
             throw new Exception("Bot not found");
         }
         ;
         oldBot.Name     = bot.Name;
         oldBot.IsActive = bot.IsActive;
         _context.TradingBots.Update(oldBot);
         _context.SaveChanges();
         return(Ok(bot));
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Beispiel #10
0
        public async Task <IActionResult> AutoTrade(int id)
        {
            Stock stockFromDb = _stockProvider.GetStockById(id);

            var TradingBot = new TradingBot(_sentimentProvider);
            var trade      = TradingBot.TradeEvaluation(stockFromDb);

            StockOrder stockOrder = new StockOrder();

            if (trade == TradeDecision.buy || trade == TradeDecision.strongBuy)
            {
                stockOrder.Id             = 1;
                stockOrder.Stock          = stockFromDb;
                stockOrder.NumberOfStocks = 10;
                var tradeOrder = new
                {
                    stockOrder = stockOrder.ToString()
                };
                _stockProvider.AutoTrade(id);
                return(Ok(tradeOrder));
            }
            if (trade == TradeDecision.noTrade)
            {
                return(Ok(new { error = "No trade" }));
            }
            if (trade == TradeDecision.shortSell || trade == TradeDecision.strongShortSell)
            {
                stockOrder.Id             = 1;
                stockOrder.Stock          = stockFromDb;
                stockOrder.NumberOfStocks = -10;
                var tradeOrder = new
                {
                    stockOrder = stockOrder.ToString()
                };
                _stockProvider.AutoTrade(id);
                return(Ok(tradeOrder));
            }

            return(Ok(new { error = "error" }));
        }