| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- namespace Easychart.Finance.DataProvider
- {
- using Easychart.Finance;
- using System;
- using System.Globalization;
- using System.Reflection;
- public class DataPacket
- {
- public double AdjClose;
- public double Close;
- public DateTime Date;
- public string Exchange;
- public double High;
- public double Last;
- public double Low;
- public static int MaxValue = 0x591c8;
- public double Open;
- public static int PacketByteSize = (PacketSize * 4);
- public static int PacketSize = 9;
- public string StockName;
- public string Symbol;
- public double TimeZone;
- public double Volume;
- public DataPacket(string Symbol, DateTime QuoteTime, double Price, double Volume) : this(Symbol, QuoteTime, 0.0, 0.0, 0.0, Price, Volume, Price, 0.0)
- {
- }
- public DataPacket(string Symbol, double DoubleDate, double Price, double Volume) : this(Symbol, DateTime.FromOADate(DoubleDate), Price, Volume)
- {
- }
- public DataPacket(string Symbol, DateTime Date, double Open, double High, double Low, double Close, double Volume, double AdjClose) : this(Symbol, Date, Open, High, Low, Close, Volume, AdjClose, 0.0)
- {
- }
- public DataPacket(string Symbol, double DoubleDate, double Open, double High, double Low, double Close, double Volume, double AdjClose) : this(Symbol, DoubleDate, Open, High, Low, Close, Volume, AdjClose, 0.0)
- {
- }
- public DataPacket(string Symbol, DateTime Date, double Open, double High, double Low, double Close, double Volume, double AdjClose, double Last)
- {
- this.Symbol = Symbol;
- this.Date = Date;
- this.Close = Close;
- if (AdjClose == 0.0)
- {
- this.AdjClose = Close;
- }
- else
- {
- this.AdjClose = AdjClose;
- }
- if (Open == 0.0)
- {
- this.Open = Close;
- }
- else
- {
- this.Open = Open;
- }
- if (High == 0.0)
- {
- this.High = Close;
- }
- else
- {
- this.High = High;
- }
- if (Low == 0.0)
- {
- this.Low = Close;
- }
- else
- {
- this.Low = Low;
- }
- this.Volume = Volume;
- this.Last = Last;
- }
- public DataPacket(string Symbol, double DoubleDate, double Open, double High, double Low, double Close, double Volume, double AdjClose, double Last) : this(Symbol, DateTime.FromOADate(DoubleDate), Open, High, Low, Close, Volume, AdjClose, Last)
- {
- }
- public static DataPacket DownloadFromYahoo(string Code)
- {
- return ParseYahoo(GetFromYahoo(Code));
- }
- public static DataPacket[] DownloadMultiFromYahoo(string Codes)
- {
- string[] strArray = GetFromYahoo(Codes).Trim().Split(new char[] { '\r' });
- DataPacket[] packetArray = new DataPacket[strArray.Length];
- for (int i = 0; i < strArray.Length; i++)
- {
- packetArray[i] = ParseYahoo(strArray[i]);
- }
- return packetArray;
- }
- public static DataPacket FromBytes(byte[] bs)
- {
- float[] dst = new float[PacketSize];
- Buffer.BlockCopy(bs, 0, dst, 0, PacketByteSize);
- double[] numArray2 = new double[1];
- Buffer.BlockCopy(dst, 0, numArray2, 0, 8);
- double doubleDate = numArray2[0];
- float num2 = dst[2];
- float num3 = dst[3];
- float num4 = dst[4];
- float num5 = dst[5];
- Buffer.BlockCopy(dst, 0x18, numArray2, 0, 8);
- double volume = numArray2[0];
- float num7 = dst[8];
- return new DataPacket("", doubleDate, (double) num2, (double) num3, (double) num4, (double) num5, volume, (double) num7);
- }
- public static DateTime GetDateTime(float[] fs)
- {
- return GetDateTime(fs, 0);
- }
- public static DateTime GetDateTime(float[] fs, int i)
- {
- DateTime time;
- double[] dst = new double[1];
- try
- {
- Buffer.BlockCopy(fs, i * PacketByteSize, dst, 0, 8);
- time = DateTime.FromOADate(dst[0]);
- }
- catch (Exception exception)
- {
- throw new Exception(string.Concat(new object[] { exception.Message, ";", dst[0], ";i=", i, ";Length=", fs.Length / PacketByteSize }));
- }
- return time;
- }
- public DateTime GetExchangeTime(ExchangeIntraday ei)
- {
- if (this.TimeZone == double.MaxValue)
- {
- return this.Date;
- }
- return this.Date.AddHours(ei.TimeZone);
- }
- public float[] GetFloat()
- {
- float[] dst = new float[PacketSize];
- double[] src = new double[] { this.DoubleDate };
- Buffer.BlockCopy(src, 0, dst, 0, 8);
- dst[2] = (float) this.Open;
- dst[3] = (float) this.High;
- dst[4] = (float) this.Low;
- dst[5] = (float) this.Close;
- src[0] = this.Volume;
- Buffer.BlockCopy(src, 0, dst, 0x18, 8);
- dst[8] = (float) this.AdjClose;
- return dst;
- }
- private static string GetFromYahoo(string Code)
- {
- return YahooDataManager.DownloadRealtimeFromYahoo(Code, "snd1t1oml1vpx");
- }
- public static double GetVolume(float[] fs)
- {
- double[] dst = new double[1];
- Buffer.BlockCopy(fs, 0x18, dst, 0, 8);
- return dst[0];
- }
- public bool Merge(DataPacket dp, DataCycle dc)
- {
- if (!dc.SameSequence(dp.Date, this.Date))
- {
- this.Open = dp.Close;
- this.High = dp.Close;
- this.Low = dp.Close;
- this.Close = dp.Close;
- this.AdjClose = dp.Close;
- this.Volume = dp.Volume;
- this.Date = dp.Date;
- return false;
- }
- this.Close = dp.Close;
- this.AdjClose = this.Close;
- this.High = Math.Max(this.High, this.Close);
- this.Low = Math.Min(this.Low, this.Close);
- this.Volume = dp.Volume;
- this.Date = dp.Date;
- return true;
- }
- public static DataPacket ParseEODData(string s)
- {
- string[] strArray = s.Split(new char[] { ',' });
- IFormatProvider uSFormat = FormulaHelper.USFormat;
- try
- {
- for (int i = 0; i < strArray.Length; i++)
- {
- strArray[i] = strArray[i].Trim();
- }
- return new DataPacket(strArray[0], DateTime.Parse(strArray[1], uSFormat), double.Parse(strArray[2], uSFormat), double.Parse(strArray[3], uSFormat), double.Parse(strArray[4], uSFormat), double.Parse(strArray[5], uSFormat), double.Parse(strArray[6], uSFormat), double.Parse(strArray[5], uSFormat));
- }
- catch
- {
- }
- return null;
- }
- public static DataPacket ParseYahoo(string s)
- {
- try
- {
- string[] strArray = FormulaHelper.Split(s, ',');
- string[] strArray2 = RemoveQuotation(strArray[5]).Split(new char[] { '-' });
- double def = 0.0;
- if (strArray.Length > 8)
- {
- def = ToDouble(strArray[8], def);
- }
- IFormatProvider invariantInfo = DateTimeFormatInfo.InvariantInfo;
- DataPacket packet = new DataPacket(RemoveQuotation(strArray[0]), DateTime.ParseExact(RemoveQuotation(strArray[2]) + " " + RemoveQuotation(strArray[3]), "M/%d/yyyy h:mmtt", invariantInfo, DateTimeStyles.AllowWhiteSpaces), ToDouble(strArray[4], 0.0), ToDouble(strArray2[1], 0.0), ToDouble(strArray2[0], 0.0), ToDouble(strArray[6], 0.0), ToDouble(strArray[7], 0.0), ToDouble(strArray[6], 0.0), def);
- packet.StockName = RemoveQuotation(strArray[1]);
- packet.TimeZone = ExchangeIntraday.YahooTimeZone;
- if (strArray.Length > 9)
- {
- packet.Exchange = RemoveQuotation(strArray[9]);
- }
- return packet;
- }
- catch
- {
- return null;
- }
- }
- public static string RemoveQuotation(string s)
- {
- return s.Trim(new char[] { ' ', '"', '\r', '\n' });
- }
- public byte[] ToByte()
- {
- byte[] dst = new byte[PacketByteSize];
- Buffer.BlockCopy(this.GetFloat(), 0, dst, 0, dst.Length);
- return dst;
- }
- public static double ToDouble(string s, double Def)
- {
- try
- {
- IFormatProvider uSFormat = FormulaHelper.USFormat;
- return double.Parse(s, uSFormat);
- }
- catch
- {
- return Def;
- }
- }
- public static float ToFloat(string s, float Def)
- {
- try
- {
- return float.Parse(s, NumberFormatInfo.InvariantInfo);
- }
- catch
- {
- return Def;
- }
- }
- public double DoubleDate
- {
- get
- {
- return this.Date.ToOADate();
- }
- }
- public bool IsZeroValue
- {
- get
- {
- return ((((this.Open == 0.0) || (this.High == 0.0)) || (this.Low == 0.0)) || (this.Close == 0.0));
- }
- }
- public double this[string Type]
- {
- get
- {
- switch (Type.ToUpper())
- {
- case "OPEN":
- return this.Open;
- case "DATE":
- return this.DoubleDate;
- case "HIGH":
- return this.High;
- case "LOW":
- return this.Low;
- case "CLOSE":
- return this.Close;
- case "VOLUME":
- return this.Volume;
- case "ADJCLOSE":
- return this.AdjClose;
- }
- return double.NaN;
- }
- }
- }
- }
|