[9139] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Data;
|
---|
| 8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 9 | using HeuristicLab.Parameters;
|
---|
| 10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 11 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 12 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[9262] | 13 | using System.Threading;
|
---|
[9139] | 14 |
|
---|
| 15 | namespace HeuristicLab.Problems.TradeRules
|
---|
| 16 | {
|
---|
| 17 | [StorableClass]
|
---|
| 18 | [Item("Interpreter", "Represents a grammar for Trading Problems")]
|
---|
[9262] | 19 | public sealed class Interpreter : ParameterizedNamedItem, ITradeRulesExpresionTree
|
---|
[9139] | 20 | {
|
---|
| 21 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
| 22 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
[9262] | 23 | private int initialTraining;
|
---|
| 24 | private int initialTest;
|
---|
[9325] | 25 |
|
---|
[9139] | 26 | [ThreadStatic]
|
---|
[9262] | 27 | private static Dictionary<ISymbolicExpressionTreeNode, double> signalCache;
|
---|
[9139] | 28 | [ThreadStatic]
|
---|
[9262] | 29 | private static Dictionary<ISymbolicExpressionTreeNode, double> firstEMACache;
|
---|
[9139] | 30 | [ThreadStatic]
|
---|
[9262] | 31 | private static Dictionary<ISymbolicExpressionTreeNode, double> secondEMACache;
|
---|
[9139] | 32 | [ThreadStatic]
|
---|
[9262] | 33 | private static Dictionary<ISymbolicExpressionTreeNode, double> RSIPositiveCache;
|
---|
[9139] | 34 | [ThreadStatic]
|
---|
[9262] | 35 | private static Dictionary<ISymbolicExpressionTreeNode, double> RSINegativeCache;
|
---|
| 36 | [ThreadStatic]
|
---|
| 37 | private static Dictionary<ISymbolicExpressionTreeNode, double> RSICache;
|
---|
| 38 | [ThreadStatic]
|
---|
| 39 | private static Dictionary<ISymbolicExpressionTreeNode, double> RSIOutputCache;
|
---|
| 40 |
|
---|
[9139] | 41 | #region private classes
|
---|
| 42 | //This class manipulate the instructions of the stack
|
---|
| 43 | private class InterpreterState
|
---|
| 44 | {
|
---|
| 45 | private double[] argumentStack;
|
---|
| 46 | private int argumentStackPointer;
|
---|
| 47 | private Instruction[] code;
|
---|
| 48 | private int pc;
|
---|
| 49 | public int ProgramCounter
|
---|
| 50 | {
|
---|
| 51 | get { return pc; }
|
---|
| 52 | set { pc = value; }
|
---|
| 53 | }
|
---|
| 54 | internal InterpreterState(Instruction[] code, int argumentStackSize)
|
---|
| 55 | {
|
---|
| 56 | this.code = code;
|
---|
| 57 | this.pc = 0;
|
---|
| 58 | if (argumentStackSize > 0)
|
---|
| 59 | {
|
---|
| 60 | this.argumentStack = new double[argumentStackSize];
|
---|
| 61 | }
|
---|
| 62 | this.argumentStackPointer = 0;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | internal void Reset()
|
---|
| 66 | {
|
---|
| 67 | this.pc = 0;
|
---|
| 68 | this.argumentStackPointer = 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | internal Instruction NextInstruction()
|
---|
| 72 | {
|
---|
| 73 | return code[pc++];
|
---|
| 74 | }
|
---|
| 75 | private void Push(double val)
|
---|
| 76 | {
|
---|
| 77 | argumentStack[argumentStackPointer++] = val;
|
---|
| 78 | }
|
---|
| 79 | private double Pop()
|
---|
| 80 | {
|
---|
| 81 | return argumentStack[--argumentStackPointer];
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | internal void CreateStackFrame(double[] argValues)
|
---|
| 85 | {
|
---|
| 86 | // push in reverse order to make indexing easier
|
---|
| 87 | for (int i = argValues.Length - 1; i >= 0; i--)
|
---|
| 88 | {
|
---|
| 89 | argumentStack[argumentStackPointer++] = argValues[i];
|
---|
| 90 | }
|
---|
| 91 | Push(argValues.Length);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | internal void RemoveStackFrame()
|
---|
| 95 | {
|
---|
| 96 | int size = (int)Pop();
|
---|
| 97 | argumentStackPointer -= size;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | internal double GetStackFrameValue(ushort index)
|
---|
| 101 | {
|
---|
| 102 | // layout of stack:
|
---|
| 103 | // [0] <- argumentStackPointer
|
---|
| 104 | // [StackFrameSize = N + 1]
|
---|
| 105 | // [Arg0] <- argumentStackPointer - 2 - 0
|
---|
| 106 | // [Arg1] <- argumentStackPointer - 2 - 1
|
---|
| 107 | // [...]
|
---|
| 108 | // [ArgN] <- argumentStackPointer - 2 - N
|
---|
| 109 | // <Begin of stack frame>
|
---|
| 110 | return argumentStack[argumentStackPointer - index - 2];
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | //Operation codes
|
---|
| 115 | private class OpCodes
|
---|
| 116 | {
|
---|
| 117 | public const byte Add = 1;
|
---|
| 118 | public const byte Sub = 2;
|
---|
| 119 | public const byte Mul = 3;
|
---|
| 120 |
|
---|
| 121 | public const byte GT = 5;
|
---|
| 122 | public const byte LT = 6;
|
---|
| 123 |
|
---|
| 124 | public const byte AND = 7;
|
---|
| 125 | public const byte OR = 8;
|
---|
| 126 | public const byte NOT = 9;
|
---|
| 127 | public const byte BOOLEAN = 10;
|
---|
| 128 |
|
---|
| 129 | public const byte Average = 11;
|
---|
| 130 | public const byte MACD = 12;
|
---|
| 131 |
|
---|
| 132 | public const byte Variable = 13;
|
---|
| 133 | public const byte ConstantInt = 16;
|
---|
| 134 | public const byte BoolConstant = 15;
|
---|
| 135 | public const byte Max = 17;
|
---|
| 136 | public const byte Min = 18;
|
---|
| 137 | public const byte Lag = 19;
|
---|
| 138 | public const byte RSI = 20;
|
---|
| 139 | }
|
---|
| 140 | #endregion
|
---|
| 141 |
|
---|
| 142 | #region IStatefulItem
|
---|
| 143 | public void InitializeState()
|
---|
| 144 | {
|
---|
| 145 | EvaluatedSolutions.Value = 0;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public void ClearState()
|
---|
| 149 | {
|
---|
| 150 | }
|
---|
| 151 | #endregion
|
---|
| 152 |
|
---|
| 153 | private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
|
---|
| 154 | { typeof(Addition), OpCodes.Add },
|
---|
| 155 | { typeof(Subtraction), OpCodes.Sub },
|
---|
| 156 | { typeof(Multiplication), OpCodes.Mul },
|
---|
| 157 | { typeof(BoolConstant), OpCodes.BoolConstant },
|
---|
| 158 | { typeof(ConstantInt), OpCodes.ConstantInt },
|
---|
| 159 | { typeof(GreaterThan), OpCodes.GT },
|
---|
| 160 | { typeof(LessThan), OpCodes.LT },
|
---|
| 161 | { typeof(And), OpCodes.AND },
|
---|
| 162 | { typeof(Or), OpCodes.OR },
|
---|
| 163 | { typeof(Not), OpCodes.NOT},
|
---|
| 164 | { typeof(AverageTrade), OpCodes.Average},
|
---|
| 165 | { typeof(MACD), OpCodes.MACD},
|
---|
| 166 | { typeof(RSI), OpCodes.RSI},
|
---|
| 167 | { typeof(Max), OpCodes.Max},
|
---|
| 168 | { typeof(Min), OpCodes.Min},
|
---|
| 169 | { typeof(Lag), OpCodes.Lag},
|
---|
| 170 | { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.Variable },
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 | public override bool CanChangeName
|
---|
| 174 | {
|
---|
| 175 | get { return false; }
|
---|
| 176 | }
|
---|
| 177 | public override bool CanChangeDescription
|
---|
| 178 | {
|
---|
| 179 | get { return false; }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | #region parameter properties
|
---|
| 183 | public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter
|
---|
| 184 | {
|
---|
| 185 | get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | public IValueParameter<IntValue> EvaluatedSolutionsParameter
|
---|
| 189 | {
|
---|
| 190 | get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
| 191 | }
|
---|
| 192 | #endregion
|
---|
| 193 |
|
---|
| 194 | #region properties
|
---|
| 195 | public BoolValue CheckExpressionsWithIntervalArithmetic
|
---|
| 196 | {
|
---|
| 197 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
|
---|
| 198 | set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | public IntValue EvaluatedSolutions
|
---|
| 202 | {
|
---|
| 203 | get { return EvaluatedSolutionsParameter.Value; }
|
---|
| 204 | set { EvaluatedSolutionsParameter.Value = value; }
|
---|
| 205 | }
|
---|
| 206 | #endregion
|
---|
| 207 |
|
---|
| 208 | private double Evaluate(Dataset dataset, ref int row, InterpreterState state)
|
---|
| 209 | {
|
---|
| 210 | Instruction currentInstr = state.NextInstruction();
|
---|
| 211 |
|
---|
| 212 | switch (currentInstr.opCode)
|
---|
| 213 | {
|
---|
| 214 | case OpCodes.Add:
|
---|
| 215 | {
|
---|
| 216 | double s = Evaluate(dataset, ref row, state);
|
---|
| 217 | for (int i = 1; i < currentInstr.nArguments; i++)
|
---|
| 218 | {
|
---|
| 219 | s += Evaluate(dataset, ref row, state);
|
---|
| 220 | }
|
---|
| 221 | return s;
|
---|
| 222 | }
|
---|
| 223 | case OpCodes.Sub:
|
---|
| 224 | {
|
---|
| 225 | double s = Evaluate(dataset, ref row, state);
|
---|
| 226 | for (int i = 1; i < currentInstr.nArguments; i++)
|
---|
| 227 | {
|
---|
| 228 | s -= Evaluate(dataset, ref row, state);
|
---|
| 229 | }
|
---|
| 230 | if (currentInstr.nArguments == 1) s = -s;
|
---|
| 231 | return s;
|
---|
| 232 | }
|
---|
| 233 | case OpCodes.Mul:
|
---|
| 234 | {
|
---|
| 235 | double p = Evaluate(dataset, ref row, state);
|
---|
| 236 | for (int i = 1; i < currentInstr.nArguments; i++)
|
---|
| 237 | {
|
---|
| 238 | p *= Evaluate(dataset, ref row, state);
|
---|
| 239 | }
|
---|
| 240 | return p;
|
---|
| 241 | }
|
---|
| 242 | case OpCodes.Average:
|
---|
| 243 | {
|
---|
| 244 | double sum = Evaluate(dataset, ref row, state);
|
---|
[9325] | 245 | int integerValue = (int)Math.Floor(sum);
|
---|
[9139] | 246 | if (integerValue > 100) integerValue = 100;
|
---|
| 247 | if (row < integerValue)
|
---|
| 248 | {
|
---|
[9262] | 249 | string variableName = dataset.GetValue(row, 3);
|
---|
[9139] | 250 | double inferiorValue = Convert.ToDouble(variableName);
|
---|
[9325] | 251 | return inferiorValue / (row + 1);
|
---|
[9139] | 252 | }
|
---|
| 253 | else
|
---|
| 254 | {
|
---|
[9262] | 255 | string variableName = dataset.GetValue(row, 3);
|
---|
[9139] | 256 | double meanValue1 = Convert.ToDouble(variableName);
|
---|
[9262] | 257 | string variableName2 = dataset.GetValue((row - integerValue), 3);
|
---|
[9139] | 258 | double meanValue2 = Convert.ToDouble(variableName2);
|
---|
| 259 | return (meanValue1 - meanValue2) / integerValue;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | case OpCodes.AND:
|
---|
| 263 | {
|
---|
| 264 | double result = Evaluate(dataset, ref row, state);
|
---|
[9325] | 265 | double result2 = Evaluate(dataset, ref row, state);
|
---|
| 266 | double total = result + result2;
|
---|
[9386] | 267 | return total < 2.0 ? -1.0 : 1.0;
|
---|
[9139] | 268 | }
|
---|
| 269 | case OpCodes.OR:
|
---|
| 270 | {
|
---|
| 271 | double result = Evaluate(dataset, ref row, state);
|
---|
[9325] | 272 | double result2 = Evaluate(dataset, ref row, state);
|
---|
| 273 | double total = result + result2;
|
---|
[9386] | 274 | return total > -2.0 ? 1.0 : -1.0;
|
---|
[9139] | 275 | }
|
---|
| 276 | case OpCodes.NOT:
|
---|
| 277 | {
|
---|
[9386] | 278 | return Evaluate(dataset, ref row, state) > -1.0 ? -1.0 : 1.0;
|
---|
[9139] | 279 | }
|
---|
| 280 |
|
---|
| 281 | case OpCodes.BOOLEAN:
|
---|
| 282 | {
|
---|
| 283 | var booleanTreeNode = currentInstr.dynamicNode as BoolConstantTreeNode;
|
---|
| 284 | return booleanTreeNode.Value;
|
---|
| 285 | }
|
---|
| 286 | case OpCodes.GT:
|
---|
| 287 | {
|
---|
| 288 | double x = Evaluate(dataset, ref row, state);
|
---|
| 289 | double y = Evaluate(dataset, ref row, state);
|
---|
| 290 | if (x > y) return 1.0;
|
---|
| 291 | else return -1.0;
|
---|
| 292 | }
|
---|
| 293 | case OpCodes.LT:
|
---|
| 294 | {
|
---|
| 295 | double x = Evaluate(dataset, ref row, state);
|
---|
| 296 | double y = Evaluate(dataset, ref row, state);
|
---|
| 297 | if (x < y) return 1.0;
|
---|
| 298 | else return -1.0;
|
---|
| 299 | }
|
---|
| 300 | case OpCodes.Variable:
|
---|
| 301 | {
|
---|
| 302 | if (row < 0 || row >= dataset.Rows)
|
---|
| 303 | return double.NaN;
|
---|
| 304 | var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
| 305 | return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
|
---|
| 306 | }
|
---|
| 307 | case OpCodes.BoolConstant:
|
---|
| 308 | {
|
---|
| 309 | var boolConstTreeNode = currentInstr.dynamicNode as BoolConstantTreeNode;
|
---|
| 310 | return boolConstTreeNode.Value;
|
---|
| 311 | }
|
---|
| 312 | case OpCodes.ConstantInt:
|
---|
| 313 | {
|
---|
| 314 | var constIntTreeNode = currentInstr.dynamicNode as ConstantIntTreeNode;
|
---|
| 315 | return constIntTreeNode.Value;
|
---|
| 316 | }
|
---|
| 317 | case OpCodes.Max:
|
---|
| 318 | {
|
---|
[9325] | 319 | int n = (int)Evaluate(dataset, ref row, state);
|
---|
[9139] | 320 | double max = Double.NegativeInfinity;
|
---|
[9325] | 321 | int i = Math.Min(n, row);
|
---|
| 322 | while (i >= 0)
|
---|
[9139] | 323 | {
|
---|
| 324 | int position = row - i;
|
---|
[9262] | 325 | string variableName = dataset.GetValue(position, 2);
|
---|
[9139] | 326 | double intValue = Convert.ToDouble(variableName);
|
---|
[9325] | 327 | if (intValue > max) max = intValue;
|
---|
| 328 | i--;
|
---|
[9139] | 329 | }
|
---|
| 330 | return max;
|
---|
| 331 | }
|
---|
| 332 | case OpCodes.Min:
|
---|
| 333 | {
|
---|
| 334 | int n = (int)Evaluate(dataset, ref row, state);
|
---|
| 335 | double min = Double.NegativeInfinity;
|
---|
| 336 | int i = Math.Min(n, row);
|
---|
| 337 | while (i >= 0)
|
---|
| 338 | {
|
---|
| 339 | int position = row - i;
|
---|
[9262] | 340 | string variableName = dataset.GetValue(position, 2);
|
---|
[9139] | 341 | double intValue = Convert.ToDouble(variableName);
|
---|
| 342 | if (intValue < min) min = intValue;
|
---|
| 343 | i--;
|
---|
| 344 | }
|
---|
[9325] | 345 | return min;
|
---|
[9139] | 346 | }
|
---|
| 347 | case OpCodes.Lag:
|
---|
| 348 | {
|
---|
[9325] | 349 | int n = (int)Evaluate(dataset, ref row, state);
|
---|
| 350 | if (n > row) return 0;
|
---|
[9139] | 351 | int position = row - n;
|
---|
[9262] | 352 | string variableName = dataset.GetValue(position, 2);
|
---|
[9139] | 353 | double intValue = Convert.ToDouble(variableName);
|
---|
| 354 | return intValue;
|
---|
| 355 | }
|
---|
[9262] | 356 | case OpCodes.MACD:
|
---|
[9139] | 357 | {
|
---|
[9262] | 358 | //Taking the number of the days for each EMA
|
---|
| 359 | double firstEMA = Evaluate(dataset, ref row, state);
|
---|
| 360 | double secondEMA = Evaluate(dataset, ref row, state);
|
---|
| 361 | double signal = Evaluate(dataset, ref row, state);
|
---|
| 362 | //Initiation of the variables
|
---|
| 363 | double firstElementEMA = -1000000;
|
---|
| 364 | double secondElementEMA = -100000;
|
---|
| 365 | double signalValue = -100000;
|
---|
| 366 | double macd = 0;
|
---|
| 367 |
|
---|
| 368 | //Check if this MACD has previous values and retrieve them
|
---|
[9325] | 369 | if (firstEMACache.ContainsKey(currentInstr.dynamicNode)) firstElementEMA = firstEMACache[currentInstr.dynamicNode];
|
---|
| 370 | if (secondEMACache.ContainsKey(currentInstr.dynamicNode)) secondElementEMA = secondEMACache[currentInstr.dynamicNode];
|
---|
| 371 | if (signalCache.ContainsKey(currentInstr.dynamicNode)) signalValue = signalCache[currentInstr.dynamicNode];
|
---|
[9262] | 372 |
|
---|
[9325] | 373 |
|
---|
[9386] | 374 | //Calculating the factor for each EMA
|
---|
| 375 | double factor = 2.0 / (firstEMA + 1.0);
|
---|
| 376 | double factor2 = 2.0 / (secondEMA + 1.0);
|
---|
| 377 | double factor3 = 2.0 / (signal + 1.0);
|
---|
[9325] | 378 |
|
---|
| 379 | //Calculate the first value in the training for the two EMAs and the signal.
|
---|
[9386] | 380 | if (row == initialTest)
|
---|
[9139] | 381 | {
|
---|
[9325] | 382 | double [] meanValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, row+1)).ToArray();
|
---|
| 383 | firstElementEMA = meanValues[0];
|
---|
| 384 | secondElementEMA = meanValues[0];
|
---|
| 385 | double max = (Math.Max(firstEMA, secondEMA)-1);//The first macd happens when the longest EMA has its first value. We need -1 because row begin in 0.
|
---|
| 386 | for (int i = 1; i < meanValues.Length; i++)
|
---|
[9139] | 387 | {
|
---|
[9325] | 388 | firstElementEMA = (meanValues[i] * factor) + ((1 - factor) * firstElementEMA);
|
---|
| 389 | secondElementEMA = (meanValues[i] * factor2) + ((1 - factor2) * secondElementEMA);
|
---|
| 390 | if (i == max) signalValue = firstElementEMA - secondElementEMA;//First signal equals to macd.
|
---|
| 391 | else if (i > max)//Calculation for the next signals
|
---|
| 392 | {
|
---|
| 393 | macd = firstElementEMA - secondElementEMA;
|
---|
| 394 | signalValue = macd * factor3 + (1 - factor3) * signalValue;
|
---|
| 395 | }
|
---|
[9139] | 396 | }
|
---|
| 397 | }
|
---|
[9325] | 398 | else //The rest of the rows are calculating with the standard EMA formula
|
---|
| 399 | {
|
---|
| 400 | //Retrieve the dataset values
|
---|
| 401 | string variableName = dataset.GetValue(row, 2);
|
---|
| 402 | double meanValue1 = Convert.ToDouble(variableName);
|
---|
[9262] | 403 |
|
---|
[9325] | 404 | //Calculating EMA
|
---|
| 405 | firstElementEMA = meanValue1 * factor + (1 - factor) * firstElementEMA;
|
---|
| 406 | secondElementEMA = meanValue1 * factor2 + (1 - factor2) * secondElementEMA;
|
---|
[9262] | 407 |
|
---|
[9325] | 408 | //Calculating signal
|
---|
| 409 | macd = firstElementEMA - secondElementEMA;
|
---|
| 410 | signalValue = macd * factor3 + (1 - factor3) * signalValue;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | //Save the values for the next iteration
|
---|
| 414 | firstEMACache[currentInstr.dynamicNode] = firstElementEMA;
|
---|
| 415 | secondEMACache[currentInstr.dynamicNode] = secondElementEMA;
|
---|
| 416 | signalCache[currentInstr.dynamicNode] = signalValue;
|
---|
[9386] | 417 |
|
---|
[9262] | 418 | return macd > signalValue ? 1.0 : -1.0;
|
---|
[9139] | 419 | }
|
---|
[9262] | 420 | case OpCodes.RSI:
|
---|
[9325] | 421 | {
|
---|
| 422 | //Taking the number of the days for EMA
|
---|
| 423 | double numberOfDays = Evaluate(dataset, ref row, state);
|
---|
[9499] | 424 | double upThreshold = Evaluate(dataset, ref row, state);
|
---|
| 425 | double downThreshold = Evaluate(dataset, ref row, state);
|
---|
[9262] | 426 |
|
---|
[9325] | 427 | double positiveEMA = 0;
|
---|
| 428 | double negativeEMA = 0;
|
---|
| 429 | double yesterdayRSI = double.NegativeInfinity;
|
---|
| 430 | double todayRSI = double.NegativeInfinity;
|
---|
| 431 | double outputRSI = double.NegativeInfinity;
|
---|
| 432 | //Retrieve EMA values
|
---|
| 433 | if (RSIPositiveCache.ContainsKey(currentInstr.dynamicNode)) positiveEMA = RSIPositiveCache[currentInstr.dynamicNode];
|
---|
| 434 | if (RSINegativeCache.ContainsKey(currentInstr.dynamicNode)) negativeEMA = RSINegativeCache[currentInstr.dynamicNode];
|
---|
| 435 | if (RSICache.ContainsKey(currentInstr.dynamicNode)) yesterdayRSI = RSICache[currentInstr.dynamicNode];
|
---|
| 436 | if (RSIOutputCache.ContainsKey(currentInstr.dynamicNode)) outputRSI = RSIOutputCache[currentInstr.dynamicNode];
|
---|
[9262] | 437 |
|
---|
[9325] | 438 |
|
---|
| 439 | //Calculate the factor for the EMA
|
---|
| 440 | double factor = 1.0 / numberOfDays;
|
---|
[9262] | 441 |
|
---|
[9386] | 442 | if (row == initialTest)
|
---|
[9325] | 443 | {
|
---|
| 444 | double[] closeValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, (row + 1))).ToArray();
|
---|
| 445 | outputRSI = -1.0;
|
---|
| 446 | for (int i = 1; i <= row; i++)
|
---|
| 447 | {
|
---|
| 448 | if (numberOfDays >= i)
|
---|
[9262] | 449 | {
|
---|
[9325] | 450 | if ((closeValues[i] - closeValues[i - 1]) > 0) positiveEMA = ((closeValues[i] - closeValues[i - 1]) + positiveEMA);
|
---|
| 451 | else negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) + negativeEMA;
|
---|
| 452 | if (numberOfDays == i)
|
---|
| 453 | {
|
---|
| 454 | positiveEMA = positiveEMA / numberOfDays;
|
---|
| 455 | negativeEMA = negativeEMA / numberOfDays;
|
---|
| 456 | yesterdayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
|
---|
| 457 | }
|
---|
[9262] | 458 | }
|
---|
| 459 | else
|
---|
| 460 | {
|
---|
[9325] | 461 | if ((closeValues[i] - closeValues[i - 1]) > 0)
|
---|
| 462 | {
|
---|
| 463 | positiveEMA = (closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * positiveEMA;
|
---|
| 464 | negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
|
---|
| 465 | }
|
---|
| 466 | else
|
---|
| 467 | {
|
---|
| 468 | positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
|
---|
| 469 | negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * negativeEMA;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
|
---|
| 473 |
|
---|
[9499] | 474 | if ((yesterdayRSI < downThreshold) && (todayRSI > downThreshold)) outputRSI = 1.0;
|
---|
| 475 | else if ((yesterdayRSI > upThreshold) && (todayRSI < upThreshold)) outputRSI = -1.0;
|
---|
[9325] | 476 | yesterdayRSI = todayRSI;
|
---|
[9262] | 477 | }
|
---|
[9325] | 478 | }
|
---|
| 479 | }
|
---|
| 480 | else
|
---|
| 481 | {
|
---|
| 482 | string todayCloseString = dataset.GetValue(row, 2);
|
---|
| 483 | string yesterdayCloseString = dataset.GetValue((row - 1), 2);
|
---|
| 484 | double todayClose = Convert.ToDouble(todayCloseString);
|
---|
| 485 | double yesterdayClose = Convert.ToDouble(yesterdayCloseString);
|
---|
[9262] | 486 |
|
---|
[9325] | 487 | //Calculating EMA
|
---|
| 488 | if ((todayClose - yesterdayClose) > 0)
|
---|
| 489 | {
|
---|
| 490 | positiveEMA = (todayClose - yesterdayClose) * factor + (1 - factor) * positiveEMA;
|
---|
| 491 | negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
|
---|
| 492 | }
|
---|
| 493 | else
|
---|
| 494 | {
|
---|
| 495 | positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
|
---|
| 496 | negativeEMA = Math.Abs(todayClose - yesterdayClose) * factor + (1 - factor) * negativeEMA;
|
---|
| 497 | }
|
---|
| 498 | todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
|
---|
[9499] | 499 | if ((yesterdayRSI < downThreshold) && (todayRSI > downThreshold)) outputRSI = 1.0;
|
---|
| 500 | else if ((yesterdayRSI > upThreshold) && (todayRSI < upThreshold)) outputRSI = -1.0;
|
---|
[9325] | 501 | }
|
---|
[9262] | 502 |
|
---|
[9325] | 503 | //Save positive and negative EMA for the next iteration
|
---|
| 504 | RSIPositiveCache[currentInstr.dynamicNode] = positiveEMA;
|
---|
| 505 | RSINegativeCache[currentInstr.dynamicNode] = negativeEMA;
|
---|
| 506 | RSICache[currentInstr.dynamicNode] = todayRSI;
|
---|
| 507 | RSIOutputCache[currentInstr.dynamicNode] = outputRSI;
|
---|
| 508 |
|
---|
| 509 |
|
---|
| 510 | return outputRSI;
|
---|
[9262] | 511 | }
|
---|
[9325] | 512 |
|
---|
[9139] | 513 | default: throw new NotSupportedException();
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode)
|
---|
| 518 | {
|
---|
| 519 | if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType()))
|
---|
| 520 | return symbolToOpcode[treeNode.Symbol.GetType()];
|
---|
| 521 | else
|
---|
| 522 | throw new NotSupportedException("Symbol: " + treeNode.Symbol);
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | // skips a whole branch
|
---|
| 526 | private void SkipInstructions(InterpreterState state)
|
---|
| 527 | {
|
---|
| 528 | int i = 1;
|
---|
| 529 | while (i > 0)
|
---|
| 530 | {
|
---|
| 531 | i += state.NextInstruction().nArguments;
|
---|
| 532 | i--;
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | [StorableConstructor]
|
---|
| 536 | private Interpreter(bool deserializing) : base(deserializing) { }
|
---|
| 537 | private Interpreter(Interpreter original, Cloner cloner) : base(original, cloner) { }
|
---|
| 538 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 539 | {
|
---|
| 540 | return new Interpreter(this, cloner);
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | public Interpreter()
|
---|
| 544 | : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")
|
---|
| 545 | {
|
---|
| 546 | Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
|
---|
| 547 | Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 551 | private void AfterDeserialization()
|
---|
| 552 | {
|
---|
| 553 | if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
|
---|
| 554 | Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 555 | }
|
---|
[9262] | 556 | public void setInitialTraining(int initialTraining)
|
---|
| 557 | {
|
---|
| 558 | this.initialTraining = initialTraining;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | public void setInitialTest(int initialTest)
|
---|
| 562 | {
|
---|
| 563 | this.initialTest = initialTest;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | public void clearVariables()
|
---|
| 567 | {
|
---|
[9386] | 568 | if (signalCache != null) signalCache.Clear();
|
---|
| 569 | if (firstEMACache != null) firstEMACache.Clear();
|
---|
| 570 | if (secondEMACache != null) secondEMACache.Clear();
|
---|
| 571 | if (RSIPositiveCache != null) RSIPositiveCache.Clear();
|
---|
| 572 | if (RSINegativeCache != null) RSINegativeCache.Clear();
|
---|
| 573 | if (RSICache != null) RSICache.Clear();
|
---|
| 574 | if (RSIOutputCache != null) RSIOutputCache.Clear();
|
---|
[9262] | 575 | }
|
---|
| 576 |
|
---|
[9139] | 577 | //Take the symbolic expression values of the tree
|
---|
| 578 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows)
|
---|
| 579 | {
|
---|
| 580 | if (CheckExpressionsWithIntervalArithmetic.Value)
|
---|
[9325] | 581 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
[9139] | 582 | EvaluatedSolutions.Value++; // increment the evaluated solutions counter
|
---|
| 583 | var compiler = new SymbolicExpressionTreeCompiler();
|
---|
| 584 | Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);//Take the type of symbol
|
---|
| 585 | int necessaryArgStackSize = 0;
|
---|
| 586 | for (int i = 0; i < code.Length; i++)
|
---|
| 587 | {
|
---|
| 588 | Instruction instr = code[i];
|
---|
| 589 | if (instr.opCode == OpCodes.Variable)
|
---|
| 590 | {
|
---|
| 591 | var variableTreeNode = instr.dynamicNode as VariableTreeNode;
|
---|
| 592 | instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
| 593 | code[i] = instr;
|
---|
| 594 | }
|
---|
[9262] | 595 | }
|
---|
| 596 |
|
---|
| 597 | if (signalCache == null) signalCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 598 | if (firstEMACache == null) firstEMACache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 599 | if (secondEMACache == null) secondEMACache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 600 | if (RSIPositiveCache == null) RSIPositiveCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 601 | if (RSINegativeCache == null) RSINegativeCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 602 | if (RSICache == null) RSICache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
| 603 | if (RSIOutputCache == null) RSIOutputCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[9325] | 604 |
|
---|
| 605 | var state = new InterpreterState(code, necessaryArgStackSize);
|
---|
| 606 | //Evaluate each row of the datase
|
---|
| 607 | foreach (var rowEnum in rows)
|
---|
| 608 | {
|
---|
| 609 | int row = rowEnum;
|
---|
| 610 | state.Reset();
|
---|
[9386] | 611 | if (row < initialTest) yield return -1;
|
---|
[9325] | 612 | else yield return Evaluate(dataset, ref row, state);
|
---|
| 613 | }
|
---|
[9139] | 614 | }
|
---|
| 615 | }
|
---|
[9262] | 616 |
|
---|
[9325] | 617 | }
|
---|
| 618 |
|
---|