[6123] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9743] | 3 | * Copyright (C) 2002-2013 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
|
---|
| 21 | using System;
|
---|
[9743] | 22 | using System.Collections.Generic;
|
---|
[6123] | 23 | using System.Drawing;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[9743] | 28 | using HeuristicLab.Problems.DataAnalysis.Views;
|
---|
[6123] | 29 |
|
---|
[9743] | 30 | namespace 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;
|
---|
[9992] | 55 | this.chart.ChartAreas[0].AxisY2.ScaleView.Zoomable = false;
|
---|
[6123] | 56 | this.chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private void RedrawChart() {
|
---|
| 60 | this.chart.Series.Clear();
|
---|
| 61 | if (Content != null) {
|
---|
[9992] | 62 | var trainingRows = Content.ProblemData.TrainingIndices;
|
---|
| 63 | var testRows = Content.ProblemData.TestIndices;
|
---|
[6123] | 64 | this.chart.Series.Add(SIGNALS_SERIES_NAME);
|
---|
[6136] | 65 | this.chart.Series[SIGNALS_SERIES_NAME].LegendText = SIGNALS_SERIES_NAME;
|
---|
[6123] | 66 | this.chart.Series[SIGNALS_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[9992] | 67 | this.chart.Series[SIGNALS_SERIES_NAME].Points.DataBindY(Content.TrainingSignals.Concat(Content.TestSignals).ToArray());
|
---|
[6123] | 68 | this.chart.Series[SIGNALS_SERIES_NAME].Tag = Content;
|
---|
[9992] | 69 | this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Secondary;
|
---|
[6136] | 70 |
|
---|
[9992] | 71 | IEnumerable<double> accumulatedPrice = GetAccumulatedProfits(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceChangeVariable));
|
---|
[6136] | 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;
|
---|
[9992] | 77 | this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Primary;
|
---|
[6136] | 78 |
|
---|
[9992] | 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);
|
---|
[6136] | 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;
|
---|
[9992] | 87 | this.chart.Series[ASSET_SERIES_NAME].Points.DataBindY(accTrainingProfit.Concat(accTestProfit).ToArray());
|
---|
[6136] | 88 | this.chart.Series[ASSET_SERIES_NAME].Tag = Content;
|
---|
[9992] | 89 | this.chart.Series[SIGNALS_SERIES_NAME].YAxisType = AxisType.Primary;
|
---|
[6136] | 90 |
|
---|
| 91 | this.UpdateStripLines();
|
---|
[6123] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[9992] | 95 | private IEnumerable<double> GetAccumulatedProfits(IEnumerable<double> xs) {
|
---|
[6136] | 96 | double sum = 0;
|
---|
| 97 | foreach (var x in xs) {
|
---|
| 98 | sum += x;
|
---|
| 99 | yield return sum;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[6123] | 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) {
|
---|
[6136] | 120 | RedrawChart();
|
---|
[6123] | 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 | }
|
---|