Beispiel #1
0
        private void twNew_MouseClick(object sender, MouseEventArgs e)
        {
            if (crNode == null)
            {
                return;
            }

            Telerik.WinControls.UI.Docking.DockWindow crWindow = rdMain.ActiveWindow;

            if (crNode.Tag.ToString() == "Web.HPLogin")
            {
                HPLogin c1 = new HPLogin();

                c1.Location = new Point(e.X - (c1.Width / 2), e.Y - (c1.Height / 2));

                c1.Click += new EventHandler(Web_HPLogin_Click);

                c1.MouseDown += new MouseEventHandler(Control_MouseDown);
                c1.MouseMove += new MouseEventHandler(Control_MouseMove);
                c1.MouseUp   += new MouseEventHandler(Control_MouseUp);

                c1.DeleteControl += new KeyEventHandler(Control_DeleteControl);

                crWindow.Controls.Add(c1);
            }
            else if (crNode.Tag.ToString() == "Logic.If")
            {
                LogicIf if1 = new LogicIf();

                if1.Location = new Point(e.X - (if1.Width / 2), e.Y - (if1.Height / 2));

                if1.Click += new EventHandler(Logic_If_Click);

                if1.MouseDown += new MouseEventHandler(Control_MouseDown);
                if1.MouseMove += new MouseEventHandler(Control_MouseMove);
                if1.MouseUp   += new MouseEventHandler(Control_MouseUp);

                if1.DeleteControl += new KeyEventHandler(Control_DeleteControl);

                crWindow.Controls.Add(if1);
            }
        }
