-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
212 lines (164 loc) · 5.27 KB
/
main.cpp
File metadata and controls
212 lines (164 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include "PriceAtDate.h"
#include "FinanceDataLoader.h"
#include "CSVParser.h"
#include "Stock.h"
#include "ScriptManager.h"
#include "mean.h"
#include "stdev.h"
#include "mvg.h"
#include "log_series.h"
#include "ValueAtRisk.h"
#include "HttpProcessServer.h"
#include "ScriptEnvironment.h"
#include <iostream>
#include <math.h>
using namespace quantserver ;
using namespace std;
void shouldParsePriceAtDateCorrectly() ;
void shouldDownloadFileCorrectly();
void shoudParseCsvCorrectly();
void shouldParseStockCorrectly();
void shouldRunChaiScript();
void shouldDoAverageStdevAndMvg();
void shouldDoAverageOfLogSeries();
void shouldCalculateVaR() ;
void shouldCreateHttpServer() ;
void shouldShouldCalculateAvgInScript() ;
int main()
{
std::cout << "Hello world from QuantServer" << std::endl;
//shouldParsePriceAtDateCorrectly() ;
//shouldDownloadFileCorrectly() ;
//shoudParseCsvCorrectly() ;
//shouldParseStockCorrectly();
//shouldRunChaiScript();
//shouldDoAverageStdevAndMvg();
//shouldDoAverageOfLogSeries();
//shouldCalculateVaR() ;
//shouldCreateHttpServer();
//shouldRunChaiScript() ;
shouldShouldCalculateAvgInScript() ;
system("pause") ;
return 0;
}
void shouldShouldCalculateAvgInScript()
{
ScriptEnvironment env ;
ScriptManager& script = env(); ;
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test1.csv");
string stockName = "test";
Stock testStock(stockName);
testStock.loadData(csvParser);
script.engine().add(chaiscript::var(testStock) , "stock") ;
try
{
script.engine().eval<BIGDECIMAL>("var c = stock[\"Close\"];var v = mean(c);print(v);");
}
catch(std::exception ex)
{
cout << ex.what() << endl ;
}
}
void shouldCreateHttpServer()
{
HttpProcessServer::start() ;
}
void shouldCalculateVaR()
{
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test1.csv");
string stockName = "test";
Stock testStock(stockName);
testStock.loadData(csvParser);
cout << "Test Data loaded" << endl;
log_series logs;
mean avg;
stdev sd;
ValueAtRisk var(0.95) ;
BIGDECIMAL meanVal = avg(logs(testStock["Close"])) ;
BIGDECIMAL lastVal = testStock["Close"][0].val() ;
cout << "Average of log is " << meanVal << endl;
cout << "Stdev of log is " << sd(logs(testStock["Close"])) << endl;
BIGDECIMAL v = var(logs(testStock["Close"])) ;
cout << "Value at Risk is " << v << endl;
cout << "Last Val is " << lastVal << endl;
cout << "Value at Risk is " << ((::abs(::exp(v) - lastVal)/lastVal)*100) << " percent" << endl;
}
void shouldDoAverageOfLogSeries()
{
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test1.csv");
string stockName = "test";
Stock testStock(stockName);
testStock.loadData(csvParser);
cout << "Test Data loaded" << endl;
log_series logs;
mean avg;
stdev sd;
cout << "Average of log is " << avg(logs(testStock["Close"])) << endl;
cout << "Stdev of log is " << sd(logs(testStock["Close"])) << endl;
}
void shouldDoAverageStdevAndMvg()
{
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test1.csv");
string stockName = "test";
Stock testStock(stockName);
testStock.loadData(csvParser);
cout << "Test Data loaded" << endl;
//Now that we have the stock data, lets print out some values
mean avg;
cout << "Average is " << avg(testStock["Close"]) << endl;
stdev sd;
cout << "Stdev is " << sd(testStock["Close"]) << endl;
mvg mvg3;
TimeSeries& movingAvgRes = mvg3(testStock["Close"]);
for(int i = 0 ; i < movingAvgRes.size() ; i++)
{
cout << "day[" << i << "] = " << movingAvgRes[i].val() << endl ;
}
}
void shouldRunChaiScript()
{
ScriptManager scriptManager;
scriptManager.engine().eval("print(\"hello world from chai\")");
double d = scriptManager.engine().eval<double>("3 + 4.75;");
cout << "Chai simple add (3 + 4.75): " << d << endl ;
}
void shouldParseStockCorrectly()
{
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test.csv");
string stockName = "test";
Stock testStock(stockName);
testStock.loadData(csvParser);
cout << "Data loaded" << endl;
//Now that we have the stock data, lets print out some values
cout << "1st day has date of: " << testStock["High"][0].getDate() << endl;
cout << "2nd day closed at: " << testStock["Close"][1].val() << endl;
cout << "7st day has volume of: " << testStock["Volume"][6].val() << endl;
}
void shoudParseCsvCorrectly()
{
CSVParser csvParser("C:\\workspaces\\cppFinance\\localfiles\\test.csv");
int numberOfLines = csvParser.getNumberOfLines();
int numberOfColumns = csvParser.getNumberOfColumns() ;
for(int i = 0 ; i < numberOfColumns ; i++ )
{
std::cout << csvParser.getHeaderOfIndex(i) << " , " ;
}
std::cout << std::endl;
std::cout << "Data read: " << numberOfLines << " lines" << endl;
}
void shouldDownloadFileCorrectly()
{
cout << " --- Downloading from http://ichart.finance.yahoo.com/table.csv?s=INTC ---" << endl;
string url = "http://ichart.finance.yahoo.com/table.csv?s=INTC" ;
FinanceDataLoader financeDataLoader(url , true);
cout << "Data available at: " << financeDataLoader.getLocalReadable() << endl;
}
void shouldParsePriceAtDateCorrectly()
{
cout << " --- Parsing PriceAtDate values ---" << endl;
PriceAtDate priceAtDate("14/4/2009" , "10.2");
cout << "Date is: " << priceAtDate.getDate() << endl ;
cout << "Price is: " << priceAtDate.getPrice() << endl ;
cout << " --- Done ---" << endl;
}