Stock.cpp 4.14 KB
Newer Older
Imanol Pérez's avatar
Imanol Pérez committed
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
#include <iostream>
#include "Quote.hpp"
#include "Stock.hpp"
#include <ctime>
#include <cstring>
//////////////////////////////////////////////////////
//                                                  //
//          Class that handles the stock            //
//                     prices                       //
//                                                  //
//                  Imanol Perez                    //
//                  January 2017                    //
//                                                  //
//////////////////////////////////////////////////////

Stock::Stock() {

}

Stock::Stock(std::string name, int size) // Creates class with historical data of the stock, with a fixed size
{
  if (size<1) throw Exception("Error in size", "The size of historical data must be at least equal to 1");
  mName=name;
  mData=new Quote[size];
  mSize = size;
  mFilled=0;

  // We fill in the array mData with objects of class Quote.
  for (int i=0; i<mSize; i++)
  {
    mData[i] = Quote(name, 0.0, "0000/00/00"); // Quotes with date 0000/00/00 are considered as Null
  }

}


std::string Stock::getName(void) {
  return mName;
}

int Stock::getFilled(void) {
  /*

  Returns the number of spaces in the class Stock.

  */

  return mFilled;
}

std::ostream& operator<<(std::ostream& output, Stock& q) {
  /*

  Allows to output an object of class Stock.

  */

  std::string name = q.getName();
  output<<"Stock name: "<<name<<"\n";
  if (!q.mData[0].isNull()) {
    output<<"Start date: "<<q.mData[0].getDate()<<"\n";
    output<<"End date: "<<q.mData[q.mFilled-1].getDate()<<"\n";  
  }
  output<<"Length of available data: "<<q.mFilled<<"\n\n";
  
  output<<"  Date\t\t Price\n ------\t\t-------\n\n";
  for (int i=0; i<q.mSize; i++) {
    if (q.mData[i].isNull()) continue;
    output<<q.mData[i].getDate()<<"\t "<<q.mData[i].getPrice()<<"\n";
  }

  return output;
}

void Stock::popLeft(void) {
  /*

  Deletes the first quote, and moves the rest of the quotes to the left.

  */

  if (mSize==1) {
    mData[0]=Quote(mName, 0.0, "0000/00/00");
    mFilled=0;
    return;
  }
  if (mFilled==0) return;
  for (int i=1; i<mFilled; i++) {
    mData[i-1]=mData[i];
  }
  mData[mFilled-1]=Quote(mName, 0.0, "0000/00/00");
  mFilled--;
}

void Stock::addData(Quote q) {
  /*

  Adds new data to the stock's historical prices.

  */

  // We add the quote q to the historical data.- If historical data is full, throw exception
  if (mFilled==mSize) throw Exception("Data size limit reached", "There is no space to store more historical data");

  // If Quote name doesn't match Stock name, throw exception
  if (q.getName()!=mName) throw Exception("Invalid name", "Quote's name does not match stock's name");
  


  // Sort quotes. Notice that the elements from 0 to mFilled-1 are already sorted.
  if (mFilled==0) {
    mData[0]=q;
    mFilled++;
    return;
  }


  for (int i=0; i<mFilled; i++) {
  {  
  if (mData[i].getDate()>q.getDate()) {
    mData[mFilled]=mData[i];
    mData[i]=q;
    break;
  }
  if (i==mFilled-1) {

    mData[mFilled]=q;
  }
  }
  }



  mFilled++; 
}

double Stock::mean(void) {
  /*

  Calculates the mean of the stock prices.

  */

  if (mFilled==0) return 0.0;
  double m=0.0;
  for (int i=0; i<mFilled; i++) {
    m+=mData[i].getPrice();
  }

  return m/mFilled;
}

Stock Stock::sample(int start, int end) {
  /*

  Returns a sample of the historical data. That is,
  it returns an object of class Stock with data from
  the start-th to the end-th positions, in the historical
  data of our stock.

  */

  if (start>end || start<0 || end>=mFilled) {
    std::cout<<start<<" "<<end<<"\n";
    throw Exception("Error in the indices", "There has been an error with the provided indices");
  } 
  Stock S(mName, end-start+1);
  for (int i=start; i<=end; i++) {
    S.addData(mData[i]);
  }
  return S;
}

Quote Stock::getData(int i) {
  /*

  Returns the quote in position i.

  */
  
  if (i<0 || i>=mSize)  throw Exception("Out of bound", "The index is out of bound");
  return mData[i];
}