Пример #1
0
        public static void DeleteWrongData(object sender)
        {
            BaseDb bd = DB.Open(true);

            try
            {
                bd.DoCommand("delete from StockData where QuoteCode in (select QuoteCode from ScanedQuote where ConditionId=-1)");
                bd.DoCommand("delete from Realtime where QuoteCode in (select QuoteCode from ScanedQuote where ConditionId=-1)");
                bd.DoCommand("delete from ScanedQuote where ConditionId=-1");
            }
            finally
            {
                bd.Close();
            }
        }
Пример #2
0
        public static void BulkInsert(ArrayList al)
        {
            BaseDb bd = DB.Open(false);

            try
            {
                DbParam[] dps = new DbParam[] {
                    new DbParam("@ConditionId", DbType.Int32, 0),
                    new DbParam("@QuoteCode", DbType.String, ""),
                };
                foreach (object oo in al)
                {
                    string[] ss = oo.ToString().Split(',');
                    dps[0].Value = ss[0];
                    dps[1].Value = ss[1];
                    try
                    {
                        bd.DoCommand("insert into ScanedQuote (ConditionId,QuoteCode) values (?,?)", dps);
                    }
                    catch (Exception e)
                    {
                        Tools.Log("Bulk Insert:" + ss[0] + "," + ss[1] + "," + e);
                    }
                }
            }
            finally
            {
                bd.Close();
            }
        }
Пример #3
0
        private void MergeOneDay(string[] ss)
        {
            Hashtable       ht = new Hashtable();
            IFormatProvider fp =
                new System.Globalization.CultureInfo("en-US", true);

            for (int i = 1; i < ss.Length; i++)
            {
                string[] sss = ss[i].Split(',');
                ht.Add(sss[0], new DataPackage(
                           (float)DateTime.Parse(sss[1], fp).ToOADate(),
                           float.Parse(sss[2]),
                           float.Parse(sss[3]),
                           float.Parse(sss[4]),
                           float.Parse(sss[5]),
                           float.Parse(sss[6]),
                           float.Parse(sss[5])));
            }

            DbParam[] dps = new DbParam[] {
                new DbParam("@HistoryData", DbType.Binary, null),
                new DbParam("@QuoteCode", DbType.String, null),
            };

            BaseDb UpdateDb = BaseDb.FromConfig("ConnStr");
            SQL    bd       = (SQL)DB.Open(false);

            try
            {
                SqlDataReader sdr = bd.GetDataReader("select * from StockData");

                while (sdr.Read())
                {
                    string      QuoteCode = sdr["QuoteCode"].ToString();
                    DataPackage dp        = (DataPackage)ht[QuoteCode];
                    if (dp != null)
                    {
                        object o = sdr["HistoryData"];
                        if (o == DBNull.Value)
                        {
                            o = new byte[0];
                        }
                        byte[] bs = MergeOneQuote((byte[])o, dp);
                        dps[0].Value = bs;
                        dps[1].Value = QuoteCode;
                        UpdateDb.DoCommand("update StockData set HistoryData=? where QuoteCode=?", dps);
                    }
                }
                sdr.Close();
            }
            finally
            {
                bd.Close();
            }
        }
