| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- namespace Easychart.Finance.DataProvider
- {
- using System;
- using System.Collections;
- using System.Globalization;
- using System.IO;
- public class YahooCSVDataManager : CacheDataManagerBase
- {
- private string Ext;
- private string Root;
- public YahooCSVDataManager()
- {
- base.CacheTimeSpan = TimeSpan.FromHours(12.0);
- }
- public YahooCSVDataManager(string Root, string Ext)
- {
- if (!Root.EndsWith(@"\"))
- {
- Root = Root + @"\";
- }
- this.Root = Root;
- if (!Ext.StartsWith("."))
- {
- Ext = "." + Ext;
- }
- this.Ext = Ext;
- }
- public override IDataProvider GetData(string Code, int Count)
- {
- string path = this.Root + Code + this.Ext;
- if (File.Exists(path))
- {
- using (StreamReader reader = new StreamReader(path))
- {
- CommonDataProvider provider = this.LoadYahooCSV(reader);
- provider.SetStringData("Code", Code);
- return provider;
- }
- }
- return CommonDataProvider.Empty;
- }
- public CommonDataProvider LoadYahooCSV(Stream stream)
- {
- using (StreamReader reader = new StreamReader(stream))
- {
- return this.LoadYahooCSV(reader);
- }
- }
- public CommonDataProvider LoadYahooCSV(StreamReader sr)
- {
- string[] strArray = sr.ReadToEnd().Trim().Split(new char[] { '\n' });
- ArrayList list = new ArrayList();
- for (int i = 1; i < strArray.Length; i++)
- {
- strArray[i] = strArray[i].Trim();
- if (!strArray[i].StartsWith("<!--"))
- {
- list.Add(strArray[i]);
- }
- }
- int count = list.Count;
- double[] numArray = new double[count];
- double[] numArray2 = new double[count];
- double[] numArray3 = new double[count];
- double[] numArray4 = new double[count];
- double[] numArray5 = new double[count];
- double[] numArray6 = new double[count];
- double[] numArray7 = new double[count];
- DateTimeFormatInfo invariantInfo = DateTimeFormatInfo.InvariantInfo;
- NumberFormatInfo info2 = NumberFormatInfo.InvariantInfo;
- for (int j = 0; j < count; j++)
- {
- string[] strArray2 = ((string) list[j]).Split(new char[] { ',' });
- if (strArray2.Length < 7)
- {
- string[] strArray3 = new string[7];
- for (int k = 0; k < strArray2.Length; k++)
- {
- strArray3[k] = strArray2[k];
- }
- if (strArray2.Length == 6)
- {
- strArray3[6] = strArray2[4];
- }
- if (strArray2.Length == 2)
- {
- for (int m = 2; m < strArray3.Length; m++)
- {
- strArray3[m] = strArray2[1];
- }
- }
- strArray2 = strArray3;
- }
- int index = (count - j) - 1;
- numArray6[index] = DateTime.ParseExact(strArray2[0], "%d-MMM-yy", invariantInfo).ToOADate();
- numArray2[index] = double.Parse(strArray2[1], info2);
- numArray3[index] = double.Parse(strArray2[2], info2);
- numArray4[index] = double.Parse(strArray2[3], info2);
- numArray[index] = double.Parse(strArray2[4], info2);
- numArray5[index] = double.Parse(strArray2[5], info2);
- numArray7[index] = double.Parse(strArray2[6], info2);
- }
- CommonDataProvider provider = new CommonDataProvider(this);
- provider.LoadBinary(new double[][] { numArray2, numArray3, numArray4, numArray, numArray5, numArray6, numArray7 });
- return provider;
- }
- public CommonDataProvider LoadYahooCSV(byte[] data)
- {
- return this.LoadYahooCSV(new MemoryStream(data));
- }
- public CommonDataProvider LoadYahooCSV(string FileName)
- {
- return this.LoadYahooCSV(File.OpenRead(FileName));
- }
- public override void SaveData(string Symbol, IDataProvider idp, Stream OutStream, DateTime Start, DateTime End, bool Intraday)
- {
- int count = idp.Count;
- double[] numArray = idp["DATE"];
- double[] numArray2 = idp["OPEN"];
- double[] numArray3 = idp["HIGH"];
- double[] numArray4 = idp["LOW"];
- double[] numArray5 = idp["CLOSE"];
- double[] numArray6 = idp["VOLUME"];
- double[] numArray7 = idp["ADJCLOSE"];
- using (StreamWriter writer = new StreamWriter(OutStream))
- {
- writer.WriteLine("Date,Open,High,Low,Close,Volume,Adj. Close*");
- double num2 = Start.ToOADate();
- double num3 = End.ToOADate();
- for (int i = count - 1; i >= 0; i--)
- {
- if ((numArray[i] >= num2) && (numArray[i] <= num3))
- {
- writer.WriteLine(DateTime.FromOADate(numArray[i]).ToString("dd-MMM-yy", DateTimeFormatInfo.InvariantInfo) + "," + numArray2[i].ToString("f2") + "," + numArray3[i].ToString("f2") + "," + numArray4[i].ToString("f2") + "," + numArray5[i].ToString("f2") + "," + numArray6[i].ToString("f0") + "," + numArray7[i].ToString("f2"));
- }
- }
- }
- }
- }
- }
|