public static void ClearTimeOfHistoricalData(object Sender)
        {
            DataTable     dt  = DB.GetDataTable("select QuoteCode from stockdata");
            DBDataManager ddm = new DBDataManager();

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                DataRow dr   = dt.Rows[j];
                string  Code = dr["QuoteCode"].ToString();
                try
                {
                    CommonDataProvider cdp = (CommonDataProvider)ddm[Code];
                    cdp.TrimTime();
                    cdp.SaveBinary(DBDataManager.GetHisDataFile(Code));
                    Thread.Sleep(1);
                    if ((j % 100) == 0)
                    {
                        Tools.Log("Clear time of historical data progress:" + j + "/" + dt.Rows.Count);
                    }
                }
                catch (Exception e)
                {
                    Tools.Log("Clear time of histoical data:" + Code + ";" + e);
                    throw;
                }
            }
        }
        public static void UpdateForNewSymbol(string Code, CommonDataProvider cdp, bool Save)
        {
            Utils.UpdateRealtime(Code, cdp);

            if (Save)
            {
                Impersonate.ChangeToAdmin();
                cdp.SaveBinary(GetHisDataFile(Code));
            }
            RefreshSymbolList();

            string[] ss = YahooDataManager.GetStockName(Code);
            if (ss.Length == 3)
            {
                try
                {
                    DB.DoCommand("insert into StockData (QuoteCode,QuoteName,Exchange) values (?,?,?)",
                                 new DbParam[] {
                        new DbParam("@Code", DbType.String, Code),
                        new DbParam("@Name", DbType.String, ss[1]),
                        new DbParam("@Exchange", DbType.String, ss[2]),
                    });
                }
                catch
                {
                }
            }
            cdp.SetStringData("Code", ss[0]);
            cdp.SetStringData("Name", ss[1]);
            cdp.SetStringData("Exchange", ss[2]);
        }
