[9139] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 |
|
---|
| 6 | using HeuristicLab.Common;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Data;
|
---|
| 9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 11 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 12 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 13 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 14 |
|
---|
| 15 | namespace HeuristicLab.Problems.TradeRules
|
---|
| 16 | {
|
---|
| 17 | [Item("Trade Rules Evaluator", "Calculates the profit")]
|
---|
| 18 | [StorableClass]
|
---|
| 19 | public class EvaluatorTradeRules : SymbolicRegressionSingleObjectiveEvaluator
|
---|
| 20 | {
|
---|
| 21 |
|
---|
| 22 | public override bool Maximization { get { return true; } }
|
---|
| 23 |
|
---|
| 24 | [StorableConstructor]
|
---|
| 25 | protected EvaluatorTradeRules(bool deserializing) : base(deserializing) { }
|
---|
| 26 | protected EvaluatorTradeRules(EvaluatorTradeRules original, Cloner cloner)
|
---|
| 27 | : base(original, cloner)
|
---|
| 28 | {
|
---|
| 29 | }
|
---|
| 30 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 31 | {
|
---|
| 32 | return new EvaluatorTradeRules(this, cloner);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public EvaluatorTradeRules() : base() { }
|
---|
| 36 |
|
---|
| 37 | public override IOperation Apply()
|
---|
| 38 | {
|
---|
| 39 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 40 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
| 41 |
|
---|
| 42 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
|
---|
| 43 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 44 |
|
---|
| 45 | return base.Apply();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows)
|
---|
| 49 | {
|
---|
| 50 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
| 51 | const double commission = 0.25;
|
---|
| 52 |
|
---|
| 53 | bool intoMarket = false;//Equivalent to be into the market
|
---|
| 54 | int totalTradeDays = 0;
|
---|
| 55 | int numberTrades = 0;
|
---|
| 56 | int tradeDays = 0;
|
---|
| 57 | double dayBefore = 0.0;
|
---|
| 58 | double yesterday = 0.0;
|
---|
| 59 | double buyPrice = 0.0;
|
---|
| 60 | double sellPrice = 0.0;
|
---|
| 61 | string stringPrice = "";
|
---|
| 62 | int nShares = 0;
|
---|
| 63 | double cash = 10000.00;
|
---|
| 64 | double expend = 0.0;
|
---|
| 65 | double equity = 0.0;
|
---|
| 66 | double profit1 = 0.0;
|
---|
| 67 | double charged = 0.0;
|
---|
| 68 |
|
---|
| 69 | //SECOND EVALUATOR
|
---|
| 70 | IEnumerator<int> initialRow = rows.GetEnumerator();
|
---|
| 71 | initialRow.MoveNext();
|
---|
| 72 | int count = initialRow.Current + 2;
|
---|
| 73 | IEnumerator<double> enumerator = estimatedValues.GetEnumerator();
|
---|
| 74 | enumerator.MoveNext();
|
---|
| 75 | dayBefore = enumerator.Current;
|
---|
| 76 | enumerator.MoveNext();
|
---|
| 77 | yesterday = enumerator.Current;
|
---|
| 78 | while (enumerator.MoveNext())
|
---|
| 79 | {
|
---|
| 80 | if (!intoMarket)
|
---|
| 81 | {
|
---|
| 82 | if (dayBefore == -1 && yesterday == 1)
|
---|
| 83 | {
|
---|
| 84 | intoMarket = true;
|
---|
| 85 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
| 86 | buyPrice = Convert.ToDouble(stringPrice);
|
---|
| 87 | totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables
|
---|
| 88 | nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100)));
|
---|
| 89 | expend = buyPrice * nShares * (1 + commission / 100);
|
---|
| 90 | cash = cash - expend;
|
---|
| 91 | equity = cash + nShares * buyPrice * (1 + commission / 100);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | else if (dayBefore == 1 && yesterday == -1)
|
---|
| 95 | {
|
---|
| 96 | intoMarket = false;
|
---|
| 97 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
| 98 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
| 99 | profit1 += sellPrice - buyPrice;
|
---|
| 100 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
| 101 | cash = cash + charged;
|
---|
| 102 | equity = cash;
|
---|
| 103 | nShares = 0;
|
---|
| 104 | }
|
---|
| 105 | else
|
---|
| 106 | {
|
---|
| 107 | tradeDays++;
|
---|
| 108 | totalTradeDays++;
|
---|
| 109 | equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count, 0)) * (1 + commission / 100);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | dayBefore = yesterday;
|
---|
| 113 | yesterday = enumerator.Current;
|
---|
| 114 | count++;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (intoMarket)
|
---|
| 118 | {
|
---|
| 119 | intoMarket = false;
|
---|
| 120 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
| 121 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
| 122 | profit1 += sellPrice - buyPrice;
|
---|
| 123 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
| 124 | cash = cash + charged;
|
---|
| 125 | equity = cash;
|
---|
| 126 | nShares = 0;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return cash;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows)
|
---|
| 133 | {
|
---|
| 134 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
| 135 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
| 136 |
|
---|
| 137 | double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
|
---|
| 138 |
|
---|
| 139 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
| 140 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
| 141 |
|
---|
| 142 | return r2;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|