示例#1
0
        private void cmdBackTest_Click(object sender, EventArgs e)
        {
            Dictionary <DateTime, int> M5Index = new Dictionary <DateTime, int>();

            Dictionary <string, DateTime[]> reff = new Dictionary <string, DateTime[]>();

            foreach (KeyValuePair <string, int> kvp in Global.Instance.TotalBars)
            {
                reff.Add(kvp.Key, new DateTime[kvp.Value]);
            }


            double transactionCost = 0;
            double leverage;
            double initialDeposit = 0;

            StringBuilder title = new StringBuilder();

            if (!double.TryParse(txtTransactionCost.Text, out transactionCost))
            {
                MessageBox.Show("Invalid transaction cost");
                return;
            }
            if (!double.TryParse(txtLeverage.Text, out leverage))
            {
                MessageBox.Show("Invalid leverage");
                return;
            }
            if (!double.TryParse(txtInitialDeposit.Text, out initialDeposit))
            {
                MessageBox.Show("Invalid initial deposit");
                return;
            }

            using (StreamReader reader = new StreamReader(Global.Instance.data_folder_path + "\\m5\\EURUSD.csv"))
            {
                string line = null;
                int    i    = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] ext = line.Split(',');
                    M5Index.Add(DateTime.Parse(ext[0]), i);
                    i++;
                }
                reader.Close();
            }

            M5Index.Add(new DateTime(2011, 12, 25, 22, 00, 00), 42311);
            M5Index.Add(new DateTime(2012, 1, 2, 2, 00, 00), 43733);

            M5Index.Add(new DateTime(2011, 2, 6, 17, 00, 00), 51265);
            M5Index.Add(new DateTime(2012, 1, 1, 22, 00, 00), 43732);

            foreach (KeyValuePair <string, int> kvp in Global.Instance.TotalBars)
            {
                if (kvp.Key == "m5")
                {
                    continue;
                }
                using (StreamReader reader = new StreamReader(Global.Instance.data_folder_path + "\\" + kvp.Key + "\\EURUSD.csv"))
                {
                    string line = null;
                    int    i    = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] ext = line.Split(',');
                        reff[kvp.Key][i] = DateTime.Parse(ext[0]);
                        i++;
                    }
                    reader.Close();
                }
            }
            ;

            string[] systems = txtSystems.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            StrategyTesterResult[] results = new StrategyTesterResult[systems.Length];
            StrategyTesterResult   res     = new StrategyTesterResult();
            List <int>             Actions = new List <int>();


            for (int i = 0; i < systems.Length; i++)
            {
                #region main loop
                string[] ext;
                if (systems[i].Contains(','))
                {
                    ext = systems[i].Split(',');
                }
                else
                {
                    ext = systems[i].Split('\t');
                }
                string Symbol1   = ext[0];
                string Symbol2   = ext[1];
                int    period    = int.Parse(ext[2]);
                double bandwidth = double.Parse(ext[3]);
                string timeframe = ext[4];

                SingleRun singlerun = new SingleRun(new string[1], timeframe);
                singlerun.TransactionCost = transactionCost;
                singlerun.Bandwidth       = bandwidth;
                results[i] = singlerun.Run();


                res.total_trades                  += results[i].total_trades;
                res.total_short_trades            += results[i].total_short_trades;
                res.total_long_trades             += results[i].total_long_trades;
                res.total_short_trades_loss       += results[i].total_short_trades_loss;
                res.total_long_trades_loss        += results[i].total_long_trades_loss;
                res.total_short_trades_profitable += results[i].total_short_trades_profitable;
                res.total_long_trades_profitable  += results[i].total_long_trades_profitable;
                res.total_profit_trades           += results[i].total_profit_trades;
                res.total_loss_trades             += results[i].total_loss_trades;



                title.Append(Symbol1 + "-" + Symbol2);
                if (i < systems.Length - 1)
                {
                    title.Append("  &  ");
                }

                #endregion
            }

            Actions.Sort();
            List <int>    OpenOrders   = new List <int>();
            List <int>    CloseOrders  = new List <int>();
            List <double> Profit1      = new List <double>();
            List <double> Profit2      = new List <double>();
            List <double> Pipcost1     = new List <double>();
            List <double> Pipcost2     = new List <double>();
            List <int>    PositionSize = new List <int>();

            int[]  pos   = new int[results.Length];
            bool[] state = new bool[results.Length];

            int max_open_positions             = 0;
            int total_number_of_open_positions = 0;


            for (int i = 0; i < Actions.Count; i++)
            {
                for (int j = 0; j < results.Length; j++)
                {
                }
                int n_open_positions = open_positions(state);
                total_number_of_open_positions += n_open_positions;
                if (n_open_positions > max_open_positions)
                {
                    max_open_positions = n_open_positions;
                }
            }



            res.leverage = leverage;

            res.title          = title.ToString();
            res.initialDeposit = initialDeposit;

            res.transactionCost = transactionCost;

            try
            {
                res.percentage_short_trades = Math.Round((double)(res.total_short_trades * 100 / res.total_trades), 1);
            }
            catch { }
            try
            {
                res.percentage_long_trades = Math.Round((double)(res.total_long_trades * 100 / res.total_trades), 1);
            }
            catch { }
            try
            {
                res.percentage_short_trades_profitable = Math.Round((double)(res.total_short_trades_profitable * 100 / res.total_short_trades), 1);
            }
            catch { }
            try
            {
                res.percentage_short_trades_loss = Math.Round((double)(res.total_short_trades_loss * 100 / res.total_short_trades), 1);
            }
            catch { }
            try
            {
                res.percentage_long_trades_profitable = Math.Round((double)(res.total_long_trades_profitable * 100 / res.total_long_trades), 1);
            }
            catch { }
            try
            {
                res.percentage_long_trades_loss = Math.Round((double)(res.total_long_trades_loss * 100 / res.total_long_trades), 1);
            }
            catch { }



            if (res.total_trades > 0)
            {
                res.accuracy = Math.Round((double)(res.total_profit_trades * 100) / res.total_trades, 1);
            }

            res.maximum_open_positions = max_open_positions;
            frmResults frm = new frmResults(res);
            frm.Show();
        }
