1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Problems.DataAnalysis;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.TradeRules
|
---|
8 | {
|
---|
9 | class OnlineTradeRulesCalculator
|
---|
10 | {
|
---|
11 | public static double Calculate(IEnumerable<double> estimatedValues, IRegressionProblemData problemData, IEnumerable<int> rows)
|
---|
12 | {/*
|
---|
13 | const double commission = 0.25;
|
---|
14 | const double EAR = 4.00; //Effective Anual Rate
|
---|
15 |
|
---|
16 | double DayRat = 0; //One day interest
|
---|
17 | double interest = 0.0; // Interest for one day
|
---|
18 | double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates in UNIX format
|
---|
19 |
|
---|
20 | //int count = rows.ToList()[0];
|
---|
21 |
|
---|
22 | bool intoMarket = false;//Equivalent to be into the market
|
---|
23 | int totalTradeDays = 0;
|
---|
24 | int numberTrades = 0;
|
---|
25 | int tradeDays = 0;
|
---|
26 | double dayBefore = 0.0;
|
---|
27 | double yesterday = 0.0;
|
---|
28 | double buyPrice = 0.0;
|
---|
29 | double sellPrice = 0.0;
|
---|
30 | string stringPrice = "";
|
---|
31 | int nShares = 0;
|
---|
32 | double cash = 10000.00;
|
---|
33 | double expend = 0.0;
|
---|
34 | double equity = 0.0;
|
---|
35 | double profit1 = 0.0;
|
---|
36 | double charged = 0.0;
|
---|
37 |
|
---|
38 | IEnumerator<int> initialRow = rows.GetEnumerator();
|
---|
39 | initialRow.MoveNext();
|
---|
40 | int numDays1 = (int)arrayDate[initialRow.Current-1];
|
---|
41 | int numDays2 = (int) arrayDate[(initialRow.Current)];
|
---|
42 | int count = initialRow.Current + 1;
|
---|
43 |
|
---|
44 | IEnumerator<double> enumerator = estimatedValues.GetEnumerator();
|
---|
45 | enumerator.MoveNext();
|
---|
46 | dayBefore = enumerator.Current;
|
---|
47 | enumerator.MoveNext();
|
---|
48 | yesterday = enumerator.Current;
|
---|
49 |
|
---|
50 | DayRat = 360 * (Math.Pow((1 + (EAR / 100)), (1 / 360)) - 1);
|
---|
51 |
|
---|
52 | while (enumerator.MoveNext())
|
---|
53 | {
|
---|
54 | if (!intoMarket)
|
---|
55 | {
|
---|
56 | if (dayBefore == -1 && yesterday == 1)
|
---|
57 | {
|
---|
58 | intoMarket = true;
|
---|
59 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
60 | buyPrice = Convert.ToDouble(stringPrice);
|
---|
61 | totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables
|
---|
62 | nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100)));
|
---|
63 | expend = buyPrice * nShares * (1 + commission / 100);
|
---|
64 | cash = cash - expend + interest;
|
---|
65 | equity = cash + nShares * buyPrice * (1 - commission / 100);
|
---|
66 | interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash;
|
---|
67 | }
|
---|
68 | else //Dia normal fuera del mercado
|
---|
69 | {
|
---|
70 | cash = cash + interest;
|
---|
71 | equity = equity + cash;
|
---|
72 | interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash;
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 | else if (dayBefore == 1 && yesterday == -1)
|
---|
77 | {
|
---|
78 | intoMarket = false;
|
---|
79 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
80 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
81 | profit1 += sellPrice - buyPrice;
|
---|
82 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
83 | cash = cash + charged + interest;
|
---|
84 | equity = cash;
|
---|
85 | interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash;
|
---|
86 | nShares = 0;
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | cash = cash + interest;
|
---|
91 | tradeDays += numDays1;
|
---|
92 | totalTradeDays += numDays1;
|
---|
93 | equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count, 0)) * (1 - commission / 100);
|
---|
94 | interest = cash * Math.Pow((1 + DayRat / 360), numDays2) - cash;
|
---|
95 | }
|
---|
96 |
|
---|
97 | dayBefore = yesterday;
|
---|
98 | yesterday = enumerator.Current;
|
---|
99 | numDays1 = numDays2;
|
---|
100 | numDays2 = (int) arrayDate[count];
|
---|
101 | count++;
|
---|
102 | }
|
---|
103 | if (intoMarket)
|
---|
104 | {
|
---|
105 | intoMarket = false;
|
---|
106 | stringPrice = problemData.Dataset.GetValue(count, 0); //Extracting Open values
|
---|
107 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
108 | profit1 += sellPrice - buyPrice;
|
---|
109 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
110 | cash = cash + charged + interest;
|
---|
111 | equity = cash;
|
---|
112 | nShares = 0;
|
---|
113 | }
|
---|
114 | return numDays1;*/
|
---|
115 |
|
---|
116 | const double commission = 0.25;
|
---|
117 | const double EAR = 4.00; //Effective Anual Rate
|
---|
118 |
|
---|
119 | double DayRat = 0; //One day interest
|
---|
120 | double interest = 0.0; // Interest for one day
|
---|
121 | double[] arrayDate = problemData.Dataset.GetDoubleValues("\"Date\"").ToArray(); //Array with all dates in UNIX format
|
---|
122 | int nDias1 = 0;
|
---|
123 | int nDias2 = 0;
|
---|
124 |
|
---|
125 |
|
---|
126 | bool intoMarket = false;//Equivalent to be into the market
|
---|
127 | int totalTradeDays = 0;
|
---|
128 | int numberTrades = 0;
|
---|
129 | int tradeDays = 0;
|
---|
130 | double buyPrice = 0.0;
|
---|
131 | double sellPrice = 0.0;
|
---|
132 | string stringPrice = "";
|
---|
133 | int nShares = 0;
|
---|
134 | double cash = 10000.00;
|
---|
135 | double expend = 0.0;
|
---|
136 | double equity = 0.0;
|
---|
137 | double profit1 = 0.0;
|
---|
138 | double charged = 0.0;
|
---|
139 |
|
---|
140 | //SECOND EVALUATOR
|
---|
141 | var enumerator = estimatedValues.GetEnumerator();
|
---|
142 | int count = rows.ToList()[2];
|
---|
143 | int count2 = 2;
|
---|
144 | int date1 = (int)arrayDate[rows.ToList()[0]];
|
---|
145 | int date2 = (int)arrayDate[rows.ToList()[1]];
|
---|
146 |
|
---|
147 | enumerator.MoveNext();
|
---|
148 | double dayBefore = enumerator.Current;
|
---|
149 | enumerator.MoveNext();
|
---|
150 | double yesterday = enumerator.Current;
|
---|
151 |
|
---|
152 | DayRat = 268 * (Math.Pow((1 + (EAR / 100)),(1 / 268)) - 1);
|
---|
153 | nDias2 = date2 - date1;
|
---|
154 |
|
---|
155 | while (enumerator.MoveNext())
|
---|
156 | {
|
---|
157 | if (!intoMarket)
|
---|
158 | {
|
---|
159 | if (dayBefore == -1 && yesterday == 1)
|
---|
160 | {
|
---|
161 | intoMarket = true;
|
---|
162 | stringPrice = problemData.Dataset.GetValue(count, 1); //Extracting Open values
|
---|
163 | buyPrice = Convert.ToDouble(stringPrice);
|
---|
164 | totalTradeDays++; numberTrades++; tradeDays++; //Increasing trading variables
|
---|
165 | nShares = (int)Math.Floor(cash / (buyPrice * (1.0 + commission / 100)));
|
---|
166 | expend = buyPrice * nShares * (1 + commission / 100);
|
---|
167 | cash = cash - expend + interest;
|
---|
168 | equity = cash + nShares * buyPrice * (1 - commission / 100);
|
---|
169 | interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash;
|
---|
170 | }
|
---|
171 | else //Dia normal fuera del mercado
|
---|
172 | {
|
---|
173 | tradeDays++;
|
---|
174 | totalTradeDays++;
|
---|
175 | cash = cash + interest;
|
---|
176 | equity = equity+cash;
|
---|
177 | interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else if (dayBefore == 1 && yesterday == -1)
|
---|
181 | {
|
---|
182 | intoMarket = false;
|
---|
183 | stringPrice = problemData.Dataset.GetValue(count, 1); //Extracting Open values
|
---|
184 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
185 | profit1 += sellPrice - buyPrice;
|
---|
186 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
187 | cash = cash + charged+interest;
|
---|
188 | equity = cash;
|
---|
189 | interest = cash + Math.Pow((1 + DayRat / 268), nDias2) - cash;
|
---|
190 | nShares = 0;
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | cash = cash + interest;
|
---|
195 | tradeDays++;
|
---|
196 | totalTradeDays++;
|
---|
197 | equity = cash + nShares * Convert.ToDouble(problemData.Dataset.GetValue(count,1)) * (1 - commission / 100);
|
---|
198 | interest = cash * Math.Pow((1 + DayRat / 268), nDias2) - cash;
|
---|
199 | }
|
---|
200 |
|
---|
201 | dayBefore = yesterday;
|
---|
202 | yesterday = enumerator.Current;
|
---|
203 | date1 = date2;
|
---|
204 | date2 = (int)arrayDate[rows.ToList()[count2]];
|
---|
205 |
|
---|
206 | int nDiasAux = nDias1;
|
---|
207 | nDias1 = nDias2;
|
---|
208 | nDias2 = date2-date1;
|
---|
209 |
|
---|
210 | count++;
|
---|
211 | count2++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (intoMarket)
|
---|
215 | {
|
---|
216 | intoMarket = false;
|
---|
217 | stringPrice = problemData.Dataset.GetValue((count-1), 1); //Extracting Open values
|
---|
218 | sellPrice = Convert.ToDouble(stringPrice);
|
---|
219 | profit1 += sellPrice - buyPrice;
|
---|
220 | charged = sellPrice * nShares * (1 - commission / 100);
|
---|
221 | cash = cash + charged + interest;
|
---|
222 | equity = cash;
|
---|
223 | nShares = 0;
|
---|
224 | }
|
---|
225 | return cash;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|