#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void get(ifstream& in, int howManyRows,string col[],int colNumber);
int rowCount(ifstream& in);
template <class T>
void print(T a[],int x, int start=0);
template <class T>
void fill(T a[],int x,int fill=0);
int ten(int k);
double string2double(string tempString);
int main()
{
ifstream in("DIA 1998-2016.csv");
int row=rowCount(in);
//cout<<"row: "<<row<<endl;
string high[row];
double highD[row];
bool gain[row-2];
//focus on "high" column
get(in,row,high,2);
///*
string tempo[row];
for(int i=1;i<row;i++)
tempo[i]=high[row-i];
for(int i=1;i<row;i++)
high[i]=tempo[i];
//*/
//print<string>(high,6);
//print<string>(high,row,row-5);
cout.precision(10);
for(int i=1;i<row;i++)
highD[i]=string2double(high[i]);
//print<double>(highD,6);
//print<double>(highD,row,row-5);
fill<bool>(gain,row-2,false);
//print<bool>(gain,5);
//print<bool>(gain,(row-2),(row-2)-5);
for(int i=2;i<row;i++)
if(highD[i]>highD[i-1])
gain[i-2]=true;
//print<bool>(gain,5);
//print<bool>(gain,(row-2),(row-2)-15);
int countG,countL;
for(int i=2;i<row;i++)
{if(gain[i-2]==true)
countG++;
else
countL++;
if(countG<countL)
cout<<"Gains:"<<countG<<"< Losses:"<<countL<<endl;
}
cout<<"Total gains = "<<countG<<
(countG<countL?" < ":" > ")<<countL<<" Losses = "<<countL<<"\n\n";
int maxSigns=7;
float plusplus[maxSigns];
float plusminus[maxSigns];
int inaRow;
fill<float>(plusplus,maxSigns);
fill<float>(plusminus,maxSigns);
for(inaRow=1;inaRow<=maxSigns;inaRow++)
for(int i=0;i<row-2;i++)
{bool Allgain=true;
for(int j=0;j<inaRow;j++)
Allgain*=gain[i+j];
if(Allgain==true)
{if(gain[i+inaRow]==true)
plusplus[inaRow-1]++;
else
plusminus[inaRow-1]++;
//cout<<inaRow<<" ";
}
}
/*
for(int i=0;i<maxSigns;i++)
{int temp = plusplus[i]+plusminus[i];
plusplus[i]/=(float)temp;
plusminus[i]/=(float)temp;
}
*/
cout<<"plusplus:"<<endl;
print<float>(plusplus,maxSigns);
cout<<"plusminus:"<<endl;
print<float>(plusminus,maxSigns);
float minusplus[maxSigns];
float minusminus[maxSigns];
fill<float>(minusplus,maxSigns);
fill<float>(minusminus,maxSigns);
for(inaRow=1;inaRow<=maxSigns;inaRow++)
for(int i=0;i<row-2;i++)
{bool Allloss=true;
for(int j=0;j<inaRow;j++)
Allloss*=!gain[i+j];
if(Allloss==true)
{if(gain[i+inaRow]==true)
minusplus[inaRow-1]++;
else
minusminus[inaRow-1]++;
//cout<<inaRow<<" ";
}
}
/*
for(int i=0;i<maxSigns;i++)
{int temp = minusplus[i]+minusminus[i];
minusplus[i]/=(float)temp;
minusminus[i]/=(float)temp;
}
*/
cout<<"minusplus:"<<endl;
print<float>(minusplus,maxSigns);
cout<<"minusminus:"<<endl;
print<float>(minusminus,maxSigns);
return 0;
}
//////////////////////////////////////////////////////////////////////////
void get(ifstream& in, int howManyRows,string col[],int colNumber)
{string temp;
for(int j=0;j<howManyRows;j++)
{for(int i=0;i<colNumber;i++)
getline(in,col[j],',');
getline(in,temp,'\n');
}
}
int rowCount(ifstream& in)
{int posTemp=in.tellg();
in.seekg (0, ios::beg);
int count=0;
string temp;
while(in)
{getline(in,temp);
count++;
}
in.clear();
in.seekg (posTemp, ios::beg);
return count-1;
}
template <class T>
void print(T a[],int x, int start)
{for(int i=start;i<x;i++)
cout<<a[i]<<endl;
cout<<endl;
}
template <class T>
void fill(T a[],int x,int fill)
{for(int i=0;i<x;i++)
a[i]=0;
}
int ten(int k)
{ int solution=1;
for(int i=0;i<k;i++)
solution*=10;
return solution;
}
double string2double(string tempString)
{
int comma=tempString.find('.');
if(comma==-1)
{tempString+='.';
comma=tempString.find('.');
}
tempString = tempString.substr(0,comma) + tempString.substr(comma+1);
int tempStringL=tempString.length();
int temp=0;
for(int i=0;i<tempString.length();i++)
{temp+=(int(tempString[i])-48)*ten(tempStringL-1-i);
}
return((double)temp/ten(tempStringL-comma));
}
Understanding the program output:
-
Array plusplus:
The -first- element(element zero) is the number of times a -single- gain(plus) was followed by another gain(plus)
The -second- element is the number of times -two- consecutive gains were followed by a gain… -
Array minusplus:
The -first- element is the number of times a -single- loss(minus) was followed by a gain(plus)…
The -third- element is the number of times -three- consecutive losses were followed by a gain… - Similarly for all the output arrays
A few conclusions from this analysis of the DIA opening values
- It was close but there were more gains than losses
- There was never a time when the total history of losses were greater than gains for open, high, low, close or even volume (there were such times, however, for the adjusted close)
- With i consecutive plus the next is more likely to be j
< i,j >: <1,+> <2,+> <3,+> <4,+> <5,-> <6,-> <7,-> - With i consecutive minus the next is more likely to be j
< i,j >: <1,+> <2,+> <3,+> <4,+> <5,-> <6,-> <7,->