Exemplo n.º 1
0
        public void Add(string Name, string Quote)
        {
            FormulaBase formulaByName = FormulaBase.GetFormulaByName(Name);

            formulaByName.Quote = Quote;
            this.Add(formulaByName);
        }
Exemplo n.º 2
0
        public static string TextBoxToParam(string FormulaName, TextBox[] tbs)
        {
            string str   = "";
            int    index = FormulaName.IndexOf('(');

            if (index >= 0)
            {
                FormulaName = FormulaName.Substring(0, index);
            }
            for (int i = 0; i < tbs.Length; i++)
            {
                if (!(tbs[i].Text != ""))
                {
                    break;
                }
                if (str != "")
                {
                    str = str + ",";
                }
                str = str + tbs[i].Text;
            }
            if (str != "")
            {
                str = "(" + str + ")";
            }
            if (FormulaBase.GetFormulaByName(FormulaName) != null)
            {
                return(FormulaName + str);
            }
            return("");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a formula string with parameters
        /// </summary>
        /// <param name="FormulaName">Formula name</param>
        /// <returns>Formula string with parameters</returns>
        private string TextBoxToParam(string FormulaName)
        {
            //string s = "";
            int j = FormulaName.IndexOf('(');

            if (j >= 0)
            {
                FormulaName = FormulaName.Substring(0, j);
            }

//			foreach(Control c in gbParam.Controls)
//				if (c is TextBox)
//				{
//					if (s!="") s +=",";
//					s +=((TextBox)c).Text;
//				}
//			if (s!="") s = "("+s+")";

            string s = SelectFormula.GetParam(gbParam);

            FormulaBase fb = FormulaBase.GetFormulaByName(FormulaName);

            if (fb != null)
            {
                return(FormulaName + s);
            }
            else
            {
                return("");
            }
        }
Exemplo n.º 4
0
        private string GetOneParam(int Index, string Name)
        {
            FormulaBase fb = null;

            if (Name != "")
            {
                fb = FormulaBase.GetFormulaByName(Name);
            }
            string s = "";

            for (int i = 0; i < 3; i++)
            {
                string r = "";
                if (fb != null)
                {
                    if (i < fb.Params.Count)
                    {
                        r += fb.Params[i].DefaultValue.ToString();
                    }
                }

                if (s != "")
                {
                    s += ";";
                }
                s += "p" + i + ".value=\"" + r + "\"";
            }
            ;
            return("if(i==" + Index + "){" + s + ";}");
        }
Exemplo n.º 5
0
        protected void btnOK_Click(object sender, System.EventArgs e)
        {
            BindData();

            FormulaBase fb = FormulaBase.GetFormulaByName(CurrentFullName);

            foreach (string s in Request.Form)
            {
                if (s.StartsWith("__Param"))
                {
                    fb.SetParam(s.Substring(7), Request.Form[s]);
                }
            }

            YahooDataManager ydm = new YahooDataManager();

            ydm.CacheRoot = HttpRuntime.AppDomainAppPath + "Cache\\";
            CommonDataProvider DataProvider = (CommonDataProvider)ydm[tbCode.Text];

            FormulaChart fc = new FormulaChart();

            fc.AddArea("MAIN", 2);
            if (CurrentProgram.IsMainView)
            {
                fc[0].AddFormula(fb);
            }
            else
            {
                fc.AddArea(fb);
            }
            fc.LatestValueType = LatestValueType.StockOnly;

            fc.SetSkin(Config.DefaultSkin);

            try
            {
                fc.DataProvider = DataProvider;
                FormulaData fdDate = DataProvider["DATE"];
                string      s      = tbCode.Text;
                if (fdDate.Length > 0)
                {
                    s += " (" + DateTime.FromOADate(fdDate[fdDate.Length - 1]).ToString("yyyy-MMM-dd", DateTimeFormatInfo.InvariantInfo) + ")";
                }

                fc[0].Formulas[0].Name          = s;
                fc[0].Formulas[0].TextInvisible = false;
                fc.StartTime = DateTime.Now.AddMonths(-8);
                fc.EndTime   = DateTime.Now;
            }
            catch (Exception ee)
            {
                Bitmap   b = new Bitmap(640, 480);
                Graphics g = Graphics.FromImage(b);
                g.FillRectangle(Brushes.WhiteSmoke, 0, 0, 640, 480);
                g.DrawRectangle(Pens.Black, 0, 0, 639, 479);
                g.DrawString(ee.Message, new Font("verdana", 10), Brushes.Red, 1, 1);
            }

            lChart.Text = "<img src=ImageFromCache.aspx?CacheId=" + fc.SaveToImageStream(640, 400, ImageFormat.Png, 0, 0) + ">";
        }
Exemplo n.º 6
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.º 7
0
        /// <summary>
        /// Extract the formula name and and parameters from a string
        /// </summary>
        /// <param name="NameAndParam">Name and parameter string , such as MA(50)</param>
        /// <param name="tbs">TextBox array, the method will extract parameters to this array</param>
        /// <param name="FormulaName">return the formula name</param>
        public void ParamToTextBox(string NameAndParam, out string FormulaName)
        {
            cbSecondYAxis.Checked = NameAndParam.EndsWith("!");
            FormulaBase fb = FormulaBase.GetFormulaByName(NameAndParam.TrimEnd('!'));

            FormulaName = fb.FormulaName;
            SelectFormula.AddParamToGroupBox(gbParam, ilFormula, fb, new EventHandler(tbP4_Leave));
        }
Exemplo n.º 8
0
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            //Create a Formula namespace
            FormulaSpace fms = new FormulaSpace("FML");

            //Create a Formula program , set Formula name and script code on the fly
            FormulaProgram fp = new FormulaProgram();

            fp.Name = tbFormulaName.Text;
            fp.Code = tbProgramScript.Text;

            //Add the script program to the Formula namespace
            fms.Programs.Add(fp);

            //Add parameters to Formula program
            for (int i = 1; i < 5; i++)
            {
                if (Request.Form["tbParamName" + i] != "")
                {
                    fp.Params.Add(new FormulaParam(
                                      Request.Form["tbParamName" + i],
                                      Request.Form["tbDefValue" + i],
                                      Request.Form["tbMinValue" + i],
                                      Request.Form["tbMaxValue" + i], FormulaParamType.Double));
                }
            }

            try
            {
                //Compile the Formula script on the fly
                Assembly    a  = fms.CompileInMemory();
                FormulaBase fb = FormulaBase.GetFormulaByName(a, fms.Name + "." + fp.Name);

                //Create YahooDataManager , Get stock data from yahoo.
                YahooDataManager ydm = new YahooDataManager();
                ydm.CacheRoot = HttpRuntime.AppDomainAppPath + "Cache\\";
                CommonDataProvider DataProvider = (CommonDataProvider)ydm[tbCode.Text];

                //Create financial chart instance
                FormulaChart fc = new FormulaChart();
                fc.PriceLabelFormat = "{CODE}";
                fc.LatestValueType  = LatestValueType.Custom;
                fc.AddArea("MAIN", 3);
                fc.AddArea(fb);
                fc.DataProvider = DataProvider;
                fc.SetSkin(Config.DefaultSkin);

                //Show the temp image just created
                lChart.Text = "<img src=ImageFromCache.aspx?CacheId=" + fc.SaveToImageStream(440, 440, ImageFormat.Png, 0, 0) + ">";
            }
            catch (FormulaErrorException fee)
            {
                //Show the compile result if the script has some errors
                lChart.Text = fee.ToHtml();
            }
        }
