public override IDataProvider GetData(string Code, int Count)
        {
            try
            {
                CommonDataProvider cdp = new CommonDataProvider(this);
                DbParam[]          dps = new DbParam[] {
                    new DbParam("@Q1", DbType.DateTime, StartTime),
                    new DbParam("@Q2", DbType.DateTime, EndTime),
                };
                BaseDb bd = DB.Open(false);
                try
                {
                    DataTable dt = bd.GetDataTable("select Tick,tstamp from " + Code + "_tick where tstamp>=? and tstamp<=? order by tstamp", dps);

                    dps[1].Value = ((DateTime)dps[0].Value).AddSeconds(-1);
                    dps[0].Value = ((DateTime)dps[0].Value).AddDays(-30);
                    DataRow drr = bd.GetFirstRow("select Tick from " + Code + "_tick where tstamp>=? and tstamp<=? order by tstamp desc", dps);

                    if (dt.Rows.Count > 0)
                    {
                        cdp.SetStringData("LastTradeTime", ((DateTime)dt.Rows[dt.Rows.Count - 1]["tstamp"]).ToString());
                    }
                    if (drr != null)
                    {
                        cdp.SetStringData("LastPrice", drr[0].ToString());
                    }

                    if (dt.Rows.Count == 0)
                    {
                        dt.Rows.Add(new object[] { Single.NaN, StartTime });
                    }
                    double[] CLOSE  = new double[dt.Rows.Count];
                    double[] VOLUME = new double[dt.Rows.Count];
                    double[] DATE   = new double[dt.Rows.Count];

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CLOSE[i]  = ToDouble(dt.Rows[i]["Tick"]);
                        VOLUME[i] = 0;
                        DATE[i]   = ((DateTime)dt.Rows[i]["tstamp"]).ToOADate();
                    }
                    cdp.LoadBinary("CLOSE", CLOSE);
                    cdp.LoadBinary("DATE", DATE);
                    cdp.LoadBinary("VOLUME", VOLUME);
                }
                finally
                {
                    bd.Close();
                }
                return(cdp);
            }
            catch
            {
            }
            return(base.GetData(Code, Count));
        }
Exemple #2
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);
            }
        }
        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;
            }
        }
        public override IDataProvider GetData(string Code, int Count)
        {
            try
            {
                CommonDataProvider cdp = new CommonDataProvider(this);
                DbParam[]          dps = new DbParam[] {
                    new DbParam("@Symbol", DbType.String, Code),
                    new DbParam("@Q1", DbType.DateTime, StartTime),
                    new DbParam("@Q2", DbType.DateTime, EndTime),
                };
                BaseDb bd = DB.Open(false);
                try
                {
                    DataTable dt    = bd.GetDataTable("select Price,Volume,QuoteTime from Intraday where Symbol=? and QuoteTime>=? and QuoteTime<=? order by QuoteTime", dps);
                    double    LastV = -1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        double NowV = (double)dr["Volume"];
                        if (LastV >= 0)
                        {
                            if (NowV > LastV)
                            {
                                dr["Volume"] = NowV - LastV;
                            }
                            else
                            {
                                dr["Volume"] = (double)0;
                            }
                        }
                        LastV = NowV;
                    }

                    dps[2].Value = ((DateTime)dps[1].Value).AddSeconds(-1);
                    dps[1].Value = ((DateTime)dps[1].Value).AddDays(-30);
                    DataRow drr = bd.GetFirstRow("select Price from Intraday where Symbol=? and QuoteTime>=? and QuoteTime<=? order by QuoteTime desc", dps);

                    if (dt.Rows.Count > 0)
                    {
                        cdp.SetStringData("LastTradeTime", ((DateTime)dt.Rows[dt.Rows.Count - 1]["QuoteTime"]).ToString());
                    }
                    if (drr != null)
                    {
                        cdp.SetStringData("LastPrice", drr[0].ToString());
                    }
                    SetStrings(cdp, Code);

                    if (dt.Rows.Count == 0)
                    {
                        dt.Rows.Add(new object[] { double.NaN, 0, StartTime });
                    }
                    double[] CLOSE  = new double[dt.Rows.Count];
                    double[] VOLUME = new double[dt.Rows.Count];
                    double[] DATE   = new double[dt.Rows.Count];

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CLOSE[i]  = ToDouble(dt.Rows[i]["Price"]);
                        VOLUME[i] = ToDouble(dt.Rows[i]["Volume"]);
                        DATE[i]   = ((DateTime)dt.Rows[i]["QuoteTime"]).ToOADate();
                    }
                    cdp.LoadBinary("CLOSE", CLOSE);
                    cdp.LoadBinary("DATE", DATE);
                    cdp.LoadBinary("VOLUME", VOLUME);
                }
                finally
                {
                    bd.Close();
                }
                return(cdp);
            }
            catch
            {
            }
            return(base.GetData(Code, Count));
        }