Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9992 was 9992, checked in by gkronber, 11 years ago

#1508

  • disabled "Variable" symbol in default time series grammar
  • in the line chart use two different axis to display signals and prices
  • in the line chart display profits for training and test partitions separately
  • fixed hard-coded strings in SolutionView
  • added plugin dependency
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
22using System.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis.Views;
29
30namespace HeuristicLab.Problems.DataAnalysis.Trading.Views {
31  [View("Line Chart")]
32  [Content(typeof(ISolution))]
33  public partial class SolutionLineChartView : DataAnalysisSolutionEvaluationView, ISolutionEvaluationView {
34    private const string PRICEVARIABLE_SERIES_NAME = "Price";
35    private const string SIGNALS_SERIES_NAME = "Signals";
36    private const string ASSET_SERIES_NAME = "Asset";
37
38
39    public new ISolution Content {
40      get { return (ISolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public SolutionLineChartView()
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;
55      this.chart.ChartAreas[0].AxisY2.ScaleView.Zoomable = false;
56      this.chart.ChartAreas[0].CursorY.Interval = 0;
57    }
58
59    private void RedrawChart() {
60      this.chart.Series.Clear();
61      if (Content != null) {
62        var trainingRows = Content.ProblemData.TrainingIndices;
63        var testRows = Content.ProblemData.TestIndices;
64        this.chart.Series.Add(SIGNALS_SERIES_NAME);
65        this.chart.Series[SIGNALS_SERIES_NAME].LegendText = SIGNALS_SERIES_NAME;
66        this.chart.Series[SIGNALS_SERIES_NAME].ChartType = SeriesChartType.FastLine;
67        this.chart.Series[SIGNALS_SERIES_NAME].Points.DataBindY(Content.TrainingSignals.Concat(Content.TestSignals).ToArray());
68        this.chart.Series[SIGNALS_SERIES_NAME].Tag = Content;
69        this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Secondary;
70
71        IEnumerable<double> accumulatedPrice = GetAccumulatedProfits(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable));
72        this.chart.Series.Add(PRICEVARIABLE_SERIES_NAME);
73        this.chart.Series[PRICEVARIABLE_SERIES_NAME].LegendText = PRICEVARIABLE_SERIES_NAME;
74        this.chart.Series[PRICEVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
75        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.DataBindY(accumulatedPrice.ToArray());
76        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Tag = Content;
77        this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Primary;
78
79
80        IEnumerable<double> trainingProfit = OnlineProfitCalculator.GetProfits(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable, trainingRows), Content.TrainingSignals, Content.ProblemData.TransactionCosts);
81        IEnumerable<double> testProfit = OnlineProfitCalculator.GetProfits(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable, testRows), Content.TestSignals, Content.ProblemData.TransactionCosts);
82        IEnumerable<double> accTrainingProfit = GetAccumulatedProfits(trainingProfit);
83        IEnumerable<double> accTestProfit = GetAccumulatedProfits(testProfit);
84        this.chart.Series.Add(ASSET_SERIES_NAME);
85        this.chart.Series[ASSET_SERIES_NAME].LegendText = ASSET_SERIES_NAME;
86        this.chart.Series[ASSET_SERIES_NAME].ChartType = SeriesChartType.FastLine;
87        this.chart.Series[ASSET_SERIES_NAME].Points.DataBindY(accTrainingProfit.Concat(accTestProfit).ToArray());
88        this.chart.Series[ASSET_SERIES_NAME].Tag = Content;
89        this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Primary;
90
91        this.UpdateStripLines();
92      }
93    }
94
95    private IEnumerable<double> GetAccumulatedProfits(IEnumerable<double> xs) {
96      double sum = 0;
97      foreach (var x in xs) {
98        sum += x;
99        yield return sum;
100      }
101    }
102
103    #region events
104    protected override void RegisterContentEvents() {
105      base.RegisterContentEvents();
106      Content.ModelChanged += new EventHandler(Content_ModelChanged);
107      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
108    }
109    protected override void DeregisterContentEvents() {
110      base.DeregisterContentEvents();
111      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
112      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
113    }
114
115    private void Content_ProblemDataChanged(object sender, EventArgs e) {
116      RedrawChart();
117    }
118
119    private void Content_ModelChanged(object sender, EventArgs e) {
120      RedrawChart();
121    }
122
123    protected override void OnContentChanged() {
124      base.OnContentChanged();
125      RedrawChart();
126    }
127
128    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
129      HitTestResult result = chart.HitTest(e.X, e.Y);
130      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
131                                       result.ChartElementType == ChartElementType.Gridlines) ||
132                                       result.ChartElementType == ChartElementType.StripLines) {
133        foreach (var axis in result.ChartArea.Axes)
134          axis.ScaleView.ZoomReset(int.MaxValue);
135      }
136    }
137    #endregion
138
139    private void UpdateStripLines() {
140      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
141      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
142        Content.ProblemData.TrainingPartition.Start,
143        Content.ProblemData.TrainingPartition.End);
144      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
145        Content.ProblemData.TestPartition.Start,
146        Content.ProblemData.TestPartition.End);
147    }
148
149    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
150      StripLine stripLine = new StripLine();
151      stripLine.BackColor = c;
152      stripLine.Text = title;
153      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
154      stripLine.StripWidth = end - start;
155      stripLine.IntervalOffset = start;
156      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.