[9262] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[9386] | 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Data;
|
---|
| 8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[9262] | 9 |
|
---|
| 10 | namespace HeuristicLab.Problems.TradeRules
|
---|
| 11 | {
|
---|
| 12 | class OnlineTradeRulesCalculator
|
---|
| 13 | {
|
---|
[9386] | 14 | private static int numberTrades;
|
---|
| 15 | private static int tradeDays;
|
---|
| 16 | private static int totalTradeDays;
|
---|
| 17 |
|
---|
[9262] | 18 | public static double Calculate(IEnumerable<double> estimatedValues, IRegressionProblemData problemData, IEnumerable<int> rows)
|
---|
[9386] | 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;
|
---|
[9262] | 31 |
|
---|
[9386] | 32 | double DayRat = 0.0; //One day interest
|
---|
[9262] | 33 | double interest = 0.0; // Interest for one day
|
---|
| 34 | int nDias1 = 0;
|
---|
| 35 | int nDias2 = 0;
|
---|
[9386] | 36 | int dayBefore=0;
|
---|
| 37 | int yesterday=0;
|
---|
[9262] | 38 |
|
---|
[9386] | 39 | int actualRow = 0;
|
---|
| 40 | int nData = tick.Length;
|
---|
[9262] | 41 |
|
---|
| 42 | bool intoMarket = false;//Equivalent to be into the market
|
---|
[9386] | 43 | numberTrades = 0;
|
---|
| 44 | tradeDays = 0;
|
---|
| 45 | totalTradeDays = 0;
|
---|
[9262] | 46 | double buyPrice = 0.0;
|
---|
| 47 | double sellPrice = 0.0;
|
---|
| 48 | int nShares = 0;
|
---|
[9386] | 49 |
|
---|
| 50 | double cash = CASHINI;
|
---|
[9262] | 51 | double expend = 0.0;
|
---|
[9386] | 52 | double profitTrade = 0.0;
|
---|
[9262] | 53 | double charged = 0.0;
|
---|
| 54 |
|
---|
[9386] | 55 | double [] equity = new double [nData];
|
---|
| 56 | DayRat = DAYSYEAR * (Math.Pow((1.0 + (EAR/100)), (1.0/ DAYSYEAR)) - 1.0);
|
---|
[9262] | 57 |
|
---|
[9386] | 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;
|
---|
| 71 |
|
---|
| 72 | while (actualRow<(nData-1))
|
---|
[9262] | 73 | {
|
---|
| 74 | if (!intoMarket)
|
---|
| 75 | {
|
---|
| 76 | if (dayBefore == -1 && yesterday == 1)
|
---|
| 77 | {
|
---|
| 78 | intoMarket = true;
|
---|
[9386] | 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));
|
---|
[9262] | 85 | }
|
---|
| 86 | else //Dia normal fuera del mercado
|
---|
| 87 | {
|
---|
| 88 | cash = cash + interest;
|
---|
[9386] | 89 | equity[actualRow] = cash;
|
---|
[9262] | 90 | }
|
---|
| 91 | }
|
---|
| 92 | else if (dayBefore == 1 && yesterday == -1)
|
---|
| 93 | {
|
---|
| 94 | intoMarket = false;
|
---|
[9386] | 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;
|
---|
[9262] | 100 | nShares = 0;
|
---|
[9386] | 101 | totalTradeDays = totalTradeDays + nDias1 - 1;
|
---|
| 102 | tradeDays = tradeDays + nDias1 - 1;
|
---|
| 103 | tradeDays = 0;
|
---|
[9262] | 104 | }
|
---|
| 105 | else
|
---|
| 106 | {
|
---|
[9386] | 107 | totalTradeDays = totalTradeDays + nDias1;
|
---|
| 108 | tradeDays = tradeDays + nDias1;
|
---|
[9262] | 109 | cash = cash + interest;
|
---|
[9386] | 110 | equity[actualRow] = cash + nShares * arrayOpen[tick[actualRow]] * (1.0 - COMISSION / 100);
|
---|
| 111 |
|
---|
[9262] | 112 | }
|
---|
| 113 |
|
---|
[9386] | 114 | nDias2 = Convert.ToInt32(arrayDate[tick[(actualRow + 1)]] - arrayDate[tick[actualRow]]);
|
---|
| 115 | interest = cash * Math.Pow((1.0 + DayRat / DAYSYEAR), nDias2) - cash;
|
---|
| 116 | actualRow++;
|
---|
[9262] | 117 | nDias1 = nDias2;
|
---|
[9386] | 118 | dayBefore = Convert.ToInt32(arrayIO[(actualRow - 2)]);
|
---|
| 119 | yesterday = Convert.ToInt32(arrayIO[(actualRow - 1)]);
|
---|
| 120 |
|
---|
[9262] | 121 | }
|
---|
| 122 | if (intoMarket)
|
---|
| 123 | {
|
---|
| 124 | intoMarket = false;
|
---|
[9386] | 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;
|
---|
[9262] | 130 | nShares = 0;
|
---|
[9386] | 131 | totalTradeDays = totalTradeDays + nDias1 - 1;
|
---|
| 132 | tradeDays = tradeDays + nDias1 - 1;
|
---|
[9262] | 133 | }
|
---|
[9386] | 134 | else
|
---|
| 135 | {
|
---|
| 136 | cash = cash + interest;
|
---|
| 137 | equity[actualRow] = cash;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[9262] | 140 | return cash;
|
---|
| 141 | }
|
---|
[9386] | 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;
|
---|
| 154 | }
|
---|
[9262] | 155 | }
|
---|
| 156 | }
|
---|