示例#1
0
        public void RegisterNewTypeOfStock(StockPriceInfo args)
        {
            if (CheckIfStockPriseConteinStockOfClientByTypeOfStock(args.TypeOfStock))
            {
                throw new ArgumentException("StockPrice Table already contain this type of stock");
            }

            var entityToAdd = new StockPriceEntity()
            {
                TypeOfStock  = args.TypeOfStock.ToUpper(),
                PriceOfStock = args.PriceOfStock
            };

            stockPriceTableRepository.Add(entityToAdd);
            stockPriceTableRepository.SaveChanges();
            logger.Info($"Stock {entityToAdd.Id} of type {entityToAdd.TypeOfStock} with price {entityToAdd.PriceOfStock} has been created");
        }
示例#2
0
        public void ShouldNotMakeATradeWhenSellerDoesntHaveEnoughStock()
        {
            //Arrange
            TradingService  tradingService  = new TradingService(accountTableRepository, stockPriceTableRepository, historyTableRepository, stockOfClientTableRepository, logger);
            TransactionInfo transactionInfo = new TransactionInfo();

            transactionInfo.BuyerId          = 1;
            transactionInfo.SellerId         = 2;
            transactionInfo.TypeOfStock      = "AAA";
            transactionInfo.QuantityOfStocks = 20;
            StockPriceEntity stockPriceEntity = new StockPriceEntity()
            {
                TypeOfStock  = transactionInfo.TypeOfStock,
                PriceOfStock = 4
            };
            AccountEntity buyer = new AccountEntity()
            {
                AccountId = 11,
                ClientId  = 1,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };
            AccountEntity seller = new AccountEntity()
            {
                AccountId = 22,
                ClientId  = 2,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(
                                                                         w => w == transactionInfo.TypeOfStock)).Returns(stockPriceEntity);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.BuyerId)).Returns(buyer);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.SellerId)).Returns(seller);
            //Act
            tradingService.MakeATrade(transactionInfo);
        }
示例#3
0
        public void ShouldNotMakeATradeWhenArgsDoesntConteinValidSellerIdOrBuyerId()
        {
            //Arrange
            TradingService  tradingService  = new TradingService(accountTableRepository, stockPriceTableRepository, historyTableRepository, stockOfClientTableRepository, logger);
            TransactionInfo transactionInfo = new TransactionInfo();

            transactionInfo.BuyerId          = 1;
            transactionInfo.SellerId         = 2;
            transactionInfo.TypeOfStock      = "AAA";
            transactionInfo.QuantityOfStocks = 20;
            StockPriceEntity stockPriceEntity = new StockPriceEntity()
            {
                TypeOfStock  = transactionInfo.TypeOfStock,
                PriceOfStock = 4
            };

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(
                                                                         w => w == transactionInfo.TypeOfStock)).Returns(stockPriceEntity);
            //Act
            tradingService.MakeATrade(transactionInfo);
        }
示例#4
0
        public void ShouldNotRegisterNewTypeOfStockIfItExists()
        {
            //Arrange
            ClientsService clientsService = new ClientsService(clientsTableRepository, accountTableRepository, stockPriceTableRepository, stockOfClientTableRepository, logger);
            StockPriceInfo args           = new StockPriceInfo()
            {
                TypeOfStock  = "A",
                PriceOfStock = 10
            };
            StockPriceEntity stockPrice = new StockPriceEntity();

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(w => w == args.TypeOfStock.ToUpper())).Returns(stockPrice);

            //Act
            clientsService.RegisterNewTypeOfStock(args);

            //Assert
            stockPriceTableRepository.Received(1).Add(Arg.Is <StockPriceEntity>(
                                                          w => w.TypeOfStock == args.TypeOfStock &&
                                                          w.PriceOfStock == args.PriceOfStock));
            stockPriceTableRepository.Received(1).SaveChanges();
        }
