So today I decided to do some analysis of stock market data and try to see if I can find an algorithm to trade in the stock market that actually makes money in c++. Not so much about money since my numerous attempts in the past have all been a failure but mostly because I find it kind of enjoyable to try and see if something works.
Case in point, after writing a very simple algorithm I noticed that it actually was making money. So I was both delighted and skeptical about it. I tried a lot of different stocks and in all of them it had made a profit.
So since I still have strong doubts about it I wrote this post for confirmation that this is actually not a viable trading strategy.
The algorithm is as follows:
if lastDayClose < thisDayOpen: buy else: short
(Essentially we buy/short at the start of the day and then before the market closes we close our positions)
Here is the C++ code (I am sorry for posting C++ it just so happened that this was the language I picked up when I started writing the code)
int main() { ifstream fin; string filename; // Name of the file to open. The File contains the open,high,low,close,etc. from Yahoo Finance cout << "Enter name of file: "; cin >> filename; fin.open(filename.c_str()); // Open File to read vector< vector<float> > data; //Vector of vectors aka Dictionary in python to store prices while(!fin.eof()) // Fill in the Vector of Vectors { data.push_back(vector<float>(6)); for(int j = 0; j < 6; j++)fin >> data.back()[j]; } // data[i][0] -> OPEN // data[i][1] -> HIGH // data[i][2] -> LOW // data[i][3] -> CLOSE // data[i][4] -> VOLUME // data[i][5] -> ADJ. CLOSE fin.close(); // Close file. We are done reading from it. float startBudget; // get starting budget cout << "Enter a starting budget: "; cin >> startBudget; float fee; // Transaction fee to buy/short a stock cout << "Enter a transaction fee: "; cin >> fee; float budget = startBudget; float lowest = budget; bool buy; for(int i = data.size()-3; i >= 0; i--)// For each trading day { int n_shares = 1; // Number of shares to buy/short if(budget < data[i][0]){ cout << "BROKE" << endl; // If we don't have enough money to buy more stock stop cout << budget << endl; break; } if(data[i+1][3] < data[i][0]) // Buy if lastDayClose > thisDayOpen { buy=true; n_shares = 1; } else // else short { buy = false; n_shares = 1; } // if buy==true then add the difference of todayClose-lastDayClose. Else subtract it since we short the stock budget += buy ? data[i][3]-data[i+1][3]*n_shares : (data[i][3]-data[i+1][3])*(-1)*n_shares; budget -= fee; // Subract transaction fee if(budget<lowest)lowest=budget; // Keep a value to show what the lowest amount of money we had at a given day was. printf("Budget: %0.5fn", budget); // Print the current budget to the screen } cout << "The percentage return is: " << ((budget-startBudget)/startBudget)*100 << "%" << endl; // Show the percentage difference cout << "Lowest value of budget: " << lowest << endl; // Show lowest amount of money we had on hand
string exit; // Ask from user to exit while(exit!="exit") { cout << "Type exit to close" << endl; cin >> exit; } }
Any kind of feedback is extremely appreciated. Special thanks to Harrison for introducing me to Data Analysis.
You must be logged in to post. Please login or register an account.