Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/05/11 19:45:02 (14 years ago)
Author:
gkronber
Message:

#1471 fixed bug in data importer. improved preprocessing of financial time series.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/CreateTimeSeriesColumnsCommand.cs

    r6134 r6137  
    3333namespace HeuristicLab.DataImporter.Command {
    3434  [StorableClass]
    35   [ViewableCommandInfo("Create time series columns", 1, ColumnGroupState.ColumnSelected, "Column Commands",
    36     Position = 16, SelectedColumns = 1)]
    37   public class CreateTimeSeriesColumnsCommand : ColumnCommandBase {
     35  [ViewableCommandInfo("Create time series columns", 1, ColumnGroupState.DoubleColumnSelected, "Column Commands",
     36    Position = 16, SelectedColumns = 4)]
     37  public class CreateTimeSeriesColumnsCommand : ColumnGroupCommandWithAffectedColumnsBase {
    3838    private int addedColumnsCount;
    39     private const double SETVALUE = 1.0;
    40     private const double NOTSETVALUE = 0.0;
    41 
    42     private CreateTimeSeriesColumnsCommand()
    43       : base(null, string.Empty, -1) {
    44     }
    45 
    46     public CreateTimeSeriesColumnsCommand(DataSet dataSet, string columnGroupName, int columnIndex)
    47       : base(dataSet, columnGroupName, columnIndex) {
     39
     40    public CreateTimeSeriesColumnsCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
     41      : base(dataSet, columnGroupName, affectedColumns) {
    4842    }
    4943
     
    5448    public override void Execute() {
    5549      base.Execute();
    56 
    57       //if (Column.NullValuesCount != 0)
    58       //  throw new CommandExecutionException("Categorial column must not contain null values.", this);
    59 
    60       //int columnIndex = this.ColumnGroup.IndexOfColumn(Column);
    61       //Dictionary<string, DoubleColumn> addedColumns = new Dictionary<string, DoubleColumn>();
    62       //foreach (string value in Column.ValuesEnumerable.Cast<IComparable>().Distinct().OrderBy(c => c).Select(o => o.ToString())) {
    63       //  DoubleColumn doubleColumn = new DoubleColumn(Column.Name + " " + value.ToString());
    64       //  addedColumns[value] = doubleColumn;
    65       //  ColumnGroup.InsertColumn(columnIndex + addedColumnsCount + 1, doubleColumn);
    66       //  addedColumnsCount++;
    67       //}
    68 
    69       //for (int row = 0; row < ColumnGroup.RowCount; row++) {
    70       //  string value = Column.GetValue(row).ToString();
    71       //  foreach (KeyValuePair<string, DoubleColumn> pair in addedColumns) {
    72       //    if (pair.Key == value)
    73       //      pair.Value.AddValue(SETVALUE);
    74       //    else
    75       //      pair.Value.AddValue(NOTSETVALUE);
    76       //  }
    77       //}
    78 
    79       // Set Formula Name
     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" };
    8052      Chart chart1 = new Chart();
    81       string formulaName = "DetrendedPriceOscillator";
    82 
    83       FinancialFormula formula = (FinancialFormula)Enum.Parse(typeof(FinancialFormula), formulaName, true);
    84      
    85       // Formulas with one input value
    86       if (formulaName == "DetrendedPriceOscillator" || formulaName == "MovingAverageConvergenceDivergence" || formulaName == "Performance" || formulaName == "RateOfChange"
    87       || formulaName == "TripleExponentialMovingAverage") {
     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) {
    88100        chart1.DataManipulator.FinancialFormula(formula, "10", "Input:Y4", "Indicators");
    89       }
    90 
    91       // Relative Strength Index
    92       else if (formulaName == "RelativeStrengthIndex") {
    93 
    94         chart1.DataManipulator.FinancialFormula(formula, "10", "Input:Y4", "Indicators");
    95 
    96         // Set minimum and maximum for Y axis
    97         chart1.ChartAreas["Indicator"].AxisY.Minimum = 0;
    98         chart1.ChartAreas["Indicator"].AxisY.Maximum = 100;
    99 
    100         // Create strip lines used with Relative strength index.
    101         StripLine stripLine = new StripLine();
    102         chart1.ChartAreas["Indicator"].AxisY.StripLines.Add(stripLine);
    103         stripLine.Interval = 70;
    104         stripLine.StripWidth = 30;
    105         stripLine.BackColor = Color.FromArgb(128, 255, 255, 0);
    106         stripLine.BorderColor = Color.Teal;
    107 
     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++;
    108134      }
    109135
    110136      // Williams %R
    111       else if (formulaName == "WilliamsR") {
    112 
     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) {
    113187        chart1.DataManipulator.FinancialFormula(formula, "Input:Y,Input:Y2,Input:Y4", "Indicators");
    114 
    115         // Set minimum and maximum for Y axis
    116         chart1.ChartAreas["Indicator"].AxisY.Minimum = -100;
    117         chart1.ChartAreas["Indicator"].AxisY.Maximum = 0;
    118 
    119         // Create strip lines used with Williams %R index.
    120         StripLine stripLine = new StripLine();
    121         chart1.ChartAreas["Indicator"].AxisY.StripLines.Add(stripLine);
    122         stripLine.Interval = 80;
    123         stripLine.StripWidth = 20;
    124         stripLine.BackColor = Color.FromArgb(128, 255, 255, 0);
    125         stripLine.BorderColor = Color.Teal;
    126 
    127       }
    128 
    129       // Formulas with two input value
    130       else if (formulaName == "MassIndex" || formulaName == "VolatilityChaikins" || formulaName == "Performance") {
    131         chart1.DataManipulator.FinancialFormula(formula, "20", "Input:Y,Input:Y2", "Indicators");
    132       }
    133 
    134       // Standard deviation
    135       else if (formulaName == "StandardDeviation") {
     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) {
    136210        chart1.DataManipulator.FinancialFormula(formula, "15", "Input:Y4", "Indicators");
    137       }
    138 
    139       // StochasticIndicator
    140       else if (formulaName == "StochasticIndicator") {
    141 
    142         chart1.DataManipulator.FinancialFormula(formula, "15", "Input:Y,Input:Y2,Input:Y4", "Indicators,SMA");
    143 
    144         // Set attributes for Simple moving average series.
    145         chart1.Series["SMA"].ChartType = SeriesChartType.Line;
    146         chart1.Series["SMA"].Color = Color.Yellow;
    147         chart1.Series["SMA"].ChartArea = "Indicator";
    148         chart1.Series["SMA"].BorderWidth = 2;
    149 
    150       }
    151         // All other formulas.
    152       else {
    153         chart1.DataManipulator.FinancialFormula(formula, "Input:Y,Input:Y2,Input:Y4", "Indicators");
    154       }
    155 
    156       // Set minimum for X axis
    157       chart1.ChartAreas["Indicator"].AxisX.Minimum = DateTime.Parse("1/1/2002").ToOADate();
    158    
     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      }
    159223
    160224      ColumnGroup.FireChanged();
     
    163227    public override void UndoExecute() {
    164228      base.UndoExecute();
    165       int columnIndex = this.ColumnGroup.IndexOfColumn(Column);
     229      int lastAffectedColumnIndex = AffectedColumns[3];
    166230      for (int i = 0; i < addedColumnsCount; i++)
    167         this.ColumnGroup.RemoveColumn(columnIndex + 1);
     231        this.ColumnGroup.RemoveColumn(lastAffectedColumnIndex + 1);
    168232      addedColumnsCount = 0;
    169233      ColumnGroup.FireChanged();
Note: See TracChangeset for help on using the changeset viewer.