1 Replies - 1130 Views - Last Post: 16 April 2016 - 05:45 PM

#1 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Stock Market Simulation Problem:

Posted 16 April 2016 - 04:04 PM

I am trying to implement a stock market simulation system that uses provided historical stock data and a strategy suggestion functionality to carry it out. Other classes include a File Reader class that reads all the files in, and just standard classes like StockDayDetails that just simply hold the details a stock would have i.e open, close, adj price. There is also a StockData class that stores all the csv files that the stock data is stored in to a list.

Metric class:

Fifteen Day Average:

void FifteenDayMetric::MarshalString(String ^ s, string& os) {
	using namespace Runtime::InteropServices;
	const char* chars =
		(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
	os = chars;

FifteenDayMetric::FifteenDayMetric(StockData^ stockData)
	this->stockData = stockData;

void FifteenDayMetric::perform()
	Console::WriteLine("Performing Fifteen Day Metric");

	mmap metricScores; //map to store our results in the format of <CompanyName, MetricResult>

	for (int i = 0; i < stockData->getStockCompanySize(); i++) // loop over each company
		double cAvgOpen = 0.0;
		double cAvgClose = 0.0;
		double different = 0.0;
	for (int j = 15; j < stockData->getStockCompany(i)->getDailyCompanySize(); j++) // loop over each company's daily values
			//accumulate the values
			cAvgOpen += stockData->getStockCompany(i)->getDailyDetails(j)->getOpen();
			cAvgClose += stockData->getStockCompany(i)->getDailyDetails(j)->getClose();
		//divide them by the numer of entries to get the average
		cAvgOpen = cAvgOpen / stockData->getStockCompany(i)->getDailyCompanySize();
		cAvgClose = cAvgClose / stockData->getStockCompany(i)->getDailyCompanySize();
		different = cAvgOpen - cAvgClose;

		//convert the String^ to a std:string for the multimap
		string epic = "";
		String ^ crlEpic = stockData->getStockCompany(i)->getEpic();
		MarshalString(crlEpic, epic);

		//add the result to the multimap
		metricScores.insert(mmap::value_type(different, epic));

	//the multimap is stored acsending order. I.e those with the largest difference are located towards the end of the hashmap.
	//Therefore, those at the end of the  multimap might be of interest. 
	for (mmap::const_iterator iter = metricScores.begin(); iter != metricScores.end(); iter++)
		cout << iter->first << '\t' << iter->second << endl;

Controller Class (So Far):

void Controller::performStrategySuggestion()

	FifteenDayMetric metric(fReader.getStockData());
	Metric^ metricPtr = %metric;


However my simulation function is the main part of the program and it needs to use the stock data (already in my metric class), take as an input a start balance, start date and an end date to calculate any potential returns.

Here is my initial idea for it so far:

void Controller::Simulation()
  int command = 0;
  int startBalance = 0;
  //Not sure what to put here for the date as I'm not sure if there is a function in C++ I can use that can provide me dates to set the startDate and endDate variables.

        cout << "Use the following options to control this program" << endl;
	cout << "type -1 to exit" << endl;
	cout << "type 1 to perform the 15 day metric" << endl;

    while (command != -1)
		cout << "Type your options now" << endl;
		cin >> command;

            switch (command)
		case (1) : //Perform 15 Day Metric
                 FifteenDayMetric metric(fReader.getStockData());
	         Metric^ metricPtr = %metric;
                 //Show stocks to buy as it iterates through the dates
           		case (2) : //Close an account
			int accountNo;


	cout << "Exiting" << endl;
	return 0;

The problem is I don't know how to make it so that the program iterates through the days and brings up different stock data to buy or sell depending on what the data is for that day. Is there a simple way to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Stock Market Simulation Problem:

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

Re: Stock Market Simulation Problem:

Posted 16 April 2016 - 05:45 PM

Moving to C++ CLI...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1