public void ShouldGetPriceInfo()
        {
            // Arrange
            var phTableRepository         = Substitute.For <ITableRepository <PriceHistory> >();
            var DTOMethods                = Substitute.For <IDTOMethodsforPriceHistory>();
            PriceHistoryService phService = new PriceHistoryService(phTableRepository, DTOMethods);
            PriceHistory        priceHist = new PriceHistory
            {
                PriceHistoryID = 1,
                StockID        = 1,
                DateTimeBegin  = new DateTime(2019, 8, 20, 09, 55, 00),
                DateTimeEnd    = new DateTime(2019, 8, 20, 19, 30, 00),
                Price          = 200
            };
            PriceArguments priceArguments = new PriceArguments()
            {
                StockId        = 1,
                DateTimeLookUp = new DateTime(2019, 8, 20, 09, 56, 00)
            };

            DTOMethods.FindEntitiesByRequestDTO(priceArguments).Returns(new List <PriceHistory> {
                priceHist
            });



            // Act
            var priceHistory = phService.GetStockPriceByDateTime(priceArguments);

            // Assert
            var hist = DTOMethods.Received(1).FindEntitiesByRequestDTO(priceArguments);
        }
示例#2
0
        public void _02_HistoricalPriceServiceTest_ByDate()
        {
            var begin   = new DateTime(2017, 5, 9);
            var end     = new DateTime(2017, 5, 9);
            var html    = new HtmlService();
            var service = new PriceHistoryService(html);

            // test Begin
            var prices = service.GetDailyTadingInfo("600030", begin).ToArray();

            Assert.IsTrue(prices.Length > 0);
            Assert.IsTrue(prices.Min(p => p.Time) == begin);

            // test end
            //prices = service.GetDailyTadingInfo("600030", null, end).ToArray();
            //Assert.IsTrue(prices.Length > 0);
            //Assert.IsTrue(prices.Max(p => p.Time) == begin);

            // test begin + end
            prices = service.GetDailyTadingInfo("600030", begin, begin).ToArray();
            Assert.IsTrue(prices.Length == 1);
            Assert.IsTrue(prices.Min(p => p.Time) == begin);
            Assert.IsTrue(prices.Max(p => p.Time) == begin);

            prices = service.GetDailyTadingInfo("600030", begin, end.AddMonths(7)).ToArray();
            Assert.IsTrue(prices.Length > 1);
            Assert.IsTrue(prices.Min(p => p.Time) == begin);
            Assert.IsTrue(prices.Max(p => p.Time) == end.AddMonths(7));
        }
示例#3
0
        public void _05_DailyTradePeriodTest()
        {
            var service = new PriceHistoryService(new HtmlService());
            var pairs   = service.GetAvailablePeriods(Code, new DateTime(2018, 6, 27), new DateTime(2017, 4, 5)).ToArray();

            Assert.AreEqual(5, pairs.Count());
        }
示例#4
0
        public void Initialize()
        {
            _priceHistoryRepository = A.Fake <IPriceHistoryRepository>();
            _priceHistoryService    = new PriceHistoryService(_priceHistoryRepository);
            _ValidStartDate         = DateTime.Now.AddDays(-1);
            _ValidEndDate           = DateTime.Now.AddDays(-1);
            _InvalidStartDate       = DateTime.Now.AddDays(1);
            _InvalidEndDate         = DateTime.Now.AddDays(1);

            A.CallTo(() => _priceHistoryRepository.GetPriceHistory(ValidSymbol, _ValidStartDate, _ValidEndDate)).Returns(new List <TradingDay>()
            {
                new TradingDay()
                {
                    OpenPrice  = 5.00m,
                    HighPrice  = 5.00m,
                    LowPrice   = 1.00m,
                    ClosePrice = 5.00m,
                    Date       = DateTime.Now
                }
            });

            A.CallTo(() => _priceHistoryRepository.GetPriceHistory(InvalidSymbol, _ValidStartDate, _ValidEndDate)).Throws <StockNotFoundException>();

            A.CallTo(() => _priceHistoryRepository.GetPriceHistory(ValidSymbol, _InvalidStartDate, _ValidEndDate)).Throws <InvalidDateException>();

            A.CallTo(() => _priceHistoryRepository.GetPriceHistory(ValidSymbol, _ValidStartDate, _InvalidEndDate)).Throws <InvalidDateException>();
        }