示例#5
0
        public void ShouldNotRegisterStockForNewClientIfItDoesntExists()
        {
            //Arrange
            ClientsService    clientsService = new ClientsService(clientsTableRepository, accountTableRepository, stockPriceTableRepository, stockOfClientTableRepository, logger);
            StockOfClientInfo args           = new StockOfClientInfo()
            {
                ClientsAccountId = 1,
                TypeOfStocks     = "A",
                quantityOfStocks = 10
            };
            StockPriceEntity stockPrice = null;

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(w => w == args.TypeOfStocks.ToUpper())).Returns(stockPrice);

            //Act
            clientsService.RegisterStockForNewClient(args);

            //Assert
            stockOfClientTableRepository.Received(1).Add(Arg.Is <StockOfClientEntity>(
                                                             w => w.AccountId == args.ClientsAccountId &&
                                                             w.TypeOfStocks == args.TypeOfStocks &&
                                                             w.quantityOfStocks == args.quantityOfStocks));
            stockOfClientTableRepository.Received(1).SaveChanges();
        }
 public void Add(StockPriceEntity entity)
 {
     dbContext.StockPrice.Add(entity);
 }
示例#7
0
 public void Remove(StockPriceEntity entity)
 {
     dbContext.StockPrice.Remove(entity);
 }
示例#8
0
        private void btn5UpDown_Click(object sender, EventArgs e)
        {
            List <StockPriceEntity>  listDetail = new List <StockPriceEntity>();
            List <StockPriceEntity>  listUp     = new List <StockPriceEntity>();
            List <StockPriceEntity>  listDown   = new List <StockPriceEntity>();
            List <PriceUpDownEntity> listUpDown = new List <PriceUpDownEntity>();

            StockPriceEntity previousPrice = new StockPriceEntity();

            using (Utility.DbUtil db = new Utility.DbUtil())
            {
                StringBuilder sb = new StringBuilder();

                sb.Length = 0;
                sb.AppendLine(" SELECT");
                sb.AppendLine("     StockCode");
                sb.AppendLine("    ,0 AS UpCnt3M ");
                sb.AppendLine("    ,0 AS DownCnt3M ");
                sb.AppendLine("    ,0 AS UpCnt2M ");
                sb.AppendLine("    ,0 AS DownCnt2M ");
                sb.AppendLine("    ,0 AS UpCnt1M ");
                sb.AppendLine("    ,0 AS DownCnt1M ");
                sb.AppendLine(" FROM");
                sb.AppendLine("     stockprice ");
                sb.AppendLine(" GROUP BY StockCode ");
                listUpDown = db.DBSelect <PriceUpDownEntity>(sb.ToString());

                // 一旦削除
                db.DBUpdate("DELETE FROM priceupdown ");


                sb.Length = 0;
                sb.AppendLine(" SELECT ");
                sb.AppendLine("      stockprice.* ");
                sb.AppendLine(" FROM  ");
                sb.AppendLine("       stockprice ");
                sb.AppendLine(" WHERE stockprice.StockDate BETWEEN :BeginDate AND :EndDate ");
                sb.AppendLine(" ORDER BY stockprice.StockCode,stockprice.StockDate ");

                listDetail = db.DBSelect <StockPriceEntity>(sb.ToString(), new { BeginDate = DateTime.Now.AddMonths(-3), EndDate = DateTime.Now.Date });


                int breakStockCode = 0;

                if (listDetail.Count != 0)
                {
                    previousPrice = listDetail[0];
                }

                Dictionary <string, int> dic = new Dictionary <string, int>();


                foreach (StockPriceEntity list in listDetail)
                {
                    if (breakStockCode == list.StockCode)
                    {
                        // 同一証券コード
                        if (list.ClosingPrice >= previousPrice.ClosingPrice * 1.05m)
                        {
                            // 5%上昇(前後データ追加)
                            var priceup = from up in listUpDown
                                          where up.StockCode == previousPrice.StockCode
                                          select up;

                            foreach (PriceUpDownEntity up in priceup)
                            {
                                up.UpCnt3M = up.UpCnt3M + 1;

                                if (DateTime.Now.AddMonths(-2) < previousPrice.StockDate)
                                {
                                    up.UpCnt2M = up.UpCnt2M + 1;
                                }

                                if (DateTime.Now.AddMonths(-1) < previousPrice.StockDate)
                                {
                                    up.UpCnt1M = up.UpCnt1M + 1;
                                }
                            }
                        }
                        else if (list.ClosingPrice <= previousPrice.ClosingPrice * 0.95m)
                        {
                            // 5%下落(前後データ追加)
                            var pricedown = from down in listUpDown
                                            where down.StockCode == previousPrice.StockCode
                                            select down;

                            foreach (PriceUpDownEntity down in pricedown)
                            {
                                down.DownCnt3M = down.DownCnt3M + 1;

                                if (DateTime.Now.AddMonths(-2) < previousPrice.StockDate)
                                {
                                    down.DownCnt2M = down.DownCnt2M + 1;
                                }

                                if (DateTime.Now.AddMonths(-1) < previousPrice.StockDate)
                                {
                                    down.DownCnt1M = down.DownCnt1M + 1;
                                }
                            }
                        }
                    }
                    else
                    {
                        // 証券コードブレイク
                        breakStockCode = list.StockCode;
                    }

                    // 前回株価
                    previousPrice = list;
                }

                this.dataGridView2.DataSource = listUpDown;
                //this.dataGridView3.DataSource = listDown;
            }
        }
