| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- namespace Easychart.Finance.DataClient
- {
- using Easychart.Finance;
- using Easychart.Finance.DataProvider;
- using System;
- using System.Collections;
- using System.Runtime.CompilerServices;
- using System.Text.RegularExpressions;
- using System.Threading;
- public class YahooDataClient : DataClientBase
- {
- public event DataProgress OnProgress;
- public event StreamingDataChanged OnStreamingData;
- public event EventHandler OnStreamingStopped;
- public override void DownloadStreaming()
- {
- try
- {
- while (true)
- {
- foreach (DataPacket packet in DataPacket.DownloadMultiFromYahoo(base.GetStreamingSymbol(",")))
- {
- if ((packet != null) && !packet.IsZeroValue)
- {
- if (base.UtcStreamingTime)
- {
- packet.Date = packet.Date.Date + packet.Date.AddHours(-packet.TimeZone).TimeOfDay;
- }
- if (this.OnStreamingData != null)
- {
- this.OnStreamingData(this, packet);
- }
- }
- }
- Thread.Sleep(0x1388);
- }
- }
- finally
- {
- if (this.OnStreamingStopped != null)
- {
- this.OnStreamingStopped(this, new EventArgs());
- }
- }
- }
- public override CommonDataProvider GetData(string symbols, DataCycle dataCycle, DateTime startTime, DateTime endTime)
- {
- string format = "http://table.finance.yahoo.com/table.csv?s={0}&d={4}&e={5}&f={6}&g=d&a={1}&b={2}&c={3}&ignore=.csv";
- string uRL = string.Format(format, new object[] { symbols, startTime.Month - 1, startTime.Day, startTime.Year, endTime.Month - 1, endTime.Day, endTime.Year });
- byte[] data = base.DownloadBinary(symbols, uRL);
- return new YahooCSVDataManager().LoadYahooCSV(data);
- }
- public override DataPacket[] GetEodData(string Exchanges, string[] symbols, DateTime time)
- {
- string format = "http://table.finance.yahoo.com/table.csv?s={0}&d={4}&e={5}&f={6}&g=d&a={1}&b={2}&c={3}&ignore=.csv";
- DataPacket[] packetArray = new DataPacket[symbols.Length];
- DateTime time2 = time.AddDays(-1.0);
- for (int i = 0; i < symbols.Length; i++)
- {
- string symbol = symbols[i];
- string uRL = string.Format(format, new object[] { symbol, time2.Month - 1, time2.Day, time2.Year, time.Month - 1, time.Day, time.Year });
- byte[] data = base.DownloadBinary(symbol, uRL);
- packetArray[i] = new YahooCSVDataManager().LoadYahooCSV(data).GetLastPackage();
- if (this.OnProgress != null)
- {
- this.OnProgress(this, new DataProgressArgs(symbol, i, symbols.Length));
- }
- }
- return packetArray;
- }
- public override string[] GetMarket()
- {
- return new string[] { "US=U.S. & Canada", "=World Markets" };
- }
- public override string[] GetStockType()
- {
- return new string[] { "S=Stocks", "E=ETF", "I=Indices", "M=Mutual Fund", "O=Options" };
- }
- public override string[] LookupSymbols(string Key, string Exchanges, string StockType, string Market)
- {
- if (StockType == null)
- {
- StockType = "";
- }
- if (Market == null)
- {
- Market = "";
- }
- MatchCollection matchs = Regex.Matches(base.DownloadString("http://finance.yahoo.com/l?s=" + Key + "&t=" + StockType + "&m=" + Market), "<TR bgcolor=#ffffff><TD><font face=arial size=-1><a href=(?<href>[^>]+)>(?<symbol>[^<]+)</a></font></TD><TD><font face=arial size=-1>(?<name>[^<]+)</font></TD><TD><font face=arial size=-1>(?<exchange>[^<]+)</font>", RegexOptions.IgnoreCase);
- ArrayList list = new ArrayList();
- foreach (Match match in matchs)
- {
- list.Add(match.Groups["symbol"].Value + ";" + match.Groups["name"].Value + ";" + match.Groups["exchange"].Value);
- }
- return (string[]) list.ToArray(typeof(string));
- }
- public override string DataFeedName
- {
- get
- {
- return "Yahoo finance";
- }
- }
- public override string Description
- {
- get
- {
- return "Provided by yahoo finance";
- }
- }
- public override string HomePage
- {
- get
- {
- return "http://finance.yahoo.com";
- }
- }
- public override bool IsFree
- {
- get
- {
- return true;
- }
- }
- public override int MaxSymbolsForStreaming
- {
- get
- {
- return 100;
- }
- }
- public override bool NeedLogin
- {
- get
- {
- return false;
- }
- }
- public override bool SupportEod
- {
- get
- {
- return false;
- }
- }
- public override bool SupportIntraday
- {
- get
- {
- return false;
- }
- }
- }
- }
|