| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- namespace IndexFormula.Finance.DataClient
- {
- using IndexFormula.Finance;
- using IndexFormula.Finance.DataProvider;
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading;
- public class DataClientBase
- {
- protected ArrayList alStreamingSymbols = new ArrayList();
- protected bool Canceled;
- private Hashtable htExtraData = new Hashtable();
- [Browsable(false)]
- public Exception LastError;
- private string proxy;
- private Thread StreamingThread;
- protected string Ticket;
- public bool UtcStreamingTime = true;
- public IndexFormula.Finance.DataClient.WorkingMode WorkingMode;
- public event DataProgress OnProgress;
- public event StreamingDataChanged OnStreamingData;
- public event EventHandler OnStreamingStopped;
- public DataClientBase()
- {
- this.OnProgress = (DataProgress) Delegate.Remove(this.OnProgress, null);
- this.OnStreamingData = (StreamingDataChanged) Delegate.Remove(this.OnStreamingData, null);
- this.OnStreamingStopped = (EventHandler) Delegate.Remove(this.OnStreamingStopped, null);
- }
- public void AddStreamingSymbol(string Symbol)
- {
- foreach (string str in Symbol.Split(new char[] { ',', ';' }))
- {
- string str2 = str.ToUpper();
- if (this.alStreamingSymbols.IndexOf(str2) < 0)
- {
- this.alStreamingSymbols.Add(str2);
- }
- }
- this.SteamingSymbolChanged();
- }
- public void ClearLastError()
- {
- this.LastError = null;
- }
- protected byte[] DownloadBinary(string Symbol, string URL)
- {
- byte[] buffer4;
- if ((URL.Length > 2) && (URL[1] == ':'))
- {
- using (FileStream stream = File.OpenRead(URL))
- {
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- return buffer;
- }
- }
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
- try
- {
- byte[] buffer2;
- request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
- request.Accept = "*/*";
- request.KeepAlive = false;
- request.Referer = "http://subscribe.IndexFormula.net";
- if ((this.Proxy != null) && (this.Proxy != ""))
- {
- request.Proxy = new WebProxy(this.Proxy);
- }
- else if (FormulaHelper.DownloadProxy != null)
- {
- request.Proxy = FormulaHelper.DownloadProxy;
- }
- if (this.Logined && (this.Ticket != null))
- {
- request.CookieContainer = new CookieContainer();
- request.CookieContainer.Add(new Cookie("dc", this.Ticket, "/", "subscribe.IndexFormula.net"));
- }
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- BinaryReader reader = new BinaryReader(response.GetResponseStream());
- int currentValue = 0;
- this.Canceled = false;
- ArrayList list = new ArrayList();
- Label_0125:
- buffer2 = reader.ReadBytes(0x3e8);
- if ((response.ContentLength > 0L) && (this.OnProgress != null))
- {
- currentValue += buffer2.Length;
- this.OnProgress(this, new DataProgressArgs(Symbol, currentValue, (int) response.ContentLength));
- }
- if (!this.Canceled && (buffer2.Length > 0))
- {
- list.AddRange(buffer2);
- goto Label_0125;
- }
- buffer4 = (byte[]) list.ToArray(typeof(byte));
- }
- finally
- {
- request.Abort();
- }
- return buffer4;
- }
- public virtual void DownloadStreaming()
- {
- }
- protected string DownloadString(string URL)
- {
- byte[] bytes = this.DownloadBinary(null, URL);
- if (bytes != null)
- {
- return Encoding.UTF8.GetString(bytes);
- }
- return null;
- }
- //public static DataClientBase[] GetAllDataFeeds()
- //{
- // Assembly[] assemblyArray;
- // assemblyArray = assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
- // ArrayList list = new ArrayList();
- // foreach (Assembly assembly in assemblyArray)
- // {
- // if (assembly.FullName.ToUpper().IndexOf("IndexFormula") >= 0)
- // {
- // try
- // {
- // foreach (Type type in assembly.GetTypes())
- // {
- // if (type.BaseType == typeof(DataClientBase))
- // {
- // list.Add(Activator.CreateInstance(type));
- // }
- // }
- // }
- // catch
- // {
- // }
- // }
- // }
- // if (list.Count == 0)
- // {
- // list.Add(new IndexFormulaDataClient());
- // }
- // return (DataClientBase[]) list.ToArray(typeof(DataClientBase));
- //}
- public CommonDataProvider GetCachedHistoricalData(string Symbol)
- {
- string path = Environment.CurrentDirectory + @"\Cache\";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- string str2 = path + Symbol + ".dat";
- CommonDataProvider historicalData = null;
- if (Symbol != "")
- {
- if (File.Exists(str2) && (File.GetLastWriteTime(str2).Date == DateTime.Now.Date))
- {
- historicalData = new CommonDataProvider(null);
- historicalData.LoadBinary(str2);
- historicalData.SetStringData("Code", Symbol.ToUpper());
- return historicalData;
- }
- historicalData = this.GetHistoricalData(Symbol);
- if ((historicalData != null) && (historicalData.Count > 0))
- {
- historicalData.SaveBinary(str2);
- }
- }
- return historicalData;
- }
- public virtual CommonDataProvider GetData(string symbol, DataCycle dataCycle, DateTime startTime, DateTime endTime)
- {
- return this.GetDatas(new string[] { symbol }, dataCycle, startTime, endTime)[0];
- }
- public virtual CommonDataProvider[] GetDatas(string[] symbols, DataCycle dataCycle, DateTime startTime, DateTime endTime)
- {
- if (symbols.Length > 0)
- {
- return new CommonDataProvider[] { this.GetData(symbols[0], dataCycle, startTime, endTime) };
- }
- return null;
- }
- public virtual DataPacket[] GetEodData(string Exchanges, string[] symbols, DateTime time)
- {
- return null;
- }
- public virtual string[] GetExchanges()
- {
- return null;
- }
- public CommonDataProvider GetHistoricalData(string Symbol)
- {
- return this.GetHistoricalData(Symbol, DateTime.Today.AddYears(-10), DateTime.Today);
- }
- public CommonDataProvider GetHistoricalData(string Symbol, DateTime StartTime, DateTime EndTime)
- {
- return this.GetData(Symbol, DataCycle.Day, StartTime, EndTime);
- }
- public virtual string[] GetIndustry()
- {
- return null;
- }
- public CommonDataProvider GetIntradayData(string Symbol, int Interval, DateTime StartTime, DateTime EndTime)
- {
- return this.GetData(Symbol, new DataCycle(DataCycleBase.MINUTE, Interval), StartTime, EndTime);
- }
- public virtual string[] GetMarket()
- {
- return null;
- }
- public virtual string[] GetStockType()
- {
- return null;
- }
- public string GetStreamingSymbol(string separator)
- {
- return string.Join(separator, (string[]) this.alStreamingSymbols.ToArray(typeof(string)));
- }
- public bool HasStreamingSymbol()
- {
- return (this.alStreamingSymbols.Count > 0);
- }
- public static void LoadDataFeed()
- {
- LoadDataFeed(FormulaHelper.DataFeedRoot);
- }
- public static void LoadDataFeed(string FilePath)
- {
- if (Directory.Exists(FilePath))
- {
- foreach (string str in Directory.GetFiles(FilePath, "*.dll"))
- {
- if (Path.GetFileNameWithoutExtension(str).ToUpper().IndexOf("IndexFormula") >= 0)
- {
- Assembly.LoadFrom(str);
- }
- }
- }
- }
- public static void LoadDataFeeds()
- {
- }
- public virtual void LoadExtraData()
- {
- }
- public virtual bool Login(string Username, string Password)
- {
- return false;
- }
- public virtual string[] LookupSymbols(string Key, string Exchanges, string StockType, string Market)
- {
- return null;
- }
- public void RemoveStreamingSymbol(string Symbol)
- {
- this.alStreamingSymbols.Remove(Symbol);
- this.SteamingSymbolChanged();
- }
- public virtual void ResetActiveX()
- {
- }
- public virtual void SaveExtraData()
- {
- }
- public void SetStreamingSymbol(string Symbol)
- {
- this.alStreamingSymbols.Clear();
- this.AddStreamingSymbol(Symbol);
- }
- public void StartStreaming(string Symbols)
- {
- this.SetStreamingSymbol(Symbols);
- if (this.ThreadStreaming)
- {
- if (this.StreamingThread == null)
- {
- this.StreamingThread = new Thread(new ThreadStart(this.DownloadStreaming));
- this.StreamingThread.Start();
- }
- }
- else
- {
- this.DownloadStreaming();
- }
- }
- public virtual void SteamingSymbolChanged()
- {
- if (!this.ThreadStreaming)
- {
- this.DownloadStreaming();
- }
- }
- public virtual void StopDownload()
- {
- }
- public virtual void StopStreaming()
- {
- if (this.ThreadStreaming && (this.StreamingThread != null))
- {
- this.StreamingThread.Abort();
- this.StreamingThread.Join();
- this.StreamingThread = null;
- }
- }
- public override string ToString()
- {
- return this.DataFeedName;
- }
- private string TrimStreamingSymbol(string s)
- {
- int num = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] == ',')
- {
- num++;
- }
- if (num >= this.MaxSymbolsForStreaming)
- {
- return s.Substring(0, i);
- }
- }
- return s;
- }
- [Category("Properties")]
- public virtual string DataFeedName
- {
- get
- {
- return "";
- }
- }
- [Category("Properties")]
- public virtual string Description
- {
- get
- {
- return this.DataFeedName;
- }
- }
- [Browsable(false)]
- public Hashtable ExtraData
- {
- get
- {
- return this.htExtraData;
- }
- }
- [Category("Properties")]
- public virtual string HomePage
- {
- get
- {
- return "";
- }
- }
- [Category("Features")]
- public virtual bool IsFree
- {
- get
- {
- return false;
- }
- }
- [Category("Features")]
- public virtual bool IsLocal
- {
- get
- {
- return false;
- }
- }
- [Browsable(false)]
- public bool Logined
- {
- get
- {
- return (!this.NeedLogin || ((this.Ticket != null) && (this.Ticket != "")));
- }
- }
- [Category("Properties")]
- public virtual string LoginURL
- {
- get
- {
- return this.HomePage;
- }
- }
- [Category("Features")]
- public virtual int MaxSymbolsForData
- {
- get
- {
- return 1;
- }
- }
- [Category("Features")]
- public virtual int MaxSymbolsForStreaming
- {
- get
- {
- return 1;
- }
- }
- [Category("Features")]
- public virtual bool NeedLogin
- {
- get
- {
- return true;
- }
- }
- [Browsable(false)]
- public string Proxy
- {
- get
- {
- return this.proxy;
- }
- set
- {
- this.proxy = value;
- }
- }
- [Category("Properties")]
- public virtual string RegURL
- {
- get
- {
- return this.HomePage;
- }
- }
- [Category("Features")]
- public virtual bool SupportEod
- {
- get
- {
- return false;
- }
- }
- [Category("Features")]
- public virtual bool SupportIndustry
- {
- get
- {
- return false;
- }
- }
- [Category("Features")]
- public virtual bool SupportIntraday
- {
- get
- {
- return false;
- }
- }
- [Category("Features")]
- public virtual bool SupportStreaming
- {
- get
- {
- return false;
- }
- }
- [Category("Features")]
- public virtual bool SupportSymbolList
- {
- get
- {
- return false;
- }
- }
- public virtual bool ThreadStreaming
- {
- get
- {
- return true;
- }
- }
- }
- }
|