using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Problems.TradeRules { [Item("Trade Rules Evaluator", "Calculates the profit")] [StorableClass] public class EvaluatorTradeRules : SymbolicRegressionSingleObjectiveEvaluator { public override bool Maximization { get { return true; } } [StorableConstructor] protected EvaluatorTradeRules(bool deserializing) : base(deserializing) { } protected EvaluatorTradeRules(EvaluatorTradeRules original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new EvaluatorTradeRules(this, cloner); } public EvaluatorTradeRules() : base() { } public override IOperation Apply() { var solution = SymbolicExpressionTreeParameter.ActualValue; IEnumerable rows = GenerateRowsToEvaluate(); double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows); QualityParameter.ActualValue = new DoubleValue(quality); return base.Apply(); } public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable rows) { IEnumerable estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); const double commission = 0.25; bool intoMarket = false;//Equivalent to be into the market int totalTradeDays = 0; int numberTrades = 0; int tradeDays = 0; double dayBefore = 0.0; double yesterday = 0.0; double buyPrice = 0.0; double sellPrice = 0.0; string stringPrice = ""; int nShares = 0; double cash = 10000.00; double expend = 0.0; double equity = 0.0; double profit1 = 0.0; double charged = 0.0; //SECOND EVALUATOR IEnumerator initialRow = rows.GetEnumerator(); initialRow.MoveNext(); int count = initialRow.Current + 2; IEnumerator enumerator = estimatedValues.GetEnumerator(); enumerator.MoveNext(); dayBefore = enumerator.Current; enumerator.MoveNext(); yesterday = enumerator.Current; while (enumerator.MoveNext()) { if (!intoMarket) { if (dayBefore == -1 && yesterday == 1) { intoMarket = true; stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values buyPrice = Convert.ToDouble(stringPrice); totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100))); expend = buyPrice * nShares * (1 + commission / 100); cash = cash - expend; equity = cash + nShares * buyPrice * (1 + commission / 100); } } else if (dayBefore == 1 && yesterday == -1) { intoMarket = false; stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values sellPrice = Convert.ToDouble(stringPrice); profit1 += sellPrice - buyPrice; charged = sellPrice * nShares * (1 - commission / 100); cash = cash + charged; equity = cash; nShares = 0; } else { tradeDays++; totalTradeDays++; equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count, 0)) * (1 + commission / 100); } dayBefore = yesterday; yesterday = enumerator.Current; count++; } if (intoMarket) { intoMarket = false; stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values sellPrice = Convert.ToDouble(stringPrice); profit1 += sellPrice - buyPrice; charged = sellPrice * nShares * (1 - commission / 100); cash = cash + charged; equity = cash; nShares = 0; } return cash; } public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows) { SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; EstimationLimitsParameter.ExecutionContext = context; double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows); SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; EstimationLimitsParameter.ExecutionContext = null; return r2; } } }