示例#5
0
 public StockExchange(ExchangeContext db,
                      ITableRepository <Client> clientTableRepository,
                      ITableRepository <ClientStock> clientStockTableRepository,
                      ITableRepository <Issuer> issuerTableRepository,
                      ITableRepository <Order> orderTableRepository,
                      ITableRepository <PriceHistory> priceHistoryTableRepository,
                      ITableRepository <Stock> stockTableRepository,
                      ITableRepository <TransactionHistory> transactionHistoryTableRepository,
                      ClientService clientService,
                      ClientStockService clientStockService,
                      OrderService orderService,
                      PriceHistoryService priceHistoryService,
                      TransactionHistoryService transactionHistoryService,
                      ILogger logger
                      )
 {
     this.db = db;
     this.clientTableRepository             = clientTableRepository;
     this.clientStockTableRepository        = clientStockTableRepository;
     this.issuerTableRepository             = issuerTableRepository;
     this.orderTableRepository              = orderTableRepository;
     this.priceHistoryTableRepository       = priceHistoryTableRepository;
     this.stockTableRepository              = stockTableRepository;
     this.transactionHistoryTableRepository = transactionHistoryTableRepository;
     this.clientService             = clientService;
     this.clientStockService        = clientStockService;
     this.orderService              = orderService;
     this.priceHistoryService       = priceHistoryService;
     this.transactionHistoryService = transactionHistoryService;
     this.logger = logger;
 }
示例#6
0
        public void _01_HistoricalPriceServiceTest()
        {
            var html    = new HtmlService();
            var service = new PriceHistoryService(html);
            var prices  = service.GetDailyTadingInfo("600030", 2018, 3).ToArray();

            Assert.IsTrue(prices.Length > 0);
        }
示例#7
0
        public void _02_DailyTradePeriodTest()
        {
            var service = new PriceHistoryService(new HtmlService());
            var years   = service.GetAvailablePeriods(Code).ToArray();

            var count = Convert.ToInt32(Math.Ceiling(DateTime.Today.Month / 3.0));

            Assert.AreEqual(count, years.Count());
        }
示例#8
0
        public void _04_DailyTradePeriodTest()
        {
            var service = new PriceHistoryService(new HtmlService());
            var pairs   = service.GetAvailablePeriods(Code, new DateTime(2017, 12, 31), new DateTime(2017, 1, 1)).ToArray();

            int beginYear = 2017;
            int endYear   = 2017;

            Assert.AreEqual(4, pairs.Count());
        }
示例#9
0
        public void _01_DailyTradeTest()
        {
            var service = new PriceHistoryService(new HtmlService());
            var years   = service.GetAvailableYears(Code).ToArray();

            int beginYear = 2010;
            int endYear   = DateTime.Today.Year;

            Assert.AreEqual((endYear - beginYear + 1), years.Count());
        }
