public IActionResult RegisterStrategy(StrategyDetailsDto strategyDetails)
        {
            StrategyManagementService service = _strategyManagementService.Service as StrategyManagementService;

            service.ResgisterStrategy(strategyDetails);
            return(Ok(strategyDetails));
        }
Esempio n. 2
0
        public void ResgisterStrategy(StrategyDetailsDto strategyDetailsDto)
        {
            decimal?startingprice = null;

            try
            {
                startingprice = reutbergService.GetQuote(strategyDetailsDto.Ticker.ToUpper());
            }
            catch (Exception ex)
            {
                // log message for Ticker not being available - Report back to user
                startingprice = null;
            }

            finally
            {
                lock (_locker)
                {
                    // even if RegisteredPrice comes back as null, we still register it and
                    // future enhancement - RegisteredPrice should get price when GetQuote becomes available
                    RegisteredStrategy.Add(new RegisteredStrategy()
                    {
                        strategyDetailsDto = strategyDetailsDto,
                        RegisteredPrice    = startingprice
                    });
                }
            }
        }
Esempio n. 3
0
        public void RegisterStrategy_returns_null_if_live_quote_is_null()
        {
            // arrange
            var dto = new StrategyDetailsDto()
            {
                Ticker        = "TESLA",
                Instruction   = BuySell.Buy,
                PriceMovement = 5,
                Quantity      = 100
            };

            this.mockStrategyTradingService
            .Setup(x => x.GetLiveQuote(It.IsAny <string>()))
            .Returns((decimal?)null)
            .Verifiable();

            // act
            var result = this.subject.RegisterStrategy(dto);

            // assert
            this.mockStrategyTradingService
            .Verify(x => x.GetLiveQuote(It.IsAny <string>()));

            Assert.IsNull(result);
        }
Esempio n. 4
0
        public void RegisterStrategy_works_as_expected()
        {
            // arrange
            var dto = new StrategyDetailsDto()
            {
                Ticker        = "TESLA",
                Instruction   = BuySell.Buy,
                PriceMovement = 5,
                Quantity      = 100
            };

            this.mockStrategyTradingService
            .Setup(x => x.GetLiveQuote(It.IsAny <string>()))
            .Returns(200)
            .Verifiable();

            this.mockLocalCache
            .Setup(x => x.Put <Strategy>(It.IsAny <string>(), It.IsAny <Strategy>()))
            .Verifiable();

            // act
            var result = this.subject.RegisterStrategy(dto);

            // assert
            this.mockStrategyTradingService
            .Verify(x => x.GetLiveQuote(It.IsAny <string>()));
            this.mockLocalCache
            .Verify(x => x.Put <Strategy>(It.IsAny <string>(), It.IsAny <Strategy>()));

            Assert.IsNotNull(result);
        }
Esempio n. 5
0
        public IActionResult RegisterStrategy(StrategyDetailsDto strategyDetails)
        {
            var result = this._strategyRepository.RegisterStrategy(strategyDetails);

            return(new OkObjectResult(new ApiResponse
            {
                Success = result != null,
                StrategyId = result
            }));
        }
Esempio n. 6
0
        public IActionResult RegisterStrategy(StrategyDetailsDto strategyDetails)
        {
            if (strategyDetails == null)
            {
                return(BadRequest());
            }
            string ID = generateID();

            strategies.Add(ID, strategyDetails);
            return(CreatedAtAction(nameof(RegisterStrategy), new { id = ID }, strategyDetails));
        }
