/// <summary> /// Sets the chart parameters /// </summary> private void InitChart(int width, int height) { try { 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); } catch { } }
/// <summary> /// Exports the bar summary /// </summary> public void ExportBarSummary() { string stage = String.Empty; if (Data.IsProgramBeta) { stage = " " + Language.T("Beta"); } else if (Data.IsProgramReleaseCandidate) { stage = " " + "RC"; } string ff = Data.Ff; // Format modifier to print float numbers string df = Data.Df; // Format modifier to print date var sb = new StringBuilder(); 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 + Environment.NewLine); sb.Append(Data.Symbol + " " + Data.PeriodString + "; Values in points" + 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) + "\t"); sb.Append(Data.Time[bar].ToString(df) + "\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] + "\t"); if (Backtester.IsPos(bar)) { sb.Append(Backtester.SummaryDir(bar) + "\t"); sb.Append(Backtester.SummaryLots(bar) + "\t"); sb.Append(Backtester.SummaryTrans(bar) + "\t"); sb.Append(Backtester.SummaryPrice(bar).ToString(ff) + "\t"); sb.Append(Backtester.ProfitLoss(bar) + "\t"); sb.Append(Backtester.FloatingPL(bar) + "\t"); } else { sb.Append("\t\t\t\t\t\t"); } sb.Append(Backtester.ChargedSpread(bar) + "\t"); sb.Append(Backtester.ChargedRollOver(bar) + "\t"); sb.Append(Backtester.Balance(bar) + "\t"); sb.Append(Backtester.Equity(bar) + "\t"); sb.Append(Backtester.BackTestEvalToString(bar) + "\t"); sb.Append(Environment.NewLine); } string fileName = Data.Strategy.StrategyName + "-" + Data.Symbol + "-" + Data.Period.ToString(); SaveData(fileName, sb); }
/// <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); } }