Changeset 9146 for branches/sluengo
- Timestamp:
- 01/11/13 14:19:59 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sluengo/HeuristicLab.Problems.TradeRules/Interpreter.cs
r9139 r9146 372 372 case OpCodes.EMA: 373 373 { 374 double timeValue = Evaluate(dataset, ref row, state); 375 if (EMAValue == null || EMAValue.GetLength(0) < dataset.Rows) 376 EMAValue = dataset.GetDoubleValues("Close").ToArray(); 377 int lastRow = row - 1; 378 EMAValue[row] = double.NaN; 379 if (row == (timeValue - 1)) 380 { 381 string variableName = dataset.GetValue(row, 2); 382 double meanValue = Convert.ToDouble(variableName); 383 EMAValue[row] = meanValue / timeValue; 384 } 385 if (row > (timeValue - 1)) 386 { 387 lastValue = ((IList<double>)currentInstr.iArg0)[row]; 388 string variableName = dataset.GetValue(row, 1); 389 double meanValue = Convert.ToDouble(variableName); 390 double factor = 2 / (timeValue + 1); 391 EMAValue[row] = (meanValue * factor) + (EMAValue[lastRow] * (1 - factor)); 392 } 393 return EMAValue[row]; 394 395 374 //I assume you want to calculate the EMA as defined at http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average 375 //for the mean value saved at position 1 of the dataset 376 //and timevalue specifies how many days you want to include in your EMA 377 double timeValue = (int)Evaluate(dataset, ref row, state); 378 379 //get all mean values from row - timeValue up to the actual row 380 double[] meanValues = dataset.GetDoubleValues("insert variable name", Enumerable.Range(row - (int)timeValue, timeValue)).ToArray(); 381 382 double EMA = meanValues[0]; 383 double factor = 2 / (timeValue + 1); 384 385 for (int i = 1; i < timeValue; i++) { 386 EMA = meanValues[i] * factor + (1 - factor) * EMA; 387 } 388 return EMA; 396 389 } 397 390 case OpCodes.Constant:
Note: See TracChangeset
for help on using the changeset viewer.