示例#10
0
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            var             logger = new Logger(log4net.LogManager.GetLogger("Logger"));
            ExchangeContext db     = new ExchangeContext();

            IDTOMethodsforPriceHistory dTOMethodsforPriceHistory = new DTOMethodsforPriceHistory(db);

            ITableRepository <Client>             clientTableRepository             = new ClientTableRepository <Client>(db);
            ITableRepository <ClientStock>        clientStockTableRepository        = new ClientStockTableRepository <ClientStock>(db);
            ITableRepository <Issuer>             issuerTableRepository             = new IssuerTableRepository <Issuer>(db);
            ITableRepository <Order>              orderTableRepository              = new OrderTableRepository <Order>(db);
            ITableRepository <PriceHistory>       priceHistoryTableRepository       = new PriceHistoryTableRepository <PriceHistory>(db);
            ITableRepository <Stock>              stockTableRepository              = new StockTableRepository <Stock>(db);
            ITableRepository <TransactionHistory> transactionHistoryTableRepository = new TransactionHistoryTableRepository <TransactionHistory>(db);

            ClientService             clientService             = new ClientService(clientTableRepository);
            ClientStockService        clientStockService        = new ClientStockService(clientStockTableRepository);
            OrderService              orderService              = new OrderService(orderTableRepository);
            PriceHistoryService       priceHistoryService       = new PriceHistoryService(priceHistoryTableRepository, dTOMethodsforPriceHistory);
            TransactionHistoryService transactionHistoryService = new TransactionHistoryService(transactionHistoryTableRepository);



            StockExchange stockExchange = new StockExchange(
                db,
                clientTableRepository,
                clientStockTableRepository,
                issuerTableRepository,
                orderTableRepository,
                priceHistoryTableRepository,
                stockTableRepository,
                transactionHistoryTableRepository,
                clientService,
                clientStockService,
                orderService,
                priceHistoryService,
                transactionHistoryService,
                logger);


            using (db)
            {
                logger.Info("Trading is started");
                stockExchange.RunTraiding();
                logger.Info("Trading is finished");
            };
        }
        public void ShouldEditPriceDateEnd()
        {
            // Arrange
            var phTableRepository         = Substitute.For <ITableRepository <PriceHistory> >();
            var DTOMethods                = Substitute.For <IDTOMethodsforPriceHistory>();
            PriceHistoryService phService = new PriceHistoryService(phTableRepository, DTOMethods);

            PriceHistory lastpriceHist = new PriceHistory
            {
                PriceHistoryID = 1,
                StockID        = 1,
                DateTimeBegin  = new DateTime(2019, 8, 20, 09, 55, 00),
                DateTimeEnd    = new DateTime(2019, 8, 20, 19, 30, 00),
                Price          = 200
            };

            IEnumerable <PriceHistory> histories = new List <PriceHistory>()
            {
                lastpriceHist
            };
            PriceArguments priceArguments = new PriceArguments()
            {
                StockId        = 1,
                DateTimeLookUp = new DateTime(2019, 8, 20, 09, 56, 00)
            };

            int      stockId     = 1;
            DateTime DateTimeEnd = new DateTime(2019, 8, 20, 09, 56, 00);

            DTOMethods.FindEntitiesByRequest(stockId).Returns(new List <PriceHistory> {
                lastpriceHist
            });



            // Act
            phService.EditPriceDateEnd(stockId, DateTimeEnd);

            // Assert
            var phistories = DTOMethods.FindEntitiesByRequest(stockId);

            lastpriceHist.DateTimeEnd = DateTimeEnd;
            phTableRepository.SaveChanges();
        }
        public void ShouldNotRegisterNewCPriceHistoryIfItExists()
        {
            // Arrange
            var phTableRepository         = Substitute.For <ITableRepository <PriceHistory> >();
            var DTOMethods                = Substitute.For <IDTOMethodsforPriceHistory>();
            PriceHistoryService phService = new PriceHistoryService(phTableRepository, DTOMethods);
            PriceInfo           priceInfo = new PriceInfo
            {
                StockId       = 1,
                DateTimeBegin = new DateTime(2019, 8, 20, 09, 55, 00),
                DateTimeEnd   = new DateTime(2019, 8, 20, 19, 30, 00),
                Price         = 200
            };

            // Act
            phService.AddPriceInfo(priceInfo);

            phTableRepository.ContainsDTO(Arg.Is <PriceHistory>(
                                              w => w.StockID == 1 &&
                                              w.DateTimeBegin == new DateTime(2019, 8, 20, 09, 55, 00) &&
                                              w.DateTimeEnd == new DateTime(2019, 8, 20, 19, 30, 00) &&
                                              w.Price == 200)).Returns(true);
            phService.AddPriceInfo(priceInfo);
        }
        public void ShouldAddNewPriceHistory()
        {
            //Arrange
            var phTableRepository         = Substitute.For <ITableRepository <PriceHistory> >();
            var DTOMethods                = Substitute.For <IDTOMethodsforPriceHistory>();
            PriceHistoryService phService = new PriceHistoryService(phTableRepository, DTOMethods);
            PriceInfo           priceInfo = new PriceInfo
            {
                StockId       = 1,
                DateTimeBegin = new DateTime(2019, 8, 20, 09, 55, 00),
                DateTimeEnd   = new DateTime(2019, 8, 20, 19, 30, 00),
                Price         = 200
            };

            //Act
            phService.AddPriceInfo(priceInfo);
            //Assert
            phTableRepository.Received(1).Add(Arg.Is <PriceHistory>(
                                                  w => w.StockID == 1 &&
                                                  w.DateTimeBegin == new DateTime(2019, 8, 20, 09, 55, 00) &&
                                                  w.DateTimeEnd == new DateTime(2019, 8, 20, 19, 30, 00) &&
                                                  w.Price == 200
                                                  ));
        }
