Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/CreateTimeSeriesColumnsCommand.cs @ 15016

Last change on this file since 15016 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 10.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
21
22using System.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms.DataVisualization.Charting;
25using HeuristicLab.DataImporter.Data;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableClass]
32  [ViewableCommandInfo("Create time series columns", 1, ColumnGroupState.DoubleColumnSelected, "Column Commands",
33    Position = 16, SelectedColumns = 4)]
34  public class CreateTimeSeriesColumnsCommand : ColumnGroupCommandWithAffectedColumnsBase {
35    private int addedColumnsCount;
36    [StorableConstructor]
37    protected CreateTimeSeriesColumnsCommand(bool deserializing) : base(deserializing) { }
38    public CreateTimeSeriesColumnsCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
39      : base(dataSet, columnGroupName, affectedColumns) {
40    }
41
42    public override string Description {
43      get { return "Create time series columns"; }
44    }
45
46    public override void Execute() {
47      base.Execute();
48      if (AffectedColumns.Length != 4) throw new CommandExecutionException("The selected columns must include high, low, open, and close price.", this);
49      string[] seriesNames = { "high", "low", "open", "close" };
50      Chart chart1 = new Chart();
51      // Y = high
52      // Y2 = low
53      // Y3 = open
54      // Y4 = close
55      List<DoubleColumn> columns = new List<DoubleColumn>();
56      for (int i = 0; i < 4; i++) {
57        int columnIndex = AffectedColumns[i];
58        DoubleColumn doubleColumn = ColumnGroup.GetColumn(columnIndex) as DoubleColumn;
59        if (doubleColumn == null) throw new CommandExecutionException("The selection column does not contain double columns.", this);
60        columns.Add(doubleColumn);
61      }
62      Series series = new Series();
63      series.ChartArea = "Default";
64      series.ChartType = SeriesChartType.Stock;
65      series.Name = "Input";
66      series.YValuesPerPoint = 4;
67      for (int i = 0; i < columns[0].TotalValuesCount; i++) {
68        DataPoint point = new DataPoint();
69        List<double> values = new List<double>();
70        for (int j = 0; j < 4; j++) {
71          double? v = (double?)columns[j].GetValue(i);
72          if (!v.HasValue) throw new CommandExecutionException("Columns must not contain missing values.", this);
73          values.Add(v.Value);
74        }
75        point.YValues = values.ToArray();
76        series.Points.AddXY(i, values[0]);
77        series.Points[i].YValues[1] = values[1];
78        series.Points[i].YValues[2] = values[2];
79        series.Points[i].YValues[3] = values[3];
80      }
81      chart1.Series.Add(series);
82
83
84      DoubleColumn firstAffectedColumn = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[0]);
85      int lastAffectedColumn = AffectedColumns[3];
86
87      // formulas using only the close price
88      FinancialFormula[] formulae = new FinancialFormula[] {
89        FinancialFormula.DetrendedPriceOscillator,
90        FinancialFormula.MovingAverageConvergenceDivergence,
91        FinancialFormula.Performance,
92        FinancialFormula.RateOfChange,
93        FinancialFormula.RelativeStrengthIndex,
94        FinancialFormula.StandardDeviation };
95      int currentColumnIndex = lastAffectedColumn + 1;
96      addedColumnsCount = 0;
97      foreach (var formula in formulae) {
98        chart1.DataManipulator.FinancialFormula(formula, "10", "Input:Y4", "Indicators");
99
100        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
101        newColumn.Name = formula + "(" + firstAffectedColumn.Name + ")";
102        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
103          newColumn.AddValue(null);
104        }
105        foreach (var value in chart1.Series["Indicators"].Points) {
106          newColumn.AddValue(value.YValues[0]);
107        }
108        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
109        currentColumnIndex++;
110        addedColumnsCount++;
111      }
112
113      // Formulas with two input value
114      formulae = new FinancialFormula[] {
115        FinancialFormula.MassIndex,
116        FinancialFormula.VolatilityChaikins
117      };
118      foreach (var formula in formulae) {
119        chart1.DataManipulator.FinancialFormula(formula, "20", "Input:Y,Input:Y2", "Indicators");
120
121        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
122        newColumn.Name = formula + "(" + firstAffectedColumn.Name + ")";
123        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
124          newColumn.AddValue(null);
125        }
126        foreach (var value in chart1.Series["Indicators"].Points) {
127          newColumn.AddValue(value.YValues[0]);
128        }
129        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
130        currentColumnIndex++;
131        addedColumnsCount++;
132      }
133
134      // Williams %R
135      {
136        chart1.DataManipulator.FinancialFormula(FinancialFormula.WilliamsR, "Input:Y,Input:Y2,Input:Y4", "Indicators");
137        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
138        newColumn.Name = "WilliamsR(" + firstAffectedColumn.Name + ")";
139        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
140          newColumn.AddValue(null);
141        }
142        foreach (var value in chart1.Series["Indicators"].Points) {
143          newColumn.AddValue(value.YValues[0]);
144        }
145        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
146        currentColumnIndex++;
147        addedColumnsCount++;
148      }
149
150      // StochasticIndicator
151      {
152        chart1.DataManipulator.FinancialFormula(FinancialFormula.StochasticIndicator, "15", "Input:Y,Input:Y2,Input:Y4", "Indicators,SMA");
153        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
154        newColumn.Name = "StochasticIndicator(" + firstAffectedColumn.Name + ")";
155        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
156          newColumn.AddValue(null);
157        }
158        foreach (var value in chart1.Series["Indicators"].Points) {
159          newColumn.AddValue(value.YValues[0]);
160        }
161        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
162        currentColumnIndex++;
163        addedColumnsCount++;
164
165        DoubleColumn newColumnSma = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
166        newColumnSma.Name = "StochasticIndicator-SMA(" + firstAffectedColumn.Name + ")";
167        for (int i = 0; i < chart1.Series["SMA"].Points.First().XValue; i++) {
168          newColumnSma.AddValue(null);
169        }
170        foreach (var value in chart1.Series["SMA"].Points) {
171          newColumnSma.AddValue(value.YValues[0]);
172        }
173        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumnSma);
174        currentColumnIndex++;
175        addedColumnsCount++;
176      }
177
178
179      // All other formulas.
180      formulae = new FinancialFormula[] {
181        FinancialFormula.AverageTrueRange,
182        FinancialFormula.CommodityChannelIndex,
183      };
184      foreach (var formula in formulae) {
185        chart1.DataManipulator.FinancialFormula(formula, "Input:Y,Input:Y2,Input:Y4", "Indicators");
186        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
187        newColumn.Name = formula + "(" + firstAffectedColumn.Name + ")";
188        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
189          newColumn.AddValue(null);
190        }
191        foreach (var value in chart1.Series["Indicators"].Points) {
192          newColumn.AddValue(value.YValues[0]);
193        }
194        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
195        currentColumnIndex++;
196        addedColumnsCount++;
197      }
198
199      // moving averages.
200      formulae = new FinancialFormula[] {
201        FinancialFormula.ExponentialMovingAverage,
202        FinancialFormula.MovingAverage,
203        FinancialFormula.TriangularMovingAverage,
204        FinancialFormula.TripleExponentialMovingAverage,
205        FinancialFormula.WeightedMovingAverage,
206      };
207      foreach (var formula in formulae) {
208        chart1.DataManipulator.FinancialFormula(formula, "15", "Input:Y4", "Indicators");
209        DoubleColumn newColumn = (DoubleColumn)firstAffectedColumn.CreateCopyOfColumnWithoutValues();
210        newColumn.Name = formula + "(" + firstAffectedColumn.Name + ")";
211        for (int i = 0; i < chart1.Series["Indicators"].Points.First().XValue; i++) {
212          newColumn.AddValue(null);
213        }
214        foreach (var value in chart1.Series["Indicators"].Points) {
215          newColumn.AddValue(value.YValues[0]);
216        }
217        this.ColumnGroup.InsertColumn(currentColumnIndex, newColumn);
218        currentColumnIndex++;
219        addedColumnsCount++;
220      }
221
222      ColumnGroup.FireChanged();
223    }
224
225    public override void UndoExecute() {
226      base.UndoExecute();
227      int lastAffectedColumnIndex = AffectedColumns[3];
228      for (int i = 0; i < addedColumnsCount; i++)
229        this.ColumnGroup.RemoveColumn(lastAffectedColumnIndex + 1);
230      addedColumnsCount = 0;
231      ColumnGroup.FireChanged();
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.