Exemplo n.º 1
0
        private void tvFormula_AfterSelect(object sender, TreeViewEventArgs e)
        {
            FormulaBase fb = (FormulaBase)e.Node.Tag;

            lbLines.Items.Clear();
            if (fb != null)
            {
                lFullName.Text = fb.LongName;
                tbDesc.Text    = fb.Description;
                try
                {
                    FormulaPackage fps = fb.Run(CommonDataProvider.Empty);
                    for (int i = 0; i < fps.Count; i++)
                    {
                        FormulaData fd = fps[i];
                        if (fd.Name == null)
                        {
                            lbLines.Items.Add("[" + i + "]");
                        }
                        else
                        {
                            lbLines.Items.Add(fd.Name);
                        }
                    }
                }
                catch
                {
                }
            }
            AddParamToGroupBox(gbParam, ilFormula, fb, null);
        }
Exemplo n.º 2
0
        private void btnTesting_Click(object sender, System.EventArgs e)
        {
            FormulaBase   fb  = FormulaBase.GetFormulaByName("Trading." + sfBackTesting.SelectedFormula);
            IDataManager  idm = Utils.GetDataManager(Config.DefaultDataManager);
            IDataProvider idp = idm[tbSymbol.Text, Tools.ToIntDef(tbBars.Text, 260)];

            if (idp != null && idp.Count > 0)
            {
                FormulaPackage fp      = fb.Run(idp);
                FormulaData    fdEnter = fp["EnterLong"];
                FormulaData    fdExit  = fp["ExitLong"];
                FormulaData    fdPrice = idp["CLOSE"];
                int            Count   = fdPrice.Length;
                dtResult = new DataTable();
                dtResult.Columns.Add("EntryDate", typeof(DateTime));
                dtResult.Columns.Add("EntryPrice", typeof(double));
                dtResult.Columns.Add("ExitDate", typeof(DateTime));
                dtResult.Columns.Add("ExitPrice", typeof(double));
                dtResult.Columns.Add("ProfitPercent", typeof(double));
                dtResult.Columns.Add("BarsHold");
                bool   Entered    = false;
                int    EntryBars  = 0;
                double EntryPrice = 0;
                for (int i = 0; i < Count; i++)
                {
                    if (Entered)
                    {
                        if (fdExit[i] > 0)
                        {
                            DataRow drLast = dtResult.Rows[dtResult.Rows.Count - 1];
                            drLast["ExitDate"] = DateTime.FromOADate(idp["DATE"][i]);
                            double ExitPrice = fdPrice[i];
                            drLast["ExitPrice"]     = ExitPrice;
                            drLast["BarsHold"]      = i - EntryBars;
                            drLast["ProfitPercent"] = (ExitPrice - EntryPrice) / EntryPrice;
                            Entered = false;
                        }
                    }
                    else
                    {
                        if (fdEnter[i] > 0)
                        {
                            EntryPrice = fdPrice[i];
                            dtResult.Rows.Add(new object[] { DateTime.FromOADate(idp["DATE"][i])
                                                             , EntryPrice });
                            EntryBars = i;
                            Entered   = true;
                        }
                    }
                }
                dgResult.DataSource = dtResult;
            }
            else
            {
                lMsg.Text = tbSymbol.Text + " not found!";
            }
            AllProfit = 1;
            dgResult.DataBind();
        }