Exemple #3
0
        public static IDataProvider MergeOneRealtime(IDataManager idm, string Code, DataPackage dp)
        {
            CommonDataProvider provider1 = (CommonDataProvider)idm[Code];

            provider1.Merge(dp);
            provider1.SaveBinary(FileDataManager.GetFileName(Code));
            return(provider1);
        }
        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            DBDataManager ddm = new DBDataManager();

            try
            {
                CommonDataProvider cdp = (CommonDataProvider)ddm[tbSymbol.Text];
                lMsg.Text = "Original data count :" + cdp.Count + "<br>";
                cdp.DeleteData(dpStart.Date, dpEnd.Date);
                lMsg.Text += "New data count : " + cdp.Count;
                string Filename = DBDataManager.GetHisDataFile(tbSymbol.Text);
                if (File.Exists(Filename))
                {
                    File.Delete(Filename);
                }
                Utils.UpdateRealtime(tbSymbol.Text, cdp);
                cdp.SaveBinary(Filename);
            }
            catch (Exception ex)
            {
                lMsg.Text = ex.Message;
            }
        }
        private void btnMerge_Click(object sender, System.EventArgs e)
        {
            string[] Keys = { "OPEN", "HIGH", "LOW", "CLOSE", "VOLUME", "DATE", "ADJCLOSE" };
            string[] ss   = tbCSVData.Text.Trim().Split('\n');

            int DateIndex   = 0;
            int TickerIndex = -1;

            string[] ssHeader;
            string   DateFormat;

            GetFormatInfo(ref DateIndex, ref TickerIndex, out ssHeader, out DateFormat);

            SortedList slAllSymbol = new SortedList(Comparer.Default);
            SortedList slOneSymbol;

            char r = GetSeperator();

            for (int i = cbHasHeader.Checked?1:0; i < ss.Length; i++)
            {
                string[] sss = ss[i].Trim().Split(r);
                try
                {
                    string Ticker = tbSymbol.Text;
                    if (TickerIndex >= 0)
                    {
                        Ticker = sss[TickerIndex];
                    }
                    slOneSymbol = (SortedList)slAllSymbol[Ticker];
                    if (Ticker == "")
                    {
                        throw new Exception("Symbol can't be empty!");
                    }
                    if (slOneSymbol == null)
                    {
                        slOneSymbol         = new SortedList(Comparer.Default);
                        slAllSymbol[Ticker] = slOneSymbol;
                    }

                    slOneSymbol[DateTime.ParseExact(sss[DateIndex].Trim(),
                                                    DateFormat,
                                                    DateTimeFormatInfo.InvariantInfo)
                    ] = sss;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + ";" + sss[DateIndex] + ";" + DateFormat);
                }
            }

            lMsg.Text = "";
            DBDataManager ddm = new DBDataManager();

            try
            {
                foreach (string s in slAllSymbol.Keys)
                {
                    slOneSymbol = (SortedList)slAllSymbol[s];
                    double[][] ds = new double[7][];
                    for (int i = 0; i < ds.Length; i++)
                    {
                        ds[i] = new double[slOneSymbol.Count];
                        for (int j = 0; j < ds[i].Length; j++)
                        {
                            ds[i][j] = double.NaN;
                        }
                    }

                    for (int i = 0; i < slOneSymbol.Count; i++)
                    {
                        ds[5][i] = ((DateTime)slOneSymbol.GetKey(i)).ToOADate();
                        for (int j = 0; j < ssHeader.Length; j++)
                        {
                            if (j != DateIndex && j != TickerIndex)
                            {
                                int k = Array.IndexOf(Keys, ssHeader[j].ToUpper());
                                if (k >= 0)
                                {
                                    string[] sss = (string[])slOneSymbol.GetByIndex(i);
                                    ds[k][i] = double.Parse(sss[j]);
                                }
                            }
                        }
                        for (int j = 0; j < ds.Length; j++)
                        {
                            if (double.IsNaN(ds[j][i]))
                            {
                                ds[j][i] = ds[3][i];
                            }
                        }
                    }

                    CommonDataProvider cdp      = (CommonDataProvider)ddm[s];
                    CommonDataProvider cdpDelta = new CommonDataProvider(null);
                    cdpDelta.LoadBinary(ds);

                    lMsg.Text += "Symbol:" + s + "; Original data count :" + cdp.Count + "; Merge data count : " + cdpDelta.Count + "; ";
                    cdp.Merge(cdpDelta);
                    lMsg.Text += "New data count : " + cdp.Count + "<br>";
                    Impersonate.ChangeToAdmin();
                    Utils.UpdateRealtime(s, cdp);
                    cdp.SaveBinary(DBDataManager.GetHisDataFile(s));
                }
            }
            catch (Exception ex)
            {
                lMsg.Text = ex.Message;
            }
        }
        public override IDataProvider GetData(string Code, int Count)
        {
            Code = Code.Trim();
            Hashtable          htList = GetSymbolListHashtable();
            string             s      = (string)htList[Code];
            CommonDataProvider cdpn   = new CommonDataProvider(this);


            byte[]   bs = new byte[] {};
            string[] ss = null;

            if (s != null)
            {
                ss = s.Split(',');
                if (ss[3] != "")
                {
                    string r = (string)htList[ss[3]];
                    if (r != null)
                    {
                        ss = r.Split(',');
                    }
                }
                if (Count > 0)
                {
                    // Load data from file
                    bs = LoadHisDataFromFile(Code, Count * DataPacket.PacketByteSize);
                    if (DownloadRealTimeQuote)
                    {
                        try
                        {
                            DataPacket dp = DataPacket.DownloadFromYahoo(Code);

                            if (dp != null && !dp.IsZeroValue)
                            {
                                bs = CommonDataProvider.MergeOneQuote(bs, dp);
                            }
                        }
                        catch
                        {
                        }
                    }

                    cdpn.LoadByteBinary(bs);

                    // Update data from yahoo
                    if (AutoYahooToDB)
                    {
                        string   FileName = GetHisDataFile(Code);
                        DateTime d        = new DateTime(1900, 1, 1);
                        try
                        {
                            d = File.GetLastWriteTime(FileName);                            // GetLastAccessTime(FileName);
                        }
                        catch
                        {
                        }
                        DateTime d1 = DateTime.Now.Date;
                        DateTime d2 = d.Date;
                        TimeSpan ts = d1 - d2;
                        if (ts.TotalDays > 0)
                        {
                            YahooDataManager   ydm      = new YahooDataManager();
                            CommonDataProvider cdpDelta = (CommonDataProvider)ydm[Code, ts.Days + 5];
                            cdpDelta.Adjusted = false;
                            if (cdpDelta.Count > 0)
                            {
                                double[] dd1 = cdpDelta["DATE"];
                                double[] dd2 = cdpn["DATE"];
                                if (cdpn.Count == 0 || (int)dd2[dd2.Length - 1] < (int)dd1[dd1.Length - 1])
                                {
                                    cdpn.Merge(cdpDelta);
                                    Impersonate.ChangeToAdmin();
                                    Utils.UpdateRealtime(Code, cdpn);
                                }
                            }
                            cdpn.SaveBinary(FileName);
                        }
                    }
                }
            }
            else
            {
                try
                {
                    // Download data from yahoo
                    if (AutoYahooToDB)
                    {
                        YahooDataManager ydm = new YahooDataManager();
                        cdpn = (CommonDataProvider)ydm[Code];
                        if (cdpn.Count > 0)
                        {
                            UpdateForNewSymbol(Code, cdpn, true);
                        }
                        else
                        {
                            throw new Exception("Invalid Quote : " + Code);
                        }
                    }
                    else
                    {
                        cdpn.LoadByteBinary(bs);
                    }
                }
                catch (Exception e)
                {
                    Tools.Log(e.Message);
                    cdpn.LoadByteBinary(bs);
                }
            }
            //cdpn = TrimToEndTime(cdpn);

            cdpn.SetStringData("Code", Code);
            if (ss != null && ss.Length > 2)
            {
                cdpn.SetStringData("Code", ss[0]);
                cdpn.SetStringData("Name", ss[1]);
                cdpn.SetStringData("Exchange", ss[2]);
            }
            return(cdpn);
        }