- Timestamp:
- 04/20/13 14:08:49 (12 years ago)
- Location:
- branches/sluengo/HeuristicLab.Problems.TradeRules/Evaluator
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sluengo/HeuristicLab.Problems.TradeRules/Evaluator/EvaluatorTradeRules.cs
r9325 r9386 44 44 double quality = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows); 45 45 QualityParameter.ActualValue = new DoubleValue(quality); 46 ITradeRulesExpresionTree interpreter = (ITradeRulesExpresionTree)SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;47 interpreter.clearVariables();48 46 return base.Apply(); 49 47 } … … 52 50 { 53 51 interpreter.setInitialTraining(initialTraining); 54 interpreter.setInitialTest( initialTest);52 interpreter.setInitialTest(rows.ToArray()[0]); 55 53 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 54 interpreter.clearVariables(); 55 double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, rows); 56 56 57 double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, problemData.TrainingIndices);58 59 57 return tradingCash; 60 58 } … … 69 67 EstimationLimitsParameter.ExecutionContext = null; 70 68 71 72 69 return r2; 73 70 } -
branches/sluengo/HeuristicLab.Problems.TradeRules/Evaluator/OnlineTradeRulesCalculator.cs
r9262 r9386 4 4 using System.Text; 5 5 using HeuristicLab.Problems.DataAnalysis; 6 using HeuristicLab.Core; 7 using HeuristicLab.Data; 8 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 9 7 10 namespace HeuristicLab.Problems.TradeRules … … 9 12 class OnlineTradeRulesCalculator 10 13 { 14 private static int numberTrades; 15 private static int tradeDays; 16 private static int totalTradeDays; 17 11 18 public static double Calculate(IEnumerable<double> estimatedValues, IRegressionProblemData problemData, IEnumerable<int> rows) 12 {/* 13 const double commission = 0.25; 14 const double EAR = 4.00; //Effective Anual Rate 19 { 20 double[] arrayOpen = problemData.Dataset.GetDoubleValues("\"Open\"").ToArray(); //Array with all Open prices 21 int[] tick = rows.ToArray(); 22 double[] arrayIO = estimatedValues.ToArray(); 23 double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates 24 25 const double COMISSION = 0.25; 26 const double EAR = 5.00; //Effective Anual Rate 27 const double CASHINI = 10000.00; 28 int tickIni = tick[0]; 29 int tickEnd = tick[tick.Length-1]; 30 const double DAYSYEAR = 365.00; 15 31 16 double DayRat = 0 ; //One day interest32 double DayRat = 0.0; //One day interest 17 33 double interest = 0.0; // Interest for one day 18 double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates in UNIX format 34 int nDias1 = 0; 35 int nDias2 = 0; 36 int dayBefore=0; 37 int yesterday=0; 19 38 20 //int count = rows.ToList()[0]; 21 39 int actualRow = 0; 40 int nData = tick.Length; 41 22 42 bool intoMarket = false;//Equivalent to be into the market 23 int totalTradeDays = 0; 24 int numberTrades = 0; 25 int tradeDays = 0; 26 double dayBefore = 0.0; 27 double yesterday = 0.0; 43 numberTrades = 0; 44 tradeDays = 0; 45 totalTradeDays = 0; 28 46 double buyPrice = 0.0; 29 47 double sellPrice = 0.0; 30 string stringPrice = "";31 48 int nShares = 0; 32 double cash = 10000.00; 49 50 double cash = CASHINI; 33 51 double expend = 0.0; 34 double equity = 0.0; 35 double profit1 = 0.0; 52 double profitTrade = 0.0; 36 53 double charged = 0.0; 37 54 38 IEnumerator<int> initialRow = rows.GetEnumerator(); 39 initialRow.MoveNext(); 40 int numDays1 = (int)arrayDate[initialRow.Current-1]; 41 int numDays2 = (int) arrayDate[(initialRow.Current)]; 42 int count = initialRow.Current + 1; 43 44 IEnumerator<double> enumerator = estimatedValues.GetEnumerator(); 45 enumerator.MoveNext(); 46 dayBefore = enumerator.Current; 47 enumerator.MoveNext(); 48 yesterday = enumerator.Current; 55 double [] equity = new double [nData]; 56 DayRat = DAYSYEAR * (Math.Pow((1.0 + (EAR/100)), (1.0/ DAYSYEAR)) - 1.0); 49 57 50 DayRat = 360 * (Math.Pow((1 + (EAR / 100)), (1 / 360)) - 1); 58 //First Day 59 equity[0] = CASHINI; 60 actualRow++; 61 nDias2 = Convert.ToInt32(arrayDate[tick[1]] - arrayDate[tick[0]]); 62 //Second day in the marquet. ----------------------------------------- 63 cash = CASHINI * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2); 64 equity[1] = cash; 65 nDias2 = Convert.ToInt32(arrayDate[tick[2]] - arrayDate[tick[1]]); 66 interest = cash * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2) - cash; 67 dayBefore = Convert.ToInt32(arrayIO[0]); 68 yesterday = Convert.ToInt32(arrayIO[1]); 69 //Earnings computation ----------------------------------------------- 70 actualRow = 2; 51 71 52 while ( enumerator.MoveNext())72 while (actualRow<(nData-1)) 53 73 { 54 74 if (!intoMarket) … … 57 77 { 58 78 intoMarket = true; 59 stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values 60 buyPrice = Convert.ToDouble(stringPrice); 61 totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables 62 nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100))); 63 expend = buyPrice * nShares * (1 + commission / 100); 64 cash = cash - expend + interest; 65 equity = cash + nShares * buyPrice * (1 - commission / 100); 66 interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash; 79 buyPrice = arrayOpen[tick[actualRow]]; 80 totalTradeDays++; numberTrades++; tradeDays=1; //Increasing trading variables 81 nShares = Convert.ToInt32(Math.Floor(cash / (buyPrice * (1.0 + COMISSION / 100)))); 82 expend = buyPrice * nShares * (1.0 + COMISSION / 100); 83 cash = cash + interest - expend; 84 equity[actualRow] = cash + nShares * (buyPrice * (1.0 - COMISSION / 100)); 67 85 } 68 86 else //Dia normal fuera del mercado 69 87 { 70 88 cash = cash + interest; 71 equity = equity + cash; 72 interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash; 73 } 74 75 } 76 else if (dayBefore == 1 && yesterday == -1) 77 { 78 intoMarket = false; 79 stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values 80 sellPrice = Convert.ToDouble(stringPrice); 81 profit1 += sellPrice - buyPrice; 82 charged = sellPrice * nShares * (1 - commission / 100); 83 cash = cash + charged + interest; 84 equity = cash; 85 interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash; 86 nShares = 0; 87 } 88 else 89 { 90 cash = cash + interest; 91 tradeDays += numDays1; 92 totalTradeDays += numDays1; 93 equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count, 0)) * (1 - commission / 100); 94 interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash; 95 } 96 97 dayBefore = yesterday; 98 yesterday = enumerator.Current; 99 numDays1 = numDays2; 100 numDays2 = (int) arrayDate[count]; 101 count++; 102 } 103 if (intoMarket) 104 { 105 intoMarket = false; 106 stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values 107 sellPrice = Convert.ToDouble(stringPrice); 108 profit1 += sellPrice - buyPrice; 109 charged = sellPrice * nShares * (1 - commission / 100); 110 cash = cash + charged + interest; 111 equity = cash; 112 nShares = 0; 113 } 114 return numDays1;*/ 115 116 const double commission = 0.25; 117 const double EAR = 4.00; //Effective Anual Rate 118 119 double DayRat = 0; //One day interest 120 double interest = 0.0; // Interest for one day 121 double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates in UNIX format 122 int nDias1 = 0; 123 int nDias2 = 0; 124 125 126 bool intoMarket = false;//Equivalent to be into the market 127 int totalTradeDays = 0; 128 int numberTrades = 0; 129 int tradeDays = 0; 130 double buyPrice = 0.0; 131 double sellPrice = 0.0; 132 string stringPrice = ""; 133 int nShares = 0; 134 double cash = 10000.00; 135 double expend = 0.0; 136 double equity = 0.0; 137 double profit1 = 0.0; 138 double charged = 0.0; 139 140 //SECOND EVALUATOR 141 var enumerator = estimatedValues.GetEnumerator(); 142 int count = rows.ToList()[2]; 143 int count2 = 2; 144 int date1 = (int)arrayDate[rows.ToList()[0]]; 145 int date2 = (int)arrayDate[rows.ToList()[1]]; 146 147 enumerator.MoveNext(); 148 double dayBefore = enumerator.Current; 149 enumerator.MoveNext(); 150 double yesterday = enumerator.Current; 151 152 DayRat = 268 * (Math.Pow((1 + (EAR / 100)),(1 / 268)) - 1); 153 nDias2 = date2 - date1; 154 155 while (enumerator.MoveNext()) 156 { 157 if (!intoMarket) 158 { 159 if (dayBefore == -1 && yesterday == 1) 160 { 161 intoMarket = true; 162 stringPrice = problemData.Dataset.GetValue(count, 1); //Extracting Open values 163 buyPrice = Convert.ToDouble(stringPrice); 164 totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables 165 nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100))); 166 expend = buyPrice * nShares * (1 + commission / 100); 167 cash = cash - expend + interest; 168 equity = cash + nShares * buyPrice * (1 - commission / 100); 169 interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash; 170 } 171 else //Dia normal fuera del mercado 172 { 173 tradeDays++; 174 totalTradeDays++; 175 cash = cash + interest; 176 equity = equity+cash; 177 interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash; 89 equity[actualRow] = cash; 178 90 } 179 91 } … … 181 93 { 182 94 intoMarket = false; 183 stringPrice = problemData.Dataset.GetValue(count, 1); //Extracting Open values 184 sellPrice = Convert.ToDouble(stringPrice); 185 profit1 += sellPrice - buyPrice; 186 charged = sellPrice * nShares * (1 - commission / 100); 187 cash = cash + charged+interest; 188 equity = cash; 189 interest = cash + Math.Pow((1 + DayRat / 268), nDias2) - cash; 95 sellPrice = Convert.ToDouble(arrayOpen[tick[actualRow]]); 96 profitTrade = profitTrade + sellPrice - buyPrice; 97 charged = nShares * sellPrice * (1.0 - COMISSION / 100); 98 cash = cash + interest + charged; 99 equity[actualRow] = cash; 190 100 nShares = 0; 101 totalTradeDays = totalTradeDays + nDias1 - 1; 102 tradeDays = tradeDays + nDias1 - 1; 103 tradeDays = 0; 191 104 } 192 105 else 193 106 { 107 totalTradeDays = totalTradeDays + nDias1; 108 tradeDays = tradeDays + nDias1; 194 109 cash = cash + interest; 195 tradeDays++; 196 totalTradeDays++; 197 equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count,1)) * (1 - commission / 100); 198 interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash; 110 equity[actualRow] = cash + nShares * arrayOpen[tick[actualRow]] * (1.0 - COMISSION / 100); 111 199 112 } 200 113 201 dayBefore = yesterday; 202 yesterday = enumerator.Current; 203 date1 = date2; 204 date2 = (int)arrayDate[rows.ToList()[count2]]; 205 206 int nDiasAux = nDias1; 114 nDias2 = Convert.ToInt32(arrayDate[tick[(actualRow + 1)]] - arrayDate[tick[actualRow]]); 115 interest = cash * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2) - cash; 116 actualRow++; 207 117 nDias1 = nDias2; 208 nDias2 = date2-date1; 209 210 count++; 211 count2++; 118 dayBefore = Convert.ToInt32(arrayIO[(actualRow - 2)]); 119 yesterday = Convert.ToInt32(arrayIO[(actualRow - 1)]); 120 212 121 } 213 214 122 if (intoMarket) 215 123 { 216 124 intoMarket = false; 217 stringPrice = problemData.Dataset.GetValue((count-1), 1); //Extracting Open values 218 sellPrice = Convert.ToDouble(stringPrice); 219 profit1 += sellPrice - buyPrice; 220 charged = sellPrice * nShares * (1 - commission / 100); 221 cash = cash + charged + interest; 222 equity = cash; 125 sellPrice = Convert.ToDouble(arrayOpen[tick[actualRow]]); 126 profitTrade = profitTrade + sellPrice - buyPrice; 127 charged = nShares * sellPrice * (1.0 - COMISSION / 100); 128 cash = cash + interest + charged; 129 equity[actualRow] = cash; 223 130 nShares = 0; 131 totalTradeDays = totalTradeDays + nDias1 - 1; 132 tradeDays = tradeDays + nDias1 - 1; 224 133 } 134 else 135 { 136 cash = cash + interest; 137 equity[actualRow] = cash; 138 } 139 225 140 return cash; 141 } 142 143 public static int getNumberTrades() 144 { 145 return numberTrades; 146 } 147 public static int getTradeDays() 148 { 149 return tradeDays; 150 } 151 public static int getTotalTradesDays() 152 { 153 return totalTradeDays; 226 154 } 227 155 }
Note: See TracChangeset
for help on using the changeset viewer.