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