示例#9
0
        public void ShouldMakeATrade()
        {
            //Arrange
            TradingService  tradingService  = new TradingService(accountTableRepository, stockPriceTableRepository, historyTableRepository, stockOfClientTableRepository, logger);
            TransactionInfo transactionInfo = new TransactionInfo();

            transactionInfo.BuyerId          = 1;
            transactionInfo.SellerId         = 2;
            transactionInfo.TypeOfStock      = "AAA";
            transactionInfo.QuantityOfStocks = 20;
            StockPriceEntity stockPriceEntity = new StockPriceEntity()
            {
                TypeOfStock  = transactionInfo.TypeOfStock,
                PriceOfStock = 4
            };
            AccountEntity buyer = new AccountEntity()
            {
                AccountId = 11,
                ClientId  = 1,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };
            AccountEntity seller = new AccountEntity()
            {
                AccountId = 22,
                ClientId  = 2,
                Balance   = 100,
                Stocks    = new List <StockOfClientEntity>()
            };
            StockOfClientEntity stockForSell = new StockOfClientEntity()
            {
                Id               = 222,
                TypeOfStocks     = "AAA",
                AccountId        = 22,
                AccountForStock  = seller,
                quantityOfStocks = 30
            };

            stockPriceTableRepository.GetStockPriceEntityByStockType(Arg.Is <string>(
                                                                         w => w == transactionInfo.TypeOfStock)).Returns(stockPriceEntity);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.BuyerId)).Returns(buyer);

            accountTableRepository.GetAccountByClientId(Arg.Is <int>(
                                                            w => w == transactionInfo.SellerId)).Returns(seller);

            stockOfClientTableRepository.GetStockOfClientEntityByAccountIdAndType(Arg.Is <int>(
                                                                                      w => w == seller.AccountId), Arg.Is <string>(
                                                                                      w => w == transactionInfo.TypeOfStock)).Returns(stockForSell);

            stockOfClientTableRepository.GetStockOfClientEntityByAccountIdAndType(Arg.Is <int>(
                                                                                      w => w == buyer.AccountId), Arg.Is <string>(
                                                                                      w => w == transactionInfo.TypeOfStock)).Returns(stockForSell);
            //Act

            seller.Stocks.Add(stockForSell);
            tradingService.MakeATrade(transactionInfo);

            //Assert
            stockOfClientTableRepository.Received(1).Add(Arg.Is <StockOfClientEntity>(
                                                             w => w.AccountForStock == buyer &&
                                                             w.quantityOfStocks == transactionInfo.QuantityOfStocks &&
                                                             w.TypeOfStocks == transactionInfo.TypeOfStock)
                                                         );

            stockOfClientTableRepository.Received(2).SaveChanges();

            accountTableRepository.Received(1).Change(Arg.Is <AccountEntity>(buyer));
            accountTableRepository.Received(1).Change(Arg.Is <AccountEntity>(seller));
            accountTableRepository.Received(2).SaveChanges();

            historyTableRepository.Received(1).Add(Arg.Is <HistoryEntity>
                                                       (w => w.BuyerId == transactionInfo.BuyerId &&
                                                       w.SellerId == transactionInfo.SellerId &&
                                                       w.QuantityOfStocks == transactionInfo.QuantityOfStocks &&
                                                       w.TypeOfStock == transactionInfo.TypeOfStock &&
                                                       w.FullPrice == 80
                                                       ));
            historyTableRepository.Received(1).SaveChanges();
        }