示例#1
0
文件: DBTrade.cs 项目: LMY/sharpTrade
        public void Purge(DateTime t0, DateTime t1, int nTI)
        {
            bool[] delflag = new bool[titles.Length];
            for (int i = 0; i < delflag.Length; i++)
            {
                delflag[i] = false;
            }


            for (int i = 0; i < titles.Length; i++)
            {
                if (titles[i].Tstart > t0 || titles[i].Tend < t1)
                {
                    delflag[i] = true;
                }

                if (!titles[i].Cut(t0, t1, nTI))
                {
                    delflag[i] = true;
                }

                double total = titles[i].Close[titles[i].Close.Length - 1] / titles[i].Close[0];
                if (total > 5)
                {
                    delflag[i] = true;
                }

                if (total < 0.2)
                {
                    delflag[i] = true;
                }

                total = titles[i].High[titles[i].High.Length - 1] / titles[i].High[0];
                if (total > 5)
                {
                    delflag[i] = true;
                }

                if (total < 0.2)
                {
                    delflag[i] = true;
                }

                total = titles[i].Low[titles[i].Low.Length - 1] / titles[i].Low[0];
                if (total > 5)
                {
                    delflag[i] = true;
                }

                if (total < 0.2)
                {
                    delflag[i] = true;
                }


                double[] dperc = TI.calculate_dperc(titles[i].Close, 1);
                for (int k = 1; k < dperc.Length; k++)
                {
                    if (Math.Abs(dperc[k]) > 0.5)
                    {
                        delflag[i] = true;
                        break;
                    }
                }

                dperc = TI.calculate_dperc(titles[i].High, 1);
                for (int k = 1; k < dperc.Length; k++)
                {
                    if (Math.Abs(dperc[k]) > 0.5)
                    {
                        delflag[i] = true;
                        break;
                    }
                }

                dperc = TI.calculate_dperc(titles[i].Low, 1);
                for (int k = 1; k < dperc.Length; k++)
                {
                    if (Math.Abs(dperc[k]) > 0.5)
                    {
                        delflag[i] = true;
                        break;
                    }
                }


                double gamma = 6;

                for (int k = 1; k < 10; k++)
                {
                    dperc = TI.calculate_dperc(titles[i].Close, k);
                    for (int z = 1; z < dperc.Length; z++)
                    {
                        if (Math.Abs(dperc[z]) > gamma * TI.SimulationSigmaFTSEraw(k))
                        {
                            delflag[i] = true;
                            break;
                        }
                    }
                }
            }

            // remove elements and stuff
            int newlength = 0;

            for (int i = 0; i < titles.Length; i++)
            {
                if (!delflag[i])
                {
                    newlength++;
                }
            }

            Title[] newtitles = new Title[newlength];
//            MessageBox.Show("Total Purged(): " + newtitles.Length + " over: " + titles.Length);
            int index = 0;

            for (int i = 0; i < titles.Length; i++)
            {
                if (!delflag[i])
                {
                    titles[i].SetDay0(t0);
                    newtitles[index++] = titles[i];
                }                 // else System.Console.WriteLine("purged: " + i);
            }


            titles = newtitles;
        }
示例#2
0
 public static void init()
 {
     TI.init();
 }
