示例#1
0
        /// <summary>
        /// Add new streaming data packet
        /// </summary>
        /// <param name="dp">streaming data packet</param>
        public void AddNewPacket(DataPacket dp)
        {
            string Symbol = dp.Symbol;

            //if code end with =X remove it
            if (Symbol.EndsWith("=X"))
            {
                Symbol = Symbol.Substring(0, Symbol.Length - 2);
            }
            CommonDataProvider cdp = htStreaming[Symbol] as CommonDataProvider;

            if (cdp == null)
            {
                cdp = CommonDataProvider.Empty;
                htStreaming[Symbol] = cdp;
            }
            cdp.Merge(dp);
        }
示例#2
0
 public string LookupDataFile(string Code, CommonDataProvider cdp, ref int Fields, ref MetaStockTimeFrame TimeFrame)
 {
     if (cdp != null)
     {
         cdp.SetStringData("Code", Code);
     }
     foreach (Master m in Masters)
     {
         if (string.Compare(m.symbol, Code, true) == 0)
         {
             if (cdp != null)
             {
                 cdp.SetStringData("Name", m.issue_name);
             }
             Fields    = m.num_fields;
             TimeFrame = m.time_frame;
             return(FilePath + "F" + m.file_num + ".DAT");
         }
     }
     if (XMasters != null)
     {
         foreach (XMaster m in XMasters)
         {
             if (string.Compare(m.StockSymbol, Code, true) == 0)
             {
                 if (cdp != null)
                 {
                     cdp.SetStringData("Name", m.StockName);
                 }
                 TimeFrame = m.TimeFrame;
                 return(FilePath + "F" + m.Fn + ".MWD");
             }
         }
     }
     return("");
 }
示例#3
0
 public override void SetStrings(CommonDataProvider cdp, string Code)
 {
     cdp.SetStringData("Code", Code);
 }
示例#4
0
        public override void SaveData(string Symbol, IDataProvider idp, Stream OutStream, DateTime Start, DateTime End, bool Intraday)
        {
            CommonDataProvider cdp = (CommonDataProvider)idp;

            if (Symbol != null && Symbol != "")
            {
                int     Count = cdp.Count;
                XMaster xm;
                Master  m         = FindBySymbol(Symbol, out xm);
                bool    NeedSave  = false;
                bool    NeedSaveX = false;

                Fields = (byte)(7 + (Intraday?1:0));
                if (m == null && xm == null)
                {
                    int NowNumber = MaxNum + 1;
                    if (NowNumber > 255)
                    {
                        xm    = new XMaster();
                        xm.Fn = (short)NowNumber;
                        ArrayList al = new ArrayList(XMasters);
                        xm.StockSymbol = Symbol;
                        xm.StockName   = cdp.GetStringData("Name");
                        if (xm.StockName == null)
                        {
                            xm.StockName = Symbol;
                        }
                        al.Add(xm);
                        XMasters  = (XMaster[])al.ToArray(typeof(XMaster));
                        NeedSaveX = true;
                    }
                    else
                    {
                        m          = new Master();
                        m.file_num = (byte)NowNumber;
                        ArrayList al = new ArrayList(Masters);
                        m.symbol     = Symbol;
                        m.num_fields = (byte)Fields;
                        m.issue_name = cdp.GetStringData("Name");
                        if (m.issue_name == null)
                        {
                            m.issue_name = Symbol;
                        }
                        al.Add(m);
                        Masters  = (Master[])al.ToArray(typeof(Master));
                        NeedSave = true;
                    }
                }

                double[] Date    = (double[])cdp["DATE"];
                double[] Open    = (double[])cdp["OPEN"];
                double[] High    = (double[])cdp["HIGH"];
                double[] Low     = (double[])cdp["LOW"];
                double[] Close   = (double[])cdp["CLOSE"];
                double[] Volume  = (double[])cdp["VOLUME"];
                double[] OpenInt = (double[])cdp["ADJCLOSE"];
                float[]  ff      = new float[Count * Fields];

                for (int i = 0; i < Count; i++)
                {
                    int      j = 0;
                    DateTime D = DateTime.FromOADate(Date[i]);
                    ff[i * Fields + j] = (D.Year - 1900) * 10000 + D.Month * 100 + D.Day;
                    if (Fields == 8)
                    {
                        j = 1;
                        ff[i * Fields + j] = D.Hour * 10000 + D.Minute * 100 + D.Second;
                    }
                    ff[i * Fields + 1 + j] = (float)Open[i];
                    ff[i * Fields + 2 + j] = (float)High[i];
                    ff[i * Fields + 3 + j] = (float)Low[i];
                    ff[i * Fields + 4 + j] = (float)Close[i];
                    ff[i * Fields + 5 + j] = (float)Volume[i];
                    ff[i * Fields + 6 + j] = (float)Close[i];
                }
                fieee2msbin(ff);
                byte[] bs = new byte[ff.Length * 4];
                Buffer.BlockCopy(ff, 0, bs, 0, bs.Length);

                string s = LookupDataFile(Symbol, cdp);               //FilePath+"F"+m.file_num+".DAT";
                using (FileStream fs = File.Create(s))
                    fs.Write(bs, 0, bs.Length);
                if (NeedSave)
                {
                    SaveMaster();
                }
                if (NeedSaveX)
                {
                    SaveXMaster();
                }
            }
        }
示例#5
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);
        }
示例#6
0
        public string LookupDataFile(string Code, CommonDataProvider cdp)
        {
            int i = 0;

            return(LookupDataFile(Code, cdp, ref i));
        }
        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);
        }
示例#8
0
 public virtual void SetStrings(CommonDataProvider cdp, string Code)
 {
     cdp.SetStringData("Code", Code);
 }
示例#9
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));
            }
        }
示例#10
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);
        }
示例#11
0
 public string LookupDataFile(string Code, CommonDataProvider cdp)
 {
     return(LookupDataFile(Code, cdp, ref Fields, ref TimeFrame));
 }
示例#12
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);
        }