Exemplo n.º 9
0
        private void btnSource_Click(object sender, System.EventArgs e)
        {
            int i = lbFormulaList.SelectedIndex;

            if (i >= 0)
            {
                FormulaBase fb = FormulaBase.GetFormulaByName((string)lbFormulaList.Items[i]);
                ChartWinControl.EditFormula(fb);
            }
        }
Exemplo n.º 10
0
        private void tvFormula_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string str = (String)e.Node.Tag;

            if (str != null)
            {
                gbParam.Controls.Clear();
                int            i1             = 10;
                int            j1             = 20;
                int            k1             = 30;
                int            i2             = 0;
                FormulaBase    formulaBase    = FormulaBase.GetFormulaByName(str);
                FormulaProgram formulaProgram = PluginManager.GetFormulaProgram(formulaBase);
                if (formulaProgram != null)
                {
                    lFullName.Text = formulaProgram.FullName;
                    tbDesc.Text    = formulaProgram.Description;
                }
                else
                {
                    lFullName.Text = formulaBase.GetType().ToString();
                    tbDesc.Text    = "";
                }
                for (int j2 = 0; j2 < formulaBase.Params.Count; j2++)
                {
                    Label label1 = new Label();
                    label1.AutoSize = true;
                    label1.Text     = String.Concat(formulaBase.Params[j2].Name, "=");
                    label1.Left     = i1;
                    label1.Top      = j1 + 3;
                    label1.Parent   = gbParam;
                    i2  = Math.Max(i2, label1.Width);
                    j1 += k1;
                }
                j1  = 20;
                i1 += i2 + 6;
                for (int k2 = 0; k2 < formulaBase.Params.Count; k2++)
                {
                    TextBox textBox = new TextBox();
                    textBox.Left   = i1;
                    textBox.Top    = j1;
                    textBox.Text   = formulaBase.Params[k2].DefaultValue;
                    textBox.Parent = gbParam;
                    Label label2 = new Label();
                    label2.AutoSize = true;
                    label2.Text     = String.Concat(new string[] { "(", formulaBase.Params[k2].MinValue, "--", formulaBase.Params[k2].MaxValue, ")" });
                    label2.Left     = i1 + textBox.Width + 6;
                    label2.Top      = j1 + 3;
                    label2.Parent   = gbParam;
                    j1 += k1;
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Extract the formula name and and parameters from a string
        /// </summary>
        /// <param name="NameAndParam">Name and parameter string , such as MA(50)</param>
        /// <param name="tbs">TextBox array, the method will extract parameters to this array</param>
        /// <param name="FormulaName">return the formula name</param>
        public static void ParamToTextBox(string NameAndParam, TextBox[] tbs, out string FormulaName)
        {
            FormulaBase fb = FormulaBase.GetFormulaByName(NameAndParam);

            FormulaName = fb.FormulaName;
            for (int i = 0; i < tbs.Length; i++)
            {
                if (i < fb.Params.Count)
                {
                    tbs[i].Text = fb.Params[i].Value;
                }
                else
                {
                    tbs[i].Text = "";
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Create instance of OverlayManager
        /// </summary>
        public OverlayManager()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            ddlAddOverlay.Items.Clear();
            ddlAddOverlay.Items.Add("Select indicators to add");
            foreach (string s in new string[] { "HL", "MA", "EMA", "BB",
                                                "AreaBB", "SAR", "ZIGLABEL", "ZIG", "ZIGW", "ZIGSR", "SR", "EXTEND.SRAxisY", "COMPARE", "COMPARE2",
                                                "Fibonnaci", "LinRegr" })
            {
                ddlAddOverlay.Items.Add(FormulaBase.GetFormulaByName(s));
            }

            ddlAddOverlay.SelectedIndex = 0;
        }
Exemplo n.º 13
0
 private void AddFavorite(string[] Formulas)
 {
     ddlFavoriteFormula.Items.Clear();
     ddlFavoriteFormula.Items.Add("Select to add new formula");
     foreach (string s in Formulas)
     {
         FormulaBase fb = FormulaBase.GetFormulaByName(s);
         foreach (object o in ddlFavoriteFormula.Items)
         {
             if (o.GetType() == fb.GetType())
             {
                 goto Next;
             }
         }
         ddlFavoriteFormula.Items.Add(fb);
         Next :;
     }
     ddlFavoriteFormula.SelectedIndex = 0;
 }
Exemplo n.º 14
0
        /// <summary>
        /// Create a formula string with parameters
        /// </summary>
        /// <param name="FormulaName">Formula name</param>
        /// <param name="tbs">Parameter TextBox array </param>
        /// <returns>Formula string with parameters</returns>
        public static string TextBoxToParam(string FormulaName, TextBox[] tbs)
        {
            string s = "";
            int    j = FormulaName.IndexOf('(');

            if (j >= 0)
            {
                FormulaName = FormulaName.Substring(0, j);
            }

            for (int i = 0; i < tbs.Length; i++)
            {
                if (tbs[i].Text != "")
                {
                    if (s != "")
                    {
                        s += ",";
                    }
                    s += tbs[i].Text;
                }
                else
                {
                    break;
                }
            }
            if (s != "")
            {
                s = "(" + s + ")";
            }
            FormulaBase fb = FormulaBase.GetFormulaByName(FormulaName);

            if (fb != null)
            {
                return(FormulaName + s);
            }
            else
            {
                return("");
            }
        }
Exemplo n.º 15
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.º 16
0
        protected void btnOK_Click(object sender, System.EventArgs e)
        {
            //Create a Formula namespace
            FormulaSpace fms = new FormulaSpace("FML");

            //Create a Formula program , set Formula name and script code on the fly
            FormulaProgram fp = new FormulaProgram();

            fp.Name = tbFormulaName.Text;
            fp.Code = tbProgramScript.Text;

            //Add the script program to the Formula namespace
            fms.Programs.Add(fp);

            //Add parameters to Formula program
            for (int i = 1; i < 5; i++)
            {
                if (Request.Form["tbParamName" + i] != "")
                {
                    fp.Params.Add(new FormulaParam(
                                      Request.Form["tbParamName" + i],
                                      float.Parse(Request.Form["tbDefValue" + i]),
                                      float.Parse(Request.Form["tbMinValue" + i]),
                                      float.Parse(Request.Form["tbMaxValue" + i])));
                }
            }

            try
            {
                //Compile the Formula script on the fly
                Assembly    a  = fms.CompileInMemory(HttpRuntime.BinDirectory);
                FormulaBase fb = FormulaBase.GetFormulaByName(a, fms.Name + "." + fp.Name);

                //Create YahooDataManager , Get stock data from yahoo.
                YahooDataManager ydm = new YahooDataManager();
                ydm.CacheRoot = HttpRuntime.AppDomainAppPath + "Cache\\";
                CommonDataProvider DataProvider = (CommonDataProvider)ydm[tbCode.Text];

                //Create financial chart instance
                FormulaChart fc = new FormulaChart();
                fc.AddArea("MAIN", 3);
                fc.AddArea(fb);
                fc.DataProvider = DataProvider;
                fc.SetSkin(Config.DefaultSkin);


                //Show the temp image just created
                lChart.Text = "<img src=ImageFromCache.aspx?CacheId=" + fc.SaveToImageStream(440, 440, ImageFormat.Png, 0, 0) + ">";
            }
            catch (FormulaErrorException fee)
            {
                //Show the compile result if the script has some errors
                lChart.Text = "<font color=red>";
                foreach (System.CodeDom.Compiler.CompilerError ce in fee.ces)
                {
                    FormulaProgram fpa = fms.GetProgramByLineNum(ce.Line);
                    if (fpa != null)
                    {
                        fpa.AdjustErrors(ce);
                        lChart.Text += string.Format("Name:{0} line:{1} column:{2} error:{3} {4}<br>", fpa.Name, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
                    }
                }
                lChart.Text += "</font>";
            }
        }
Exemplo n.º 17
0
        public static void UpdateFormula(object sender)
        {
            if (StartTime > DateTime.MinValue)
            {
                return;
            }
            StartTime = DateTime.Now;
            try
            {
                string[]      ss        = Config.AutoPullFormulaData.Split(';');
                string[]      ssName    = new string[ss.Length];
                string[]      ssFormula = new string[ss.Length];
                FormulaBase[] fbs       = new FormulaBase[ss.Length];
                int[]         Ns        = new int[ss.Length];
                int           N         = 0;
                for (int i = 0; i < ss.Length; i++)
                {
                    ssName[i]    = Utils.GetName(ss[i]);
                    ssFormula[i] = Utils.GetValue(ss[i]);
                    fbs[i]       = FormulaBase.GetFormulaByName(ssFormula[i]);
                    Ns[i]        = fbs[i].DataCountAtLeast();
                    N            = Math.Max(N, Ns[i]);
                }

                DataManagerBase dmb = Utils.GetDataManager(Config.DefaultDataManager);
                DataTable       dt  = dmb.GetSymbolList(null, null, null);
                if (dt == null)
                {
                    return;
                }

                DbParam[] dps = new DbParam[] {
                    new DbParam("@QuoteCode", DbType.String, ""),
                    new DbParam("@FormulaName", DbType.String, ""),
                    new DbParam("@FormulaValue", DbType.Double, 0),
                    new DbParam("@CalculateTime", DbType.DateTime, DateTime.Now)
                };
                DB.DoCommand("delete from FormulaValue");
                //IDataManager idm = new DBDataManager();
                foreach (DataRow dr in dt.Rows)
                {
                    IDataProvider idp = dmb[dr[0].ToString(), N];
                    dps[0].Value = dr[0].ToString();
                    for (int i = 0; i < fbs.Length; i++)
                    {
                        idp.MaxCount = Ns[i];
                        dps[1].Value = ssName[i];
                        if (fbs[i] != null)
                        {
                            FormulaData fd = fbs[i].GetFormulaData(idp, ssFormula[i]);
                            if (fd.Length > 0)
                            {
                                double d = fd.LASTDATA;
                                if (!double.IsNaN(d))
                                {
                                    dps[2].Value = d;
                                    DB.DoCommand("insert into FormulaValue (QuoteCode,FormulaName,FormulaValue,CalculateTime) values (?,?,?,?)", dps);
                                }
                            }
                        }
                    }
                    Thread.Sleep(1);
                }
            }
            catch (Exception e)
            {
                Tools.Log("Update Formula Values: " + e.Message);
            }
            finally
            {
                StartTime = DateTime.MinValue;
            }
        }
Exemplo n.º 18
0
        public void Insert(int Index, string Name)
        {
            FormulaBase formulaByName = FormulaBase.GetFormulaByName(Name);

            this.Insert(Index, formulaByName);
        }
        public static void UpdateFormula(object sender)
        {
            if (StartTime > DateTime.MinValue)
            {
                return;
            }
            StartTime = DateTime.Now;
            try
            {
                string[]      ss        = Config.AutoPullFormulaData.Split(';');
                string[]      ssName    = new string[ss.Length];
                string[]      ssFormula = new string[ss.Length];
                FormulaBase[] fbs       = new FormulaBase[ss.Length];
                int[]         Ns        = new int[ss.Length];
                int           N         = 0;
                for (int i = 0; i < ss.Length; i++)
                {
                    ssName[i]    = Utils.GetName(ss[i]);
                    ssFormula[i] = Utils.GetValue(ss[i]);
                    fbs[i]       = FormulaBase.GetFormulaByName(ssFormula[i]);
                    Ns[i]        = fbs[i].DataCountAtLeast();
                    Tools.Log(ssName[i] + "=" + Ns[i]);
                    N = Math.Max(N, Ns[i]);
                }

                DataManagerBase dmb = Utils.GetDataManager(Config.DefaultDataManager);
                DataTable       dt  = dmb.GetSymbolList(null, null, null);
                if (dt == null)
                {
                    return;
                }

                DbParam[] dps = new DbParam[] {
                    new DbParam("@QuoteCode", DbType.String, ""),
                    new DbParam("@FormulaName", DbType.String, ""),
                    new DbParam("@FormulaValue", DbType.Double, 0),
                    new DbParam("@CalculateTime", DbType.DateTime, DateTime.Now)
                };

                DB.DoCommand("delete from FormulaValue");
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    try
                    {
                        DataRow       dr  = dt.Rows[j];
                        IDataProvider idp = dmb[dr[0].ToString(), N];

                        dps[0].Value = dr[0].ToString();
                        for (int i = 0; i < fbs.Length; i++)
                        {
                            idp.MaxCount = Ns[i] + 10;
                            dps[1].Value = ssName[i];
                            if (fbs[i] != null)
                            {
                                FormulaData fd = fbs[i].GetFormulaData(idp, ssFormula[i]);
                                if (fd.Length > 0)
                                {
                                    double d = fd.LASTDATA;
                                    if (!double.IsNaN(d))
                                    {
                                        dps[2].Value = d;
                                        DB.DoCommand("insert into FormulaValue (QuoteCode,FormulaName,FormulaValue,CalculateTime) values (?,?,?,?)", dps);
                                    }
                                    else if (ssFormula[i] != "NAME")
                                    {
                                        Tools.Log("Insert value error:" + fbs[i].Name + ";Data Length=" + fd.Length + ";" + dr[0]);
                                    }
                                }
                            }
                        }
                        Msg = ";" + j + "/" + dt.Rows.Count + ";" + dr[0];
                    }
                    catch (Exception e)
                    {
                        Tools.Log("Update Formula Values: " + dt.Rows[j][0] + "," + e);
                    }
                }
            }
            catch (Exception e)
            {
                Tools.Log("Update Formula Values: " + e);
            }
            finally
            {
                StartTime = DateTime.MinValue;
                tUpdate   = null;
                Msg       = "";
            }
        }
Exemplo n.º 20
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.º 21
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;
            }
        }