Stock Market Simulator main e8c3612
A game that provides a realistic stock buying experience with unpredictable trends to test investment strategies.
Loading...
Searching...
No Matches
stock.h
Go to the documentation of this file.
1/// @file stock.h
2/// Declaration of the Stock class.
3/*
4This program is free software: you can redistribute it and/or modify it under the
5terms of the GNU Lesser General Public License as published by the Free Software
6Foundation, either version 3 of the License, or (at your option) any later version.
7
8This program is distributed in the hope that it will be useful, but WITHOUT ANY
9WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
11
12You should have received a copy of the GNU Lesser General Public License along with this
13program. If not, see <https://www.gnu.org/licenses/>.
14*/
15#ifndef STOCK_H
16#define STOCK_H
17
18#include "events.h"
19#include "names.h"
20
21#include <istream>
22#include <list>
23#include <map>
24#include <string>
25#include <vector>
26
27/** @brief Initial stock count */
28const int initial_stock_count = 20;
29
30/**
31 * The upper limit of the stock price.
32 * @see Stock::next_round for the "stock split" event.
33 */
34const float STOCK_PRICE_LIMIT = 1000.0f;
35
36/**
37 * @class Stock stock.h "stock.h"
38 * @brief A class that represents a stock object in the game.
39 * The stock has a name, price, quantity, category, money spent, events, attributes, and
40 * history. The stock can be purchased, sold, and updated.
41 * @note Example usage:
42 * @code {.cpp}
43 * // Create a stock object. The constructor will initialize the stock automatically.
44 * Stock stock;
45 * // Purchase a stock.
46 * float balance = 1000; stock.purchase(balance, 1, 0.01);
47 * // Sell a stock.
48 * stock.sell(balance, 1, 0.01);
49 * // What is the name of the stock?
50 * std::string name = stock.get_name();
51 * // Get the upper limit of the percentage change of the stock price:
52 * float upper_limit = stock.get_total_attribute(stock_modifiers::upper_limit);
53 * @endcode
54 */
55class Stock {
56 public:
57 /** @brief Constructor of class Stock */
58 Stock(void);
59
60 /**
61 * @brief Save the stock from a file.
62 * @param playerName The name of the player.
63 * @param i The index of the stock.
64 */
65 void save(const std::string & playerName, int i);
66
67 /**
68 * @brief Load the stock from a file.
69 * @param playerName The name of the player.
70 * @param i The index of the stock.
71 */
72 void load(const std::string & playerName, int i);
73
74 /**
75 * Purchase a given number of stocks.
76 * @param balance The balance ($) of the player. Pass-by-reference.
77 * @param amount The number of stocks to purchase.
78 * @param trading_fees_percent The trading fees percentage we charge the player.
79 * @return Successful: Total cost of the purchase.
80 * Failed: -1 if the player does not have enough balance to buy the
81 * stock.
82 */
83 float purchase(
84 float & balance, unsigned int amount, float trading_fees_percent);
85
86 /**
87 * @brief Sell a given number of stocks.
88 * @param balance The balance ($) of the player. Pass-by-reference.
89 * @param amount The number of stocks to sell.
90 * @param trading_fees_percent The trading fees percentage we charge the player.
91 * @return Successful: Amount of money the player receive.
92 * Failed: -1 if the player does not have enough stocks to sell.
93 */
94 float sell(float & balance, unsigned int amount, float trading_fees_percent);
95
96 /**
97 * @param balance The balance of the player.
98 * @param trading_fees_percent The trading fees percentage we charge the player.
99 * @return Number of stocks that the player can afford with the balance.
100 */
101 unsigned int num_stocks_affordable(
102 float balance, float trading_fees_percent) const;
103
104 /**
105 * @brief Return the name of the category the stock belongs to.
106 * @return Name of the category as a string.
107 */
108 std::string category_name(void) const;
109
110 /**
111 * @brief Return the change of stock price using the most recent price and the
112 * current price
113 * @return change in stock price.
114 */
115 float delta_price(void);
116
117 /** @brief Return the percentage of change of stock price.
118 * @return Percentage of change in stock price. E.g. 0.05 means 5% increase in
119 * price.
120 */
121 float delta_price_percentage(void);
122
123 /**
124 * @brief Get the total change of attribute of the stock due to events only.
125 * Getter function. \n Example usage: @code {.cpp}
126 * stock.get_base_attribute(stock_modifiers::standard_deviation);
127 * @endcode
128 * @param attribute The attribute to get.
129 * @return Total change of attribute due to Stock_event. Does not include the
130 * base value.
131 */
132 float get_event_attribute(stock_modifiers attribute);
133
134 /**
135 * @brief Call this when the game proceed to next round.
136 */
137 void next_round(void);
138
139 /** @brief Add an event to the stock.
140 * @param event The event to be added. See events.h for more information about
141 * the class Stock_Event.
142 */
143 void add_event(const Stock_event & event);
144
145 /**
146 * @brief Get the stock price of recent rounds.
147 * @param rounds The number of rounds to look back. 5 means the last 5 rounds.
148 * @return A vector of stock prices.
149 * If the number of rounds is greater than the history size, return the
150 * whole history. Otherwise, return the most recent rounds. If the history is
151 * empty, return an empty vector.
152 */
153 std::vector<float> return_most_recent_history(unsigned int rounds);
154
155 /**
156 * @brief Get the name of the stock. Getter function.
157 * @return Name of the stock.
158 */
159 std::string get_name(void) { return name; }
160
161 /**
162 * @brief Get the price of the stock. Getter function.
163 * @return Price of the stock as float.
164 */
165 float get_price(void) { return price; }
166
167 /**
168 * @brief Get the category of the stock. Getter function.
169 * @return Category of the stock as unsigned int.
170 */
171 unsigned int get_category(void) { return category; }
172
173 /**
174 * @brief Get the quantity of the stock. Getter function.
175 * @return Stock.quantity as unsigned int.
176 */
177 unsigned int get_quantity(void) { return quantity; }
178
179 /**
180 * @brief Get the initial price of the stock. Getter function.
181 * @return Initial price of the stock as float.
182 */
183 float get_initial_price(void) { return history[0]; }
184
185 /**
186 * @brief Get the base value of stock_attributes of the stock. Getter function.
187 * @param attribute The attribute to get. E.g. standard_deviation, mean,
188 * lower_limit, upper_limit
189 * @return Base value of stock_attributes as float.
190 */
192 return attributes[attribute];
193 }
194
195 /**
196 * @brief Get size of the history.
197 * @return Size of the history as unsigned int.
198 */
199 unsigned int get_history_size(void) { return history.size(); }
200
201 /**
202 * @brief Change the mean of the stock by delta_mean. Setter function.
203 * @param delta_mean The change in mean.
204 */
205 void change_mean(float delta_mean) { attributes[mean] += delta_mean; }
206
207 /**
208 * @brief Return all the events that will apply to this stock specifically.
209 * Getter function.
210 * @return A list of Stock_event.
211 */
212 std::list<Stock_event> get_events(void) { return events; }
213
214 /**
215 * @brief Get the event ids of all the events that will apply to this stock
216 * specifically. Getter function.
217 * @return A vector of unsigned int.
218 */
219 std::vector<unsigned int> get_event_ids(void);
220
221 /**
222 * @brief Get the split count of the stock. Getter function.
223 * @return Stock.split_count as unsigned int.
224 */
225 unsigned int get_split_count(void) { return split_count; }
226
227 /**
228 * @brief Check if we can add the event to the stock.
229 * @note
230 * A event can be added if the event is not mutually exclusive with any of the
231 * existing events.
232 * @param event The event to be added.
233 * @return True if the event can be added. False otherwise.
234 */
235 bool can_add_event(const Stock_event & event);
236
237 /**
238 * @brief Sums up get_base_attribute() and get_event_attribute().
239 * @param attribute The attribute to get.
240 * @return Total value of the attribute. `float` type.
241 */
242 float get_total_attribute(stock_modifiers attribute);
243
244 /**
245 * @brief Calculate the total value of the stocks the player is holding.
246 * @param trading_fees_percent The trading fees percentage we charge the player.
247 * Set to 0 if you want to exclude trading fees from calculation.
248 * @return The total value of the stocks.
249 */
250 float calculateStockValue(const float & trading_fees_percent) const;
251
252 /**
253 * @brief Calculate the amount of money player lost due to trading fees.
254 * @param trading_fees_percent The trading fees percentage we charge the player.
255 * @return The amount of money player lost due to trading fees.
256 */
257 float calculateTradingFeesLost(const float & trading_fees_percent) const;
258
259 /**
260 * @brief Set up a getStockSplitEvent() with proper values.
261 */
263
264 friend std::ostream & operator<<(std::ostream & fout, const Stock & stock);
265
266 friend std::istream & operator>>(std::istream & fin, Stock & stock);
267
268 private:
269 /** @brief Name of the stock that we assigned to it. */
270 std::string name;
271
272 /** @brief Current price of the stock. */
273 float price;
274
275 /** @brief Number of stocks the player has purchased. */
276 unsigned int quantity;
277
278 /** @brief Use numbers to represent the category of the stock.
279 * @note The range of the numbers should be `[0, category_list_size - 1]`.
280 * @ref category_list_size
281 */
282 unsigned int category;
283
284 /** @brief Stores all the events that will apply to this stock specifically. */
285 std::list<Stock_event> events;
286
287 /** @brief Stores the initial value of the stock_modifiers (e.g. standard
288 * deviation, mean and limits). */
289 std::map<stock_modifiers, float> attributes;
290
291 /** @brief Contains the stock price history. First element (index 0) is the
292 * oldest. */
293 std::vector<float> history;
294
295 /** @brief Contains the spliting count of a stock */
296 unsigned int split_count;
297
298 /** @brief Update the history array with the current price */
299 void update_history(void);
300
301 /** @brief Remove obselete events from the list `events` that durations are
302 * less than/equal to 0 (In other words, expired).
303 * For internal use after the `Stock::next_round` function is called.
304 */
305 void remove_obselete_event(void);
306};
307
318
320
321/**
322 * @brief Sorts the stocks.
323 * @param stocks_list A vector of stocks. Pass by reference to modify the stocks.
324 * @param sortMethod Sorting method. Default is by_category.
325 * @param sortDirection Sorting direction. True for ascending, false for descending.
326 * Default is ascending.
327 */
328void sortStocksList(std::vector<Stock> & stocks_list,
329 SortingMethods sortMethod = by_category,
330 SortingDirections sortDirection = ascending);
331
332#endif
A class that represents a stock object in the game.
Definition stock.h:55
std::string category_name(void) const
Return the name of the category the stock belongs to.
Definition stock.cpp:164
unsigned int split_count
Contains the spliting count of a stock.
Definition stock.h:296
float sell(float &balance, unsigned int amount, float trading_fees_percent)
Sell a given number of stocks.
Definition stock.cpp:152
float calculateStockValue(const float &trading_fees_percent) const
Calculate the total value of the stocks the player is holding.
Definition stock.cpp:371
void add_event(const Stock_event &event)
Add an event to the stock.
Definition stock.cpp:210
unsigned int num_stocks_affordable(float balance, float trading_fees_percent) const
Definition stock.cpp:166
std::vector< float > history
Contains the stock price history.
Definition stock.h:293
friend std::ostream & operator<<(std::ostream &fout, const Stock &stock)
Definition stock.cpp:71
unsigned int get_category(void)
Get the category of the stock.
Definition stock.h:171
std::map< stock_modifiers, float > attributes
Stores the initial value of the stock_modifiers (e.g.
Definition stock.h:289
unsigned int category
Use numbers to represent the category of the stock.
Definition stock.h:282
unsigned int get_split_count(void)
Get the split count of the stock.
Definition stock.h:225
std::list< Stock_event > get_events(void)
Return all the events that will apply to this stock specifically.
Definition stock.h:212
float price
Current price of the stock.
Definition stock.h:273
float delta_price(void)
Return the change of stock price using the most recent price and the current price.
Definition stock.cpp:188
std::string get_name(void)
Get the name of the stock.
Definition stock.h:159
std::string name
Name of the stock that we assigned to it.
Definition stock.h:270
float get_initial_price(void)
Get the initial price of the stock.
Definition stock.h:183
unsigned int get_history_size(void)
Get size of the history.
Definition stock.h:199
bool can_add_event(const Stock_event &event)
Check if we can add the event to the stock.
Definition stock.cpp:231
float delta_price_percentage(void)
Return the percentage of change of stock price.
Definition stock.cpp:198
std::list< Stock_event > events
Stores all the events that will apply to this stock specifically.
Definition stock.h:285
void update_history(void)
Update the history array with the current price.
Definition stock.cpp:172
void next_round(void)
Call this when the game proceed to next round.
Definition stock.cpp:276
std::vector< float > return_most_recent_history(unsigned int rounds)
Get the stock price of recent rounds.
Definition stock.cpp:177
unsigned int quantity
Number of stocks the player has purchased.
Definition stock.h:276
void save(const std::string &playerName, int i)
Save the stock from a file.
Definition stock.cpp:47
void remove_obselete_event(void)
Remove obselete events from the list events that durations are less than/equal to 0 (In other words,...
Definition stock.cpp:247
Stock(void)
Constructor of class Stock.
Definition stock.cpp:29
void load(const std::string &playerName, int i)
Load the stock from a file.
Definition stock.cpp:57
float calculateTradingFeesLost(const float &trading_fees_percent) const
Calculate the amount of money player lost due to trading fees.
Definition stock.cpp:375
std::vector< unsigned int > get_event_ids(void)
Get the event ids of all the events that will apply to this stock specifically.
Definition stock.cpp:307
float get_total_attribute(stock_modifiers attribute)
Sums up get_base_attribute() and get_event_attribute().
Definition stock.cpp:317
float purchase(float &balance, unsigned int amount, float trading_fees_percent)
Purchase a given number of stocks.
Definition stock.cpp:139
unsigned int get_quantity(void)
Get the quantity of the stock.
Definition stock.h:177
void change_mean(float delta_mean)
Change the mean of the stock by delta_mean.
Definition stock.h:205
float get_base_attribute(stock_modifiers attribute)
Get the base value of stock_attributes of the stock.
Definition stock.h:191
float get_price(void)
Get the price of the stock.
Definition stock.h:165
friend std::istream & operator>>(std::istream &fin, Stock &stock)
Definition stock.cpp:94
float get_event_attribute(stock_modifiers attribute)
Get the total change of attribute of the stock due to events only.
Definition stock.cpp:259
Stock_event setup_STOCK_SPLIT_EVENT(void)
Set up a getStockSplitEvent() with proper values.
Definition stock.cpp:269
Implements event-related data structures and (declarations of) such functions.
stock_modifiers
The attributes of a stock that Events will modify are hardcoded here.
Definition events.h:30
@ mean
The expectation of the stock price percentage change.
Definition events.h:36
const float trading_fees_percent
/ 100 means charging % more/portion of the money involved in stock operations.
Definition main.cpp:65
float balance
Player's balance.
Definition main.cpp:68
std::string playerName
Player's name.
Definition main.cpp:73
Declaration of the name generating function.
const float STOCK_PRICE_LIMIT
The upper limit of the stock price.
Definition stock.h:34
void sortStocksList(std::vector< Stock > &stocks_list, SortingMethods sortMethod=by_category, SortingDirections sortDirection=ascending)
Sorts the stocks.
Definition stock.cpp:321
SortingMethods
Definition stock.h:308
@ by_upper_limit
Definition stock.h:313
@ by_mean
Definition stock.h:315
@ by_price
Definition stock.h:310
@ by_quantity
Definition stock.h:312
@ by_category
Definition stock.h:311
@ by_lower_limit
Definition stock.h:314
@ by_sd
Definition stock.h:316
@ by_name
Definition stock.h:309
const int initial_stock_count
Initial stock count.
Definition stock.h:28
SortingDirections
Definition stock.h:319
@ ascending
Definition stock.h:319
@ descending
Definition stock.h:319
The data structure of an event that will be applied to the stocks.
Definition events.h:104