コード例 #1
0
 public IDataProvider this[string Code, int Count]
 {
     get
     {
         string path = "";
         if (this.EnableMemoryCache && (HttpContext.Current != null))
         {
             object obj2 = HttpContext.Current.Cache["Cache_" + Code];
             if (obj2 != null)
             {
                 return((CommonDataProvider)obj2);
             }
         }
         if ((this.EnableFileCache && (this.CacheRoot != null)) && (this.CacheRoot != ""))
         {
             try
             {
                 if (!Directory.Exists(this.CacheRoot))
                 {
                     Directory.CreateDirectory(this.CacheRoot);
                 }
                 if (!this.CacheRoot.EndsWith(@"\"))
                 {
                     this.CacheRoot = this.CacheRoot + @"\";
                 }
                 path = this.CacheRoot + Code;
                 if (File.Exists(path) && (File.GetLastWriteTime(path).Date == DateTime.Now.Date))
                 {
                     CommonDataProvider provider = new CommonDataProvider(this);
                     provider.LoadBinary(path);
                     return(provider);
                 }
             }
             catch
             {
                 path = "";
             }
         }
         IDataProvider data = this.GetData(Code, Count);
         if ((data is CommonDataProvider) && (data.Count > 0))
         {
             try
             {
                 if (this.EnableFileCache && (path != ""))
                 {
                     (data as CommonDataProvider).SaveBinary(path);
                 }
             }
             catch
             {
                 if (this.EnableMemoryCache && (HttpContext.Current != null))
                 {
                     HttpContext.Current.Cache.Add("Cache_" + Code, data, null, DateTime.Now.AddDays(1.0), TimeSpan.Zero, CacheItemPriority.Normal, null);
                 }
             }
         }
         return(data);
     }
 }
コード例 #2
0
        public override IDataProvider GetData(string Code, int Count)
        {
            CommonDataProvider cdp = new CommonDataProvider(this);
            string             s   = LookupDataFile(Code, cdp, ref Fields);

            if (s != "" && File.Exists(s))
            {
                using (FileStream fs = ReadData(s))
                {
                    byte[] bb = new byte[Fields * 4];
                    byte[] bs = new byte[fs.Length - bb.Length];
                    fs.Read(bb, 0, bb.Length);
                    fs.Read(bs, 0, bs.Length);

                    float[] ff = new float[bs.Length / 4];
                    Buffer.BlockCopy(bs, 0, ff, 0, bs.Length);
                    fmsbin2ieee(ff);
                    int      N       = ff.Length / Fields;
                    double[] Date    = new double[N];
                    double[] Open    = new double[N];
                    double[] High    = new double[N];
                    double[] Low     = new double[N];
                    double[] Close   = new double[N];
                    double[] Volume  = new double[N];
                    double[] OpenInt = new double[N];
                    if (Fields == 5)
                    {
                        Open    = Close;
                        OpenInt = Close;
                    }

                    for (int i = 0; i < N; i++)
                    {
                        int      D  = (int)ff[i * Fields];
                        DateTime DD = new DateTime(D / 10000 + 1900, (D / 100) % 100, D % 100);
                        int      j  = 0;
                        if (Fields == 8)
                        {
                            int T = (int)ff[i * Fields + 1];
                            DD += new TimeSpan(T / 10000, (T / 100) % 100, T % 100);
                            j   = 1;
                        }
                        Date[i] = DD.ToOADate();

                        if (Fields >= 7)
                        {
                            Open[i]    = ff[i * Fields + 1 + j];
                            High[i]    = ff[i * Fields + 2 + j];
                            Low[i]     = ff[i * Fields + 3 + j];
                            Close[i]   = ff[i * Fields + 4 + j];
                            Volume[i]  = ff[i * Fields + 5 + j];
                            OpenInt[i] = Close[i];
                        }
                        else
                        {
                            High[i]   = ff[i * Fields + 1];
                            Low[i]    = ff[i * Fields + 2];
                            Close[i]  = ff[i * Fields + 3];
                            Volume[i] = ff[i * Fields + 4];
                        }
                    }
                    cdp.LoadBinary(new double[][] { Open, High, Low, Close, Volume, Date, OpenInt });
                    return(cdp);
                }
            }
            else
            {
                cdp.LoadByteBinary(new byte[] {});
            }
            return(cdp);
        }
コード例 #3
0
        public CommonDataProvider LoadYahooCSV(StreamReader sr)
        {
            string s = sr.ReadToEnd().Trim();

            string[]  ss = s.Split('\n');
            ArrayList al = new ArrayList();

            for (int i = 1; i < ss.Length; i++)
            {
                ss[i] = ss[i].Trim();
                if (!ss[i].StartsWith("<!--"))
                {
                    al.Add(ss[i]);
                }
            }

            int N = al.Count;

            double[] CLOSE    = new double[N];
            double[] OPEN     = new double[N];
            double[] HIGH     = new double[N];
            double[] LOW      = new double[N];
            double[] VOLUME   = new double[N];
            double[] DATE     = new double[N];
            double[] ADJCLOSE = new double[N];

            DateTimeFormatInfo dtfi = DateTimeFormatInfo.InvariantInfo;
            NumberFormatInfo   nfi  = NumberFormatInfo.InvariantInfo;

            for (int i = 0; i < N; i++)
            {
                string[] sss = ((string)al[i]).Split(',');
                if (sss.Length < 7)
                {
                    string[] rrr = new string[7];
                    for (int k = 0; k < sss.Length; k++)
                    {
                        rrr[k] = sss[k];
                    }
                    if (sss.Length == 6)
                    {
                        rrr[6] = sss[4];
                    }
                    //Format: 3-Mar-1904,13
                    if (sss.Length == 2)
                    {
                        for (int k = 2; k < rrr.Length; k++)
                        {
                            rrr[k] = sss[1];
                        }
                    }

                    sss = rrr;
                }
                int j = N - i - 1;
                DATE[j]     = DateTime.ParseExact(sss[0], "yyyy-MM-dd", dtfi).ToOADate();            //%d-MMM-yy
                OPEN[j]     = double.Parse(sss[1], nfi);
                HIGH[j]     = double.Parse(sss[2], nfi);
                LOW[j]      = double.Parse(sss[3], nfi);
                CLOSE[j]    = double.Parse(sss[4], nfi);
                VOLUME[j]   = double.Parse(sss[5], nfi);
                ADJCLOSE[j] = double.Parse(sss[6], nfi);
            }

            CommonDataProvider cdp = new CommonDataProvider(this);

            cdp.LoadBinary(new double[][] { OPEN, HIGH, LOW, CLOSE, VOLUME, DATE, ADJCLOSE });
            return(cdp);
        }
コード例 #4
0
        /// <summary>
        /// Implement the interface
        /// </summary>
        public override IDataProvider this[string Code, int Count]
        {
            get
            {
                if (CacheTimeSpan == TimeSpan.Zero)
                {
                    return(base[Code, Count]);
                }
                if (EnableMemoryCache && HttpContext.Current != null)
                {
                    object o = HttpContext.Current.Cache[GetKey(Code)];
                    if (o != null)
                    {
                        return(MergeRealtime((CommonDataProvider)o, Code));
                    }
                }

                string Cache = "";
                if (EnableFileCache && CacheRoot != null && CacheRoot != "")
                {
                    try
                    {
                        if (CacheRoot.EndsWith("\\"))
                        {
                            CacheRoot = CacheRoot.Substring(0, CacheRoot.Length - 1);
                        }

                        if (!Directory.Exists(CacheRoot))
                        {
                            Directory.CreateDirectory(CacheRoot);
                        }

                        CacheRoot += "\\";
                        Cache      = CacheRoot + GetKey(Code);

                        if (File.Exists(Cache))
                        {
                            if (File.GetLastWriteTime(Cache).Add(CacheTimeSpan) > DateTime.Now)
                            {
                                CommonDataProvider cdp = new CommonDataProvider(this);
                                cdp.LoadBinary(Cache);
                                return(MergeRealtime(cdp, Code));
                            }
                        }
                    }
                    catch
                    {
                        Cache = "";
                    }
                }

                IDataProvider idp        = base[Code, Count];
                bool          FileCached = false;
                if (idp is CommonDataProvider && idp.Count > 0)
                {
                    try
                    {
                        if (EnableFileCache && Cache != "")
                        {
                            (idp as CommonDataProvider).SaveBinary(Cache);
                            FileCached = true;
                        }
                    }
                    catch
                    {
                    }
                }

                if (EnableMemoryCache && HttpContext.Current != null && !FileCached && idp != null)
                {
                    HttpContext.Current.Cache.Add(GetKey(Code), idp, null, DateTime.Now.Add(CacheTimeSpan), TimeSpan.Zero, CacheItemPriority.Default, null);
                }
                return(MergeRealtime((CommonDataProvider)idp, Code));
            }
        }
コード例 #5
0
        public override IDataProvider GetData(string Code, int Count)
        {
            CommonDataProvider cdp = new CommonDataProvider(this);

            if (Code == null)
            {
                Code = "MSFT";
            }
            Random Rnd = new Random(Code.GetHashCode());

            /// ds[0] : OPEN
            /// ds[1] : HIGH
            /// ds[2] : LOW
            /// ds[3] : CLOSE
            /// ds[4] : VOLUME
            /// ds[5] : DATE
            double[][] ds = new double[6][];
            double[]   dd = null;
            if (IntradayInfo != null)
            {
                //ExchangeIntraday ei = ExchangeIntraday.US;
                dd       = IntradayInfo.GetMinuteDate(DateTime.Today.AddDays(-7), DateTime.Today);
                MaxCount = dd.Length;
            }

            for (int i = 0; i < ds.Length; i++)
            {
                ds[i] = new double[MaxCount];
            }

            for (int i = 0; i < MaxCount; i++)
            {
                if (i == 0)
                {
                    ds[0][i] = 20;
                    ds[3][i] = 21;
                    ds[4][i] = 100000;
                }
                else
                {
                    ds[0][i] = ds[0][i - 1] + Rnd.NextDouble() - 0.48;
                    ds[3][i] = ds[0][i - 1] + Rnd.NextDouble() - 0.48;
                    ds[4][i] = Math.Abs(ds[4][i - 1] + Rnd.Next(100000) - 50000);
                }
                ds[1][i] = Math.Max(ds[0][i], ds[3][i]) + Rnd.NextDouble();
                ds[2][i] = Math.Min(ds[0][i], ds[3][i]) - Rnd.NextDouble();
                if (IntradayInfo != null)
                {
                    ds[5][i] = dd[i];
                }
                else
                {
                    ds[5][i] = DateTime.Today.AddDays(i - MaxCount).ToOADate();
                }
            }
            cdp.LoadBinary(ds);
            cdp.SetStringData("Code", Code);
//			if (IntradayInfo!=null)
//				cdp.SetStringData("Code",Code+"@Random Intraday");
//			else cdp.SetStringData("Code",Code+"@Random");
            return(cdp);
        }
コード例 #6
0
        public static CommonDataProvider LoadYahooCSV(IDataManager idm, Stream stream)
        {
            StreamReader reader = new StreamReader(stream);

            string[]  strArray = reader.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], DateFormat, 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(idm);

            provider.LoadBinary(new double[][] { numArray2, numArray3, numArray4, numArray, numArray5, numArray6, numArray7 });
            return(provider);
        }