Free cookie consent management tool by TermsFeed Policy Generator

source: branches/sluengo/HeuristicLab.Problems.TradeRules/Evaluator/OnlineTradeRulesCalculator.cs @ 15567

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