stock algorithm: double top

i am currently working on an algorithm for my final project, part of it includes finding if the vector of objects that was imputed from a stock csv file. I was hoping for some feedback on my code and what i have so far.

here are the requirements based upon the stocks closing cost for a day.
Makes a new High
Drops 5 % or more – from High
Rallys 5 % or more(less 7 % ) --from Drop
Drops 5 % or more again – From 2nd top


Data dataContainer::getDoubleTop(vector<Data> vData) {
Data newHigh;
bool firstPeek = false;
int index = 1;

sort(vData.begin(), vData.end(), [](Data &d1, Data &d2) {
return d1.getClose() > d2.getClose();
});

while (firstPeek = false) {
Data nwHi;

for (int i = vData.size(); 0 < i; i++) {
newHigh = vData.at(i);
for (vector<Data>::iterator it = vData.begin(); it != vData.end(); ++it) {
nwHi = *it;
if (newHigh.getClose() < nwHi.getClose() + (nwHi.getClose()* .05)) {
return vData.at(i);
firstPeek = true;
}
else {
index++;
cout << index << " iterations\n";
}
}
}
}
Last edited on
your code, indented
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
Data dataContainer::getDoubleTop(vector<Data> vData) {
	Data newHigh;
	bool firstPeek = false;
	int index = 1;

	sort(vData.begin(), vData.end(), [](Data &d1, Data &d2) {
		return d1.getClose() > d2.getClose();
	});

	while(firstPeek = false) { //assignment, not comparison, loop never enters
		Data nwHi;

		for(int i = vData.size(); 0 < i; i++) { //¿ah?
			newHigh = vData.at(i); //out of bounds
			for(vector<Data>::iterator it = vData.begin(); it != vData.end();
			    ++it) {
				nwHi = *it;
				if(newHigh.getClose()
				   < nwHi.getClose() + (nwHi.getClose() * .05)) {
					return vData.at(i); //here the function ends
					firstPeek = true; //so this line will never be reached
				} else {
					index++;
					cout << index << " iterations\n";
				}
			}
		}
	}
//missing brace 
¿what's the purpose of `firstPeek'?
It is not quite clear what you are doing / should do.

Does the order of elements in vData play a role? You do sort, and hence wipe out information about "after".

You can seek both global and local minima/maxima without sorting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
T
fubar( const std::vector<Data> & vData )
{
  // largest element (by getClose) in range [begin,end)
  auto allHigh = std::max_element( vData.begin(), vData.end(),
    [](const Data &lhs, const Data &rhs) { return lhs.getClose() < rhs.getClose(); } );

  // largest element (by getClose) in range (allHigh,end)
  auto nextPeak = std::max_element( allHigh+1, vData.end(),
    [](const Data &lhs, const Data &rhs) { return lhs.getClose() < rhs.getClose(); } );

  // smallest element (by getClose) in range [allHigh,nextPeak)
  auto restMin = std::min_element( allHigh, nextPeak,
    [](const Data &lhs, const Data &rhs) { return lhs.getClose() < rhs.getClose(); } );

  return ...
}
Topic archived. No new replies allowed.