示例#3
0
        private static bool evaluate_subexpr(String expression, DBTrade db)
        {
            String lefttitle  = "";
            String leftfield  = "";
            String righttitle = "";
            String rightfield = "";
            String command    = "";

            double[]         parms = new double[10];
            NumberFormatInfo ni    = (NumberFormatInfo)CultureInfo.InstalledUICulture.NumberFormat.Clone();

            ni.NumberDecimalSeparator = ".";

            expression = expression.Trim();

            if (expression.Contains('='))
            {
                leftfield = expression.Substring(0, expression.IndexOf('=')).Trim();

                // per farlo bene, x.(__) x è un nome di titolo?
                if (leftfield.Contains('.'))
                {
                    lefttitle = leftfield.Substring(0, leftfield.IndexOf('.'));
                    leftfield = leftfield.Substring(leftfield.IndexOf('.') + 1);
                }
                expression = expression.Substring(expression.IndexOf('=') + 1).Trim();
            }
            // lefttitle e leftfield apposto.

            expression = expression.Replace(")", "");
            if (!expression.Contains('('))
            {
                return(false);
            }
            command    = expression.Substring(0, expression.IndexOf('('));
            expression = expression.Substring(expression.IndexOf('(') + 1);


            String[] otherparms = expression.Trim().Split(',');
            if (!otherparms[0].Contains('.'))
            {
                return(false);
            }

            righttitle = otherparms[0].Substring(0, otherparms[0].IndexOf('.'));
            rightfield = otherparms[0].Substring(otherparms[0].IndexOf('.') + 1);


            for (int j = 1; j < otherparms.Length; j++)
            {
                try { parms[j - 1] = Double.Parse(otherparms[j], ni); }
                catch (FormatException) { return(false); }
            }

            if (lefttitle.Equals(""))       // if not specified left title
            {
                lefttitle = righttitle;
            }

            // now left* right*, command, parms are ok
//            MessageBox.Show(lefttitle + "." + leftfield + " = "+command+"(" + righttitle + "." + rightfield+")");
            DBTitle srcTitle = db.getByName(righttitle);
            DBTitle dstTitle = db.getByName(lefttitle);

            if (srcTitle == null || dstTitle == null)
            {
                return(false);
            }

            double[] srcSerie = srcTitle.GetSerieByName(rightfield);
            if (srcSerie == null)
            {
                return(false);
            }



            double[] result = null;
            if (command.Equals("const"))
            {
                result = TI.calculate_const(srcSerie.Length, parms[0]);
            }
            else if (command.Equals("mm"))
            {
                result = TI.calculate_mm(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("emm"))
            {
                result = TI.calculate_emm(srcSerie, (int)parms[0], parms[1]);
            }

            else if (command.Equals("d") || command.Equals("d-"))
            {
                result = TI.calculate_d(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("d%") || command.Equals("d-%") || command.Equals("dperc") || command.Equals("d-perc"))
            {
                result = TI.calculate_dperc(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("d2") || command.Equals("d2-"))
            {
                result = TI.calculate_d2(srcSerie, (int)parms[0]);
            }

            else if (command.Equals("sigma"))
            {
                result = TI.calculate_sigma(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("esigma"))
            {
                result = TI.calculate_esigma(srcSerie, (int)parms[0], parms[1]);
            }

            else if (command.Equals("BBhigh"))
            {
                result = TI.calculate_BBhigh(srcSerie, (int)parms[0], parms[1]);
            }
            else if (command.Equals("BBlow"))
            {
                result = TI.calculate_BBlow(srcSerie, (int)parms[0], parms[1]);
            }
            else if (command.Equals("BBWp"))
            {
                result = TI.calculate_BBWp(srcSerie, (int)parms[0], parms[1]);
            }
            else if (command.Equals("BBBp"))
            {
                result = TI.calculate_BBBp(srcSerie, (int)parms[0], parms[1]);
            }

            else if (command.Equals("range"))
            {
                result = TI.calculate_range(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("stochK"))
            {
                result = TI.calculate_stochasticK(srcSerie, (int)parms[0], parms[1]);
            }

            else if (command.Equals("RSIC"))
            {
                result = TI.calculate_RSIC(srcSerie, (int)parms[0]);
            }
            else if (command.Equals("RSIV"))
            {
                result = TI.calculate_RSIV(srcSerie, (int)parms[0]);
            }



            else if (command.Equals("rand"))
            {
                result = TI.calculate_rand(srcSerie, parms[0], parms[1]);
            }



            if (result != null)
            {
                dstTitle.AddSerie(leftfield, result);
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#4
0
        private void TradeSystemActionsRun_Click(object sender, EventArgs e)
        {
            TradeSystemActionsTextbox.Clear();
            sim_results = null;
            sim_done    = 0;


            if (!first_run)
            {
                first_run = false;
                db.Clear();
                db.ImportCSVDir(CSV_Dirname);
            }

            int RunN;

            try { RunN = Int32.Parse(textBox1.Text); }
            catch (Exception) { RunN = 1; }

            try { InitLambda = Int32.Parse(initlambdaTextbox.Text); }
            catch (Exception) { InitLambda = 1; }

            try { MaxLambda = Int32.Parse(maxlambdaTextbox.Text); }
            catch (Exception) { MaxLambda = InitLambda + 1; }

            if (MaxLambda <= InitLambda)
            {
                MaxLambda = InitLambda + 1;
            }

            sim_done                   = 0;
            textBox1.ReadOnly          = true;
            sim_results                = new RunResults[RunN * MaxLambda];
            maxlambdaTextbox.ReadOnly  = true;
            initlambdaTextbox.ReadOnly = true;


            if (tradesystem_tasklist != null)
            {
                MessageBox.Show("Treads still running");
                return;
            }
            tradesystem_tasklist = new LinkedList <TradeSystem>();

            for (int k = InitLambda; k < MaxLambda; k++)
            {
                for (int i = 0; i < RunN; i++)
                {
                    TradeSystem tss = null;
                    if (TradesystemTypeComboBox.SelectedItem.ToString().Equals("Random"))
                    {
                        if ((k == InitLambda) && (i == 0))
                        {
                            InitDB();
                        }
                        tss = new TradeSystemRandom(db, this);
                    }
                    else if (TradesystemTypeComboBox.SelectedItem.ToString().Equals("Analytic"))
                    {
                        if ((k == InitLambda) && (i == 0))
                        {
                            InitAnalytic();
                        }

                        tss = new TradeSystemAnalytic(db, this);
                    }
                    else if (TradesystemTypeComboBox.SelectedItem.ToString().Equals("Delu"))
                    {
                        if ((k == InitLambda) && (i == 0))
                        {
                            InitDelu();
                        }
                        tss = new TradeSystemDelu(db, this);
                        System.Threading.Thread.Sleep(1);
                    }
                    else if (TradesystemTypeComboBox.SelectedItem.ToString().Equals("Silly"))
                    {
                        if ((k == InitLambda) && (i == 0))
                        {
                            InitDB();
                        }
                        tss = new TradeSystemSilly(db, this);
                    }

                    else if (TradesystemTypeComboBox.SelectedItem.ToString().Equals("Neural"))
                    {
                        double[] prova = new double[100];
                        for (int zz = 0; zz < prova.Length; zz++)
                        {
                            prova[zz] = zz;
                        }

                        double[] prova1  = TI.calculate_mm(prova, 30);
                        double[] prova11 = TI.calculate_mm_base(prova, 30, 1);
                        double[] prova2  = TI.calculate_mm_base_new(prova, 30, 1);
                        double[] prova3  = TI.calculate_emm(prova, 30, 0.5);
                        double[] prova33 = TI.calculate_mm_base(prova, 30, 2);
                        double[] prova4  = TI.calculate_mm_base_new(prova, 30, 2);

                        System.Console.WriteLine(prova1[99] + " " + prova11[99] + " " + prova2[99]);
                        System.Console.WriteLine(prova3[99] + " " + prova33[99] + " " + prova4[99]);

                        /*
                         * StreamWriter SW = null;
                         * try { SW = new StreamWriter("test"); }
                         * catch (Exception) { SW = null; }
                         *
                         *
                         * for(int z=0;z<vals.Length;z++)
                         *      SW.WriteLine(vals[z] + " " + oldmean[z] + " " + newmean[z]);
                         */
                        if ((k == InitLambda) && (i == 0))
                        {
                            InitDB();
                        }
                        tss = new TradeSystemRandom(db, this);
                    }

                    else
                    {
                        continue;
                    }

//	                try { tss.InitialCapital = Double.Parse(textBoxInitialCapital.Text); }
//	                catch (Exception) { tss.InitialCapital = 0; }

                    try { tss.MaxInvestment = Double.Parse(textBox2.Text); }
                    catch (Exception) { tss.MaxInvestment = 0; }

                    tss.AdvancedStatistics = checkBox2.Checked;
                    tss.ScatterPlot        = checkBox1.Checked;
                    tss.BeginLambda        = InitLambda;
                    tss.Lambda             = k;

                    tradesystem_tasklist.AddLast(tss);
                }
            }

            time_start = DateTime.Now;
            int thread_num = Math.Min(max_threads, tradesystem_tasklist.Count);

            for (int i = 0; i < thread_num; i++)
            {
                tradesystem_tasklist.ElementAt(i).run();
            }
        }