示例#14
0
        static void Main(string[] args)
        {
            StockRepository        stockRepository        = new StockRepository();
            StockService           stockService           = new StockService(stockRepository);
            PriceHistoryRepository priceHistoryRepository = new PriceHistoryRepository();
            PriceHistoryService    priceHistoryService    = new PriceHistoryService(priceHistoryRepository);
            List <Stock>           stocks          = stockService.GetStocks();
            List <Stock>           increasedStocks = new List <Stock>();
            //var processDate = DateTime.Now;
            var processDate = new DateTime(2020, 07, 10);

            foreach (var stock in stocks)
            {
                if (stock.LastPrice >= 1m && stock.LastPrice <= 5m)
                {
                    IEnumerable <TradingDay> priceHistory = priceHistoryService.GetPriceHistory(stock.Symbol, DateTime.Now.AddDays(-365), DateTime.Now).OrderBy(x => x.Date);
                    Analyzer analyzer           = new Analyzer(stocks, priceHistory);
                    var      volumeAverage      = analyzer.GetVolumeAverage(stock.Symbol, processDate, 20);
                    var      senokuSpanB        = analyzer.GetSenokuSpanB(stock.Symbol, processDate, 52);
                    var      vwap               = analyzer.GetVwap(stock.Symbol, processDate, 20);
                    decimal  percentR           = analyzer.GetPercentR(stock.Symbol, processDate, 14);
                    var      closeLocationValue = analyzer.GetCloseLocationValue(stock.Symbol, processDate);

                    stock.NetChange = 0m;
                    if (priceHistory.Count() > 0 &&
                        priceHistory.Any(x => x.Date.Date == processDate.Date) &&
                        priceHistory.Any(x => x.Date.Date == processDate.AddDays(-1).Date))
                    {
                        decimal lastClosePrice = priceHistory.Where(x => x.Date.Date == processDate.AddDays(-1).Date).First().ClosePrice;
                        decimal openPrice      = priceHistory.Where(x => x.Date.Date == processDate.Date).First().OpenPrice;

                        if (openPrice > 0m && lastClosePrice > 0m &&
                            openPrice - lastClosePrice > 0m)
                        {
                            var addToList = true;
                            for (int i = priceHistory.Count() - 2; i >= priceHistory.Count() - 10; i--)
                            {
                                if (openPrice <= priceHistory.ElementAt(i).HighPrice)
                                {
                                    addToList = false;
                                }
                            }

                            if (addToList)
                            {
                                stock.NetChange = openPrice - lastClosePrice;
                                increasedStocks.Add(stock);
                                Console.WriteLine("Symbol: {0}, Change: {1}, PctR: {2}, CLV: {3}, VWAP: {4}, Senoku: {5}", stock.Symbol, stock.NetChange, percentR, closeLocationValue, vwap, senokuSpanB);
                            }
                        }
                    }

                    /*Analyzer analyzer = new Analyzer(stocks, priceHistory);
                     *
                     * var volumeAverage = analyzer.GetVolumeAverage(stock.Symbol, processDate, 20);
                     * var senokuSpanB = analyzer.GetSenokuSpanB(stock.Symbol, processDate, 52);
                     * var vwap = analyzer.GetVwap(stock.Symbol, processDate, 20);
                     * decimal percentR = analyzer.GetPercentR(stock.Symbol, processDate, 14);
                     * var closeLocationValue = analyzer.GetCloseLocationValue(stock.Symbol, processDate);
                     *
                     * if (volumeAverage > 0 && senokuSpanB > 0 && vwap > 0)
                     * {
                     *  var tradingDay = priceHistory.Where(x => x.Date.Date <= processDate.Date).OrderByDescending(x => x.Date).First();
                     *  var lastTypicalPrice = (tradingDay.HighPrice + tradingDay.LowPrice + tradingDay.ClosePrice) / 3;
                     *
                     *  if (lastTypicalPrice < senokuSpanB
                     *      && tradingDay.Volume > (volumeAverage * 0.5) &&
                     *      tradingDay.OpenPrice > vwap && tradingDay.ClosePrice < vwap /*&&
                     *      closeLocationValue > 0)
                     *  {
                     *      stock.SpanBPercentage = ((lastTypicalPrice / senokuSpanB));
                     *      topStocks.Add(stock);
                     *      Console.WriteLine("Top Stock: {0}, Pct: {1}, CLV: {2}", stock.Symbol, stock.SpanBPercentage, percentR);
                     *  }
                     * }*/
                }
            }

            Console.WriteLine("DONE!");
            Console.ReadLine();
        }
示例#15
0
 public void _06_DailyTradeInfoTest()
 {
     var service = new PriceHistoryService(new HtmlService());
     var info    = service.GetDailyTadingInfo(Code, 2018, 1).ToArray();
 }