Пример #4
0
        private void btnGet_Click(object sender, System.EventArgs e)
        {
            string[] ss = DownloadData("http://eoddata.com/SymbolList.asp?e=" + Exchange);

            DbParam[] dps = new DbParam[] {
                new DbParam("@QuoteCode", DbType.String, ""),
                new DbParam("@QuoteName", DbType.String, ""),
                new DbParam("@Exchange", DbType.String, Exchange),
            };
            int    succ   = 0;
            int    failed = 0;
            BaseDb bd     = DB.Open(false);

            try
            {
                for (int i = 1; i < ss.Length; i++)
                {
                    string[] rr = ss[i].Split('\t');
                    if (rr.Length != 2)
                    {
                        continue;
                    }
                    if (Exchange == "INDEX")
                    {
                        dps[0].Value = "^" + rr[0].Trim();
                    }
                    else
                    {
                        dps[0].Value = rr[0].Trim();
                    }
                    dps[1].Value = rr[1].Trim();
                    try
                    {
                        if (bd.GetFirstRow("select QuoteCode from StockData where QuoteCode=?", new DbParam[] { dps[0] }) == null)
                        {
                            bd.DoCommand("insert into stockdata (QuoteCode,QuoteName,Exchange) values (?,?,?)", dps);
                            succ++;
                        }
                    }
                    catch
                    {
                        failed++;
                    }
                }
            }
            finally
            {
                bd.Close();
            }
            lExchangeMsg.Text = "succ: " + succ + "; failed " + failed;
        }
Пример #5
0
        public override void SaveSymbolList(string[] ss, out int succ, out int failed)
        {
            DbParam[] dps = new DbParam[] {
                new DbParam("@QuoteCode", DbType.String, ""),
                new DbParam("@QuoteName", DbType.String, ""),
                new DbParam("@Exchange", DbType.String, ""),
            };

            succ   = 0;
            failed = 0;
            // false to support mysql
            BaseDb bd = DB.Open(false);

            try
            {
                for (int i = 0; i < ss.Length; i++)
                {
                    string[] rr = ss[i].Trim().Split(';');
                    if (rr.Length != 3)
                    {
                        continue;
                    }
                    dps[0].Value = rr[0].Trim();
                    dps[1].Value = rr[1].Trim();
                    dps[2].Value = rr[2].Trim();
                    try
                    {
                        if (bd.GetFirstRow("select QuoteCode from StockData where QuoteCode=?", new DbParam[] { dps[0] }) == null)
                        {
                            bd.DoCommand("insert into StockData (QuoteCode,QuoteName,Exchange) values (?,?,?)", dps);
                            succ++;
                        }
                    }
                    catch
                    {
                        failed++;
                    }
                }
            }
            finally
            {
                bd.Close();
            };
        }
Пример #6
0
        private void btnScan_Click(object sender, System.EventArgs e)
        {
            CurrentFullName = ddlFormula.SelectedItem.Value;
            Exchange        = ddlExchange.SelectedItem.Value;

            string Param = "";

            foreach (string s in Request.Form)
            {
                if (s.StartsWith("__Param"))
                {
                    if (Param != "")
                    {
                        Param += ",";
                    }
                    Param += double.Parse(Request.Form[s]);
                }
            }
            Condition = CurrentFullName;
            if (Param != "")
            {
                Condition = CurrentFullName + "(" + Param + ")";
            }

            DbParam[] dps = new DbParam[] {
                new DbParam("@Condition", DbType.String, Condition),
                new DbParam("@Exchange", DbType.String, Exchange),
                new DbParam("@StartTime", DbType.DateTime, DateTime.Now.AddHours(-
                                                                                 Tools.ToIntDef(ConfigurationManager.AppSettings["ScanCacheTime"], 0)))
            };
            bool   b  = true;
            BaseDb bd = DB.Open(true);

            try
            {
                DataRow dr = bd.GetFirstRow("select * from condition where Condition=? and Exchange=? and StartTime>?", dps);
                if (dr != null && !Config.KeepLatestScanResultOnly)
                {
                    ConditionId = dr["ConditionId"].ToString();
                    b           = false;
                }
                else
                {
                    dps[2] = null;
                    if (Config.KeepLatestScanResultOnly)
                    {
                        DataTable dt = bd.GetDataTable("select ConditionId from condition where Condition=? and Exchange=?", dps);
                        foreach (DataRow drDel in dt.Rows)
                        {
                            bd.DoCommand("delete from ScanedQuote where ConditionId = " + drDel["ConditionId"]);
                        }
                        bd.DoCommand("delete from condition where Condition=? and Exchange=?", dps);
                    }
                    bd.DoCommand("insert into condition (Condition,Exchange,Scaned) values (?,?,0)", dps);
                    ConditionId = bd.GetFirstRow("select max(ConditionId) from condition")[0].ToString();
                }
            }
            finally
            {
                bd.Close();
            }

            if (b)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Scanning), new ScanId(ConditionId, Condition, Exchange));
                Response.Redirect("Progress.aspx?ConditionId=" + ConditionId);
            }
            else
            {
                Response.Redirect("StockList.aspx?ConditionId=" + ConditionId);
            }
        }
