Stock Market Simulator main e8c3612
A game that provides a realistic stock buying experience with unpredictable trends to test investment strategies.
|
A class that represents a stock object in the game. More...
#include "stock.h"
Public Member Functions | |
Stock (void) | |
Constructor of class Stock. | |
void | add_event (const Stock_event &event) |
Add an event to the stock. | |
float | calculateStockValue (const float &trading_fees_percent) const |
Calculate the total value of the stocks the player is holding. | |
float | calculateTradingFeesLost (const float &trading_fees_percent) const |
Calculate the amount of money player lost due to trading fees. | |
bool | can_add_event (const Stock_event &event) |
Check if we can add the event to the stock. | |
std::string | category_name (void) const |
Return the name of the category the stock belongs to. | |
void | change_mean (float delta_mean) |
Change the mean of the stock by delta_mean. | |
float | delta_price (void) |
Return the change of stock price using the most recent price and the current price. | |
float | delta_price_percentage (void) |
Return the percentage of change of stock price. | |
float | get_base_attribute (stock_modifiers attribute) |
Get the base value of stock_attributes of the stock. | |
unsigned int | get_category (void) |
Get the category of the stock. | |
float | get_event_attribute (stock_modifiers attribute) |
Get the total change of attribute of the stock due to events only. | |
std::vector< unsigned int > | get_event_ids (void) |
Get the event ids of all the events that will apply to this stock specifically. | |
std::list< Stock_event > | get_events (void) |
Return all the events that will apply to this stock specifically. | |
unsigned int | get_history_size (void) |
Get size of the history. | |
float | get_initial_price (void) |
Get the initial price of the stock. | |
std::string | get_name (void) |
Get the name of the stock. | |
float | get_price (void) |
Get the price of the stock. | |
unsigned int | get_quantity (void) |
Get the quantity of the stock. | |
unsigned int | get_split_count (void) |
Get the split count of the stock. | |
float | get_total_attribute (stock_modifiers attribute) |
Sums up get_base_attribute() and get_event_attribute(). | |
void | load (const std::string &playerName, int i) |
Load the stock from a file. | |
void | next_round (void) |
Call this when the game proceed to next round. | |
unsigned int | num_stocks_affordable (float balance, float trading_fees_percent) const |
float | purchase (float &balance, unsigned int amount, float trading_fees_percent) |
Purchase a given number of stocks. | |
std::vector< float > | return_most_recent_history (unsigned int rounds) |
Get the stock price of recent rounds. | |
void | save (const std::string &playerName, int i) |
Save the stock from a file. | |
float | sell (float &balance, unsigned int amount, float trading_fees_percent) |
Sell a given number of stocks. | |
Stock_event | setup_STOCK_SPLIT_EVENT (void) |
Set up a getStockSplitEvent() with proper values. | |
Private Member Functions | |
void | remove_obselete_event (void) |
Remove obselete events from the list events that durations are less than/equal to 0 (In other words, expired). | |
void | update_history (void) |
Update the history array with the current price. | |
Private Attributes | |
std::map< stock_modifiers, float > | attributes |
Stores the initial value of the stock_modifiers (e.g. | |
unsigned int | category |
Use numbers to represent the category of the stock. | |
std::list< Stock_event > | events |
Stores all the events that will apply to this stock specifically. | |
std::vector< float > | history |
Contains the stock price history. | |
std::string | name |
Name of the stock that we assigned to it. | |
float | price |
Current price of the stock. | |
unsigned int | quantity |
Number of stocks the player has purchased. | |
unsigned int | split_count |
Contains the spliting count of a stock. | |
Friends | |
std::ostream & | operator<< (std::ostream &fout, const Stock &stock) |
std::istream & | operator>> (std::istream &fin, Stock &stock) |
A class that represents a stock object in the game.
The stock has a name, price, quantity, category, money spent, events, attributes, and history. The stock can be purchased, sold, and updated.
Stock::Stock | ( | void | ) |
Constructor of class Stock.
The distribution of initial stock price will be consistent across the same categories. Note that the value '3' is because currently init_stock_price has 3 possible input values.
Definition at line 29 of file stock.cpp.
References attributes, category, category_list_size, defaultLowerLimit, defaultMean, defaultUpperLimit, generate_name(), init_sd(), init_stock_price(), lower_limit, mean, name, price, quantity, random_integer(), split_count, standard_deviation, update_history(), and upper_limit.
void Stock::add_event | ( | const Stock_event & | event | ) |
Add an event to the stock.
event | The event to be added. See events.h for more information about the class Stock_Event. |
Definition at line 210 of file stock.cpp.
References can_add_event(), and events.
Referenced by new_events_next_round(), and next_round().
float Stock::calculateStockValue | ( | const float & | trading_fees_percent | ) | const |
Calculate the total value of the stocks the player is holding.
trading_fees_percent | The trading fees percentage we charge the player. Set to 0 if you want to exclude trading fees from calculation. |
Definition at line 371 of file stock.cpp.
References price, quantity, and trading_fees_percent.
float Stock::calculateTradingFeesLost | ( | const float & | trading_fees_percent | ) | const |
Calculate the amount of money player lost due to trading fees.
trading_fees_percent | The trading fees percentage we charge the player. |
Definition at line 375 of file stock.cpp.
References price, quantity, and trading_fees_percent.
bool Stock::can_add_event | ( | const Stock_event & | event | ) |
Check if we can add the event to the stock.
event | The event to be added. |
Definition at line 231 of file stock.cpp.
References Stock_event::event_id, and events.
Referenced by add_event(), and new_events_next_round().
std::string Stock::category_name | ( | void | ) | const |
Return the name of the category the stock belongs to.
Definition at line 164 of file stock.cpp.
References category, and category_list.
|
inline |
Change the mean of the stock by delta_mean.
Setter function.
delta_mean | The change in mean. |
Definition at line 205 of file stock.h.
References attributes, and mean.
float Stock::delta_price | ( | void | ) |
Return the change of stock price using the most recent price and the current price.
Definition at line 188 of file stock.cpp.
References history.
Referenced by delta_price_percentage().
float Stock::delta_price_percentage | ( | void | ) |
Return the percentage of change of stock price.
If there are less than two prices in the history array, return 0 If the last two prices are negative, return 0, as it is not possible to calculate the percentage change
Definition at line 198 of file stock.cpp.
References delta_price(), and history.
|
inline |
Get the base value of stock_attributes of the stock.
Getter function.
attribute | The attribute to get. E.g. standard_deviation, mean, lower_limit, upper_limit |
Definition at line 191 of file stock.h.
References attributes.
Referenced by getProcessedModifiers().
|
inline |
float Stock::get_event_attribute | ( | stock_modifiers | attribute | ) |
Get the total change of attribute of the stock due to events only.
Getter function.
Example usage:
attribute | The attribute to get. |
Definition at line 259 of file stock.cpp.
References events.
Referenced by get_total_attribute(), and getProcessedModifiers().
std::vector< unsigned int > Stock::get_event_ids | ( | void | ) |
Get the event ids of all the events that will apply to this stock specifically.
Getter function.
Definition at line 307 of file stock.cpp.
References events.
|
inline |
Return all the events that will apply to this stock specifically.
Getter function.
Definition at line 212 of file stock.h.
References events.
|
inline |
Get size of the history.
Definition at line 199 of file stock.h.
References history.
Referenced by getProcessedModifiers().
|
inline |
Get the initial price of the stock.
Getter function.
Definition at line 183 of file stock.h.
References history.
Referenced by getProcessedModifiers().
|
inline |
Get the name of the stock.
Getter function.
Definition at line 159 of file stock.h.
References name.
Referenced by new_events_next_round().
|
inline |
Get the price of the stock.
Getter function.
Definition at line 165 of file stock.h.
References price.
Referenced by getProcessedModifiers().
|
inline |
Get the quantity of the stock.
Getter function.
Definition at line 177 of file stock.h.
References quantity.
|
inline |
Get the split count of the stock.
Getter function.
Definition at line 225 of file stock.h.
References split_count.
Referenced by percentage_change_price().
float Stock::get_total_attribute | ( | stock_modifiers | attribute | ) |
Sums up get_base_attribute() and get_event_attribute().
attribute | The attribute to get. |
float
type. Definition at line 317 of file stock.cpp.
References attributes, and get_event_attribute().
Referenced by getProcessedModifiers().
void Stock::load | ( | const std::string & | playerName, |
int | i ) |
Load the stock from a file.
playerName | The name of the player. |
i | The index of the stock. |
Definition at line 57 of file stock.cpp.
References playerName, price, SAVE_FILE_EXTENSION_TXT, SAVE_FOLDER_PREFIX, and STOCK_PRICE_LIMIT.
void Stock::next_round | ( | void | ) |
Call this when the game proceed to next round.
Update the price of the stock. If the price is less than 1000, the price will increase or decrease by a random percentage. If the price is more than 1000, the price will be halved and the quantity will be doubled.
Definition at line 276 of file stock.cpp.
References add_event(), durationDecreaseMultiplier, events, percentage_change_price(), price, quantity, remove_obselete_event(), setup_STOCK_SPLIT_EVENT(), split_count, STOCK_PRICE_LIMIT, and update_history().
unsigned int Stock::num_stocks_affordable | ( | float | balance, |
float | trading_fees_percent ) const |
balance | The balance of the player. |
trading_fees_percent | The trading fees percentage we charge the player. |
Definition at line 166 of file stock.cpp.
References balance, price, and trading_fees_percent.
float Stock::purchase | ( | float & | balance, |
unsigned int | amount, | ||
float | trading_fees_percent ) |
Purchase a given number of stocks.
balance | The balance ($) of the player. Pass-by-reference. |
amount | The number of stocks to purchase. |
trading_fees_percent | The trading fees percentage we charge the player. |
Definition at line 139 of file stock.cpp.
References balance, INVALID_OPERATION, price, quantity, and trading_fees_percent.
|
private |
Remove obselete events from the list events
that durations are less than/equal to 0 (In other words, expired).
For internal use after the Stock::next_round
function is called.
Definition at line 247 of file stock.cpp.
References events.
Referenced by next_round().
std::vector< float > Stock::return_most_recent_history | ( | unsigned int | rounds | ) |
Get the stock price of recent rounds.
rounds | The number of rounds to look back. 5 means the last 5 rounds. |
Definition at line 177 of file stock.cpp.
References history.
void Stock::save | ( | const std::string & | playerName, |
int | i ) |
Save the stock from a file.
playerName | The name of the player. |
i | The index of the stock. |
Definition at line 47 of file stock.cpp.
References playerName, SAVE_FILE_EXTENSION_TXT, and SAVE_FOLDER_PREFIX.
float Stock::sell | ( | float & | balance, |
unsigned int | amount, | ||
float | trading_fees_percent ) |
Sell a given number of stocks.
balance | The balance ($) of the player. Pass-by-reference. |
amount | The number of stocks to sell. |
trading_fees_percent | The trading fees percentage we charge the player. |
Definition at line 152 of file stock.cpp.
References balance, INVALID_OPERATION, price, quantity, and trading_fees_percent.
Stock_event Stock::setup_STOCK_SPLIT_EVENT | ( | void | ) |
Set up a getStockSplitEvent() with proper values.
Definition at line 269 of file stock.cpp.
References category, Stock_event::category, getStockSplitEvent(), name, and Stock_event::text.
Referenced by next_round().
|
private |
|
friend |
Definition at line 71 of file stock.cpp.
|
friend |
Definition at line 94 of file stock.cpp.
|
private |
Stores the initial value of the stock_modifiers (e.g.
standard deviation, mean and limits).
Definition at line 289 of file stock.h.
Referenced by Stock(), change_mean(), get_base_attribute(), and get_total_attribute().
|
private |
Use numbers to represent the category of the stock.
[0, category_list_size - 1]
. category_list_size Definition at line 282 of file stock.h.
Referenced by Stock(), category_name(), get_category(), and setup_STOCK_SPLIT_EVENT().
|
private |
Stores all the events that will apply to this stock specifically.
Definition at line 285 of file stock.h.
Referenced by add_event(), can_add_event(), get_event_attribute(), get_event_ids(), get_events(), next_round(), and remove_obselete_event().
|
private |
Contains the stock price history.
First element (index 0) is the oldest.
Definition at line 293 of file stock.h.
Referenced by delta_price(), delta_price_percentage(), get_history_size(), get_initial_price(), return_most_recent_history(), and update_history().
|
private |
Name of the stock that we assigned to it.
Definition at line 270 of file stock.h.
Referenced by Stock(), get_name(), and setup_STOCK_SPLIT_EVENT().
|
private |
Current price of the stock.
Definition at line 273 of file stock.h.
Referenced by Stock(), calculateStockValue(), calculateTradingFeesLost(), get_price(), load(), next_round(), num_stocks_affordable(), purchase(), sell(), and update_history().
|
private |
Number of stocks the player has purchased.
Definition at line 276 of file stock.h.
Referenced by Stock(), calculateStockValue(), calculateTradingFeesLost(), get_quantity(), next_round(), purchase(), and sell().
|
private |
Contains the spliting count of a stock.
Definition at line 296 of file stock.h.
Referenced by Stock(), get_split_count(), and next_round().