Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9325 for branches/sluengo


Ignore:
Timestamp:
03/20/13 21:02:10 (11 years ago)
Author:
sluengo
Message:
 
Location:
branches/sluengo/HeuristicLab.Problems.TradeRules
Files:
4 edited

Legend:

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

    r9262 r9325  
    5656           
    5757            double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, problemData.TrainingIndices);
    58            
     58
    5959            return tradingCash;
    6060        }
  • branches/sluengo/HeuristicLab.Problems.TradeRules/HeuristicLab.Problems.TradeRules-1.0.csproj

    r9262 r9325  
    3232  </PropertyGroup>
    3333  <ItemGroup>
    34     <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    35       <SpecificVersion>False</SpecificVersion>
    36       <HintPath>..\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
    37     </Reference>
    38     <Reference Include="HeuristicLab.Collections-3.3">
    39       <HintPath>..\bin\HeuristicLab.Collections-3.3.dll</HintPath>
    40     </Reference>
    41     <Reference Include="HeuristicLab.Common-3.3">
    42       <HintPath>..\bin\HeuristicLab.Common-3.3.dll</HintPath>
    43     </Reference>
    44     <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    45       <SpecificVersion>False</SpecificVersion>
    46       <HintPath>..\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>
    47     </Reference>
    48     <Reference Include="HeuristicLab.Core-3.3">
    49       <HintPath>..\bin\HeuristicLab.Core-3.3.dll</HintPath>
    50     </Reference>
    51     <Reference Include="HeuristicLab.Data-3.3">
    52       <HintPath>..\bin\HeuristicLab.Data-3.3.dll</HintPath>
    53     </Reference>
    54     <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4">
    55       <HintPath>..\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath>
    56     </Reference>
    57     <Reference Include="HeuristicLab.Operators-3.3">
    58       <HintPath>..\bin\HeuristicLab.Operators-3.3.dll</HintPath>
    59     </Reference>
    60     <Reference Include="HeuristicLab.Operators.Programmable-3.3">
    61       <HintPath>..\bin\HeuristicLab.Operators.Programmable-3.3.dll</HintPath>
    62     </Reference>
    63     <Reference Include="HeuristicLab.Optimization-3.3">
    64       <HintPath>..\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
    65     </Reference>
    66     <Reference Include="HeuristicLab.Optimizer-3.3">
    67       <HintPath>..\bin\HeuristicLab.Optimizer-3.3.dll</HintPath>
    68     </Reference>
    69     <Reference Include="HeuristicLab.Parameters-3.3">
    70       <HintPath>..\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
    71     </Reference>
    72     <Reference Include="HeuristicLab.Persistence-3.3">
    73       <HintPath>..\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
    74     </Reference>
    75     <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    76       <HintPath>..\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
    77     </Reference>
    78     <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    79       <SpecificVersion>False</SpecificVersion>
    80       <HintPath>..\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
    81     </Reference>
    82     <Reference Include="HeuristicLab.Problems.Instances-3.3">
    83       <HintPath>..\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
    84     </Reference>
    85     <Reference Include="HeuristicLab.Random-3.3">
    86       <HintPath>..\bin\HeuristicLab.Random-3.3.dll</HintPath>
    87     </Reference>
    8834    <Reference Include="System" />
    8935    <Reference Include="System.Core" />
     
    13076  </ItemGroup>
    13177  <ItemGroup>
     78    <ProjectReference Include="..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
     79      <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
     80      <Name>HeuristicLab.Analysis-3.3</Name>
     81    </ProjectReference>
     82    <ProjectReference Include="..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
     83      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     84      <Name>HeuristicLab.Collections-3.3</Name>
     85    </ProjectReference>
     86    <ProjectReference Include="..\HeuristicLab.Common.Resources\3.3\HeuristicLab.Common.Resources-3.3.csproj">
     87      <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project>
     88      <Name>HeuristicLab.Common.Resources-3.3</Name>
     89    </ProjectReference>
     90    <ProjectReference Include="..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj">
     91      <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project>
     92      <Name>HeuristicLab.Common-3.3</Name>
     93    </ProjectReference>
     94    <ProjectReference Include="..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
     95      <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
     96      <Name>HeuristicLab.Core-3.3</Name>
     97    </ProjectReference>
     98    <ProjectReference Include="..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">
     99      <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
     100      <Name>HeuristicLab.Data-3.3</Name>
     101    </ProjectReference>
     102    <ProjectReference Include="..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
     103      <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
     104      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
     105    </ProjectReference>
     106    <ProjectReference Include="..\HeuristicLab.Operators.Programmable\3.3\HeuristicLab.Operators.Programmable-3.3.csproj">
     107      <Project>{6A5F8C2D-B0C3-4B36-BC20-9B1A91EE6DB6}</Project>
     108      <Name>HeuristicLab.Operators.Programmable-3.3</Name>
     109    </ProjectReference>
     110    <ProjectReference Include="..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
     111      <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
     112      <Name>HeuristicLab.Operators-3.3</Name>
     113    </ProjectReference>
     114    <ProjectReference Include="..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
     115      <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>
     116      <Name>HeuristicLab.Optimization-3.3</Name>
     117    </ProjectReference>
     118    <ProjectReference Include="..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">
     119      <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>
     120      <Name>HeuristicLab.Parameters-3.3</Name>
     121    </ProjectReference>
     122    <ProjectReference Include="..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
     123      <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
     124      <Name>HeuristicLab.Persistence-3.3</Name>
     125    </ProjectReference>
     126    <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
     127      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
     128      <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
     129    </ProjectReference>
    132130    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj">
    133131      <Project>{5AC82412-911B-4FA2-A013-EDC5E3F3FCC2}</Project>
     
    138136      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name>
    139137    </ProjectReference>
     138    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
     139      <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>
     140      <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>
     141    </ProjectReference>
     142    <ProjectReference Include="..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj">
     143      <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project>
     144      <Name>HeuristicLab.Problems.Instances-3.3</Name>
     145    </ProjectReference>
     146    <ProjectReference Include="..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
     147      <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
     148      <Name>HeuristicLab.Random-3.3</Name>
     149    </ProjectReference>
    140150  </ItemGroup>
    141151  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
     152  <PropertyGroup>
     153    <PreBuildEvent>set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     154set ProjectDir=$(ProjectDir)
     155set SolutionDir=$(SolutionDir)
     156set Outdir=$(Outdir)
     157</PreBuildEvent>
     158  </PropertyGroup>
    142159  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
    143160       Other similar extension points exist, see Microsoft.Common.targets.
  • branches/sluengo/HeuristicLab.Problems.TradeRules/Interpreter.cs

    r9262 r9325  
    2424        private int initialTraining;
    2525        private int initialTest;
     26
    2627        [ThreadStatic]
    2728        private static Dictionary<ISymbolicExpressionTreeNode, double> signalCache;
     
    208209        #endregion
    209210
    210 
    211        
    212 
    213211        private double Evaluate(Dataset dataset, ref int row, InterpreterState state)
    214212        {
     
    248246                    {
    249247                        double sum = Evaluate(dataset, ref row, state);
    250                         int integerValue = (int) Math.Floor(sum);
     248                        int integerValue = (int)Math.Floor(sum);
    251249                        if (integerValue > 100) integerValue = 100;
    252250                        if (row < integerValue)
     
    254252                            string variableName = dataset.GetValue(row, 3);
    255253                            double inferiorValue = Convert.ToDouble(variableName);
    256                             return inferiorValue/(row+1);
     254                            return inferiorValue / (row + 1);
    257255                        }
    258256                        else
     
    268266                    {
    269267                        double result = Evaluate(dataset, ref row, state);
    270                         for (int i = 1; i < currentInstr.nArguments; i++)
    271                         {
    272                             if (result > 0.0) result = Evaluate(dataset, ref row, state);
    273                             else
    274                             {
    275                                 SkipInstructions(state);
    276                             }
    277                         }
    278                         return result > 0.0 ? 1.0 : -1.0;
     268                        double result2 = Evaluate(dataset, ref row, state);
     269                        double total = result + result2;
     270                        return total < 2 ? -1.0 : 1.0;
    279271                    }
    280272                case OpCodes.OR:
    281273                    {
    282274                        double result = Evaluate(dataset, ref row, state);
    283                         for (int i = 1; i < currentInstr.nArguments; i++)
    284                         {
    285                             if (result <= 0.0) result = Evaluate(dataset, ref row, state);
    286                             else
    287                             {
    288                                 SkipInstructions(state);
    289                             }
    290                         }
    291                         return result > 0.0 ? 1.0 : -1.0;
     275                        double result2 = Evaluate(dataset, ref row, state);
     276                        double total = result + result2;
     277                        return total > -2 ? 1.0 : -1.0;
    292278                    }
    293279                case OpCodes.NOT:
     
    339325                case OpCodes.Max:
    340326                    {
    341                         int n = (int) Evaluate(dataset, ref row, state);
     327                        int n = (int)Evaluate(dataset, ref row, state);
    342328                        double max = Double.NegativeInfinity;
    343                         int i = Math.Min(n,row);
    344                         while(i>=0)
     329                        int i = Math.Min(n, row);
     330                        while (i >= 0)
    345331                        {
    346332                            int position = row - i;
    347333                            string variableName = dataset.GetValue(position, 2);
    348334                            double intValue = Convert.ToDouble(variableName);
    349                             if (intValue>max) max = intValue;
    350                         i--;
     335                            if (intValue > max) max = intValue;
     336                            i--;
    351337                        }
    352338                        return max;
     
    365351                            i--;
    366352                        }
    367                           return min;
     353                        return min;
    368354                    }
    369355                case OpCodes.Lag:
    370356                    {
    371                         int n = (int) Evaluate(dataset, ref row, state);
    372                         if (n>row) return 0;
     357                        int n = (int)Evaluate(dataset, ref row, state);
     358                        if (n > row) return 0;
    373359                        int position = row - n;
    374360                        string variableName = dataset.GetValue(position, 2);
     
    378364                case OpCodes.MACD:
    379365                    {
    380                         var MACDNode = currentInstr.dynamicNode;
    381366                        //Taking the number of the days for each EMA
    382367                        double firstEMA = Evaluate(dataset, ref row, state);
    383368                        double secondEMA = Evaluate(dataset, ref row, state);
    384369                        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 
    391370                        //Initiation of the variables
    392371                        double firstElementEMA = -1000000;
     
    394373                        double signalValue = -100000;
    395374                        double macd = 0;
     375                        double longitud = 0;
    396376
    397377                        //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++)
     378                        if (firstEMACache.ContainsKey(currentInstr.dynamicNode)) firstElementEMA = firstEMACache[currentInstr.dynamicNode];
     379                        if (secondEMACache.ContainsKey(currentInstr.dynamicNode)) secondElementEMA = secondEMACache[currentInstr.dynamicNode];
     380                        if (signalCache.ContainsKey(currentInstr.dynamicNode)) signalValue = signalCache[currentInstr.dynamicNode];
     381
     382               
     383                            //Calculating the factor for each EMA
     384                            double factor = 2.0 / (firstEMA + 1.0);
     385                            double factor2 = 2.0 / (secondEMA + 1.0);
     386                            double factor3 = 2.0 / (signal + 1.0);
     387
     388                            //Calculate the first value in the training for the two EMAs and the signal.
     389                            if (row <= initialTraining || row == initialTest)
    410390                            {
    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
     391                                double [] meanValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, row+1)).ToArray();
     392                                firstElementEMA = meanValues[0];
     393                                secondElementEMA = meanValues[0];
     394                                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.
     395                                for (int i = 1; i < meanValues.Length; i++)
    415396                                {
    416                                     macd = firstElementEMA - secondElementEMA;
    417                                     signalValue = macd * factor3 + (1 - factor3) * signalValue;
     397                                    firstElementEMA = (meanValues[i] * factor) + ((1 - factor) * firstElementEMA);
     398                                    secondElementEMA = (meanValues[i] * factor2) + ((1 - factor2) * secondElementEMA);
     399                                    if (i == max) signalValue = firstElementEMA - secondElementEMA;//First signal equals to macd.
     400                                    else if (i > max)//Calculation for the next signals
     401                                    {
     402                                        macd = firstElementEMA - secondElementEMA;
     403                                        signalValue = macd * factor3 + (1 - factor3) * signalValue;
     404                                    }
    418405                                }
    419406                            }
    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;
     407                            else  //The rest of the rows are calculating with the standard EMA formula
     408                            {
     409                                //Retrieve the dataset values
     410                                string variableName = dataset.GetValue(row, 2);
     411                                double meanValue1 = Convert.ToDouble(variableName);
     412
     413                                //Calculating EMA
     414                                firstElementEMA = meanValue1 * factor + (1 - factor) * firstElementEMA;
     415                                secondElementEMA = meanValue1 * factor2 + (1 - factor2) * secondElementEMA;
     416
     417                                //Calculating signal
     418                                macd = firstElementEMA - secondElementEMA;
     419                                signalValue = macd * factor3 + (1 - factor3) * signalValue;
     420                            }
     421                           
     422                            //Save the values for the next iteration
     423                            firstEMACache[currentInstr.dynamicNode] = firstElementEMA;
     424                            secondEMACache[currentInstr.dynamicNode] = secondElementEMA;
     425                            signalCache[currentInstr.dynamicNode] = signalValue;
     426
    442427                        return macd > signalValue ? 1.0 : -1.0;
    443428                    }
    444429                case OpCodes.RSI:
    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)
     430                    {
     431                        //Taking the number of the days for EMA
     432                        double numberOfDays = Evaluate(dataset, ref row, state);
     433
     434                        double positiveEMA = 0;
     435                        double negativeEMA = 0;
     436                        double yesterdayRSI = double.NegativeInfinity;
     437                        double todayRSI = double.NegativeInfinity;
     438                        double outputRSI = double.NegativeInfinity;
     439                        //Retrieve EMA values
     440                        if (RSIPositiveCache.ContainsKey(currentInstr.dynamicNode)) positiveEMA = RSIPositiveCache[currentInstr.dynamicNode];
     441                        if (RSINegativeCache.ContainsKey(currentInstr.dynamicNode)) negativeEMA = RSINegativeCache[currentInstr.dynamicNode];
     442                        if (RSICache.ContainsKey(currentInstr.dynamicNode)) yesterdayRSI = RSICache[currentInstr.dynamicNode];
     443                        if (RSIOutputCache.ContainsKey(currentInstr.dynamicNode)) outputRSI = RSIOutputCache[currentInstr.dynamicNode];
     444
     445       
     446                            //Calculate the factor for the EMA
     447                            double factor = 1.0 / numberOfDays;
     448
     449                            if (row == initialTraining || row == initialTest)
     450                            {
     451                                double[] closeValues = dataset.GetDoubleValues("\"Close\"", Enumerable.Range(0, (row + 1))).ToArray();
     452                                outputRSI = -1.0;
     453                                for (int i = 1; i <= row; i++)
     454                                {
     455                                    if (numberOfDays >= i)
    482456                                    {
    483                                         positiveEMA = (closeValues[i]-closeValues[i-1]) * factor + (1 - factor) * positiveEMA;
    484                                         negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
     457                                        if ((closeValues[i] - closeValues[i - 1]) > 0) positiveEMA = ((closeValues[i] - closeValues[i - 1]) + positiveEMA);
     458                                        else negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) + negativeEMA;
     459                                        if (numberOfDays == i)
     460                                        {
     461                                            positiveEMA = positiveEMA / numberOfDays;
     462                                            negativeEMA = negativeEMA / numberOfDays;
     463                                            yesterdayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     464                                        }
    485465                                    }
    486466                                    else
    487467                                    {
    488                                         positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
    489                                         negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * negativeEMA;
     468                                        if ((closeValues[i] - closeValues[i - 1]) > 0)
     469                                        {
     470                                            positiveEMA = (closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * positiveEMA;
     471                                            negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
     472                                        }
     473                                        else
     474                                        {
     475                                            positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
     476                                            negativeEMA = Math.Abs(closeValues[i] - closeValues[i - 1]) * factor + (1 - factor) * negativeEMA;
     477                                        }
     478
     479                                        todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     480
     481                                        if ((yesterdayRSI < 30) && (todayRSI > 30)) outputRSI = 1.0;
     482                                        else if ((yesterdayRSI > 70) && (todayRSI < 70)) outputRSI = -1.0;
     483                                        yesterdayRSI = todayRSI;
    490484                                    }
    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;
    530                     }
    531                
     485                                }
     486                            }
     487                            else
     488                            {
     489                                string todayCloseString = dataset.GetValue(row, 2);
     490                                string yesterdayCloseString = dataset.GetValue((row - 1), 2);
     491                                double todayClose = Convert.ToDouble(todayCloseString);
     492                                double yesterdayClose = Convert.ToDouble(yesterdayCloseString);
     493
     494                                //Calculating EMA
     495                                if ((todayClose - yesterdayClose) > 0)
     496                                {
     497                                    positiveEMA = (todayClose - yesterdayClose) * factor + (1 - factor) * positiveEMA;
     498                                    negativeEMA = 0 * factor + (1 - factor) * negativeEMA;
     499                                }
     500                                else
     501                                {
     502                                    positiveEMA = 0 * factor + (1 - factor) * positiveEMA;
     503                                    negativeEMA = Math.Abs(todayClose - yesterdayClose) * factor + (1 - factor) * negativeEMA;
     504                                }
     505                                todayRSI = 100 - (100 / (1 + (positiveEMA / negativeEMA)));
     506                                if ((yesterdayRSI < 30) && (todayRSI > 30)) outputRSI = 1.0;
     507                                else if ((yesterdayRSI > 70) && (todayRSI < 70)) outputRSI = -1.0;
     508                            }
     509
     510                            //Save positive and negative EMA for the next iteration
     511                            RSIPositiveCache[currentInstr.dynamicNode] = positiveEMA;
     512                            RSINegativeCache[currentInstr.dynamicNode] = negativeEMA;
     513                            RSICache[currentInstr.dynamicNode] = todayRSI;
     514                            RSIOutputCache[currentInstr.dynamicNode] = outputRSI;
     515                           
     516                       
     517                        return outputRSI;
     518                    }
     519
    532520                default: throw new NotSupportedException();
    533521            }
     
    598586        {
    599587            if (CheckExpressionsWithIntervalArithmetic.Value)
    600             throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
     588                throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    601589            EvaluatedSolutions.Value++; // increment the evaluated solutions counter
    602590            var compiler = new SymbolicExpressionTreeCompiler();
     
    614602            }
    615603
    616 
    617604            if (signalCache == null) signalCache = new Dictionary<ISymbolicExpressionTreeNode, double>();
    618605            if (firstEMACache == null) firstEMACache = new Dictionary<ISymbolicExpressionTreeNode, double>();
     
    622609            if (RSICache == null) RSICache = new Dictionary<ISymbolicExpressionTreeNode, double>();
    623610            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                                         }
    635                 }
    636         }
    637 
     611
     612
     613            var state = new InterpreterState(code, necessaryArgStackSize);
     614            //Evaluate each row of the datase
     615            foreach (var rowEnum in rows)
     616            {
     617                int row = rowEnum;
     618                state.Reset();
     619                if (row < initialTraining) yield return -1;
     620                else yield return Evaluate(dataset, ref row, state);
     621            }
     622        }
    638623    }
    639624
     625}
     626
  • branches/sluengo/HeuristicLab.Problems.TradeRules/Symbols/MACDTreeNode.cs

    r9262 r9325  
    66using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    77using HeuristicLab.Common;
     8using HeuristicLab.Core;
    89
    910namespace HeuristicLab.Problems.TradeRules.Symbols
     
    1112    public sealed class  MACDTreeNode : SymbolicExpressionTreeNode
    1213    {
    13       public new MACD Symbol {
    14       get { return (MACD)base.Symbol;}
    15     }
     14
     15        public new MACD Symbol
     16        {
     17            get { return (MACD)base.Symbol; }
     18        }
     19       
     20        [Storable]
     21        private double idMACD;
     22        public double ID
     23        {
     24            get {return idMACD; }
     25            set { idMACD = value; }
     26        }
    1627
    1728    [StorableConstructor]
     
    2031    private MACDTreeNode(MACDTreeNode original, Cloner cloner)
    2132      : base(original, cloner) {
    22      
     33          idMACD = original.idMACD;
    2334    }
    2435
     
    3142    }
    3243
     44    public override void ResetLocalParameters(IRandom random)
     45    {
     46        base.ResetLocalParameters(random);
     47
     48        idMACD = random.NextDouble();
     49    }
     50
    3351    public override IDeepCloneable Clone(Cloner cloner) {
    3452      return new MACDTreeNode(this, cloner);
Note: See TracChangeset for help on using the changeset viewer.