Beispiel #2
0
        //Runs the backtest and displays the chart with buy/sell/exit icons.
        private string RunBacktest()
        {
            string ret = string.Empty;

            try
            {
                oScript.ClearRecords();
                oBacktest.ClearRecords();

                if (!VerifyForm())
                {
                    return(string.Empty);
                }
                if (!TestScripts())
                {
                    return(string.Empty);
                }
                if (m_ctlData == null)
                {
                    return(string.Empty);
                }

                cmdBacktest.Text = "&Stop Backtest";
                EnableControls(false);

                Periodicity periodicity;
                switch (cboPeriodicity.Text)
                {
                case "Minute":
                    periodicity = Periodicity.Minutely;
                    break;

                case "Hour":
                    periodicity = Periodicity.Hourly;
                    break;

                case "Day":
                    periodicity = Periodicity.Daily;
                    break;

                case "Week":
                    periodicity = Periodicity.Weekly;
                    break;

                default:
                    periodicity = Periodicity.Minutely;
                    break;
                }

                cmdBacktest.Enabled = false;


                Telerik.WinControls.UI.Docking.DockWindow activeDoc = frmMain2.GInstance.radDock2.DocumentManager.ActiveDocument;


                //Get the data selection
                M4Core.Entities.ChartSelection selection = new M4Core.Entities.ChartSelection
                {
                    Periodicity = (M4Core.Entities.Periodicity)periodicity,
                    Symbol      = txtSymbol.Text,
                    Interval    = Convert.ToInt32(txtInterval.Text),
                    Bars        = Convert.ToInt32(txtBars.Text)
                };

                m_ctlData.LoadRealTimeCtlPainelChartAsync2(selection, new Action <CtlPainelChart>(chart =>
                {
                    m_chart = chart;
                    if (m_chart == null)
                    {
                        goto Quit;
                    }

                    DataManager.BarData[] bars = m_chart.GetDataFromChart();
                    if (bars.Length < 1)
                    {
                        goto Quit;
                    }

                    m_chart.Subscribers    += 1;
                    m_chart.RealTimeUpdates = false;

                    //Get historic data
                    cmdBacktest.Enabled = true;
                    if (bars.Length < 3)
                    {
                        goto Quit;                  //Bad request
                    }
                    //Insert the data into all four instances of TradeScript
                    oScript.ClearRecords();
                    oBacktest.ClearRecords();

                    DateTime td;
                    double jdate;
                    for (int n = 1; n < bars.Length - 1; n++)
                    {
                        td    = bars[n].TradeDate;
                        jdate = oScript.ToJulianDate(td.Year, td.Month, td.Day, td.Hour, td.Minute, td.Second, 0);
                        oScript.AppendRecord(jdate, bars[n].OpenPrice, bars[n].HighPrice,
                                             bars[n].LowPrice, bars[n].ClosePrice, (int)bars[n].Volume);
                        oBacktest.AppendRecord(jdate, bars[n].OpenPrice, bars[n].HighPrice,
                                               bars[n].LowPrice, bars[n].ClosePrice, (int)bars[n].Volume);
                    }

                    //TODO
                    ret = oBacktest.Backtest(txtBuyScript, txtSellScript, txtExitLongScript, txtExitShortScript, 0.001);
                    if (string.IsNullOrEmpty(ret))
                    {
                        goto Quit;
                    }

                    string output =
                        oScript.GetScriptOutput(txtBuyScript + " AND \r\n" + txtSellScript + " AND \r\n" + txtExitLongScript +
                                                " AND \r\n" + txtExitShortScript);
                    if (string.IsNullOrEmpty(output))
                    {
                        goto Quit;
                    }

                    int row;
                    string[] cols;
                    int col;
                    string[] rows   = output.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] Header = SplitHeader(rows[0]);


                    //StockChartX
                    AxSTOCKCHARTXLib.AxStockChartX StockChartX1 = m_chart.StockChartX1;
                    StockChartX1.RemoveAllSeries();

                    string symbol = selection.Symbol;

                    StockChartX1.Symbol = symbol;


                    StockChartX1.AddChartPanel();
                    StockChartX1.AddSeries(symbol + ".open", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                    StockChartX1.AddSeries(symbol + ".high", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                    StockChartX1.AddSeries(symbol + ".low", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                    StockChartX1.AddSeries(symbol + ".close", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                    StockChartX1.set_SeriesColor(symbol + ".close", ColorTranslator.ToOle(Color.Black));



                    //OLD BEHAVIOR: Add generic serie

                    /*
                     * for (col = 6; col < Header.Length; col++)
                     * {
                     *  int panel = 0;
                     *  panel = StockChartX1.AddChartPanel();
                     *
                     *  StockChartX1.AddSeries(Header[col], STOCKCHARTXLib.SeriesType.stLineChart, panel);
                     *
                     *  System.Drawing.Color color;
                     *  switch (col)
                     *  {
                     *      case 6:
                     *          color = System.Drawing.Color.Blue;
                     *          break;
                     *      case 7:
                     *          color = System.Drawing.Color.Red;
                     *          break;
                     *      case 8:
                     *          color = System.Drawing.Color.Green;
                     *          break;
                     *      case 9:
                     *          color = System.Drawing.Color.Orange;
                     *          break;
                     *      case 10:
                     *          color = System.Drawing.Color.Purple;
                     *          break;
                     *      default:
                     *          color = System.Drawing.Color.Blue;
                     *          break;
                     *  }
                     *  StockChartX1.set_SeriesColor(Header[col], System.Drawing.ColorTranslator.ToOle(color));
                     *
                     * }*/

                    for (row = 1; row < rows.Length; row++)
                    {
                        cols  = rows[row].Split(',');
                        jdate = GetJDate(cols[0]);
                        StockChartX1.AppendValue(symbol + ".open", jdate, Convert.ToDouble(cols[1], ciEnUs));
                        StockChartX1.AppendValue(symbol + ".high", jdate, Convert.ToDouble(cols[2], ciEnUs));
                        StockChartX1.AppendValue(symbol + ".low", jdate, Convert.ToDouble(cols[3], ciEnUs));
                        StockChartX1.AppendValue(symbol + ".close", jdate, Convert.ToDouble(cols[4], ciEnUs));

                        //OLD BEHAVIOR: Add generic serie

                        /*
                         *  for (col = 6; col < cols.Length; col++)
                         *  {
                         *      double value = Convert.ToDouble(cols[col], ciEnUs);
                         *      if (value == 0 && row < rows.Length * 0.2)
                         *      {
                         *          value = (double)STOCKCHARTXLib.DataType.dtNullValue;
                         *      }
                         *
                         *      StockChartX1.AppendValue(Header[col], jdate, value);
                         *  }
                         */
                    }

                    //NEW BEHAVIOR: Add known indicators (it doesnt work with others indicators!)
                    for (col = 6; col < Header.Length; col++)
                    {
                        //Try to add until available key isnt found:
                        bool errorKey    = true;
                        int indicatorKey = 1;
                        while (errorKey)
                        {
                            try
                            {
                                System.Drawing.Color color;
                                switch (col)
                                {
                                case 6:
                                    color = System.Drawing.Color.Blue;
                                    break;

                                case 7:
                                    color = System.Drawing.Color.Red;
                                    break;

                                case 8:
                                    color = System.Drawing.Color.Green;
                                    break;

                                case 9:
                                    color = System.Drawing.Color.Orange;
                                    break;

                                case 10:
                                    color = System.Drawing.Color.Purple;
                                    break;

                                default:
                                    color = System.Drawing.Color.Blue;
                                    break;
                                }
                                //param[0  1   2   3]
                                //   MM(1,14,CLOSE,0)
                                if (Header[col].Contains("MM("))
                                {
                                    string indScript = Header[col].Replace("MM(", "");
                                    indScript        = indScript.Replace(")", "");
                                    string[] param   = indScript.Split(new char[] { ',' });
                                    string source    = symbol + ".Close";
                                    switch (param[2])
                                    {
                                    case "OPEN":
                                        source = symbol + ".Open";
                                        break;

                                    case "HIGH":
                                        source = symbol + ".High";
                                        break;

                                    case "LOW":
                                        source = symbol + ".Low";
                                        break;

                                    default:
                                        source = symbol + ".Close";
                                        break;
                                    }
                                    StockChartX1.AddIndicatorGenericMovingAverage("MA" + indicatorKey, 0, source,
                                                                                  int.Parse(param[1]), int.Parse(param[3]), int.Parse(param[0]), 0, color.R,
                                                                                  color.G,
                                                                                  color.B, 0, 1);
                                }
                                errorKey = false;
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message == "Key not unique")
                                {
                                    errorKey = true;
                                    indicatorKey++;
                                }
                                else
                                {
                                    Telerik.WinControls.RadMessageBox.Show(ex.Message);
                                }
                            }
                        }
                    }
                    StockChartX1.Update();

                    Quit:

                    if (m_chart != null)
                    {
                        m_chart.Show();
                    }

                    cmdBacktest.Text = "&Back Test";
                    EnableControls(true);

                    if (m_chart != null)
                    {
                        m_chart.Subscribers -= 1;
                    }
                })); //Ensure the chart is new

                while (ret == string.Empty)
                {
                    Application.DoEvents();
                }
            }
            catch (Exception ex) { }
            return(ret);
        }
Beispiel #3
0
        //Runs the backtest and displays the chart with buy/sell/exit icons.
        private string RunBacktest()
        {
            string ret = string.Empty;

            oScript.ClearRecords();
            oBacktest.ClearRecords();

            if (!VerifyForm())
            {
                return(string.Empty);
            }
            if (!TestScripts())
            {
                return(string.Empty);
            }
            if (m_ctlData == null)
            {
                return(string.Empty);
            }

            cmdBacktest.Text = "&Stop Backtest";
            EnableControls(false);

            Periodicity periodicity;

            switch (cboPeriodicity.Text)
            {
            case "Minute":
                periodicity = Periodicity.Minutely;
                break;

            case "Hour":
                periodicity = Periodicity.Hourly;
                break;

            case "Day":
                periodicity = Periodicity.Daily;
                break;

            case "Week":
                periodicity = Periodicity.Weekly;
                break;

            default:
                periodicity = Periodicity.Minutely;
                break;
            }

            cmdBacktest.Enabled = false;


            Telerik.WinControls.UI.Docking.DockWindow activeDoc = frmMain2.GInstance.radDock2.DocumentManager.ActiveDocument;


            //Get the data selection
            M4Core.Entities.ChartSelection selection = new M4Core.Entities.ChartSelection
            {
                Periodicity = (M4Core.Entities.Periodicity)periodicity,
                Symbol      = txtSymbol.Text,
                Interval    = Convert.ToInt32(txtInterval.Text),
                Bars        = Convert.ToInt32(txtBars.Text)
            };

            m_ctlData.LoadRealTimeCtlPainelChartAsync2(selection, new Action <CtlPainelChart>(chart => {
                m_chart = chart;
                if (m_chart == null)
                {
                    goto Quit;
                }

                DataManager.BarData[] bars = m_chart.GetDataFromChart();
                if (bars.Length < 1)
                {
                    goto Quit;
                }

                m_chart.Subscribers    += 1;
                m_chart.RealTimeUpdates = false;

                //Get historic data
                cmdBacktest.Enabled = true;
                if (bars.Length < 3)
                {
                    goto Quit;            //Bad request
                }
                //Insert the data into all four instances of TradeScript
                oScript.ClearRecords();
                oBacktest.ClearRecords();

                DateTime td;
                double jdate;
                for (int n = 1; n < bars.Length - 1; n++)
                {
                    td    = bars[n].TradeDate;
                    jdate = oScript.ToJulianDate(td.Year, td.Month, td.Day, td.Hour, td.Minute, td.Second, 0);
                    oScript.AppendRecord(jdate, bars[n].OpenPrice, bars[n].HighPrice,
                                         bars[n].LowPrice, bars[n].ClosePrice, (int)bars[n].Volume);
                    oBacktest.AppendRecord(jdate, bars[n].OpenPrice, bars[n].HighPrice,
                                           bars[n].LowPrice, bars[n].ClosePrice, (int)bars[n].Volume);
                }

                //TODO
                ret = oBacktest.Backtest(txtBuyScript.Text, txtSellScript.Text, txtExitLongScript.Text, txtExitShortScript.Text, 0.001);
                if (string.IsNullOrEmpty(ret))
                {
                    goto Quit;
                }

                string output =
                    oScript.GetScriptOutput(txtBuyScript.Text + " AND \r\n" + txtSellScript.Text + " AND \r\n" + txtExitLongScript.Text +
                                            " AND \r\n" + txtExitShortScript.Text);
                if (string.IsNullOrEmpty(output))
                {
                    goto Quit;
                }

                int row;
                string[] cols;
                int col;
                string[] rows   = output.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                string[] Header = SplitHeader(rows[0]);


                //StockChartX
                AxSTOCKCHARTXLib.AxStockChartX StockChartX1 = m_chart.StockChartX1;
                StockChartX1.RemoveAllSeries();

                string symbol = selection.Symbol;

                StockChartX1.Symbol = symbol;


                StockChartX1.AddChartPanel();
                StockChartX1.AddSeries(symbol + ".open", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                StockChartX1.AddSeries(symbol + ".high", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                StockChartX1.AddSeries(symbol + ".low", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                StockChartX1.AddSeries(symbol + ".close", STOCKCHARTXLib.SeriesType.stCandleChart, 0);
                StockChartX1.set_SeriesColor(symbol + ".close", ColorTranslator.ToOle(Color.Black));

                bool hasVolume = bars[0].Volume != -987654321;
                if (hasVolume)
                {
                    StockChartX1.AddChartPanel();
                    StockChartX1.AddSeries(symbol + ".volume", STOCKCHARTXLib.SeriesType.stVolumeChart, 1);
                    StockChartX1.set_SeriesColor(symbol + ".volume", ColorTranslator.ToOle(Color.Blue));
                    StockChartX1.set_SeriesWeight(symbol + ".volume", 3);
                    StockChartX1.VolumePostfixLetter = "M"; //Google trades in millions
                }

                for (col = 6; col < Header.Length; col++)
                {
                    int panel = StockChartX1.AddChartPanel();
                    StockChartX1.AddSeries(Header[col], STOCKCHARTXLib.SeriesType.stLineChart, panel);

                    System.Drawing.Color color;
                    switch (col)
                    {
                    case 6:
                        color = System.Drawing.Color.Blue;
                        break;

                    case 7:
                        color = System.Drawing.Color.Red;
                        break;

                    case 8:
                        color = System.Drawing.Color.Green;
                        break;

                    case 9:
                        color = System.Drawing.Color.Orange;
                        break;

                    case 10:
                        color = System.Drawing.Color.Purple;
                        break;

                    default:
                        color = System.Drawing.Color.Blue;
                        break;
                    }
                    StockChartX1.set_SeriesColor(Header[col], System.Drawing.ColorTranslator.ToOle(color));
                }

                for (row = 1; row < rows.Length; row++)
                {
                    cols  = rows[row].Split(',');
                    jdate = GetJDate(cols[0]);
                    StockChartX1.AppendValue(symbol + ".open", jdate, Convert.ToDouble(cols[1], ciEnUs));
                    StockChartX1.AppendValue(symbol + ".high", jdate, Convert.ToDouble(cols[2], ciEnUs));
                    StockChartX1.AppendValue(symbol + ".low", jdate, Convert.ToDouble(cols[3], ciEnUs));
                    StockChartX1.AppendValue(symbol + ".close", jdate, Convert.ToDouble(cols[4], ciEnUs));
                    if (hasVolume)
                    {
                        StockChartX1.AppendValue(symbol + ".volume", jdate, Convert.ToDouble(cols[5], ciEnUs) / 1000000);
                    }

                    for (col = 6; col < cols.Length; col++)
                    {
                        double value = Convert.ToDouble(cols[col], ciEnUs);
                        if (value == 0 && row < rows.Length * 0.2)
                        {
                            value = (double)STOCKCHARTXLib.DataType.dtNullValue;
                        }

                        StockChartX1.AppendValue(Header[col], jdate, value);
                    }
                }

                StockChartX1.Update();

                Quit:

                if (m_chart != null)
                {
                    m_chart.Show();
                }

                cmdBacktest.Text = "&Back Test";
                EnableControls(true);

                if (m_chart != null)
                {
                    m_chart.Subscribers -= 1;
                }
            })); //Ensure the chart is new

            while (ret == string.Empty)
            {
                Application.DoEvents();
            }
            return(ret);
        }