Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Trading.Views/3.4/SolutionLineChartView.cs @ 17369

Last change on this file since 17369 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.2 KB
RevLine 
[6123]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6123]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
[9743]22using System.Collections.Generic;
[6123]23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.MainForm;
[9743]28using HeuristicLab.Problems.DataAnalysis.Views;
[6123]29
[9743]30namespace HeuristicLab.Problems.DataAnalysis.Trading.Views {
[6123]31  [View("Line Chart")]
[9745]32  [Content(typeof(ISolution))]
33  public partial class SolutionLineChartView : DataAnalysisSolutionEvaluationView, ISolutionEvaluationView {
[6136]34    private const string PRICEVARIABLE_SERIES_NAME = "Price";
[6123]35    private const string SIGNALS_SERIES_NAME = "Signals";
[6136]36    private const string ASSET_SERIES_NAME = "Asset";
[6123]37
[9176]38
[9745]39    public new ISolution Content {
40      get { return (ISolution)base.Content; }
[6123]41      set { base.Content = value; }
42    }
43
[9745]44    public SolutionLineChartView()
[6123]45      : base() {
46      InitializeComponent();
47      //configure axis
48      this.chart.CustomizeAllChartAreas();
49      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
50      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
51      this.chart.ChartAreas[0].CursorX.Interval = 1;
52
53      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
54      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
[9997]55      this.chart.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
[9992]56      this.chart.ChartAreas[0].AxisY2.ScaleView.Zoomable = false;
[9997]57      this.chart.ChartAreas[0].AxisY2.IntervalAutoMode = IntervalAutoMode.VariableCount;
58      this.chart.ChartAreas[0].AxisY2.LabelStyle.Enabled = false;
59      this.chart.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
60      this.chart.ChartAreas[0].AxisY2.MinorGrid.Enabled = false;
61      this.chart.ChartAreas[0].AxisY2.MajorTickMark.Enabled = false;
62      this.chart.ChartAreas[0].AxisY2.MinorTickMark.Enabled = false;
[6123]63      this.chart.ChartAreas[0].CursorY.Interval = 0;
64    }
65
66    private void RedrawChart() {
67      this.chart.Series.Clear();
68      if (Content != null) {
[9992]69        var trainingRows = Content.ProblemData.TrainingIndices;
70        var testRows = Content.ProblemData.TestIndices;
[6123]71        this.chart.Series.Add(SIGNALS_SERIES_NAME);
[9997]72        this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Secondary;
[6136]73        this.chart.Series[SIGNALS_SERIES_NAME].LegendText = SIGNALS_SERIES_NAME;
[6123]74        this.chart.Series[SIGNALS_SERIES_NAME].ChartType = SeriesChartType.FastLine;
[9997]75        this.chart.Series[SIGNALS_SERIES_NAME].Points.DataBindXY(
76          trainingRows.Concat(testRows).ToArray(),
77          Content.TrainingSignals.Concat(Content.TestSignals).ToArray());
[6123]78        this.chart.Series[SIGNALS_SERIES_NAME].Tag = Content;
[6136]79
[9997]80        var trainingPriceChanges = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable,
81                                                                               trainingRows);
82        var testPriceChanges = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable,
83                                                                               testRows);
84        IEnumerable<double> accumulatedTrainingPrice = GetAccumulatedProfits(trainingPriceChanges);
85        IEnumerable<double> accumulatedTestPrice = GetAccumulatedProfits(testPriceChanges);
[6136]86        this.chart.Series.Add(PRICEVARIABLE_SERIES_NAME);
[9997]87        this.chart.Series[PRICEVARIABLE_SERIES_NAME].YAxisType = AxisType.Primary;
[6136]88        this.chart.Series[PRICEVARIABLE_SERIES_NAME].LegendText = PRICEVARIABLE_SERIES_NAME;
89        this.chart.Series[PRICEVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
[9997]90        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.DataBindXY(
91          trainingRows.Concat(testRows).ToArray(),
92          accumulatedTrainingPrice.Concat(accumulatedTestPrice).ToArray());
[6136]93        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Tag = Content;
94
[9992]95
[9997]96        IEnumerable<double> trainingProfit = OnlineProfitCalculator.GetProfits(trainingPriceChanges, Content.TrainingSignals, Content.ProblemData.TransactionCosts);
97        IEnumerable<double> testProfit = OnlineProfitCalculator.GetProfits(testPriceChanges, Content.TestSignals, Content.ProblemData.TransactionCosts);
[9992]98        IEnumerable<double> accTrainingProfit = GetAccumulatedProfits(trainingProfit);
99        IEnumerable<double> accTestProfit = GetAccumulatedProfits(testProfit);
[6136]100        this.chart.Series.Add(ASSET_SERIES_NAME);
[9997]101        this.chart.Series[ASSET_SERIES_NAME].YAxisType = AxisType.Primary;
[6136]102        this.chart.Series[ASSET_SERIES_NAME].LegendText = ASSET_SERIES_NAME;
103        this.chart.Series[ASSET_SERIES_NAME].ChartType = SeriesChartType.FastLine;
[9997]104        this.chart.Series[ASSET_SERIES_NAME].Points.DataBindXY(
105          trainingRows.Concat(testRows).ToArray(),
106          accTrainingProfit.Concat(accTestProfit).ToArray());
[6136]107        this.chart.Series[ASSET_SERIES_NAME].Tag = Content;
108
109        this.UpdateStripLines();
[6123]110      }
111    }
112
[9992]113    private IEnumerable<double> GetAccumulatedProfits(IEnumerable<double> xs) {
[6136]114      double sum = 0;
115      foreach (var x in xs) {
116        sum += x;
117        yield return sum;
118      }
119    }
120
[6123]121    #region events
122    protected override void RegisterContentEvents() {
123      base.RegisterContentEvents();
124      Content.ModelChanged += new EventHandler(Content_ModelChanged);
125      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
126    }
127    protected override void DeregisterContentEvents() {
128      base.DeregisterContentEvents();
129      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
130      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
131    }
132
133    private void Content_ProblemDataChanged(object sender, EventArgs e) {
134      RedrawChart();
135    }
136
137    private void Content_ModelChanged(object sender, EventArgs e) {
[6136]138      RedrawChart();
[6123]139    }
140
141    protected override void OnContentChanged() {
142      base.OnContentChanged();
143      RedrawChart();
144    }
145
146    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
147      HitTestResult result = chart.HitTest(e.X, e.Y);
148      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
149                                       result.ChartElementType == ChartElementType.Gridlines) ||
150                                       result.ChartElementType == ChartElementType.StripLines) {
151        foreach (var axis in result.ChartArea.Axes)
152          axis.ScaleView.ZoomReset(int.MaxValue);
153      }
154    }
155    #endregion
156
157    private void UpdateStripLines() {
158      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
159      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
160        Content.ProblemData.TrainingPartition.Start,
161        Content.ProblemData.TrainingPartition.End);
162      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
163        Content.ProblemData.TestPartition.Start,
164        Content.ProblemData.TestPartition.End);
165    }
166
167    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
168      StripLine stripLine = new StripLine();
169      stripLine.BackColor = c;
170      stripLine.Text = title;
171      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
172      stripLine.StripWidth = end - start;
173      stripLine.IntervalOffset = start;
174      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.