Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9262 for branches/sluengo


Ignore:
Timestamp:
02/28/13 23:48:00 (11 years ago)
Author:
sluengo
Message:
 
Location:
branches/sluengo/HeuristicLab.Problems.TradeRules
Files:
34 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/sluengo/HeuristicLab.Problems.TradeRules/Grammar.cs

    r9139 r9262  
    4141    private const string BooleanConstant = "Boolean Constant";
    4242    private const string TradeMean = "Average Trade";
    43     private const string MACDIndicator = "MACD Indicator";
     43    private const string Indicators = "Indicators";
    4444    private const string SerialTime = "Serial Operators";
    4545     
     
    6464
    6565        var mean = new AverageTrade();
    66         var expMovAverage = new EMA();
    6766        var macd = new MACD();
    68         var RSI = new RSI();
     67        var rsi = new RSI();
    6968
    7069        var gt = new GreaterThan();
     
    9190        var booleanConstantsSymbols = new GroupSymbol(BooleanConstant, new List<ISymbol> { boolean });
    9291        var meanTradeSymbols = new GroupSymbol(TradeMean, new List<ISymbol> { mean});
    93         var indicatorTradeSymbols = new GroupSymbol(MACDIndicator, new List<ISymbol> { macd,RSI, expMovAverage });
     92        var indicatorTradeSymbols = new GroupSymbol(Indicators, new List<ISymbol> { macd,rsi});
    9493       
    9594        var serialTime = new GroupSymbol(SerialTime, new List<ISymbol> {max,min, lag });
     
    110109        SetSubtreeCount(arithmeticSymbols, 2, 2);
    111110        SetSubtreeCount(terminalSymbols, 0, 0);
    112 
    113111        SetSubtreeCount(booleanConstantsSymbols, 0, 0);
    114112        SetSubtreeCount(meanTradeSymbols, 1, 1);
    115113        SetSubtreeCount(macd, 3, 3);
    116         SetSubtreeCount(RSI, 1, 1);
    117         SetSubtreeCount(expMovAverage, 1, 1);
     114        SetSubtreeCount(rsi, 1, 1);
    118115        SetSubtreeCount(comparisonSymbols, 2, 2);
    119116        SetSubtreeCount(and, 2, 2);
     
    124121
    125122      #region allowed child symbols configuration
    126         //AddAllowedChildSymbol(StartSymbol, booleanOperationSymbols);
    127         //AddAllowedChildSymbol(StartSymbol, comparisonSymbols);
    128         AddAllowedChildSymbol(StartSymbol, expMovAverage);
     123        AddAllowedChildSymbol(StartSymbol, booleanOperationSymbols);
     124        AddAllowedChildSymbol(StartSymbol, comparisonSymbols);
    129125
    130126        AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
    131127        AddAllowedChildSymbol(booleanOperationSymbols, booleanConstantsSymbols);
    132         //AddAllowedChildSymbol(booleanOperationSymbols, indicatorTradeSymbols);
     128        AddAllowedChildSymbol(booleanOperationSymbols, indicatorTradeSymbols);
     129        AddAllowedChildSymbol(booleanOperationSymbols, booleanOperationSymbols);
    133130
    134131        AddAllowedChildSymbol(comparisonSymbols, meanTradeSymbols);
     
    142139        AddAllowedChildSymbol(arithmeticSymbols, serialTime);
    143140
     141        AddAllowedChildSymbol(indicatorTradeSymbols, constantint);
    144142        AddAllowedChildSymbol(serialTime, constantint);
    145 
    146         AddAllowedChildSymbol(meanTradeSymbols, constantint);
    147         AddAllowedChildSymbol(indicatorTradeSymbols, constantint);
     143        AddAllowedChildSymbol(meanTradeSymbols, constantint);   
    148144      #endregion
    149145    }
  • branches/sluengo/HeuristicLab.Problems.TradeRules/HeuristicLab.Problems.TradeRules-1.0.csproj

    r9139 r9262  
    9696  </ItemGroup>
    9797  <ItemGroup>
    98     <Compile Include="AverageTrade.cs" />
    99     <Compile Include="BoolConstant.cs" />
    100     <Compile Include="BoolConstantTreeNode.cs" />
    101     <Compile Include="ConstantInt.cs" />
    102     <Compile Include="ConstantIntTreeNode.cs" />
    103     <Compile Include="EMA.cs" />
    104     <Compile Include="EvaluatorTradeRules.cs" />
     98    <Compile Include="Symbols\AverageTrade.cs" />
     99    <Compile Include="Symbols\BoolConstant.cs" />
     100    <Compile Include="Symbols\BoolConstantTreeNode.cs" />
     101    <Compile Include="Symbols\ConstantInt.cs" />
     102    <Compile Include="Symbols\ConstantIntTreeNode.cs" />
     103    <Compile Include="Evaluator\EvaluatorTradeRules.cs" />
    105104    <Compile Include="Grammar.cs" />
    106105    <Compile Include="Interpreter.cs" />
    107     <Compile Include="Lag.cs" />
    108     <Compile Include="MACD.cs" />
    109     <Compile Include="Max.cs" />
    110     <Compile Include="Min.cs" />
     106    <Compile Include="Symbols\ITradeRulesExpresionTree.cs" />
     107    <Compile Include="ITradeRulesModel.cs" />
     108    <Compile Include="Solution\ITradeRulesSolution.cs" />
     109    <Compile Include="Solution\ITradingRulesSolution.cs" />
     110    <Compile Include="Symbols\Lag.cs" />
     111    <Compile Include="Symbols\MACD.cs" />
     112    <Compile Include="Symbols\MACDTreeNode.cs" />
     113    <Compile Include="Symbols\Max.cs" />
     114    <Compile Include="Symbols\Min.cs" />
     115    <Compile Include="Evaluator\OnlineTradeRulesCalculator.cs" />
    111116    <Compile Include="Plugin.cs" />
    112117    <Compile Include="Properties\HeuristicLab.Problems.TradeRules-1.0.cs" />
    113     <Compile Include="RSI.cs" />
    114     <Compile Include="TradeAnalysisProblem.cs" />
    115     <Compile Include="TradeRulesAbstractProblem.cs" />
    116     <Compile Include="TradeRulesProblem.cs" />
     118    <Compile Include="Symbols\RSI.cs" />
     119    <Compile Include="Evaluator\TradeRulesAnalysisEvaluator.cs" />
     120    <Compile Include="Evaluator\TradeRulesAnalysisSingleObjectiveEvaluator.cs" />
     121    <Compile Include="Evaluator\TradeRulesSingleObjectiveEvaluator.cs" />
     122    <Compile Include="Problem\TradeAnalysisProblem.cs" />
     123    <Compile Include="Problem\TradeRulesAbstractProblem.cs" />
     124    <Compile Include="Problem\TradeRulesProblem.cs" />
     125    <Compile Include="TradeRulesSingleObjectiveTrainingBestSolutionAnalyzer.cs" />
     126    <Compile Include="TradeRulesSingleObjectiveValidationBestSolutionAnalyzer.cs" />
     127    <Compile Include="Solution\TradeRulesSolution.cs" />
     128    <Compile Include="Solution\TradeRulesSolutionBase.cs" />
     129    <Compile Include="Solution\TradingRulesSolution.cs" />
    117130  </ItemGroup>
    118131  <ItemGroup>
  • branches/sluengo/HeuristicLab.Problems.TradeRules/Interpreter.cs

    r9171 r9262  
    1111using HeuristicLab.Problems.DataAnalysis.Symbolic;
    1212using HeuristicLab.Problems.DataAnalysis;
     13using System.Threading;
     14using HeuristicLab.Problems.TradeRules.Symbols;
    1315
    1416namespace HeuristicLab.Problems.TradeRules
     
    1618    [StorableClass]
    1719    [Item("Interpreter", "Represents a grammar for Trading Problems")]
    18     public sealed class Interpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter
     20    public sealed class Interpreter : ParameterizedNamedItem, ITradeRulesExpresionTree
    1921    {
    2022        private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
    2123        private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
     24        private int initialTraining;
     25        private int initialTest;
    2226        [ThreadStatic]
    23         private static double [] EMAValue;
     27        private static Dictionary<ISymbolicExpressionTreeNode, double> signalCache;
    2428        [ThreadStatic]
    25         private static double inOut;
     29        private static Dictionary<ISymbolicExpressionTreeNode, double> firstEMACache;
    2630        [ThreadStatic]
    27         private static double meanUp;
     31        private static Dictionary<ISymbolicExpressionTreeNode, double> secondEMACache;
    2832        [ThreadStatic]
    29         private static double meanDown;
     33        private static Dictionary<ISymbolicExpressionTreeNode, double> RSIPositiveCache;
    3034        [ThreadStatic]
    31         private static double lastRSI;
    32    
    33     // [ThreadStatic]
    34     // private Dictionary<Instruction, double> lastEMACache;
    35    
     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
    3641        #region private classes
    3742        //This class manipulate the instructions of the stack
     
    133138            public const byte Lag = 19;
    134139            public const byte RSI = 20;
    135             public const byte EMA = 21;
    136            
    137 
    138140        }
    139141        #endregion
     
    165167      { typeof(MACD), OpCodes.MACD},
    166168      { typeof(RSI), OpCodes.RSI},
    167       { typeof(EMA), OpCodes.EMA},
    168169      { typeof(Max), OpCodes.Max},
    169170      { typeof(Min), OpCodes.Min},
     
    251252                        if (row < integerValue)
    252253                        {
    253                             string variableName = dataset.GetValue(row, 2);
     254                            string variableName = dataset.GetValue(row, 3);
    254255                            double inferiorValue = Convert.ToDouble(variableName);
    255256                            return inferiorValue/(row+1);
     
    257258                        else
    258259                        {
    259                             string variableName = dataset.GetValue(row, 2);
     260                            string variableName = dataset.GetValue(row, 3);
    260261                            double meanValue1 = Convert.ToDouble(variableName);
    261                             string variableName2 = dataset.GetValue((row - integerValue), 2);
     262                            string variableName2 = dataset.GetValue((row - integerValue), 3);
    262263                            double meanValue2 = Convert.ToDouble(variableName2);
    263264                            return (meanValue1 - meanValue2) / integerValue;
    264265                        }
    265                     }
    266                 case OpCodes.MACD:
    267                     {                 
    268                         /*double firstMean = Evaluate(dataset, ref row, state);
    269                         double secondMean = Evaluate(dataset, ref row, state);
    270                         double signal = Evaluate(dataset, ref row, state);
    271                         double firstEMA = 0.0;
    272                         double secondEMA = 0.0;
    273                         double lastFirstEMA = 0.0;
    274                         double lastSecondEMA = 0.0;
    275                         double macd = 0.0;
    276                         double firstFactor = 0.0;
    277                         double signalValue = Double.NegativeInfinity;
    278                         if (row == (firstMean - 1))
    279                         {
    280                             string variableName = dataset.GetValue(row, 2);
    281                             double meanValue1 = Convert.ToDouble(variableName);
    282                             firstEMA = meanValue1 / firstMean;
    283                         }
    284                         if (row == (secondMean-1))
    285                         {
    286                             string variableName = dataset.GetValue(row, 2);
    287                             double meanValue2 = Convert.ToDouble(variableName);
    288                             secondEMA = meanValue2 / secondMean;
    289                         }
    290                         string variableName2 = dataset.GetValue(row, 1);
    291                         double intValue = Convert.ToDouble(variableName2);
    292                         if (row > (firstMean-1))
    293                         {
    294                               firstFactor = 2 / (firstMean + 1);                         
    295                               firstEMA = (intValue * firstFactor) + (lastFirstEMA * (1-firstFactor));
    296                         }
    297                             if (row > (secondMean-1))
    298                             {
    299                                 double secondFactor = 2 / (secondMean + 1);
    300                                 secondEMA = (intValue * secondFactor) + (lastSecondEMA * (1 - secondFactor));     
    301                             }
    302                             lastFirstEMA = firstEMA;
    303                             lastSecondEMA = secondEMA;
    304                             macd = firstEMA - secondEMA;
    305                         if ((row < (firstMean-1)) || (row < (secondMean-1))) return 0.0;
    306                         else return macd;*/
    307                         return 0.0;
    308 
    309                         //macd = firstEMA - secondEMA;
    310                         //if (row == Math.Max(firstMean, secondMean)) signalValue = macd;
    311                         //if (row > firstMean && row > secondMean)
    312                         //{
    313                         //    double factor = 2 / (signal + 1);
    314                         //    signalValue = (macd * factor) + (lastSignal * (1 - factor));
    315                         //}
    316                         //if (!Double.IsNegativeInfinity(signalValue)) lastSignal = signalValue;
    317                         //return signalValue > macd ? 1.0 : -1.0;
    318266                    }
    319267                case OpCodes.AND:
     
    374322                        return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
    375323                    }
    376                 case OpCodes.EMA:
    377                     {
    378                         //I assume you want to calculate the EMA as defined at http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
    379             //for the mean value saved at position 1 of the dataset
    380             //and timevalue specifies how many days you want to include in your EMA
    381             double timeValue = (int)Evaluate(dataset, ref row, state);
    382 
    383             // with caching
    384             // if (lastEmaCache.ContainsKey(currentInst)
    385             //   lastEma = lastEmaCache[currentInstr]
    386             // else
    387             //   lastEma = 1;
    388            
    389             //get all mean values from row - timeValue up to the actual row
    390             double[] meanValues = dataset.GetDoubleValues("insert variable name", Enumerable.Range(row - (int)timeValue, timeValue)).ToArray();
    391 
    392             double EMA = meanValues[0];
    393             double factor = 2.0 / (timeValue + 1.0);
    394 
    395             for (int i = 1; i < timeValue; i++) {
    396               EMA = meanValues[i] * factor + (1 - factor) * EMA;
    397             }
    398            
    399             // save in cache for next evaluation
    400             // lastEmaCache[currentInstr] = EMA;
    401            
    402             return EMA;
    403                     }
    404324                case OpCodes.Constant:
    405325                    {
     
    425345                        {
    426346                            int position = row - i;
    427                             string variableName = dataset.GetValue(position, 1);
     347                            string variableName = dataset.GetValue(position, 2);
    428348                            double intValue = Convert.ToDouble(variableName);
    429349                            if (intValue>max) max = intValue;
     
    440360                        {
    441361                            int position = row - i;
    442                             string variableName = dataset.GetValue(position, 1);
     362                            string variableName = dataset.GetValue(position, 2);
    443363                            double intValue = Convert.ToDouble(variableName);
    444364                            if (intValue < min) min = intValue;
     
    452372                        if (n>row) return 0;
    453373                        int position = row - n;
    454                         string variableName = dataset.GetValue(position, 1);
     374                        string variableName = dataset.GetValue(position, 2);
    455375                        double intValue = Convert.ToDouble(variableName);
    456376                        return intValue;
    457377                    }
     378                case OpCodes.MACD:
     379                    {
     380                        var MACDNode = currentInstr.dynamicNode;
     381                        //Taking the number of the days for each EMA
     382                        double firstEMA = Evaluate(dataset, ref row, state);
     383                        double secondEMA = Evaluate(dataset, ref row, state);
     384                        double signal = Evaluate(dataset, ref row, state);
     385
     386                        //Calculating the factor for each EMA
     387                        double factor = 2.0 / (firstEMA + 1.0);
     388                        double factor2 = 2.0 / (secondEMA + 1.0);
     389                        double factor3 = 2.0 / (signal + 1.0);
     390
     391                        //Initiation of the variables
     392                        double firstElementEMA = -1000000;
     393                        double secondElementEMA = -100000;
     394                        double signalValue = -100000;
     395                        double macd = 0;
     396
     397                        //Check if this MACD has previous values and retrieve them
     398                        if (firstEMACache.ContainsKey(MACDNode)) firstElementEMA = firstEMACache[MACDNode];
     399                        if (secondEMACache.ContainsKey(MACDNode)) secondElementEMA = secondEMACache[MACDNode];
     400                        if (signalCache.ContainsKey(MACDNode)) signalValue = signalCache[MACDNode];
     401
     402                        //Calculate the first value in the training for the two EMAs and the signal.
     403                        if (row <= initialTraining || row == initialTest)
     404                        {
     405                            double[] meanValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, (row + 1))).ToArray();
     406                            firstElementEMA = meanValues[0];
     407                            secondElementEMA = meanValues[0];
     408                            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.
     409                            for (int i = 1; i <= row; i++)
     410                            {
     411                                firstElementEMA = meanValues[i] * factor + (1 - factor) * firstElementEMA;
     412                                secondElementEMA = meanValues[i] * factor2 + (1 - factor2) * secondElementEMA;
     413                                if (i == max) signalValue = firstElementEMA - secondElementEMA;//First signal equals to macd.
     414                                else if (i > max)//Calculation for the next signals
     415                                {
     416                                    macd = firstElementEMA - secondElementEMA;
     417                                    signalValue = macd * factor3 + (1 - factor3) * signalValue;
     418                                }
     419                            }
     420                        }
     421                        else  //The rest of the rows are calculating with the standard EMA formula
     422                        {
     423                            //Retrieve the dataset values
     424                            string variableName = dataset.GetValue(row, 2);
     425                            double meanValue1 = Convert.ToDouble(variableName);
     426
     427                            //Calculating EMA
     428                            firstElementEMA = meanValue1 * factor + (1 - factor) * firstElementEMA;
     429                            secondElementEMA = meanValue1 * factor2 + (1 - factor2) * secondElementEMA;
     430
     431                            //Calculating signal
     432                            macd = firstElementEMA - secondElementEMA;
     433                            signalValue = macd * factor3 + (1 - factor3) * signalValue;
     434                        }
     435
     436                        //Save the values for the next iteration
     437                        firstEMACache[MACDNode] = firstElementEMA;
     438                        secondEMACache[MACDNode] = secondElementEMA;
     439                        signalCache[MACDNode] = signalValue;
     440
     441                        macd = firstElementEMA - secondElementEMA;
     442                        return macd > signalValue ? 1.0 : -1.0;
     443                    }
    458444                case OpCodes.RSI:
    459                     {
    460                         /*
    461                         double numberOfDays = Evaluate(dataset, ref row, state);
    462                         double todayRSI=0.0;
    463                         if (numberOfDays > row) return 0.0;
    464                         else
    465                         {
    466                             double addUp = 0;
    467                             double addDown = 0;
    468                             double change=0;
    469                             if (row == (numberOfDays))
    470                             {
    471                                 for (int k = 1; k < (numberOfDays+1); k++)
    472                                 {
    473                                     string variableName = dataset.GetValue(k, 1);
    474                                     double intValue = Convert.ToDouble(variableName);
    475                                     variableName = dataset.GetValue((k - 1), 1);
    476                                     double intValue2 = Convert.ToDouble(variableName);
    477                                      change = intValue - intValue2;
    478                                     if (change > 0) addUp = addUp + change;
    479                                     else addDown = addDown - change;
    480                                  
    481                                 }
    482                                 meanUp = addUp / numberOfDays;
    483                                 meanDown = addDown / numberOfDays;
    484                                 if (meanDown != 0) todayRSI = 100 - (100 / (1 + (meanUp / meanDown)));
    485                                 else todayRSI = 100;
    486                              }
    487                             else
    488                             {
    489                                 string variableName = dataset.GetValue(row, 1);
    490                                 double intValue = Convert.ToDouble(variableName);
    491                                 variableName = dataset.GetValue((row - 1), 1);
    492                                 double intValue2 = Convert.ToDouble(variableName);
    493                                 change = intValue - intValue2;
    494 
    495                                 addUp = meanUp * (numberOfDays - 1);
    496                                 addDown = meanDown * (numberOfDays - 1);
    497                                 if (change > 0) addUp = addUp + change;
    498                                 else addDown = addDown - change;
    499                                 meanUp = addUp / numberOfDays;
    500                                 meanDown = addDown / numberOfDays;
    501                                 if (meanDown != 0) todayRSI = 100 - (100 / (1 + (meanUp / meanDown)));
    502                                 else todayRSI = 100;
    503                             }
    504                         }
    505                         if ((lastRSI < 70) && (todayRSI >= 70)) inOut = 1;
    506                         else if((lastRSI > 30) && (todayRSI <= 30)) inOut=-1;
    507                         lastRSI = todayRSI;*/
    508                         return 0.0;
    509                        
     445                   {
     446                       
     447                       //Taking the number of the days for EMA
     448                       double numberOfDays = Evaluate(dataset, ref row, state);
     449
     450                       //Calculate the factor for the EMA
     451                       double factor = 1.0 / numberOfDays;
     452
     453                       double positiveEMA = 0;
     454                       double negativeEMA = 0;
     455                       double yesterdayRSI = double.NegativeInfinity;
     456                       double todayRSI = double.NegativeInfinity;
     457                       double outputRSI = double.NegativeInfinity;
     458                       //Retrieve EMA values
     459                       if (RSIPositiveCache.ContainsKey(currentInstr.dynamicNode)) positiveEMA = RSIPositiveCache[currentInstr.dynamicNode];
     460                       if (RSINegativeCache.ContainsKey(currentInstr.dynamicNode)) negativeEMA = RSINegativeCache[currentInstr.dynamicNode];
     461                       if (RSICache.ContainsKey(currentInstr.dynamicNode)) yesterdayRSI = RSICache[currentInstr.dynamicNode];
     462                       if (RSIOutputCache.ContainsKey(currentInstr.dynamicNode)) outputRSI = RSIOutputCache[currentInstr.dynamicNode];
     463
     464                       if (row == initialTraining || row == initialTest)
     465                       {
     466                           double[] closeValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, (row + 1))).ToArray();
     467                           outputRSI = -1.0;
     468                           for (int i = 1; i <= row; i++)
     469                           {
     470                               if (numberOfDays>=i)
     471                               {
     472                                   if ((closeValues[i] - closeValues[i - 1]) > 0) positiveEMA = ((closeValues[i] - closeValues[i - 1])+positiveEMA);
     473                                   else negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) + negativeEMA;
     474                                   if (numberOfDays == i)
     475                                   {
     476                                       positiveEMA = positiveEMA/numberOfDays;
     477                                       negativeEMA = negativeEMA / numberOfDays;
     478                                       yesterdayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     479                                   }
     480                               }else{
     481                                    if ((closeValues[i] - closeValues[i - 1]) > 0)
     482                                    {
     483                                        positiveEMA = (closeValues[i]-closeValues[i-1]) * factor + (1 - factor) * positiveEMA;
     484                                        negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
     485                                    }
     486                                    else
     487                                    {
     488                                        positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
     489                                        negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * negativeEMA;
     490                                    }
     491                                   
     492                                    todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     493
     494                                    if ((yesterdayRSI < 30) && (todayRSI > 30)) outputRSI = 1.0;
     495                                   else if ((yesterdayRSI > 70) && (todayRSI < 70)) outputRSI = -1.0;
     496                                   yesterdayRSI = todayRSI;
     497                               }
     498                           }
     499                       }
     500                       else
     501                       {
     502                           string todayCloseString = dataset.GetValue(row, 2);
     503                           string yesterdayCloseString = dataset.GetValue((row - 1), 2);
     504                           double todayClose = Convert.ToDouble(todayCloseString);
     505                           double yesterdayClose = Convert.ToDouble(yesterdayCloseString);
     506
     507                           //Calculating EMA
     508                           if ((todayClose - yesterdayClose) > 0)
     509                           {
     510                               positiveEMA = (todayClose-yesterdayClose) * factor + (1 - factor) * positiveEMA;
     511                               negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
     512                           }
     513                           else
     514                           {
     515                               positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
     516                               negativeEMA = Math.Abs(todayClose - yesterdayClose) * factor + (1 - factor) * negativeEMA;
     517                           }
     518                           todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     519                           if ((yesterdayRSI < 30) && (todayRSI > 30)) outputRSI = 1.0;
     520                           else if ((yesterdayRSI > 70) && (todayRSI < 70)) outputRSI = -1.0;
     521                       }
     522                       
     523                       //Save positive and negative EMA for the next iteration
     524                       RSIPositiveCache[currentInstr.dynamicNode] = positiveEMA;
     525                       RSINegativeCache[currentInstr.dynamicNode] = negativeEMA;
     526                       RSICache[currentInstr.dynamicNode] = todayRSI;
     527                       RSIOutputCache[currentInstr.dynamicNode] = outputRSI;   
     528                       
     529                       return outputRSI;
    510530                    }
    511531               
     
    553573                Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    554574        }
    555        
     575        public void setInitialTraining(int initialTraining)
     576        {
     577            this.initialTraining = initialTraining;
     578        }
     579
     580        public void setInitialTest(int initialTest)
     581        {
     582            this.initialTest = initialTest;
     583        }
     584
     585        public void clearVariables()
     586        {
     587            signalCache.Clear();
     588            firstEMACache.Clear();
     589            secondEMACache.Clear();
     590            RSIPositiveCache.Clear();
     591            RSINegativeCache.Clear();
     592            RSICache.Clear();
     593            RSIOutputCache.Clear();
     594        }
     595
    556596        //Take the symbolic expression values of the tree
    557597        public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows)
    558598        {
    559599            if (CheckExpressionsWithIntervalArithmetic.Value)
    560                 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
     600            throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    561601            EvaluatedSolutions.Value++; // increment the evaluated solutions counter
    562602            var compiler = new SymbolicExpressionTreeCompiler();
     
    572612                    code[i] = instr;
    573613                }
    574                 else if (instr.opCode == OpCodes.EMA)
    575                 {
    576                     instr.iArg0 = EMAValue;
     614            }
     615
     616
     617            if (signalCache == null) signalCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     618            if (firstEMACache == null) firstEMACache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     619            if (secondEMACache == null) secondEMACache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     620            if (RSIPositiveCache == null) RSIPositiveCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     621            if (RSINegativeCache == null) RSINegativeCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     622            if (RSICache == null) RSICache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     623            if (RSIOutputCache == null) RSIOutputCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     624                                 
     625                                       
     626                                        var state = new InterpreterState(code, necessaryArgStackSize);
     627                                        //Evaluate each row of the datase
     628                                        foreach (var rowEnum in rows)
     629                                        {
     630                                            int row = rowEnum;
     631                                            state.Reset();
     632                                            if (row < initialTraining) yield return -1;
     633                                            else yield return Evaluate(dataset, ref row, state);
     634                                        }
    577635                }
    578             }
    579      
    580       // with caching
    581       // if (lastEmaCache == null) lastEmaCache = new Dictionary<Instruction, double>();
    582       // else lastEmaCache.Clear();
    583      
    584             var state = new InterpreterState(code, necessaryArgStackSize);
    585             //Evaluate each row of the datase
    586             foreach (var rowEnum in rows)
    587             {
    588                 int row = rowEnum;
    589                 state.Reset();
    590                 yield return Evaluate(dataset, ref row, state);
    591             }
    592636        }
    593637
    594638    }
    595 }
     639
Note: See TracChangeset for help on using the changeset viewer.