Exemplo n.º 3
0
        private void btnScan_Click(object sender, EventArgs e)
        {
            TreeNode node1 = this.tvFormula.SelectedNode;

            if (node1 != null)
            {
                string text1 = (string)node1.Tag;
                int    num1  = 0;
                if (text1 != null)
                {
                    text1 = text1 + this.GetParam();
                    FormulaBase     base1      = FormulaBase.GetFormulaByName(text1);
                    int             num2       = base1.DataCountAtLeast();
                    FileDataManager manager1   = new FileDataManager(null);
                    string[]        textArray1 = ListForm.Current.GetSymbolList();
                    ArrayList       list1      = new ArrayList();
                    this.pbScan.Maximum = textArray1.Length;
                    this.pbScan.Visible = true;
                    if (textArray1.Length > 0)
                    {
                        string[] textArray2 = textArray1;
                        for (int num4 = 0; num4 < textArray2.Length; num4++)
                        {
                            string             text2     = textArray2[num4];
                            CommonDataProvider provider1 = (CommonDataProvider)manager1[text2, num2];
                            FormulaPackage     package1  = base1.Run(provider1);
                            FormulaData        data1     = package1[package1.Count - 1];
                            if ((data1.Length > 0) && (data1.LASTDATA > 0))
                            {
                                list1.Add(text2);
                            }
                            num1++;
                            if (((num1 % 10) == 0) || (num1 == textArray1.Length))
                            {
                                this.pbScan.Value = num1;
                                double num5 = ((double)num1) / ((double)textArray1.Length);
                                this.lMsg.Text = num1.ToString() + "(" + num5.ToString("p2") + ")";
                                Application.DoEvents();
                                if ((num1 % 100) == 0)
                                {
                                    GC.Collect();
                                }
                            }
                        }
                    }
                    StockDB.LoadFolderRow(1, text1);
                    int num3 = StockDB.GetMaxFolderId();
                    if (list1.Count > 0)
                    {
                        this.pbScan.Maximum = list1.Count;
                        num1 = 0;
                        IEnumerator enumerator1 = list1.GetEnumerator();
                        try
                        {
                            while (enumerator1.MoveNext())
                            {
                                StockDB.LoadFolderRelRow(enumerator1.Current, num3);
                                num1++;
                                if ((num1 % 100) == 0)
                                {
                                    this.pbScan.Value = num1;
                                    Application.DoEvents();
                                }
                            }
                        }
                        finally
                        {
                            IDisposable disposable1 = enumerator1 as IDisposable;
                            if (disposable1 != null)
                            {
                                disposable1.Dispose();
                            }
                        }
                    }
                    StockDB.ResetFolderDatabase();
                    ListForm.Current.FolderId = num3;
                    return;
                }
            }
            MessageBox.Show("Please select a condition!");
        }
Exemplo n.º 4
0
        public static void Scanning(object o)
        {
            ScanId          si  = (ScanId)o;
            FormulaBase     fb  = FormulaBase.GetFormulaByName("Scan." + si.Condition);
            DataManagerBase dmb = (DataManagerBase)Utils.GetDataManager(Config.DefaultDataManager);

            if (fb != null)
            {
                int N = fb.DataCountAtLeast();
                int M = N;
                if (Config.PrescanLoadToMemory)
                {
                    N = FormulaBase.MaxForAllScan;
                }

                string Where = "";
                if (si.Exchange != "")
                {
                    Where = " where Exchange = '" + si.Exchange + "'";
                }
                //DataTable dt = DB.GetDataTable("select QuoteCode from StockData"+Where);
                string[] ss = dmb.GetSymbolStrings(si.Exchange, null, null);
                if (ss != null)
                {
                    DB.DoCommand("update condition set Total=?,Scaned=0,StartTime=? where conditionId=?",
                                 new DbParam[] {
                        new DbParam("@Total", DbType.Int32, ss.Length),
                        new DbParam("@StartTime", DbType.DateTime, DateTime.Now),
                        new DbParam("@ConditionId", DbType.Int32, si.ConditionId)
                    });

                    int       i  = 0;
                    ArrayList al = new ArrayList();
                    try
                    {
                        foreach (string s in ss)
                        {
                            try
                            {
                                //ddm.AutoYahooToDB = false;
                                //ddm.DownloadRealTimeQuote = false;
                                IDataProvider idp = Admin.UpdatePreScan.GetDataProvider(dmb, s, N);
                                idp.MaxCount = M;
                                //IDataProvider idp = ddm[s,N];
                                if (Utils.VerifyVolumeAndDate(idp))
                                {
                                    FormulaPackage fp = fb.Run(idp);
                                    FormulaData    fd = fp[fp.Count - 1];
                                    if (fd.Length > 0)
                                    {
                                        if (fd.LASTDATA > 0)
                                        {
                                            al.Add(si.ConditionId + "," + s);
                                        }
                                    }
                                }
                                i++;
                                if ((i % 10) == 0)
                                {
                                    HttpRuntime.Cache[si.ConditionId] = i;
                                    if ((i % 100) == 0)
                                    {
                                        Thread.Sleep(1);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Tools.Log("Scanning:" + ex.Message);
                            }
                        }
                    }
                    finally
                    {
                        Utils.BulkInsert(al);
                    }

                    HttpRuntime.Cache[si.ConditionId] = ss.Length;
                    DB.DoCommand("update condition set Scaned=?,ResultCount=?,EndTime=? where ConditionId=" + si.ConditionId,
                                 new DbParam[] {
                        new DbParam("@Total", DbType.Int32, ss.Length),
                        new DbParam("@ResultCount", DbType.Int32, al.Count),
                        new DbParam("@EndDate", DbType.DateTime, DateTime.Now)
                    });
                }
            }
        }
Exemplo n.º 5
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;
            }
        }