Hi
I'm working through a finance book which is doing something rather simple, which I could probably do in excel but wanted to try in C++.
Being not overly versed in this, I'm coming across a problem trying to calculate a value in a loop which I then want to assign that value into an array. The code below should compile fine, and doesn't error, but each time I run the loop I keep getting 0s for all values of the array.
Basically I'm not sure I understand how I should get the value of
double test
into
double* pDailyRet
which I then want to use to fill up the array
double dailyRet[sizeVector];
The first 20 lines of the input file are at the bottom.
All the couts are to give me an idea of what is in the pointer and what it is pointing to ... but I'm still not getting it right.
Or if there is a better more elegant way of doing this, I'm all ears.
Any help would be super appreciated.
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
|
#include <iostream>
#include <fstream>
#include <istream>
#include <sstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
vector < vector < string > > matrix;
vector <vector <double> > data;
void read_file()
{
ifstream file ("/home/brian/Data/Trading/IGE/table.csv");
string input;
while(file)
{
string s;
// if there isn't anything in the line, then break
if (!getline(file, s)) break;
istringstream ss(s);
vector <string> record;
while (ss)
{
string s;
if (!getline(ss,s,',')) break;
record.push_back(s);
}
matrix.push_back(record);
}
if (!file.eof())
{
cerr << "Fooey!\n";
}
};
void string2num()
{
string temp;
double numTemp;
int size = matrix.size();
for(long int i=0; i<size;i++)
{
vector <double> record1;
for(int j=0;j<7;j++)
{
temp= matrix[i][j];
stringstream(temp) >> numTemp;
// cout << "Temp is: " << temp << " and numTemp is: " << numTemp << endl;
record1.push_back(numTemp);
}
data.push_back(record1);
}
}
void outz(vector<double> a)
{
for (unsigned int i =0; i< a.size();i++)
cout << "a[" << i << "]: " << a[i] << endl;
}
int main()
{
read_file();
string2num();
int sizeVector=data.size();
cout << sizeVector;
double dailyRet[sizeVector];
//vector <double> dailyRet(0);
vector <double> excessDailyRet(sizeVector);
double* pDailyRet = dailyRet;
double* pTest;
for(int i=0;i<=10 /*sizeVector-1*/;i++)
{
if(i > 2)
{
cout << "1i: " << i << endl;
cout << data[i][6] << endl;
cout << data[i-1][6] << endl;
cout << data[i][6] - data[i-1][6] << endl;
double test = (data[i][6] - data[i-1][6])/data[i-1][6];
cout << test << endl;
cout << "*pDailyRet: " << *pDailyRet << endl;
cout << "dailyRet: " << dailyRet[i] << endl;
cout << "pDailyRet: " << pDailyRet << endl;
pDailyRet = &test;
cout << "pDailyRet: " << pDailyRet << endl;
cout << "*pDailyRet: " << *pDailyRet << endl;
cout << "i is: " << i << endl;
cout << "DailyRet: " << dailyRet[i] << endl;
excessDailyRet.push_back(test-0.04/252);
cout << "dailyRet["<<i<<"] " << dailyRet[i] <<endl;
}
else
{
dailyRet[i] = 0;
excessDailyRet.push_back(0);
cout << "bleh: " ;
}
cout << "Out of loop. " << i << endl;
}
return 0;
}
|
The first 20 lines of the input file are:
Date,Open,High,Low,Close,Volume,Adj Close
2011-12-19,2.32,2.32,2.00,2.32,40000,2.32
2011-12-16,2.32,2.32,2.32,2.32,43100,2.32
2011-12-15,2.34,2.34,2.34,2.34,10100,2.34
2011-12-14,2.25,2.25,2.12,2.12,96200,2.12
2011-12-13,2.70,2.70,2.50,2.50,240000,2.50
2011-12-12,2.50,2.50,2.50,2.50,000,2.50
2011-12-09,2.26,2.26,2.26,2.26,205000,2.26
2011-12-08,2.50,2.50,2.50,2.50,000,2.50
2011-12-07,2.62,2.62,2.62,2.62,000,2.62
2011-12-06,2.25,2.55,2.25,2.55,56600,2.55
2011-12-05,2.35,2.55,2.35,2.55,220000,2.55
2011-12-02,2.25,2.25,2.25,2.25,000,2.25
2011-12-01,2.01,2.01,2.01,2.01,7700,2.01
2011-11-30,2.25,2.25,2.25,2.25,000,2.25
2011-11-29,2.25,2.25,2.25,2.25,5600,2.25
2011-11-28,2.50,2.50,2.50,2.50,50000,2.50
2011-11-25,2.62,2.62,2.62,2.62,000,2.62
2011-11-24,2.50,2.50,2.50,2.50,000,2.50
2011-11-23,2.75,2.75,2.50,2.50,92400,2.50
For bonus love from myself:
I've declared the two matrices globally which is know is poor form, but I couldn't manage to get my functions to read them in correctly otherwise. Any obvious way around this, which I'm sure is solved by pointers, which as you can see, I suck at!
Thanks in advance.