Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Views/TradingSolutionLineChartView.cs @ 6136

Last change on this file since 6136 was 6136, checked in by gkronber, 13 years ago

#1508 improved view for trading solutions.

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Drawing;
23using System.Linq;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.Common;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using System.Collections.Generic;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("Line Chart")]
34  [Content(typeof(ITradingSolution))]
35  public partial class TradingSolutionLineChartView : ItemView, ITradingSolutionEvaluationView {
36    private const string PRICEVARIABLE_SERIES_NAME = "Price";
37    private const string SIGNALS_SERIES_NAME = "Signals";
38    private const string ASSET_SERIES_NAME = "Asset";
39
40    public new ITradingSolution Content {
41      get { return (ITradingSolution)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public TradingSolutionLineChartView()
46      : base() {
47      InitializeComponent();
48      //configure axis
49      this.chart.CustomizeAllChartAreas();
50      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
51      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
52      this.chart.ChartAreas[0].CursorX.Interval = 1;
53
54      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
55      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
56      this.chart.ChartAreas[0].CursorY.Interval = 0;
57    }
58
59    private void RedrawChart() {
60      this.chart.Series.Clear();
61      if (Content != null) {
62        //this.chart.Series.Add(PRICEVARIABLE_SERIES_NAME);
63        //this.chart.Series[PRICEVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.PriceVariable;
64        //this.chart.Series[PRICEVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
65        //this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.PriceVariable));
66
67
68        this.chart.Series.Add(SIGNALS_SERIES_NAME);
69        this.chart.Series[SIGNALS_SERIES_NAME].LegendText = SIGNALS_SERIES_NAME;
70        this.chart.Series[SIGNALS_SERIES_NAME].ChartType = SeriesChartType.FastLine;
71        this.chart.Series[SIGNALS_SERIES_NAME].Points.DataBindY(Content.Signals.ToArray());
72        this.chart.Series[SIGNALS_SERIES_NAME].Tag = Content;
73
74        IEnumerable<double> accumulatedPrice = GetAccumulatedPrices(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.PriceVariable));
75        this.chart.Series.Add(PRICEVARIABLE_SERIES_NAME);
76        this.chart.Series[PRICEVARIABLE_SERIES_NAME].LegendText = PRICEVARIABLE_SERIES_NAME;
77        this.chart.Series[PRICEVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
78        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.DataBindY(accumulatedPrice.ToArray());
79        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Tag = Content;
80
81        IEnumerable<double> profit = GetProfits(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.PriceVariable), Content.Signals, Content.ProblemData.TransactionCosts);
82        IEnumerable<double> accumulatedProfits = GetAccumulatedPrices(profit);
83        this.chart.Series.Add(ASSET_SERIES_NAME);
84        this.chart.Series[ASSET_SERIES_NAME].LegendText = ASSET_SERIES_NAME;
85        this.chart.Series[ASSET_SERIES_NAME].ChartType = SeriesChartType.FastLine;
86        this.chart.Series[ASSET_SERIES_NAME].Points.DataBindY(accumulatedProfits.ToArray());
87        this.chart.Series[ASSET_SERIES_NAME].Tag = Content;
88
89        this.UpdateStripLines();
90
91        // UpdateCursorInterval();
92      }
93    }
94
95    private IEnumerable<double> GetProfits(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost) {
96      double p = 0;
97      double iterationReturn = 0.0;
98      foreach (var signalReturn in returns.Zip(signals, (r, s) => new { Return = r, Signal = s })) {
99        double signal = signalReturn.Signal;
100        double actualReturn = signalReturn.Return;
101        if (p == 0 && signal.IsAlmost(0)) {
102        } else if (p == 0 && signal.IsAlmost(1)) {
103          p = 1;
104        } else if (p == 0 && signal.IsAlmost(-1)) {
105          p = -1;
106        } else if (p == 1 && signal.IsAlmost(1)) {
107          iterationReturn = actualReturn;
108        } else if (p == 1 && signal.IsAlmost(0)) {
109          iterationReturn = actualReturn - transactionCost;
110          p = 0;
111        } else if (p == 1 && signal.IsAlmost(-1)) {
112          iterationReturn = actualReturn - transactionCost;
113          p = -1;
114        } else if (p == -1 && signal.IsAlmost(-1)) {
115          iterationReturn = -actualReturn;
116        } else if (p == -1 && signal.IsAlmost(0)) {
117          iterationReturn = -actualReturn - transactionCost;
118          p = 0;
119        } else if (p == -1 && signal.IsAlmost(1)) {
120          iterationReturn = -actualReturn - transactionCost;
121          p = -1;
122        }
123        yield return iterationReturn;
124      }
125    }
126
127    private IEnumerable<double> GetAccumulatedPrices(IEnumerable<double> xs) {
128      double sum = 0;
129      foreach (var x in xs) {
130        sum += x;
131        yield return sum;
132      }
133    }
134
135    //private void UpdateCursorInterval() {
136    //  var estimatedValues = this.chart.Series[SIGNALS_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
137    //  var targetValues = this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
138    //  double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
139    //  double targetValuesRange = targetValues.Max() - targetValues.Min();
140    //  double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
141    //  double digits = (int)Math.Log10(interestingValuesRange) - 3;
142    //  double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
143    //  this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
144    //}
145
146    #region events
147    protected override void RegisterContentEvents() {
148      base.RegisterContentEvents();
149      Content.ModelChanged += new EventHandler(Content_ModelChanged);
150      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
151    }
152    protected override void DeregisterContentEvents() {
153      base.DeregisterContentEvents();
154      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
155      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
156    }
157
158    private void Content_ProblemDataChanged(object sender, EventArgs e) {
159      RedrawChart();
160    }
161
162    private void Content_ModelChanged(object sender, EventArgs e) {
163      RedrawChart();
164    }
165
166    protected override void OnContentChanged() {
167      base.OnContentChanged();
168      RedrawChart();
169    }
170
171    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
172      HitTestResult result = chart.HitTest(e.X, e.Y);
173      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
174                                       result.ChartElementType == ChartElementType.Gridlines) ||
175                                       result.ChartElementType == ChartElementType.StripLines) {
176        foreach (var axis in result.ChartArea.Axes)
177          axis.ScaleView.ZoomReset(int.MaxValue);
178      }
179    }
180    #endregion
181
182    private void UpdateStripLines() {
183      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
184      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
185        Content.ProblemData.TrainingPartition.Start,
186        Content.ProblemData.TrainingPartition.End);
187      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
188        Content.ProblemData.TestPartition.Start,
189        Content.ProblemData.TestPartition.End);
190    }
191
192    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
193      StripLine stripLine = new StripLine();
194      stripLine.BackColor = c;
195      stripLine.Text = title;
196      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
197      stripLine.StripWidth = end - start;
198      stripLine.IntervalOffset = start;
199      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.