Program for Peak Stock Prices using Historical Data

Using historical data of any stock of your choice, compute the span for each data point.

Find all peaks in the stock price using the span.

Here is my implementation using a stack and template class.

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
#include <iostream>
using namespace std;

class RuntimeException { //generic run-time exception
private:
string errorMsg;
public:
RuntimeException(const string& err) {
errorMsg = err;
}

string getMessage() const {
return errorMsg;
}
};

class StackEmpty : public RuntimeException {
public:
StackEmpty(const string& err) : RuntimeException(err) {}
};

class StackFull : public RuntimeException {
public:
StackFull(const string& err) : RuntimeException(err) {}
};

template <typename E>
class ArrayStack {
enum { DEF_CAPACITY = 100};
public:
ArrayStack(int cap = DEF_CAPACITY); // constructor from capacity
int size() const; // number of items in the stack
bool empty() const; // is the stack empty?
const E& top() const throw(StackEmpty); // get top element
void push(const E& e) throw(StackFull); // push element onto stack
void pop() throw(StackEmpty); // pop the stack
// ... housekeeping functions omitted
E* spans2(E*, int); // linear algo to determine span
void printArray(E*, int); // prints stack
E peakPricebySpan(E*, int);

~ArrayStack(); // destructor to delete dynamically allocated S in spans2()


private:
E* S; // member data
int capacity; // stack capacity
int t; // index of the top of the stack

};

template <typename E>
ArrayStack<E>::ArrayStack(int cap) // constructor from capacity
: S(new E[cap]), capacity(cap), t(-1) { }

template <typename E>
int ArrayStack<E>::size() const
{ return (t + 1); }

template <typename E>
bool ArrayStack<E>::empty() const
{ return (t < 0); }

template <typename E>
const E& ArrayStack<E>::top() const throw(StackEmpty) {
if (empty()) throw StackEmpty("Top of empty stack");
return S[t];
}

template <typename E>
void ArrayStack<E>::push(const E& e) throw(StackFull) {
if (size() == capacity) throw StackFull("Push to full stack");
S[++t] = e;
}

template <typename E>
void ArrayStack<E>::pop() throw (StackEmpty) {
if (empty()) throw StackEmpty("Pop from empty stack");
--t;
}

template <typename E>
E* ArrayStack<E>::spans2(E* X, int n) { //span2
E* S = new E[n]; //dynamic allocation, might need to delete.
ArrayStack<E> A;
for (int i = 0; i <= n - 1; i++) {
while (!A.empty() && X[A.top()] <= X[i])
A.pop();

if (A.empty())
S[i] = i + 1;
else
S[i] = i - A.top();

A.push(i);
}

return S;
}

template <typename E>
void ArrayStack<E>::printArray(E arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << ", ";
}

template <typename E>
E ArrayStack<E>::peakPricebySpan(E arr[], int n) {
int position;
E spanVal = arr[0];
for (int i = 1; i < n; i++) {
if (spanVal < arr[i]) {
spanVal = arr[i];
position = i;
}
}

return position;
}

template <typename E>
ArrayStack<E>::~ArrayStack() {
delete [] S;
}


int main() {
//DOW HISTORICAL PAST 6 DAY CHART
const int SIZE = 6;
int stockA[SIZE] = {30333, 30423, 30523, 30185, 29634, 30038};

ArrayStack<int> price;
int* spanValue = price.spans2(stockA, SIZE);

cout << "The price of the last " << SIZE << " days of the stock are: ";

for (int i = 0; i < SIZE; i++)
cout << stockA[i] << ", ";

cout << "\nThe corresponding span values of the stock are: ";

price.printArray(spanValue, SIZE);

//highest span is same position as highest price
cout << "\nThe peak price of the last " << SIZE << " days of the stock is: "
<< stockA[price.peakPricebySpan(spanValue, SIZE)];

return 0;
}