示例#2
0
        private void SingleRunrunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int    window, leverage, responseVariableIndex, maxOpenPositions;
            double bandwidth, transactionCost, initialDeposit;

            string[] basket;

            if (!int.TryParse(txtWindow.Text, out window))
            {
                MessageBox.Show("Invalid window");
                return;
            }
            if (!int.TryParse(txtLeverage.Text, out leverage))
            {
                MessageBox.Show("Invalid leverage");
                return;
            }
            if (!int.TryParse(txtMaxOpenPositions.Text, out maxOpenPositions))
            {
                MessageBox.Show("Invalid max open positions");
                return;
            }
            if (!double.TryParse(txtBandwidth.Text, out bandwidth))
            {
                MessageBox.Show("Invalid bandwidth");
                return;
            }
            if (!double.TryParse(txtTransactionCost.Text, out transactionCost))
            {
                MessageBox.Show("Invalid transaction cost");
                return;
            }
            if (!double.TryParse(txtInitialDeposit.Text, out initialDeposit))
            {
                MessageBox.Show("Invalid initial deposit");
                return;
            }
            basket = txtBasket.Text.Split(',');
            responseVariableIndex = cboResponseV.SelectedIndex;
            if (basket.Length <= 1)
            {
                MessageBox.Show("Basket must contain a minimum of two symbols separated by comma");
                return;
            }
            if (responseVariableIndex < 0 || responseVariableIndex >= basket.Length)
            {
                MessageBox.Show("Response variable index must be greater than 0 and less than the total number of symbols in the basket");
                return;
            }
            SingleRun test = new SingleRun(basket, cboTimeFrame.Text);

            test.Leverage              = leverage;
            test.InitialDeposit        = initialDeposit;
            test.Bandwidth             = bandwidth;
            test.TransactionCost       = transactionCost;
            test.Responsevariableindex = responseVariableIndex;
            test.Window           = window;
            test.MaxOpenPositions = maxOpenPositions;
            StrategyTesterResult res = test.Run();

            frmResults frm = new frmResults(res);

            frm.Show();
        }