public static void ExportStrategyToIndicator() { var sbLong = new StringBuilder(); var sbShort = new StringBuilder(); for (int bar = Data.FirstBar; bar < Data.Bars; bar++) { for (int pos = 0; pos < Backtester.Positions(bar); pos++) { if (Backtester.PosDir(bar, pos) == PosDirection.Long) { sbLong.AppendLine(" \""+ Data.Time[bar] + "\","); } if (Backtester.PosDir(bar, pos) == PosDirection.Short) { sbShort.AppendLine(" \""+ Data.Time[bar] + "\","); } } } string strategy = Resources.StrategyToIndicator; strategy = strategy.Replace("#MODIFIED#", DateTime.Now.ToString(CultureInfo.InvariantCulture)); strategy = strategy.Replace("#INSTRUMENT#", Data.Symbol); strategy = strategy.Replace("#BASEPERIOD#", Data.DataPeriodToString(Data.Period)); strategy = strategy.Replace("#STARTDATE#", Data.Time[Data.FirstBar].ToString(CultureInfo.InvariantCulture)); strategy = strategy.Replace("#ENDDATE#", Data.Time[Data.Bars - 1].ToString(CultureInfo.InvariantCulture)); strategy = strategy.Replace("#PERIODMINUTES#", ((int)Data.Period).ToString(CultureInfo.InvariantCulture)); strategy = strategy.Replace("#LISTLONG#", sbLong.ToString()); strategy = strategy.Replace("#LISTSHORT#", sbShort.ToString()); var savedlg = new SaveFileDialog { InitialDirectory = Data.SourceFolder, AddExtension = true, Title = Language.T("Custom Indicators"), Filter = Language.T("Custom Indicators") + " (*.cs)|*.cs" }; if (savedlg.ShowDialog() == DialogResult.OK) { strategy = strategy.Replace("#INDICATORNAME#", Path.GetFileNameWithoutExtension(savedlg.FileName)); var sw = new StreamWriter(savedlg.FileName); try { sw.Write(strategy); } catch (Exception exc) { MessageBox.Show(exc.Message, Language.T("Custom Indicators")); } finally { sw.Close(); } } }
/// <summary> /// Loads the data. /// </summary> private void LoadData(BackgroundWorker worker) { int periodsToLoad = 0; int allPeriods = Enum.GetValues(typeof(DataPeriod)).Length; Data.IntraBars = new int[allPeriods]; Data.IntraBarData = new Bar[Data.Bars][]; Data.IntraBarBars = new int[Data.Bars]; Data.IntraBarsPeriods = new DataPeriod[Data.Bars]; Data.LoadedIntraBarPeriods = 0; for (int bar = 0; bar < Data.Bars; bar++) { Data.IntraBarsPeriods[bar] = Data.Period; Data.IntraBarBars[bar] = 0; } // Counts how many periods to load for (int prd = 0; prd < allPeriods; prd++) { var period = (DataPeriod)Enum.GetValues(typeof(DataPeriod)).GetValue(prd); if (period < Data.Period) { periodsToLoad++; } } // Load the intrabar data (Starts from 1 Min) for (int prd = 0; prd < allPeriods && isLoadingNow; prd++) { if (worker.CancellationPending) { break; } int loadedBars = 0; var period = (DataPeriod)Enum.GetValues(typeof(DataPeriod)).GetValue(prd); SetLabelProgressText(Language.T("Loading:") + " " + Data.DataPeriodToString(period) + "..."); if (period < Data.Period) { loadedBars = LoadIntrabarData(period); if (loadedBars > 0) { Data.IsIntrabarData = true; Data.LoadedIntraBarPeriods++; } } else if (period == Data.Period) { loadedBars = Data.Bars; Data.LoadedIntraBarPeriods++; } Data.IntraBars[prd] = loadedBars; // Report progress as a percentage of the total task. int percentComplete = periodsToLoad > 0 ? 100 * (prd + 1) / periodsToLoad : 100; percentComplete = percentComplete > 100 ? 100 : percentComplete; if (percentComplete > progressPercent) { progressPercent = percentComplete; worker.ReportProgress(percentComplete); } } CheckIntrabarData(); RepairIntrabarData(); if (Configs.UseTickData) { SetLabelProgressText(Language.T("Loading:") + " " + Language.T("Ticks") + "..."); worker.ReportProgress(200); try { LoadTickData(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// Loads the Intrabar data. /// </summary> private int LoadIntrabarData(DataPeriod period) { var instrument = new Instrument(Data.InstrProperties.Clone(), (int)period) { DataDir = Data.OfflineDataDir, MaxBars = Configs.MaxIntraBars }; // Loads the data int loadingResult = instrument.LoadData(); int loadedIntrabars = instrument.Bars; if (loadingResult == 0 && loadedIntrabars > 0) { if (Data.Period != DataPeriod.W1) { if (instrument.DaysOff > 5) { warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " + Data.DataPeriodToString(period) + " - " + Language.T("Maximum days off:") + " " + instrument.DaysOff; } if (Data.Update - instrument.Update > new TimeSpan(24, 0, 0)) { warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " + Data.DataPeriodToString(period) + " - " + Language.T("Updated on:") + " " + instrument.Update.ToString(CultureInfo.InvariantCulture); } } int startBigBar; for (startBigBar = 0; startBigBar < Data.Bars; startBigBar++) { if (Data.Time[startBigBar] >= instrument.Time(0)) { break; } } int stopBigBar; for (stopBigBar = startBigBar; stopBigBar < Data.Bars; stopBigBar++) { if (Data.IntraBarsPeriods[stopBigBar] != Data.Period) { break; } } // Seek for a place to put the intrabars. int lastIntraBar = 0; for (int bar = startBigBar; bar < stopBigBar; bar++) { Data.IntraBarData[bar] = new Bar[(int)Data.Period / (int)period]; DateTime endTime = Data.Time[bar] + new TimeSpan(0, (int)Data.Period, 0); int indexBar = 0; for (int intrabar = lastIntraBar; intrabar < loadedIntrabars && instrument.Time(intrabar) < endTime; intrabar++) { if (instrument.Time(intrabar) >= Data.Time[bar]) { Data.IntraBarData[bar][indexBar].Time = instrument.Time(intrabar); Data.IntraBarData[bar][indexBar].Open = instrument.Open(intrabar); Data.IntraBarData[bar][indexBar].High = instrument.High(intrabar); Data.IntraBarData[bar][indexBar].Low = instrument.Low(intrabar); Data.IntraBarData[bar][indexBar].Close = instrument.Close(intrabar); Data.IntraBarsPeriods[bar] = period; Data.IntraBarBars[bar]++; indexBar++; lastIntraBar = intrabar; } } } } return(loadedIntrabars); }
/// <summary> /// Repaint the panel Info /// </summary> private void PnlInfoPaint(object sender, PaintEventArgs e) { // +------------------------------------------------------+ // | Data | // |------------------- ----------------------------------+ // | Period | Bars | From | Until | Cover | % | Label | // |------------------------------------------------------+ //xp0 xp1 xp2 xp3 xp4 xp5 xp6 xp7 Graphics g = e.Graphics; g.Clear(LayoutColors.ColorControlBack); if (!Data.IsData || !Data.IsResult) { return; } var pnl = (Panel)sender; const int border = 2; const int xp0 = border; const int xp1 = 80; const int xp2 = 140; const int xp3 = 200; const int xp4 = 260; const int xp5 = 320; const int xp6 = 370; int xp7 = pnl.ClientSize.Width - border; var size = new Size(xp7 - xp0, infoRowHeight); var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Near }; // Caption background var pntStart = new PointF(0, 0); SizeF szfCaption = new Size(pnl.ClientSize.Width - 0, 2 * infoRowHeight); var rectfCaption = new RectangleF(pntStart, szfCaption); Data.GradientPaint(g, rectfCaption, LayoutColors.ColorCaptionBack, LayoutColors.DepthCaption); // Caption Text var stringFormatCaption = new StringFormat { LineAlignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter, FormatFlags = StringFormatFlags.NoWrap, Alignment = StringAlignment.Near }; string stringCaptionText = Language.T("Intrabar Data"); float captionWidth = Math.Min(InfoPanel.ClientSize.Width, xp7 - xp0); float captionTextWidth = g.MeasureString(stringCaptionText, fontInfo).Width; float captionTextX = Math.Max((captionWidth - captionTextWidth) / 2f, 0); var pfCaptionText = new PointF(captionTextX, 0); var sfCaptionText = new SizeF(captionWidth - captionTextX, infoRowHeight); rectfCaption = new RectangleF(pfCaptionText, sfCaptionText); Brush brush = new SolidBrush(LayoutColors.ColorCaptionText); // First caption row g.DrawString(stringCaptionText, fontInfo, brush, rectfCaption, stringFormatCaption); // Second title row g.DrawString(Language.T("Period"), fontInfo, brush, (xp1 + xp0) / 2f, infoRowHeight, sf); g.DrawString(Language.T("Bars"), fontInfo, brush, (xp2 + xp1) / 2f, infoRowHeight, sf); g.DrawString(Language.T("From"), fontInfo, brush, (xp3 + xp2) / 2f, infoRowHeight, sf); g.DrawString(Language.T("Until"), fontInfo, brush, (xp4 + xp3) / 2f, infoRowHeight, sf); g.DrawString(Language.T("Coverage"), fontInfo, brush, (xp5 + xp4) / 2f, infoRowHeight, sf); g.DrawString("%", fontInfo, brush, (xp6 + xp5) / 2f, infoRowHeight, sf); g.DrawString(Language.T("Label"), fontInfo, brush, (xp7 + xp6) / 2f, infoRowHeight, sf); brush = new SolidBrush(LayoutColors.ColorControlText); int allPeriods = Enum.GetValues(typeof(DataPeriod)).Length; for (int period = 0; period <= allPeriods; period++) { int y = (period + 2) * infoRowHeight; var point = new Point(xp0, y); if (Math.Abs(period % 2f - 0) > 0.0001) { g.FillRectangle(new SolidBrush(LayoutColors.ColorEvenRowBack), new Rectangle(point, size)); } } // Tick statistics if (isTickDataFile) { g.DrawString(Language.T("Tick"), fontInfo, brush, (xp1 + xp0) / 2, 2 * infoRowHeight, sf); if (Data.IsTickData && Configs.UseTickData) { int firstBarWithTicks = -1; int lastBarWithTicks = -1; int tickBars = 0; for (int b = 0; b < Data.Bars; b++) { if (firstBarWithTicks == -1 && Data.TickData[b] != null) { firstBarWithTicks = b; } if (Data.TickData[b] != null) { lastBarWithTicks = b; tickBars++; } } double percentage = 100d * tickBars / Data.Bars; int y = 2 * infoRowHeight; string ticks = (Data.Ticks > 999999) ? (Data.Ticks / 1000).ToString(CultureInfo.InvariantCulture) + "K" : Data.Ticks.ToString(CultureInfo.InvariantCulture); g.DrawString(ticks, fontInfo, brush, (xp2 + xp1) / 2, y, sf); g.DrawString((firstBarWithTicks + 1).ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp3 + xp2) / 2, y, sf); g.DrawString((lastBarWithTicks + 1).ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp4 + xp3) / 2, y, sf); g.DrawString(tickBars.ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp5 + xp4) / 2, y, sf); g.DrawString(percentage.ToString("F2"), fontInfo, brush, (xp6 + xp5) / 2, y, sf); var rectf = new RectangleF(xp6 + 10, y + 4, xp7 - xp6 - 20, 9); Data.GradientPaint(g, rectf, Data.PeriodColor[DataPeriod.M1], 60); rectf = new RectangleF(xp6 + 10, y + 7, xp7 - xp6 - 20, 3); Data.GradientPaint(g, rectf, Data.PeriodColor[DataPeriod.D1], 60); } } for (int prd = 0; prd < allPeriods; prd++) { int startY = isTickDataFile ? 3 : 2; int y = (prd + startY) * infoRowHeight; var period = (DataPeriod)Enum.GetValues(typeof(DataPeriod)).GetValue(prd); int intraBars = Data.IntraBars == null || !Data.IsIntrabarData ? 0 : Data.IntraBars[prd]; int fromBar = 0; int untilBar = 0; int coveredBars = 0; double percentage = 0; bool isMultyAreas = false; if (intraBars > 0) { bool isFromBarFound = false; bool isUntilBarFound = false; untilBar = Data.Bars; for (int bar = 0; bar < Data.Bars; bar++) { if (!isFromBarFound && Data.IntraBarsPeriods[bar] == period) { fromBar = bar; isFromBarFound = true; } if (isFromBarFound && !isUntilBarFound && (Data.IntraBarsPeriods[bar] != period || bar == Data.Bars - 1)) { if (bar < Data.Bars - 1) { isUntilBarFound = true; untilBar = bar; } else { untilBar = Data.Bars; } coveredBars = untilBar - fromBar; } if (isFromBarFound && isUntilBarFound && Data.IntraBarsPeriods[bar] == period) { isMultyAreas = true; coveredBars++; } } if (isFromBarFound) { percentage = 100d * coveredBars / Data.Bars; fromBar++; } else { fromBar = 0; untilBar = 0; coveredBars = 0; percentage = 0; } } else if (period == Data.Period) { intraBars = Data.Bars; fromBar = 1; untilBar = Data.Bars; coveredBars = Data.Bars; percentage = 100; } g.DrawString(Data.DataPeriodToString(period), fontInfo, brush, (xp1 + xp0) / 2, y, sf); if (coveredBars > 0 || period == Data.Period) { g.DrawString(intraBars.ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp2 + xp1) / 2, y, sf); g.DrawString(fromBar.ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp3 + xp2) / 2, y, sf); g.DrawString(untilBar.ToString(CultureInfo.InvariantCulture), fontInfo, brush, (xp4 + xp3) / 2, y, sf); g.DrawString(coveredBars.ToString(CultureInfo.InvariantCulture) + (isMultyAreas ? "*" : ""), fontInfo, brush, (xp5 + xp4) / 2, y, sf); g.DrawString(percentage.ToString("F2"), fontInfo, brush, (xp6 + xp5) / 2, y, sf); var rectf = new RectangleF(xp6 + 10, y + 4, xp7 - xp6 - 20, 9); Data.GradientPaint(g, rectf, Data.PeriodColor[period], 60); } } var penLine = new Pen(LayoutColors.ColorJournalLines); g.DrawLine(penLine, xp1, 2 * infoRowHeight, xp1, pnl.ClientSize.Height); g.DrawLine(penLine, xp2, 2 * infoRowHeight, xp2, pnl.ClientSize.Height); g.DrawLine(penLine, xp3, 2 * infoRowHeight, xp3, pnl.ClientSize.Height); g.DrawLine(penLine, xp4, 2 * infoRowHeight, xp4, pnl.ClientSize.Height); g.DrawLine(penLine, xp5, 2 * infoRowHeight, xp5, pnl.ClientSize.Height); g.DrawLine(penLine, xp6, 2 * infoRowHeight, xp6, pnl.ClientSize.Height); // Border var penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption), border); g.DrawLine(penBorder, 1, 2 * infoRowHeight, 1, pnl.ClientSize.Height); g.DrawLine(penBorder, pnl.ClientSize.Width - border + 1, 2 * infoRowHeight, pnl.ClientSize.Width - border + 1, pnl.ClientSize.Height); g.DrawLine(penBorder, 0, pnl.ClientSize.Height - border + 1, pnl.ClientSize.Width, pnl.ClientSize.Height - border + 1); }