public static bool UpdateQuote(DateTime d, string Exchange)
 {
     try
     {
         Impersonate.ChangeToAdmin();
         string   s  = d.ToString("yyyyMMMdd", DateTimeFormatInfo.InvariantInfo);
         string[] ss = DownloadData("http://www.eoddata.com/Data.asp?e=" + Exchange + "&d=" + s);
         if (string.Compare(Exchange, "INDEX", true) == 0)
         {
             for (int i = 0; i < ss.Length; i++)
             {
                 ss[i] = "^" + ss[i];
             }
         }
         if (ss.Length > 0)
         {
             MergeOneDay(ss);
         }
         return(ss.Length > 0);
     }
     catch (Exception e)
     {
         Tools.Log("Update Quote:" + e.Message);
         throw;
     }
 }
Esempio n. 2
0
        public static void Write(string Key, string Value)
        {
            XmlDocument xd = new XmlDocument();
            string      s  = HttpRuntime.AppDomainAppPath + @"\web.config";

            xd.Load(s);
            XmlNode xns = xd.SelectSingleNode("/configuration/appSettings");

            for (int i = 0; i < xns.ChildNodes.Count; i++)
            {
                if (xns.ChildNodes[i] is XmlElement)
                {
                    XmlElement xe = xns.ChildNodes[i] as XmlElement;
                    if (xe.Name.ToLower() == "add")
                    {
                        if (string.Compare(xe.GetAttribute("key").ToString(), Key, true) == 0)
                        {
                            xe.SetAttribute("value", Value);
                        }
                    }
                }
            }
            Impersonate.ChangeToAdmin();
            xd.Save(s);
        }
Esempio n. 3
0
        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]);
        }
Esempio n. 4
0
        /// <summary>
        /// Update end of day data automatically
        /// </summary>
        public static void AutoUpdate()
        {
            Thread.Sleep(10000);
            Impersonate.ChangeToAdmin();
            Tools.Log("Auto Update: Started");
            string[]  ss = Config.AutoUpdate.Split(',');
            Hashtable ht = new Hashtable();

            while (true)
            {
                try
                {
                    LoadHashtable(ht, "Update.txt");
                    foreach (string s in ss)
                    {
                        int    i        = s.IndexOf("=");
                        string Exchange = s.Substring(0, i);
                        string r        = s.Substring(i + 1);

                        DateTime LastTime = DateTime.MinValue;
                        if (ht[Exchange] != null)
                        {
                            LastTime = (DateTime)ht[Exchange];
                        }

                        DateTime ServiceTime;
                        DateTime CurrentTime;
                        GetTime(r, out ServiceTime, out CurrentTime);

                        if (CurrentTime.DayOfWeek != DayOfWeek.Saturday && CurrentTime.DayOfWeek != DayOfWeek.Sunday)
                        {
                            if (CurrentTime.TimeOfDay > ServiceTime.TimeOfDay && LastTime.Date < CurrentTime.Date)
                            {
                                try
                                {
                                    Tools.Log(r + ";ServiceTime=" + ServiceTime + ";CurrentTime=" + CurrentTime);
                                    if (Config.AutoUpdateSource(CurrentTime, Exchange))
                                    {
                                        ht[Exchange] = CurrentTime;
                                        SaveHashtable(ht, "Update.txt");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Tools.Log("AutoUpdate:UpdateQuote:" + ex.Message);
                                }
                            }
                        }
                    }
                    Thread.Sleep(60000);
                }
                catch (Exception ex)
                {
                    Tools.Log("AutoUpdate:Loop:" + ex.Message);
                    Thread.Sleep(60000);
                }
            }
        }
Esempio n. 5
0
 static private void DoUpdateQuote()
 {
     Impersonate.ChangeToAdmin();
     try
     {
         UpdateQuote(UpdateQuoteDate, UpdateQuoteExchange);
     }
     finally
     {
         UpdateQuoteThread = null;
     }
 }
        public void DownloadHistory()
        {
            Impersonate.ChangeToAdmin();
            try
            {
                DB.DoCommand("update StockData set HasHistory=0");
                string Where = "";
                if (DownloadMode == 1)
                {
                    Where = " where LastTime<'" + tbDateBefore.Text + "' or LastTime is null ";
                }
                DataTable dt = DB.GetDataTable(
                    "select a.QuoteCode,a.QuoteName,a.HasHistory,b.LastTime from StockData a " +
                    " left join realtime b on a.QuoteCode=b.QuoteCode " + Where + " order by a.QuoteCode");

                DbParam[] dps = new DbParam[] {
                    new DbParam("@QuoteCode", DbType.String, null),
                };
                DownloadProgress = "Download started:" + dt.Rows.Count;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    try
                    {
                        DataRow dr = dt.Rows[i];
                        if (StopDownload)
                        {
                            break;
                        }
                        string QuoteCode = dr["QuoteCode"].ToString();
                        Utils.DownloadYahooHistory(QuoteCode, DownloadMode != 0, false);
                        Tools.Log("Downloaded " + QuoteCode + " from yahoo finance");
                        dps[0].Value = QuoteCode;
                        DB.DoCommand("update StockData set HasHistory=1 where QuoteCode=?", dps);
                        DownloadProgress = (i + 1) + "/" + dt.Rows.Count;
                    }
                    catch (Exception ex)
                    {
                        Tools.Log(ex.Message);
                    }
                }
            }
            finally
            {
                DownloadProgress      = "Download history finished!";
                DownloadHistoryThread = null;
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        /// <summary>
        /// Running back ground services at curtain time.
        /// The service and curtain time are defined in web.config
        /// </summary>
        static public void AutoService()
        {
            Thread.Sleep(1000);
            Impersonate.ChangeToAdmin();
            Tools.Log("Auto Service: Started");
            Hashtable ht = new Hashtable();

            while (true)
            {
                try
                {
                    LoadHashtable(ht, "Service.txt");
                    for (int i = 1; i < 10; i++)
                    {
                        string ServiceName = "Service" + i;
                        string s           = ConfigurationManager.AppSettings[ServiceName];
                        if (s != null)
                        {
                            string[] ss = s.Split(',');
                            if (ss.Length >= 2)
                            {
                                int[] WeekDay = { 1, 2, 3, 4, 5 };
                                if (ss.Length == 3)
                                {
                                    string[] sss = ss[2].Split(';');
                                    WeekDay = new int[sss.Length];
                                    for (int j = 0; j < ss.Length; j++)
                                    {
                                        WeekDay[j] = int.Parse(sss[j]);
                                    }
                                }
                                DateTime LastTime = DateTime.MinValue;
                                if (ht[ServiceName] != null)
                                {
                                    LastTime = (DateTime)ht[ServiceName];
                                }

                                DateTime ServiceTime;
                                DateTime CurrentTime;
                                GetTime(ss[1], out ServiceTime, out CurrentTime);

                                if (Array.IndexOf(WeekDay, (int)CurrentTime.DayOfWeek) >= 0)
                                {
                                    if (CurrentTime.TimeOfDay > ServiceTime.TimeOfDay && LastTime.Date < CurrentTime.Date)
                                    {
                                        try
                                        {
                                            Execute(ss[0], CurrentTime);
                                            ht[ServiceName] = CurrentTime;
                                            SaveHashtable(ht, "Service.txt");
                                        }
                                        catch (Exception ex)
                                        {
                                            Tools.Log(ServiceName + ":" + ex.Message);
                                        }
                                    }
                                }
                            }
                        }
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception ex)
                {
                    Tools.Log("AutoService:Loop:" + ex.Message);
                    Thread.Sleep(10000);
                }
            }
        }