using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; namespace HeuristicLab.Problems.TradeRules { class OnlineTradeRulesCalculator { private static int numberTrades; private static int tradeDays; private static int totalTradeDays; public static double Calculate(IEnumerable estimatedValues, IRegressionProblemData problemData, IEnumerable rows) { double[] arrayOpen = problemData.Dataset.GetDoubleValues("\"Open\"").ToArray(); //Array with all Open prices int[] tick = rows.ToArray(); double[] arrayIO = estimatedValues.ToArray(); double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates const double COMISSION = 0.25; const double EAR = 5.00; //Effective Anual Rate const double CASHINI = 10000.00; int tickIni = tick[0]; int tickEnd = tick[tick.Length-1]; const double DAYSYEAR = 365.00; double DayRat = 0.0; //One day interest double interest = 0.0; // Interest for one day int nDias1 = 0; int nDias2 = 0; int dayBefore=0; int yesterday=0; int actualRow = 0; int nData = tick.Length; bool intoMarket = false;//Equivalent to be into the market numberTrades = 0; tradeDays = 0; totalTradeDays = 0; double buyPrice = 0.0; double sellPrice = 0.0; int nShares = 0; double cash = CASHINI; double expend = 0.0; double profitTrade = 0.0; double charged = 0.0; double [] equity = new double [nData]; DayRat = DAYSYEAR * (Math.Pow((1.0 + (EAR/100)), (1.0/ DAYSYEAR)) - 1.0); //First Day equity[0] = CASHINI; actualRow++; nDias2 = Convert.ToInt32(arrayDate[tick[1]] - arrayDate[tick[0]]); //Second day in the marquet. ----------------------------------------- cash = CASHINI * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2); equity[1] = cash; nDias2 = Convert.ToInt32(arrayDate[tick[2]] - arrayDate[tick[1]]); interest = cash * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2) - cash; dayBefore = Convert.ToInt32(arrayIO[0]); yesterday = Convert.ToInt32(arrayIO[1]); //Earnings computation ----------------------------------------------- actualRow = 2; while (actualRow<(nData-1)) { if (!intoMarket) { if (dayBefore == -1 && yesterday == 1) { intoMarket = true; buyPrice = arrayOpen[tick[actualRow]]; totalTradeDays++; numberTrades++; tradeDays=1; //Increasing trading variables nShares = Convert.ToInt32(Math.Floor(cash / (buyPrice * (1.0 + COMISSION / 100)))); expend = buyPrice * nShares * (1.0 + COMISSION / 100); cash = cash + interest - expend; equity[actualRow] = cash + nShares * (buyPrice * (1.0 - COMISSION / 100)); } else //Dia normal fuera del mercado { cash = cash + interest; equity[actualRow] = cash; } } else if (dayBefore == 1 && yesterday == -1) { intoMarket = false; sellPrice = Convert.ToDouble(arrayOpen[tick[actualRow]]); profitTrade = profitTrade + sellPrice - buyPrice; charged = nShares * sellPrice * (1.0 - COMISSION / 100); cash = cash + interest + charged; equity[actualRow] = cash; nShares = 0; totalTradeDays = totalTradeDays + nDias1 - 1; tradeDays = tradeDays + nDias1 - 1; tradeDays = 0; } else { totalTradeDays = totalTradeDays + nDias1; tradeDays = tradeDays + nDias1; cash = cash + interest; equity[actualRow] = cash + nShares * arrayOpen[tick[actualRow]] * (1.0 - COMISSION / 100); } nDias2 = Convert.ToInt32(arrayDate[tick[(actualRow + 1)]] - arrayDate[tick[actualRow]]); interest = cash * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2) - cash; actualRow++; nDias1 = nDias2; dayBefore = Convert.ToInt32(arrayIO[(actualRow - 2)]); yesterday = Convert.ToInt32(arrayIO[(actualRow - 1)]); } if (intoMarket) { intoMarket = false; sellPrice = Convert.ToDouble(arrayOpen[tick[actualRow]]); profitTrade = profitTrade + sellPrice - buyPrice; charged = nShares * sellPrice * (1.0 - COMISSION / 100); cash = cash + interest + charged; equity[actualRow] = cash; nShares = 0; totalTradeDays = totalTradeDays + nDias1 - 1; tradeDays = tradeDays + nDias1 - 1; } else { cash = cash + interest; equity[actualRow] = cash; } return cash; } public static int getNumberTrades() { return numberTrades; } public static int getTradeDays() { return tradeDays; } public static int getTotalTradesDays() { return totalTradeDays; } } }