Пример #7
0
        static public void PreScan(object Sender)
        {
            Tools.Log("Prescan starting");
            if (StartTime > DateTime.MinValue)
            {
                return;
            }
            StartTime = DateTime.Now;
            try
            {
                string[] PreExchange = Config.PreScanExchange.Split(';');
                for (int i = 0; i < PreExchange.Length; i++)
                {
                    PreExchange[i] = Utils.GetPart1(PreExchange[i]);
                }

                string[]  PreScan          = Config.PreScan.Split(';');
                Hashtable htConditionIdMap = new Hashtable();
                DbParam[] dpPreScan        = new DbParam[] {
                    new DbParam("@Condition", DbType.String, ""),
                    new DbParam("@Exchange", DbType.String, ""),
                    new DbParam("@StartTime", DbType.DateTime, DateTime.Now),
                    new DbParam("@ScanType", DbType.Int32, 1),
                };

                // Insert pre-defined scan to condition
                // Get condition id .
                BaseDb bd = DB.Open(false);
                try
                {
                    string s = bd.GetCommaValues("select ConditionId from Condition " + GetWhere("EndTime"), "");
                    if (s != "")
                    {
                        bd.DoCommand("delete from ScanedQuote where ConditionId in (" + s + ")");
                        bd.DoCommand("delete from Condition " + GetWhere("EndTime"));
                    }

                    Tools.Log("PreScan=" + PreScan.Length + ";PreExchange=" + PreExchange.Length);

                    for (int i = 0; i < PreScan.Length; i++)
                    {
                        for (int j = 0; j < PreExchange.Length; j++)
                        {
                            dpPreScan[0].Value = Utils.GetName(PreScan[i]);
                            dpPreScan[1].Value = PreExchange[j];
                            dpPreScan[3].Value = Utils.GetParam(PreScan[i], "1");
                            bd.DoCommand("insert into Condition (Condition,Exchange,StartTime,Scaned,ScanType) values (?,?,?,0,?)", dpPreScan);
                        }
                    }

                    DataTable dtPreScan =
                        bd.GetDataTable("select ConditionId,Condition,Exchange from Condition " + GetWhere("StartTime"), null, PreScan.Length * PreExchange.Length);
                    foreach (DataRow dr in dtPreScan.Rows)
                    {
                        htConditionIdMap[dr["Condition"].ToString().Trim() + dr["Exchange"].ToString().Trim()] = dr["ConditionId"].ToString();
                    }
                }
                finally
                {
                    bd.Close();
                }

                Tools.Log("Get scan formulas");

                // Get scan formulas
                FormulaBase[] fbs = new FormulaBase[PreScan.Length];
                int[]         Ns  = new int[PreScan.Length];
                int           N   = 0;
                for (int i = 0; i < fbs.Length; i++)
                {
                    fbs[i] = FormulaBase.GetFormulaByName("Scan." + Utils.GetValue(PreScan[i]));
                    Tools.Log(fbs[i].FullName);
                    if (fbs[i] != null)
                    {
                        Ns[i] = fbs[i].DataCountAtLeast();
                        N     = Math.Max(N, Ns[i]);
                    }
                }
                if (Config.PrescanLoadToMemory)
                {
                    N = Config.MaxDataForPull;
                }

                Tools.Log("Pre-Scan- N = " + N);

                DataManagerBase dmb = Utils.GetDataManager(Config.DefaultDataManager);
                DataTable       dt  = dmb.GetStockList();
                if (dt == null)
                {
                    return;
                }
                Tools.Log(dt.Rows.Count.ToString());

                // Scan
                int       Progress      = 0;
                Hashtable htTotal       = new Hashtable();
                Hashtable htResultCount = new Hashtable();
                ArrayList al            = new ArrayList();
                try
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        try
                        {
                            string Symbol = dr[0].ToString();

                            IDataProvider idp = GetDataProvider(dmb, Symbol, N);

                            if (!Utils.VerifyVolumeAndDate(idp))
                            {
                                continue;
                            }
                            string NowExchange = dr["Exchange"].ToString();
                            foreach (string s in PreExchange)
                            {
                                if (s.Length <= NowExchange.Length)
                                {
                                    if (string.Compare(s, NowExchange.Substring(0, s.Length), true) == 0)
                                    {
                                        NowExchange = s;
                                        break;
                                    }
                                }
                            }

                            for (int j = 0; j < fbs.Length; j++)
                            {
                                try
                                {
                                    if (fbs[j] != null)
                                    {
                                        idp.MaxCount = Ns[j];
                                        FormulaPackage fp          = fbs[j].Run(idp);
                                        string         ConditionId = (string)htConditionIdMap[Utils.GetName(PreScan[j]) + NowExchange];
                                        if (ConditionId != null)
                                        {
                                            FormulaData fd = fp[fp.Count - 1];
                                            if (fd.Length > 0)
                                            {
                                                if (fd.LASTDATA > 0)
                                                {
                                                    al.Add(ConditionId + "," + Symbol);
                                                    htResultCount[ConditionId] = Utils.ObjPlusDef(htResultCount[ConditionId], 1);
                                                }
                                            }
                                            htTotal[ConditionId] = Utils.ObjPlusDef(htTotal[ConditionId], 1);
                                        }
                                        Progress++;
                                        if ((Progress % 10) == 0)
                                        {
                                            HttpRuntime.Cache["PreScan"] = Progress;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Tools.Log(Symbol + "/" + fbs[j] + "/" + e);
                                }
                            }
                            Thread.Sleep(1);
                        }
                        catch (Exception e)
                        {
                            Tools.Log("Pre-scan symbol loop:" + e.Message);
                        }
                    }
                }
                finally
                {
                    Utils.BulkInsert(al);
                }

                // Update pre-scan conditions
                dpPreScan = new DbParam[] {
                    new DbParam("@Scaned", DbType.Int32, 0),
                    new DbParam("@Total", DbType.Int32, 0),
                    new DbParam("@ResultCount", DbType.Int32, 0),
                    new DbParam("@EndTime", DbType.DateTime, DateTime.Now),
                };
                bd = DB.Open(false);
                try
                {
                    for (int i = 0; i < PreScan.Length; i++)
                    {
                        for (int j = 0; j < PreExchange.Length; j++)
                        {
                            string ConditionId = (string)htConditionIdMap[Utils.GetName(PreScan[i]) + PreExchange[j]];
                            dpPreScan[0].Value = Utils.ObjDef(htTotal[ConditionId], 0);
                            dpPreScan[1].Value = Utils.ObjDef(htTotal[ConditionId], 0);
                            dpPreScan[2].Value = Utils.ObjDef(htResultCount[ConditionId], 0);

                            bd.DoCommand("update Condition set Scaned=?,Total=?,ResultCount=?,EndTime=? where ConditionId=" +
                                         ConditionId, dpPreScan);
                        }
                    }
                }
                finally
                {
                    bd.Close();
                }
            }
            catch (Exception e)
            {
                Tools.Log("Update pre-scan service:" + e.Message);
            }
            finally
            {
                StartTime = DateTime.MinValue;
            }
        }