public HistoricalPriceData GetLastestHistoricalPriceFetchIfNotAvailable(string stockCode)
		{
			var historicalData = GetLatestHistoricalPrice(stockCode);

			var dataExistsInDatabase = historicalData != null &&  DateTime.Today.AddDays(-1) > historicalData.Date;
			if (dataExistsInDatabase)
			{
				return historicalData;
			}

			var yahooFinanceApi = new YahooFinanceClient();

			List<YahooHistoricalPriceData> yahooData;

			if (GetHistoricalPriceAny(stockCode))
			{
				yahooData = yahooFinanceApi.GetDailyHistoricalPriceData(stockCode, DateTime.Today, DateTime.Today.AddMonths(-1));
			}
			else
			{
				yahooData = yahooFinanceApi.GetDailyHistoricalPriceData(stockCode);
			}

			var historicalPriceData = new List<HistoricalPriceData>();

			foreach (var priceData in yahooData)
			{
				var data = new HistoricalPriceData()
				{
					Symbol = stockCode,
					DataSource = "YAHOO",
					Date = priceData.Date,
					Open = priceData.Open,
					High = priceData.High,
					Low = priceData.Low,
					Close = priceData.Close,
					Volume = priceData.Volume,
					AdjClose = priceData.AdjClose,
				};

				historicalPriceData.Add(data);

				if (!GetHistoricalPriceExists(data))
				{
					AddHistoricalPrice(data);
				}
			}

			SaveAll();

			return GetLatestHistoricalPrice(stockCode);
		}
        public void ReadMeExample()
        {
            string exchange = "ASX";
            string symbol   = "AFI";

            YahooFinanceClient yahooFinance   = new YahooFinanceClient();
            string             yahooStockCode = yahooFinance.GetYahooStockCode(exchange, symbol);
            List <YahooHistoricalPriceData>    yahooPriceHistory    = yahooFinance.GetDailyHistoricalPriceData(yahooStockCode);
            List <YahooHistoricalDividendData> yahooDividendHistory = yahooFinance.GetHistoricalDividendData(yahooStockCode);
        }
        public void TestDailyPriceHistoy()
        {
            var exchange = "ASX";
            var symbol   = "AFI";

            var yahooFinance      = new YahooFinanceClient(cookie, crumb);
            var yahooStockCode    = yahooFinance.GetYahooStockCode(exchange, symbol);
            var yahooPriceHistory = yahooFinance.GetDailyHistoricalPriceData(yahooStockCode, new DateTime(2016, 7, 1), new DateTime(2016, 7, 30));

            Assert.Equal(21, yahooPriceHistory.Count);
        }
Ejemplo n.º 4
0
        public void GetTick()
        {
            string cookie   = "31km9s9dm7p94&b=3&s=3d";
            string crumb    = "gFXT6LioH24";
            string exchange = "NSE";  //"ASX";
            string symbol   = "INFY"; // "AFI";

            YahooFinanceClient yahooFinance   = new YahooFinanceClient(cookie, crumb);
            string             yahooStockCode = yahooFinance.GetYahooStockCode(exchange, symbol);
            List <YahooHistoricalPriceData>    yahooPriceHistory    = yahooFinance.GetDailyHistoricalPriceData(yahooStockCode);
            List <YahooHistoricalDividendData> yahooDividendHistory = yahooFinance.GetHistoricalDividendData(yahooStockCode);
            YahooRealTimeData yahooRealTimeData = yahooFinance.GetRealTimeData(yahooStockCode);
        }
        Task <ICollection <HistoricalQuote> > IYahooFinanceService.GetDailyHistoricalPriceData(string stockExchange, string companySymbol, DateTime?startDate, DateTime?endDate)
        {
            var yahooFinance      = new YahooFinanceClient();
            var yahooStockCode    = yahooFinance.GetYahooStockCode(stockExchange, companySymbol);
            var yahooPriceHistory = yahooFinance.GetDailyHistoricalPriceData(yahooStockCode);

            var priceHistory = _mapper.Map <ICollection <HistoricalQuote> >(yahooPriceHistory);

            foreach (var historicalQuote in priceHistory)
            {
                historicalQuote.Symbol = $"{stockExchange}:{companySymbol}";
            }

            return(Task.FromResult(priceHistory));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string cookie = GetYahooBCookieValueByName();
            string crumb  = "enp5cLfls8Q";


            string exchange = "NYSE";
            string symbol   = "enp5cLfls8Q";

            YahooFinanceClient yahooFinance   = new YahooFinanceClient(cookie, crumb);
            string             yahooStockCode = yahooFinance.GetYahooStockCode(exchange, symbol);
            List <YahooHistoricalPriceData>    yahooPriceHistory    = yahooFinance.GetDailyHistoricalPriceData(yahooStockCode);
            List <YahooHistoricalDividendData> yahooDividendHistory = yahooFinance.GetHistoricalDividendData(yahooStockCode);
            YahooRealTimeData yahooRealTimeData = yahooFinance.GetRealTimeData(yahooStockCode);
        }
