Ejemplo n.º 1
0
        /// <summary>
        /// Exports the bar summary
        /// </summary>
        public void ExportBarSummary()
        {
            string stage = String.Empty;

            if (Data.IsProgramBeta)
            {
                stage = " " + Language.T("Beta");
            }
            else if (Data.IsProgramRC)
            {
                stage = " " + "RC";
            }

            sb.Append("Forex Strategy Builder v" + Data.ProgramVersion + stage + Environment.NewLine);
            sb.Append("Strategy name: " + Data.Strategy.StrategyName + Environment.NewLine);
            sb.Append("Exported on " + DateTime.Now.ToString() + Environment.NewLine);
            sb.Append(Data.Symbol + " " + Data.PeriodString + "; Values in pips" + Environment.NewLine);

            sb.Append("Bar Numb\t");
            sb.Append("Date\t");
            sb.Append("Hour\t");
            sb.Append("Open\t");
            sb.Append("High\t");
            sb.Append("Low\t");
            sb.Append("Close\t");
            sb.Append("Volume\t");
            sb.Append("Direction\t");
            sb.Append("Lots\t");
            sb.Append("Transaction\t");
            sb.Append("Price\t");
            sb.Append("Profit Loss\t");
            sb.Append("Floating P/L\t");
            sb.Append("Spread\t");
            sb.Append("Rollover\t");
            sb.Append("Balance\t");
            sb.Append("Equity\t");
            sb.Append("Interpolation" + Environment.NewLine);

            for (int bar = 0; bar < Data.Bars; bar++)
            {
                sb.Append((bar + 1).ToString() + "\t");
                sb.Append(Data.Time[bar].ToString(sDF) + "\t");
                sb.Append(Data.Time[bar].ToString("HH:mm") + "\t");
                sb.Append(Data.Open[bar].ToString(FF) + "\t");
                sb.Append(Data.High[bar].ToString(FF) + "\t");
                sb.Append(Data.Low[bar].ToString(FF) + "\t");
                sb.Append(Data.Close[bar].ToString(FF) + "\t");
                sb.Append(Data.Volume[bar].ToString() + "\t");
                if (Backtester.IsPos(bar))
                {
                    sb.Append(Backtester.SummaryDir(bar).ToString() + "\t");
                    sb.Append(Backtester.SummaryLots(bar).ToString() + "\t");
                    sb.Append(Backtester.SummaryTrans(bar).ToString() + "\t");
                    sb.Append(Backtester.SummaryPrice(bar).ToString(FF) + "\t");
                    sb.Append(Backtester.ProfitLoss(bar).ToString() + "\t");
                    sb.Append(Backtester.FloatingPL(bar).ToString() + "\t");
                }
                else
                {
                    sb.Append("\t\t\t\t\t\t");
                }
                sb.Append(Backtester.ChargedSpread(bar).ToString() + "\t");
                sb.Append(Backtester.ChargedRollOver(bar).ToString() + "\t");
                sb.Append(Backtester.Balance(bar).ToString() + "\t");
                sb.Append(Backtester.Equity(bar).ToString() + "\t");
                sb.Append(Backtester.BackTestEval(bar) + "\t");
                sb.Append(Environment.NewLine);
            }

            string fileName = Data.Strategy.StrategyName + "-" + Data.Symbol.ToString() + "-" + Data.Period.ToString();

            SaveData(fileName);
            return;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the chart parameters
        /// </summary>
        private void InitChart(int width, int height)
        {
            Chart = new Bitmap(width, height);

            if (!Data.IsData || !Data.IsResult || Data.Bars <= Data.FirstBar)
            {
                return;
            }

            const int border = 1;
            const int space  = 2;

            int maximum;
            int minimum;

            int firstBar   = Data.FirstBar;
            int bars       = Data.Bars;
            int chartBars  = Data.Bars - firstBar;
            int maxBalance = Configs.AccountInMoney ? (int)Backtester.MaxMoneyBalance : Backtester.MaxBalance;
            int minBalance = Configs.AccountInMoney ? (int)Backtester.MinMoneyBalance : Backtester.MinBalance;
            int maxEquity  = Configs.AccountInMoney ? (int)Backtester.MaxMoneyEquity : Backtester.MaxEquity;
            int minEquity  = Configs.AccountInMoney ? (int)Backtester.MinMoneyEquity : Backtester.MinEquity;

            if (Configs.AdditionalStatistics)
            {
                int maxLongBalance  = Configs.AccountInMoney ? (int)Backtester.MaxLongMoneyBalance : Backtester.MaxLongBalance;
                int minLongBalance  = Configs.AccountInMoney ? (int)Backtester.MinLongMoneyBalance : Backtester.MinLongBalance;
                int maxShortBalance = Configs.AccountInMoney ? (int)Backtester.MaxShortMoneyBalance : Backtester.MaxShortBalance;
                int minShortBalance = Configs.AccountInMoney ? (int)Backtester.MinShortMoneyBalance : Backtester.MinShortBalance;
                int maxLSBalance    = Math.Max(maxLongBalance, maxShortBalance);
                int minLSBalance    = Math.Min(minLongBalance, minShortBalance);

                maximum = Math.Max(Math.Max(maxBalance, maxEquity), maxLSBalance) + 1;
                minimum = Math.Min(Math.Min(minBalance, minEquity), minLSBalance) - 1;
            }
            else
            {
                maximum = Math.Max(maxBalance, maxEquity) + 1;
                minimum = Math.Min(minBalance, minEquity) - 1;
            }

            const int yTop    = border + space;
            int       yBottom = height - border - space;
            const int xLeft   = border;
            int       xRight  = width - border - space;
            float     xScale  = (xRight - xLeft) / (float)chartBars;
            float     yScale  = (yBottom - yTop) / (float)(maximum - minimum);

            var penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption), border);

            var balancePoints      = new PointF[chartBars];
            var equityPoints       = new PointF[chartBars];
            var longBalancePoints  = new PointF[chartBars];
            var shortBalancePoints = new PointF[chartBars];

            int index = 0;

            for (int bar = firstBar; bar < bars; bar++)
            {
                balancePoints[index].X = xLeft + index * xScale;
                equityPoints[index].X  = xLeft + index * xScale;
                if (Configs.AccountInMoney)
                {
                    balancePoints[index].Y = (float)(yBottom - (Backtester.MoneyBalance(bar) - minimum) * yScale);
                    equityPoints[index].Y  = (float)(yBottom - (Backtester.MoneyEquity(bar) - minimum) * yScale);
                }
                else
                {
                    balancePoints[index].Y = yBottom - (Backtester.Balance(bar) - minimum) * yScale;
                    equityPoints[index].Y  = yBottom - (Backtester.Equity(bar) - minimum) * yScale;
                }

                if (Configs.AdditionalStatistics)
                {
                    longBalancePoints[index].X  = xLeft + index * xScale;
                    shortBalancePoints[index].X = xLeft + index * xScale;
                    if (Configs.AccountInMoney)
                    {
                        longBalancePoints[index].Y  = (float)(yBottom - (Backtester.LongMoneyBalance(bar) - minimum) * yScale);
                        shortBalancePoints[index].Y = (float)(yBottom - (Backtester.ShortMoneyBalance(bar) - minimum) * yScale);
                    }
                    else
                    {
                        longBalancePoints[index].Y  = yBottom - (Backtester.LongBalance(bar) - minimum) * yScale;
                        shortBalancePoints[index].Y = yBottom - (Backtester.ShortBalance(bar) - minimum) * yScale;
                    }
                }

                index++;
            }

            Graphics g = Graphics.FromImage(Chart);

            // Paints the background by gradient
            var rectField = new RectangleF(1, 1, width - 2, height - 2);

            g.FillRectangle(new SolidBrush(LayoutColors.ColorChartBack), rectField);

            // Border
            g.DrawRectangle(penBorder, 0, 0, width - 1, height - 1);

            // Equity line
            g.DrawLines(new Pen(LayoutColors.ColorChartEquityLine), equityPoints);

            // Draw Long and Short balance
            if (Configs.AdditionalStatistics)
            {
                g.DrawLines(new Pen(Color.Red), shortBalancePoints);
                g.DrawLines(new Pen(Color.Green), longBalancePoints);
            }

            // Draw the balance line
            g.DrawLines(new Pen(LayoutColors.ColorChartBalanceLine), balancePoints);
        }
        /// <summary>
        /// Sets the chart params
        /// </summary>
        void InitChart(int width, int height)
        {
            chart = new Bitmap(width, height);

            if (!Data.IsData || !Data.IsResult || Data.Bars <= Data.FirstBar)
            {
                return;
            }

            int border = 1;
            int space  = 2;

            int   XLeft, XRight, YTop, YBottom;
            float XScale, YScale;

            int bars;
            int chartBars;
            int maximum;
            int minimum;
            int firstBar;
            Pen penBorder;

            PointF[] apntBalance;
            PointF[] apntEquity;
            PointF[] apntLongBalance;
            PointF[] apntShortBalance;

            firstBar  = Data.FirstBar;
            bars      = Data.Bars;
            chartBars = Data.Bars - firstBar;
            int iMaxBalance = Configs.AccountInMoney ? (int)Backtester.MaxMoneyBalance : Backtester.MaxBalance;
            int iMinBalance = Configs.AccountInMoney ? (int)Backtester.MinMoneyBalance : Backtester.MinBalance;
            int iMaxEquity  = Configs.AccountInMoney ? (int)Backtester.MaxMoneyEquity : Backtester.MaxEquity;
            int iMinEquity  = Configs.AccountInMoney ? (int)Backtester.MinMoneyEquity : Backtester.MinEquity;

            if (Configs.AdditionalStatistics)
            {
                int iMaxLongBalance  = Configs.AccountInMoney ? (int)Backtester.MaxLongMoneyBalance : Backtester.MaxLongBalance;
                int iMinLongBalance  = Configs.AccountInMoney ? (int)Backtester.MinLongMoneyBalance : Backtester.MinLongBalance;
                int iMaxShortBalance = Configs.AccountInMoney ? (int)Backtester.MaxShortMoneyBalance : Backtester.MaxShortBalance;
                int iMinShortBalance = Configs.AccountInMoney ? (int)Backtester.MinShortMoneyBalance : Backtester.MinShortBalance;
                int iMaxLSBalance    = Math.Max(iMaxLongBalance, iMaxShortBalance);
                int iMinLSBalance    = Math.Min(iMinLongBalance, iMinShortBalance);

                maximum = Math.Max(Math.Max(iMaxBalance, iMaxEquity), iMaxLSBalance) + 1;
                minimum = Math.Min(Math.Min(iMinBalance, iMinEquity), iMinLSBalance) - 1;
            }
            else
            {
                maximum = Math.Max(iMaxBalance, iMaxEquity) + 1;
                minimum = Math.Min(iMinBalance, iMinEquity) - 1;
            }

            YTop    = border + space;
            YBottom = height - border - space;
            XLeft   = border;
            XRight  = width - border - space;
            XScale  = (XRight - XLeft) / (float)chartBars;
            YScale  = (YBottom - YTop) / (float)(maximum - minimum);

            penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption), border);

            apntBalance      = new PointF[chartBars];
            apntEquity       = new PointF[chartBars];
            apntLongBalance  = new PointF[chartBars];
            apntShortBalance = new PointF[chartBars];

            int index = 0;

            for (int iBar = firstBar; iBar < bars; iBar++)
            {
                apntBalance[index].X = XLeft + index * XScale;
                apntEquity[index].X  = XLeft + index * XScale;
                if (Configs.AccountInMoney)
                {
                    apntBalance[index].Y = (float)(YBottom - (Backtester.MoneyBalance(iBar) - minimum) * YScale);
                    apntEquity[index].Y  = (float)(YBottom - (Backtester.MoneyEquity(iBar) - minimum) * YScale);
                }
                else
                {
                    apntBalance[index].Y = YBottom - (Backtester.Balance(iBar) - minimum) * YScale;
                    apntEquity[index].Y  = YBottom - (Backtester.Equity(iBar) - minimum) * YScale;
                }

                if (Configs.AdditionalStatistics)
                {
                    apntLongBalance[index].X  = XLeft + index * XScale;
                    apntShortBalance[index].X = XLeft + index * XScale;
                    if (Configs.AccountInMoney)
                    {
                        apntLongBalance[index].Y  = (float)(YBottom - (Backtester.LongMoneyBalance(iBar) - minimum) * YScale);
                        apntShortBalance[index].Y = (float)(YBottom - (Backtester.ShortMoneyBalance(iBar) - minimum) * YScale);
                    }
                    else
                    {
                        apntLongBalance[index].Y  = YBottom - (Backtester.LongBalance(iBar) - minimum) * YScale;
                        apntShortBalance[index].Y = YBottom - (Backtester.ShortBalance(iBar) - minimum) * YScale;
                    }
                }

                index++;
            }

            Graphics g = Graphics.FromImage(chart);

            // Paints the background by gradient
            RectangleF rectField = new RectangleF(1, 1, width - 2, height - 2);

            g.FillRectangle(new SolidBrush(LayoutColors.ColorChartBack), rectField);

            // Border
            g.DrawRectangle(penBorder, 0, 0, width - 1, height - 1);

            // Equity line
            g.DrawLines(new Pen(LayoutColors.ColorChartEquityLine), apntEquity);

            // Draw Long and Short balance
            if (Configs.AdditionalStatistics)
            {
                g.DrawLines(new Pen(Color.Red), apntShortBalance);
                g.DrawLines(new Pen(Color.Green), apntLongBalance);
            }

            // Draw the balance line
            g.DrawLines(new Pen(LayoutColors.ColorChartBalanceLine), apntBalance);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculates the balance lines
        /// </summary>
        private int Calculate(BackgroundWorker worker)
        {
            // Determine the number of lines
            // For each method per line
            // The random line shows the averaged values
            // Also we have two border lines for the random method
            // Plus the average balance line

            _isRandom      = false;
            _minimum       = float.MaxValue;
            _maximum       = float.MinValue;
            _minimumRandom = float.MaxValue;
            _maximumRandom = float.MinValue;
            var randomLines = (int)NumRandom.Value;

            _checkedMethods = 0;
            _lines          = 1;
            for (int m = 0; m < _countMethods; m++)
            {
                if (AchboxMethods[m].Checked)
                {
                    _checkedMethods++;
                    _lines++;
                    if ((InterpolationMethod)AchboxMethods[m].Tag == InterpolationMethod.Random)
                    {
                        _isRandom = true;
                    }
                }
            }

            if (_checkedMethods == 0 && Configs.PlaySounds)
            {
                SystemSounds.Hand.Play();
                return(-1);
            }

            _afBalance = new float[Data.Bars - Data.FirstBar];
            _afMethods = new float[_countMethods, Data.Bars - Data.FirstBar];
            if (_isRandom)
            {
                _afRandoms   = new float[randomLines, Data.Bars - Data.FirstBar];
                _afMinRandom = new float[Data.Bars - Data.FirstBar];
                _afMaxRandom = new float[Data.Bars - Data.FirstBar];
            }

            // Progress parameters
            int computedCycles           = 0;
            int cycles                   = _lines + (_isRandom ? randomLines : 0);
            int highestPercentageReached = 0;
            int percentComplete;

            // Calculates the lines
            for (int m = 0; m < _countMethods; m++)
            {
                if (worker.CancellationPending)
                {
                    return(-1);
                }
                if (!AchboxMethods[m].Checked)
                {
                    continue;
                }

                var method = (InterpolationMethod)AchboxMethods[m].Tag;

                if (method == InterpolationMethod.Random)
                {
                    for (int r = 0; r < randomLines; r++)
                    {
                        if (worker.CancellationPending)
                        {
                            return(-1);
                        }

                        Backtester.InterpolationMethod = method;
                        Backtester.Calculate();

                        if (Configs.AccountInMoney)
                        {
                            for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++)
                            {
                                _afRandoms[r, iBar] = (float)Backtester.MoneyBalance(iBar + Data.FirstBar);
                            }
                        }
                        else
                        {
                            for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++)
                            {
                                _afRandoms[r, iBar] = Backtester.Balance(iBar + Data.FirstBar);
                            }
                        }


                        // Report progress as a percentage of the total task.
                        computedCycles++;
                        percentComplete = 100 * computedCycles / cycles;
                        percentComplete = percentComplete > 100 ? 100 : percentComplete;
                        if (percentComplete > highestPercentageReached)
                        {
                            highestPercentageReached = percentComplete;
                            worker.ReportProgress(percentComplete);
                        }
                    }

                    for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++)
                    {
                        float randomSum = 0;
                        float minRandom = float.MaxValue;
                        float maxRandom = float.MinValue;
                        for (int r = 0; r < randomLines; r++)
                        {
                            float value = _afRandoms[r, iBar];
                            randomSum += value;
                            minRandom  = value < minRandom ? value : minRandom;
                            maxRandom  = value > maxRandom ? value : maxRandom;
                        }
                        _afMethods[m, iBar] = randomSum / randomLines;
                        _afMinRandom[iBar]  = minRandom;
                        _afMaxRandom[iBar]  = maxRandom;
                        _minimumRandom      = minRandom < _minimumRandom ? minRandom : _minimumRandom;
                        _maximumRandom      = maxRandom > _maximumRandom ? maxRandom : _maximumRandom;
                    }

                    // Report progress as a percentage of the total task.
                    computedCycles++;
                    percentComplete = 100 * computedCycles / cycles;
                    percentComplete = percentComplete > 100 ? 100 : percentComplete;
                    if (percentComplete > highestPercentageReached)
                    {
                        highestPercentageReached = percentComplete;
                        worker.ReportProgress(percentComplete);
                    }
                }
                else
                {
                    Backtester.InterpolationMethod = method;
                    Backtester.Calculate();

                    if (Configs.AccountInMoney)
                    {
                        for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++)
                        {
                            _afMethods[m, iBar] = (float)Backtester.MoneyBalance(iBar + Data.FirstBar);
                        }
                    }
                    else
                    {
                        for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++)
                        {
                            _afMethods[m, iBar] = Backtester.Balance(iBar + Data.FirstBar);
                        }
                    }

                    // Report progress as a percentage of the total task.
                    computedCycles++;
                    percentComplete = 100 * computedCycles / cycles;
                    percentComplete = percentComplete > 100 ? 100 : percentComplete;
                    if (percentComplete > highestPercentageReached)
                    {
                        highestPercentageReached = percentComplete;
                        worker.ReportProgress(percentComplete);
                    }
                }
            }

            // Calculates the average balance, Min and Max
            for (int bar = 0; bar < Data.Bars - Data.FirstBar; bar++)
            {
                float sum = 0;
                for (int m = 0; m < _countMethods; m++)
                {
                    if (!AchboxMethods[m].Checked)
                    {
                        continue;
                    }

                    float value = _afMethods[m, bar];
                    sum += value;
                    if (value < _minimum)
                    {
                        _minimum = value;
                    }
                    if (value > _maximum)
                    {
                        _maximum = value;
                    }
                }
                _afBalance[bar] = sum / _checkedMethods;
            }

            // Report progress as a percentage of the total task.
            computedCycles++;
            percentComplete = 100 * computedCycles / cycles;
            percentComplete = percentComplete > 100 ? 100 : percentComplete;
            if (percentComplete > highestPercentageReached)
            {
                worker.ReportProgress(percentComplete);
            }

            return(0);
        }
        /// <summary>
        /// Updates the journal data from the backtester
        /// </summary>
        void UpdateJournalData()
        {
            asJournalData   = new string[shownBars, columns];
            aiPositionIcons = new Image[shownBars];

            for (int bar = firstBar; bar < firstBar + shownBars; bar++)
            {
                int row = bar - firstBar;

                asJournalData[row, 0] = (bar + 1).ToString();
                asJournalData[row, 1] = Data.Time[bar].ToString(Data.DF);
                asJournalData[row, 2] = Data.Time[bar].ToString("HH:mm");
                asJournalData[row, 3] = Data.Open[bar].ToString(Data.FF);
                asJournalData[row, 4] = Data.High[bar].ToString(Data.FF);
                asJournalData[row, 5] = Data.Low[bar].ToString(Data.FF);
                asJournalData[row, 6] = Data.Close[bar].ToString(Data.FF);
                asJournalData[row, 7] = Data.Volume[bar].ToString();
                if (Backtester.IsPos(bar))
                {
                    asJournalData[row, 8] = Language.T(Backtester.SummaryTrans(bar).ToString());
                    asJournalData[row, 9] = Language.T(Backtester.SummaryDir(bar).ToString());
                    if (Configs.AccountInMoney)
                    {
                        string sign = Backtester.SummaryDir(bar) == PosDirection.Short ? "-" : "";
                        asJournalData[row, 10] = sign + Backtester.SummaryAmount(bar).ToString();
                    }
                    else
                    {
                        asJournalData[row, 10] = Backtester.SummaryLots(bar).ToString();
                    }
                    asJournalData[row, 11] = Backtester.SummaryPrice(bar).ToString(Data.FF);
                    if (Configs.AccountInMoney)
                    {
                        // Profit Loss
                        if (Backtester.SummaryTrans(bar) == Transaction.Close ||
                            Backtester.SummaryTrans(bar) == Transaction.Reduce ||
                            Backtester.SummaryTrans(bar) == Transaction.Reverse)
                        {
                            asJournalData[row, 12] = Backtester.MoneyProfitLoss(bar).ToString("F2");
                        }
                        else
                        {
                            asJournalData[row, 12] = "-";
                        }

                        // Floating Profit Loss
                        if (Backtester.SummaryTrans(bar) != Transaction.Close)
                        {
                            asJournalData[row, 13] = Backtester.MoneyFloatingPL(bar).ToString("F2");
                        }
                        else
                        {
                            asJournalData[row, 13] = "-";
                        }
                    }
                    else
                    {
                        // Profit Loss
                        if (Backtester.SummaryTrans(bar) == Transaction.Close ||
                            Backtester.SummaryTrans(bar) == Transaction.Reduce ||
                            Backtester.SummaryTrans(bar) == Transaction.Reverse)
                        {
                            asJournalData[row, 12] = Backtester.ProfitLoss(bar).ToString();
                        }
                        else
                        {
                            asJournalData[row, 12] = "-";
                        }

                        // Floating Profit Loss
                        if (Backtester.SummaryTrans(bar) != Transaction.Close)
                        {
                            asJournalData[row, 13] = Backtester.FloatingPL(bar).ToString();
                        }
                        else
                        {
                            asJournalData[row, 13] = "-";
                        }
                    }

                    // Icons
                    aiPositionIcons[row] = Backtester.SummaryPositionIcon(bar);
                }
                else
                {
                    // Icons
                    aiPositionIcons[row] = Properties.Resources.pos_square;
                }


                if (Configs.AccountInMoney)
                {
                    asJournalData[row, 14] = Backtester.MoneyBalance(bar).ToString("F2");
                    asJournalData[row, 15] = Backtester.MoneyEquity(bar).ToString("F2");
                }
                else
                {
                    asJournalData[row, 14] = Backtester.Balance(bar).ToString();
                    asJournalData[row, 15] = Backtester.Equity(bar).ToString();
                }
                asJournalData[row, 16] = Backtester.SummaryRequiredMargin(bar).ToString("F2");
                asJournalData[row, 17] = Backtester.SummaryFreeMargin(bar).ToString("F2");
                asJournalData[row, 18] = Language.T(Backtester.BackTestEval(bar));
            }

            return;
        }
        /// <summary>
        /// Sets chart's back testing data.
        /// </summary>
        public void SetChartData()
        {
            _isNotPaint = !Data.IsData || !Data.IsResult || Data.Bars <= Data.FirstBar;

            if (_isNotPaint)
            {
                return;
            }

            _showPriceLine   = Configs.ShowPriceChartOnAccountChart && Backtester.ExecutedOrders > 0;
            _isScanPerformed = Backtester.IsScanPerformed;

            _data.FirstBar = Data.FirstBar;
            _data.Bars     = Data.Bars;
            _chartBars     = Data.Bars - Data.FirstBar;

            int maxBalance = Configs.AccountInMoney ? (int)Backtester.MaxMoneyBalance : Backtester.MaxBalance;
            int minBalance = Configs.AccountInMoney ? (int)Backtester.MinMoneyBalance : Backtester.MinBalance;
            int maxEquity  = Configs.AccountInMoney ? (int)Backtester.MaxMoneyEquity : Backtester.MaxEquity;
            int minEquity  = Configs.AccountInMoney ? (int)Backtester.MinMoneyEquity : Backtester.MinEquity;

            if (Configs.AdditionalStatistics)
            {
                int maxLongBalance      = Configs.AccountInMoney ? (int)Backtester.MaxLongMoneyBalance : Backtester.MaxLongBalance;
                int minLongBalance      = Configs.AccountInMoney ? (int)Backtester.MinLongMoneyBalance : Backtester.MinLongBalance;
                int maxShortBalance     = Configs.AccountInMoney ? (int)Backtester.MaxShortMoneyBalance : Backtester.MaxShortBalance;
                int minShortBalance     = Configs.AccountInMoney ? (int)Backtester.MinShortMoneyBalance : Backtester.MinShortBalance;
                int maxLongShortBalance = Math.Max(maxLongBalance, maxShortBalance);
                int minLongShortBalance = Math.Min(minLongBalance, minShortBalance);

                _data.Maximum = Math.Max(Math.Max(maxBalance, maxEquity), maxLongShortBalance) + 1;
                _data.Minimum = Math.Min(Math.Min(minBalance, minEquity), minLongShortBalance) - 1;
            }
            else
            {
                _data.Maximum = Math.Max(maxBalance, maxEquity) + 1;
                _data.Minimum = Math.Min(minBalance, minEquity) - 1;
            }

            _data.Minimum = (int)(Math.Floor(_data.Minimum / 10f) * 10);

            _data.DataMaxPrice = Data.MaxPrice;
            _data.DataMinPrice = Data.MinPrice;

            if (_showPriceLine)
            {
                _data.ClosePrice = new double[_data.Bars];
                Data.Close.CopyTo(_data.ClosePrice, 0);
            }

            if (Configs.AccountInMoney)
            {
                _data.MoneyBalance = new double[_data.Bars];
                _data.MoneyEquity  = new double[_data.Bars];
            }
            else
            {
                _data.Balance = new int[_data.Bars];
                _data.Equity  = new int[_data.Bars];
            }

            if (Configs.AdditionalStatistics)
            {
                if (Configs.AccountInMoney)
                {
                    _data.LongMoneyBalance  = new double[_data.Bars];
                    _data.ShortMoneyBalance = new double[_data.Bars];
                }
                else
                {
                    _data.LongBalance  = new int[_data.Bars];
                    _data.ShortBalance = new int[_data.Bars];
                }
            }


            for (int bar = _data.FirstBar; bar < _data.Bars; bar++)
            {
                if (Configs.AccountInMoney)
                {
                    _data.MoneyBalance[bar] = Backtester.MoneyBalance(bar);
                    _data.MoneyEquity[bar]  = Backtester.MoneyEquity(bar);
                }
                else
                {
                    _data.Balance[bar] = Backtester.Balance(bar);
                    _data.Equity[bar]  = Backtester.Equity(bar);
                }

                if (Configs.AdditionalStatistics)
                {
                    if (Configs.AccountInMoney)
                    {
                        _data.LongMoneyBalance[bar]  = Backtester.LongMoneyBalance(bar);
                        _data.ShortMoneyBalance[bar] = Backtester.ShortMoneyBalance(bar);
                    }
                    else
                    {
                        _data.LongBalance[bar]  = Backtester.LongBalance(bar);
                        _data.ShortBalance[bar] = Backtester.ShortBalance(bar);
                    }
                }
            }

            _data.MarginCallBar = Backtester.MarginCallBar;

            if (IsOOS && OOSBar > _data.FirstBar)
            {
                _data.NetBalance     = (float)(Configs.AccountInMoney ? Backtester.MoneyBalance(OOSBar) : Backtester.Balance(OOSBar));
                _data.DataTimeBarOOS = Data.Time[OOSBar];
            }
            else
            {
                _data.NetBalance = (float)(Configs.AccountInMoney ? Backtester.NetMoneyBalance : Backtester.NetBalance);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Generates dynamic info on the status bar
        /// when we are Moving the mouse over the SmallBalanceChart.
        /// </summary>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (!isShowDynamicInfo || !Data.IsData || !Data.IsResult)
            {
                return;
            }

            int bar = (int)((e.X - XLeft) / XScale) + firstBar;

            bar = Math.Max(firstBar, bar);
            bar = Math.Min(Data.Bars - 1, bar);

            if (Configs.AccountInMoney)
            {
                strStatusBarText = String.Format("{0} {1} {2}: {3} {4} {5}: {6} {7}",
                                                 Data.Time[bar].ToString(Data.DF),
                                                 Data.Time[bar].ToString("HH:mm"),
                                                 Language.T("Balance"),
                                                 Backtester.MoneyBalance(bar).ToString("F2"),
                                                 Configs.AccountCurrency,
                                                 Language.T("Equity"),
                                                 Backtester.MoneyEquity(bar).ToString("F2"),
                                                 Configs.AccountCurrency);
            }
            else
            {
                strStatusBarText = String.Format("{0} {1} {2}: {3} {4} {5}: {6} {7}",
                                                 Data.Time[bar].ToString(Data.DF),
                                                 Data.Time[bar].ToString("HH:mm"),
                                                 Language.T("Balance"),
                                                 Backtester.Balance(bar),
                                                 Language.T("pips"),
                                                 Language.T("Equity"),
                                                 Backtester.Equity(bar),
                                                 Language.T("pips"));
            }

            if (Configs.AdditionalStatistics)
            {
                if (Configs.AccountInMoney)
                {
                    strStatusBarText += String.Format(" {0}: {1} {2} {3}: {4} {5}",
                                                      Language.T("Long balance"),
                                                      Backtester.LongMoneyBalance(bar).ToString("F2"),
                                                      Configs.AccountCurrency,
                                                      Language.T("Short balance"),
                                                      Backtester.ShortMoneyBalance(bar).ToString("F2"),
                                                      Configs.AccountCurrency);
                }
                else
                {
                    strStatusBarText += String.Format(" {0}: {1} {2} {3}: {4} {5}",
                                                      Language.T("Long balance"),
                                                      Backtester.LongBalance(bar),
                                                      Language.T("pips"),
                                                      Language.T("Short balance"),
                                                      Backtester.ShortBalance(bar),
                                                      Language.T("pips"));
                }
            }
            if (Configs.ShowPriceChartOnAccountChart)
            {
                strStatusBarText += String.Format(" {0}: {1}",
                                                  Language.T("Price close"),
                                                  Data.Close[bar]);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Sets chart's instrument and back testing data.
        /// </summary>
        public void SetChartData()
        {
            isNotPaint = !Data.IsData || !Data.IsResult || Data.Bars <= Data.FirstBar;

            if (isNotPaint)
            {
                return;
            }

            showPriceLine   = Configs.ShowPriceChartOnAccountChart && Backtester.ExecutedOrders > 0;
            isScanPerformed = Backtester.IsScanPerformed;

            firstBar  = Data.FirstBar;
            bars      = Data.Bars;
            chartBars = Data.Bars - firstBar;

            int maxBalance = Configs.AccountInMoney ? (int)Backtester.MaxMoneyBalance : Backtester.MaxBalance;
            int minBalance = Configs.AccountInMoney ? (int)Backtester.MinMoneyBalance : Backtester.MinBalance;
            int maxEquity  = Configs.AccountInMoney ? (int)Backtester.MaxMoneyEquity  : Backtester.MaxEquity;
            int minEquity  = Configs.AccountInMoney ? (int)Backtester.MinMoneyEquity  : Backtester.MinEquity;

            if (Configs.AdditionalStatistics)
            {
                int maxLongBalance  = Configs.AccountInMoney ? (int)Backtester.MaxLongMoneyBalance  : Backtester.MaxLongBalance;
                int minLongBalance  = Configs.AccountInMoney ? (int)Backtester.MinLongMoneyBalance  : Backtester.MinLongBalance;
                int maxShortBalance = Configs.AccountInMoney ? (int)Backtester.MaxShortMoneyBalance : Backtester.MaxShortBalance;
                int minShortBalance = Configs.AccountInMoney ? (int)Backtester.MinShortMoneyBalance : Backtester.MinShortBalance;
                int maxLSBalance    = Math.Max(maxLongBalance, maxShortBalance);
                int minLSBalance    = Math.Min(minLongBalance, minShortBalance);

                maximum = Math.Max(Math.Max(maxBalance, maxEquity), maxLSBalance) + 1;
                minimum = Math.Min(Math.Min(minBalance, minEquity), minLSBalance) - 1;
            }
            else
            {
                maximum = Math.Max(maxBalance, maxEquity) + 1;
                minimum = Math.Min(minBalance, minEquity) - 1;
            }

            minimum = (int)(Math.Floor(minimum / 10f) * 10);

            dataMaxPrice = Data.MaxPrice;
            dataMinPrice = Data.MinPrice;

            if (showPriceLine)
            {
                dataClose = new double[bars];
                Data.Close.CopyTo(dataClose, 0);
            }

            if (Configs.AccountInMoney)
            {
                backtesterMoneyBalance = new double[bars];
                backtesterMoneyEquity  = new double[bars];
            }
            else
            {
                backtesterBalance = new int[bars];
                backtesterEquity  = new int[bars];
            }

            if (Configs.AdditionalStatistics)
            {
                if (Configs.AccountInMoney)
                {
                    backtesterLongMoneyBalance  = new double[bars];
                    backtesterShortMoneyBalance = new double[bars];
                }
                else
                {
                    backtesterLongBalance  = new int[bars];
                    backtesterShortBalance = new int[bars];
                }
            }


            for (int bar = firstBar; bar < bars; bar++)
            {
                if (Configs.AccountInMoney)
                {
                    backtesterMoneyBalance[bar] = Backtester.MoneyBalance(bar);
                    backtesterMoneyEquity[bar]  = Backtester.MoneyEquity(bar);
                }
                else
                {
                    backtesterBalance[bar] = Backtester.Balance(bar);
                    backtesterEquity[bar]  = Backtester.Equity(bar);
                }

                if (Configs.AdditionalStatistics)
                {
                    if (Configs.AccountInMoney)
                    {
                        backtesterLongMoneyBalance[bar]  = Backtester.LongMoneyBalance(bar);
                        backtesterShortMoneyBalance[bar] = Backtester.ShortMoneyBalance(bar);
                    }
                    else
                    {
                        backtesterLongBalance[bar]  = Backtester.LongBalance(bar);
                        backtesterShortBalance[bar] = Backtester.ShortBalance(bar);
                    }
                }
            }

            marginCallBar = Backtester.MarginCallBar;

            if (isOOS && barOOS > firstBar)
            {
                balance        = (float)(Configs.AccountInMoney ? Backtester.MoneyBalance(barOOS) : Backtester.Balance(barOOS));
                dataTimeBarOOS = Data.Time[barOOS];
            }
            else
            {
                balance = (float)(Configs.AccountInMoney ? Backtester.NetMoneyBalance : Backtester.NetBalance);
            }

            return;
        }
        /// <summary>
        /// Sets the chart params
        /// </summary>
        public void InitChart()
        {
            if (!Data.IsData || !Data.IsResult || Data.Bars <= Data.FirstBar)
            {
                return;
            }

            firstBar  = Data.FirstBar;
            bars      = Data.Bars;
            chartBars = Data.Bars - firstBar;
            int iMaxBalance = Configs.AccountInMoney ? (int)Backtester.MaxMoneyBalance : Backtester.MaxBalance;
            int iMinBalance = Configs.AccountInMoney ? (int)Backtester.MinMoneyBalance : Backtester.MinBalance;
            int iMaxEquity  = Configs.AccountInMoney ? (int)Backtester.MaxMoneyEquity  : Backtester.MaxEquity;
            int iMinEquity  = Configs.AccountInMoney ? (int)Backtester.MinMoneyEquity  : Backtester.MinEquity;

            if (Configs.AdditionalStatistics)
            {
                int iMaxLongBalance  = Configs.AccountInMoney ? (int)Backtester.MaxLongMoneyBalance  : Backtester.MaxLongBalance;
                int iMinLongBalance  = Configs.AccountInMoney ? (int)Backtester.MinLongMoneyBalance  : Backtester.MinLongBalance;
                int iMaxShortBalance = Configs.AccountInMoney ? (int)Backtester.MaxShortMoneyBalance : Backtester.MaxShortBalance;
                int iMinShortBalance = Configs.AccountInMoney ? (int)Backtester.MinShortMoneyBalance : Backtester.MinShortBalance;
                int iMaxLSBalance    = Math.Max(iMaxLongBalance, iMaxShortBalance);
                int iMinLSBalance    = Math.Min(iMinLongBalance, iMinShortBalance);

                maximum = Math.Max(Math.Max(iMaxBalance, iMaxEquity), iMaxLSBalance) + 1;
                minimum = Math.Min(Math.Min(iMinBalance, iMinEquity), iMinLSBalance) - 1;
            }
            else
            {
                maximum = Math.Max(iMaxBalance, iMaxEquity) + 1;
                minimum = Math.Min(iMinBalance, iMinEquity) - 1;
            }

            YTop    = border + space;
            YBottom = ClientSize.Height - border - space;
            XLeft   = border;
            XRight  = ClientSize.Width - border - space;
            XScale  = (XRight - XLeft) / (float)chartBars;
            YScale  = (YBottom - YTop) / (float)(maximum - minimum);

            penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption), border);

            apntBalance = new PointF[chartBars];
            apntEquity  = new PointF[chartBars];

            if (Configs.AdditionalStatistics)
            {
                apntLongBalance  = new PointF[chartBars];
                apntShortBalance = new PointF[chartBars];
            }

            int index = 0;

            for (int iBar = firstBar; iBar < bars; iBar++)
            {
                apntBalance[index].X = XLeft + index * XScale;
                apntEquity[index].X  = XLeft + index * XScale;
                if (Configs.AccountInMoney)
                {
                    apntBalance[index].Y = (float)(YBottom - (Backtester.MoneyBalance(iBar) - minimum) * YScale);
                    apntEquity[index].Y  = (float)(YBottom - (Backtester.MoneyEquity(iBar) - minimum) * YScale);
                }
                else
                {
                    apntBalance[index].Y = YBottom - (Backtester.Balance(iBar) - minimum) * YScale;
                    apntEquity[index].Y  = YBottom - (Backtester.Equity(iBar) - minimum) * YScale;
                }

                if (Configs.AdditionalStatistics)
                {
                    apntLongBalance[index].X  = XLeft + index * XScale;
                    apntShortBalance[index].X = XLeft + index * XScale;
                    if (Configs.AccountInMoney)
                    {
                        apntLongBalance[index].Y  = (float)(YBottom - (Backtester.LongMoneyBalance(iBar) - minimum) * YScale);
                        apntShortBalance[index].Y = (float)(YBottom - (Backtester.ShortMoneyBalance(iBar) - minimum) * YScale);
                    }
                    else
                    {
                        apntLongBalance[index].Y  = YBottom - (Backtester.LongBalance(iBar) - minimum) * YScale;
                        apntShortBalance[index].Y = YBottom - (Backtester.ShortBalance(iBar) - minimum) * YScale;
                    }
                }

                index++;
            }
        }