Esempio n. 7
0
        public void RegisterStrategyTest2()
        {
            StrategyDetailsDto strategyDetails = new StrategyDetailsDto();

            strategyDetails.Instruction   = TradingPlaces.Resources.BuySell.Sell;
            strategyDetails.PriceMovement = 2;
            strategyDetails.Quantity      = 10;
            strategyDetails.Ticker        = "MSBT";
            StrategyController strategyController = new StrategyController();
            var result   = strategyController.RegisterStrategy(strategyDetails);
            var okResult = result as OkObjectResult;

            Assert.NotNull(okResult);
            Assert.Equal(200, okResult.StatusCode);
        }
        public IActionResult RegisterStrategy(StrategyDetailsDto strategyDetails)
        {
            try
            {
                //var strategyDet = _mapper.Map<StrategyDetails>(strategyDetails);
                //// Setting a new ID
                //strategyDet.Id = Guid.NewGuid().ToString().Substring(0,8).ToUpper();

                //strategyDet.Ticker = strategyDet.Ticker.ToUpper();

                _strategyManagementService.Service.AddStrategy(strategyDetails);
                return(Ok(strategyDetails));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 9
0
        public void UnregisterStrategyTest2()
        {
            StrategyDetailsDto strategyDetails = new StrategyDetailsDto();

            strategyDetails.Instruction   = TradingPlaces.Resources.BuySell.Sell;
            strategyDetails.PriceMovement = 2;
            strategyDetails.Quantity      = 10;
            strategyDetails.Ticker        = "MSBT";
            StrategyController strategyController = new StrategyController();
            var           mockDependency          = new Mock <StrategyController>();
            CreatedResult result = new CreatedResult("12345678", strategyDetails);

            mockDependency.Setup(x => x.RegisterStrategy(strategyDetails)).Returns(result);
            string id       = "12345678";
            var    result2  = strategyController.UnregisterStrategy(id);
            var    okResult = result2 as OkObjectResult;

            Assert.NotNull(okResult);
            Assert.Equal(200, okResult.StatusCode);
        }
Esempio n. 10
0
        /// <inheritdoc />
        public string RegisterStrategy(StrategyDetailsDto strategyDetails)
        {
            var liveQuote = this._strategyTradingService.GetLiveQuote(strategyDetails.Ticker);

            if (liveQuote != null)
            {
                var strategyId = Guid.NewGuid().ToString();
                this._localCache.Put(strategyId, new Strategy
                {
                    Id            = strategyId,
                    Instruction   = strategyDetails.Instruction,
                    PriceMovement = strategyDetails.PriceMovement,
                    Quantity      = strategyDetails.Quantity,
                    Ticker        = strategyDetails.Ticker,
                    StartPrice    = liveQuote.Value
                });
                return(strategyId);
            }

            return(null);
        }
        public void AddStrategy(StrategyDetailsDto entity)
        {
            var strategyDet = _mapper.Map <StrategyDetails>(entity);

            // Setting a new ID
            strategyDet.Id     = Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
            strategyDet.Ticker = strategyDet.Ticker.ToUpper();

            if (strategyDet == null || !IsValidQuantity(strategyDet.Quantity) ||
                !IsValidTicker(strategyDet.Ticker) ||
                !IsValidPriceMovement(strategyDet))
            {
                throw new Exception("INVALID STRATEGY!");
            }


            StrategyDetails strategy = _context.Strategies.Find(strategyDet.Id);

            if (strategy != null)
            {
                throw new Exception("STRATEGY ID ALREADY EXISTS!");
            }

            decimal fullPercentage = 100;

            strategyDet.StartPrice  = GetCurrentPrice(strategyDet.Ticker);
            strategyDet.TargetPrice = Math.Round(strategyDet.Instruction == BuySell.Buy ? strategyDet.StartPrice * (1 - (strategyDet.PriceMovement / fullPercentage))
                : strategyDet.StartPrice * (1 + (strategyDet.PriceMovement / fullPercentage)), 2);

            lock (_context)
            {
                _context.Add(strategyDet);
                _context.SaveChanges();
#if DEBUG
                _logger.LogInformation(string.Format("ADDING NEW STRATEGY {0}: {1} {2} SHARES OF {3} AT PRICE {4} USD (CURRENT PRICE: {5} USD)", strategyDet.Id, strategyDet.Instruction == BuySell.Buy ? "BUY" : "SELL",
                                                     strategyDet.Quantity, strategyDet.Ticker, strategyDet.TargetPrice, strategyDet.StartPrice));
#endif
            }
        }