Ejemplo n.º 7
0
        public void Download(IJobCancellationToken cancellationToken)
        {
            var ti = uow.TimeIntervals.GetAll().FirstOrDefault(x => x.Duration == TimeSpan.FromDays(1));

            var result = uow.BrokerInstruments.GetAllForBroker("YA");//.Where(x => x.Id>54 && x.Id <58);

            logger.LogInformation("YahooDownloader will download " + result.Count() + " BrokerInstruments");
            foreach (var element in result)
            {
                logger.LogInformation("YahooDownloader start " + element.BrokerSymbol.Name);
                List <YahooHistoricalPriceData> yahooPriceHistory = null;
                try
                {
                    yahooPriceHistory =
                        _yahooFinance.GetDailyHistoricalPriceData(element.BrokerSymbol.Name,
                                                                  endDate: DateTime.Today + TimeSpan.FromDays(1),
                                                                  startDate: DateTime.Today - TimeSpan.FromDays(3));

                    yahooPriceHistory.Sort((x, y) => x.Date.CompareTo(y.Date));

                    foreach (var price in yahooPriceHistory)
                    {
                        if (price.Volume > 0)
                        {
                            var priceDate  = element.BrokerSymbol.Exchange.CloseTimeOnDate(price.Date);
                            var priceEntry = uow.PriceEntries.GetForBrokerInstrument(element.Id, ti, priceDate);
                            if (priceEntry == null)
                            {
                                priceEntry = new PriceEntry();
                                logger.LogInformation("YahooDownloader " + element.BrokerSymbol.Name +
                                                      " new Price for " + price.Date);
                                uow.PriceEntries.Add(priceEntry);
                            }
                            else
                            {
                                logger.LogInformation("YahooDownloader " + element.BrokerSymbol.Name + " updated Price for " + price.Date);
                            }
                            priceEntry.BrokerInstrument = element;
                            priceEntry.IsFinished       = true;
                            priceEntry.Open             = price.Open;
                            priceEntry.High             = price.High;
                            priceEntry.Low          = price.Low;
                            priceEntry.Close        = price.Close;
                            priceEntry.Volume       = price.Volume;
                            priceEntry.TimeInterval = ti;
                            priceEntry.TimeStamp    = priceDate;
                            uow.PriceEntries.UpdateIndicatorData(priceEntry);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogInformation("YahooDownloader Exception " + e.ToString());
                }

                var currentPriceEntry = uow.PriceEntries.GetForBrokerInstrument(element.Id, ti).FirstOrDefault(x => x.IsFinished == false);

                if (yahooPriceHistory?.FirstOrDefault()?.Date != DateTime.Now.Date &&
                    element.BrokerSymbol.Exchange.IsOpen
                    )
                {
                    YahooRealTimeData rt = _yahooFinance.GetRealTimeData(element.BrokerSymbol.Name);
                    if (rt != null && rt.LastTradeTime.TimeOfDay < DateTime.Now.TimeOfDay)
                    {
                        DateTime compare = rt.LastTradeTime.Date;

                        if (currentPriceEntry == null)
                        {
                            currentPriceEntry = new PriceEntry();
                            uow.PriceEntries.AddPrice(currentPriceEntry);
                        }
                        //var realTradeTime = rt.LastTradeTime + element.Exchange.TimeZoneOffset;

                        currentPriceEntry.BrokerInstrument = element;
                        currentPriceEntry.IsFinished       = false;
                        currentPriceEntry.Open             = rt.Open;
                        currentPriceEntry.High             = rt.High;
                        currentPriceEntry.Low          = rt.Low;
                        currentPriceEntry.Close        = rt.Last;
                        currentPriceEntry.Volume       = rt.Volume;
                        currentPriceEntry.TimeInterval = ti;
                        currentPriceEntry.TimeStamp    = element.BrokerSymbol.Exchange.getLocalTime(rt.LastTradeTime);
                        uow.PriceEntries.UpdateIndicatorData(currentPriceEntry);
                    }
                }
                else
                {
                    if (currentPriceEntry != null)
                    {
                        uow.PriceEntries.Remove(currentPriceEntry);
                    }
                }
            }

            uow.Complete();

            BackgroundJob.Enqueue <SignalProcessor>(x => x.ProcessAllSignals(JobCancellationToken.Null));
        }