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 Class Reference

A class that represents a stock object in the game. More...

#include "stock.h"

Collaboration diagram for Stock:

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_eventget_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_eventevents
 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)
 

Detailed Description

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.

Note
Example usage:
// Create a stock object. The constructor will initialize the stock automatically.
Stock stock;
// Purchase a stock.
float balance = 1000; stock.purchase(balance, 1, 0.01);
// Sell a stock.
stock.sell(balance, 1, 0.01);
// What is the name of the stock?
std::string name = stock.get_name();
// Get the upper limit of the percentage change of the stock price:
A class that represents a stock object in the game.
Definition stock.h:55
float sell(float &balance, unsigned int amount, float trading_fees_percent)
Sell a given number of stocks.
Definition stock.cpp:152
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_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
@ upper_limit
The upper limit of the stock price percentage change.
Definition events.h:48
float balance
Player's balance.
Definition main.cpp:68

Definition at line 55 of file stock.h.

Constructor & Destructor Documentation

◆ Stock()

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.

29 {
32 /** The distribution of initial stock price will be consistent across the same
33 * categories.
34 * Note that the value '3' is because currently init_stock_price has 3
35 * possible input values.
36 */
38 quantity = 0;
43 split_count = 0;
45}
unsigned int split_count
Contains the spliting count of a stock.
Definition stock.h:296
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
float price
Current price of the stock.
Definition stock.h:273
void update_history(void)
Update the history array with the current price.
Definition stock.cpp:172
unsigned int quantity
Number of stocks the player has purchased.
Definition stock.h:276
@ standard_deviation
Amount of variation of the stock price percentage change.
Definition events.h:32
@ mean
The expectation of the stock price percentage change.
Definition events.h:36
@ lower_limit
The lower limit of the stock price percentage change.
Definition events.h:42
const float defaultUpperLimit
Default upper limit.
Definition events.h:67
const float defaultLowerLimit
Default lower limit.
Definition events.h:64
constexpr float defaultMean
Default mean.
Definition events.h:70
vector< string > generate_name(unsigned int category, unsigned int num)
Generates a set of unique stock names based on the specified category and quantity.
Definition names.cpp:37
const int category_list_size
The size of the category list.
Definition names.h:27
float init_stock_price(int price_profile)
Initialize starting stock price.
unsigned int random_integer(unsigned int max_integer)
python randint like function
float init_sd(void)
Initialize the standard deviation of the stock price.

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.

Here is the call graph for this function:

Member Function Documentation

◆ add_event()

void Stock::add_event ( const Stock_event & event)

Add an event to the stock.

Parameters
eventThe event to be added. See events.h for more information about the class Stock_Event.

Definition at line 210 of file stock.cpp.

210 {
211 if (!can_add_event(event)) {
212 // If the event is mutually exclusive with ongoing events,
213 // ignore it and do nothing.
214 return;
215 }
216 // If the event does not exist, add it to the std::list of events
217 // Otherwise, update the duration of the event by deleting the old one and add the
218 // new one.
219 std::list<Stock_event>::iterator event_itr = events.begin();
220 while (event_itr != events.end()) {
221 if (*event_itr == event) {
222 event_itr = events.erase(event_itr);
223 }
224 else {
225 event_itr++;
226 }
227 }
228 events.emplace_back(event);
229}
bool can_add_event(const Stock_event &event)
Check if we can add the event to the stock.
Definition stock.cpp:231
std::list< Stock_event > events
Stores all the events that will apply to this stock specifically.
Definition stock.h:285

References can_add_event(), and events.

Referenced by new_events_next_round(), and next_round().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calculateStockValue()

float Stock::calculateStockValue ( const float & trading_fees_percent) const

Calculate the total value of the stocks the player is holding.

Parameters
trading_fees_percentThe trading fees percentage we charge the player. Set to 0 if you want to exclude trading fees from calculation.
Returns
The total value of the stocks.

Definition at line 371 of file stock.cpp.

371 {
372 return price * quantity * (1 - trading_fees_percent);
373}
const float trading_fees_percent
/ 100 means charging % more/portion of the money involved in stock operations.
Definition main.cpp:65

References price, quantity, and trading_fees_percent.

◆ calculateTradingFeesLost()

float Stock::calculateTradingFeesLost ( const float & trading_fees_percent) const

Calculate the amount of money player lost due to trading fees.

Parameters
trading_fees_percentThe trading fees percentage we charge the player.
Returns
The amount of money player lost due to trading fees.

Definition at line 375 of file stock.cpp.

375 {
377}

References price, quantity, and trading_fees_percent.

◆ can_add_event()

bool Stock::can_add_event ( const Stock_event & event)

Check if we can add the event to the stock.

Note
A event can be added if the event is not mutually exclusive with any of the existing events.
Parameters
eventThe event to be added.
Returns
True if the event can be added. False otherwise.

Definition at line 231 of file stock.cpp.

231 {
232 std::list<Stock_event>::iterator event_itr = events.begin();
233 while (event_itr != events.end()) {
234 if (!event_itr->mutually_exclusive_events.empty()) {
235 for (unsigned int i = 0; i < event_itr->mutually_exclusive_events.size();
236 i++) {
237 if (event_itr->mutually_exclusive_events[i] == event.event_id) {
238 return false;
239 }
240 }
241 }
242 event_itr++;
243 }
244 return true;
245}
unsigned int event_id
The id of the event.
Definition events.h:110

References Stock_event::event_id, and events.

Referenced by add_event(), and new_events_next_round().

Here is the caller graph for this function:

◆ category_name()

std::string Stock::category_name ( void ) const

Return the name of the category the stock belongs to.

Returns
Name of the category as a string.

Definition at line 164 of file stock.cpp.

164{ return category_list[category]; }
string const category_list[category_list_size]
List of stock categories.
Definition names.cpp:22

References category, and category_list.

◆ change_mean()

void Stock::change_mean ( float delta_mean)
inline

Change the mean of the stock by delta_mean.

Setter function.

Parameters
delta_meanThe change in mean.

Definition at line 205 of file stock.h.

205{ attributes[mean] += delta_mean; }

References attributes, and mean.

◆ delta_price()

float Stock::delta_price ( void )

Return the change of stock price using the most recent price and the current price.

Returns
change in stock price.

Definition at line 188 of file stock.cpp.

188 {
189 // Stock prices are stored in the history array
190 if (history.size() < 2) {
191 // If there are less than two prices in the history array, return 0
192 return 0;
193 }
194 // Return the change of the last two prices
195 return history[history.size() - 1] - history[history.size() - 2];
196}
std::vector< float > history
Contains the stock price history.
Definition stock.h:293

References history.

Referenced by delta_price_percentage().

Here is the caller graph for this function:

◆ delta_price_percentage()

float Stock::delta_price_percentage ( void )

Return the percentage of change of stock price.

Returns
Percentage of change in stock price. E.g. 0.05 means 5% increase in 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.

198 {
199 if (history.size() < 2 || history[history.size() - 1] < 0 ||
200 history[history.size() - 2] < 0) {
201 /** If there are less than two prices in the history array, return 0
202 * If the last two prices are negative, return 0, as it is not possible to
203 * calculate the percentage change
204 */
205 return 0;
206 }
207 return delta_price() / history[history.size() - 2];
208}
float delta_price(void)
Return the change of stock price using the most recent price and the current price.
Definition stock.cpp:188

References delta_price(), and history.

Here is the call graph for this function:

◆ get_base_attribute()

float Stock::get_base_attribute ( stock_modifiers attribute)
inline

Get the base value of stock_attributes of the stock.

Getter function.

Parameters
attributeThe attribute to get. E.g. standard_deviation, mean, lower_limit, upper_limit
Returns
Base value of stock_attributes as float.

Definition at line 191 of file stock.h.

191 {
192 return attributes[attribute];
193 }

References attributes.

Referenced by getProcessedModifiers().

Here is the caller graph for this function:

◆ get_category()

unsigned int Stock::get_category ( void )
inline

Get the category of the stock.

Getter function.

Returns
Category of the stock as unsigned int.

Definition at line 171 of file stock.h.

171{ return category; }

References category.

◆ get_event_attribute()

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:

stock.get_base_attribute(stock_modifiers::standard_deviation);
Parameters
attributeThe attribute to get.
Returns
Total change of attribute due to Stock_event. Does not include the base value.

Definition at line 259 of file stock.cpp.

259 {
260 float sum = 0;
261 std::list<Stock_event>::iterator event_itr = events.begin();
262 while (event_itr != events.end()) {
263 sum += event_itr->modifiers[attribute];
264 event_itr++; // Bug fix: infinite loop
265 }
266 return sum;
267}

References events.

Referenced by get_total_attribute(), and getProcessedModifiers().

Here is the caller graph for this function:

◆ get_event_ids()

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.

Returns
A vector of unsigned int.

Definition at line 307 of file stock.cpp.

307 {
308 std::vector<unsigned int> event_ids;
309 std::list<Stock_event>::iterator event_itr = events.begin();
310 while (event_itr != events.end()) {
311 event_ids.emplace_back(event_itr->event_id);
312 event_itr++;
313 }
314 return event_ids;
315}

References events.

◆ get_events()

std::list< Stock_event > Stock::get_events ( void )
inline

Return all the events that will apply to this stock specifically.

Getter function.

Returns
A list of Stock_event.

Definition at line 212 of file stock.h.

212{ return events; }

References events.

◆ get_history_size()

unsigned int Stock::get_history_size ( void )
inline

Get size of the history.

Returns
Size of the history as unsigned int.

Definition at line 199 of file stock.h.

199{ return history.size(); }

References history.

Referenced by getProcessedModifiers().

Here is the caller graph for this function:

◆ get_initial_price()

float Stock::get_initial_price ( void )
inline

Get the initial price of the stock.

Getter function.

Returns
Initial price of the stock as float.

Definition at line 183 of file stock.h.

183{ return history[0]; }

References history.

Referenced by getProcessedModifiers().

Here is the caller graph for this function:

◆ get_name()

std::string Stock::get_name ( void )
inline

Get the name of the stock.

Getter function.

Returns
Name of the stock.

Definition at line 159 of file stock.h.

159{ return name; }

References name.

Referenced by new_events_next_round().

Here is the caller graph for this function:

◆ get_price()

float Stock::get_price ( void )
inline

Get the price of the stock.

Getter function.

Returns
Price of the stock as float.

Definition at line 165 of file stock.h.

165{ return price; }

References price.

Referenced by getProcessedModifiers().

Here is the caller graph for this function:

◆ get_quantity()

unsigned int Stock::get_quantity ( void )
inline

Get the quantity of the stock.

Getter function.

Returns
Stock.quantity as unsigned int.

Definition at line 177 of file stock.h.

177{ return quantity; }

References quantity.

◆ get_split_count()

unsigned int Stock::get_split_count ( void )
inline

Get the split count of the stock.

Getter function.

Returns
Stock.split_count as unsigned int.

Definition at line 225 of file stock.h.

225{ return split_count; }

References split_count.

Referenced by percentage_change_price().

Here is the caller graph for this function:

◆ get_total_attribute()

float Stock::get_total_attribute ( stock_modifiers attribute)

Sums up get_base_attribute() and get_event_attribute().

Parameters
attributeThe attribute to get.
Returns
Total value of the attribute. float type.

Definition at line 317 of file stock.cpp.

317 {
318 return attributes[attribute] + get_event_attribute(attribute);
319}
float get_event_attribute(stock_modifiers attribute)
Get the total change of attribute of the stock due to events only.
Definition stock.cpp:259

References attributes, and get_event_attribute().

Referenced by getProcessedModifiers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

void Stock::load ( const std::string & playerName,
int i )

Load the stock from a file.

Parameters
playerNameThe name of the player.
iThe index of the stock.

Definition at line 57 of file stock.cpp.

57 {
58 std::string fileToBeLoaded;
59 std::ifstream fin;
60 fileToBeLoaded = SAVE_FOLDER_PREFIX + playerName + "/" + std::to_string(i) + "" +
62 std::cout << "Loading " << fileToBeLoaded << " ... ";
63 fin.open(fileToBeLoaded.c_str());
64 // get the first line, which is category
65 fin >> *this; // use operator>> to load the Stock object
66 fin.close();
67 assert(price <= STOCK_PRICE_LIMIT && "Price exceed the limit");
68 std::cout << "done" << std::endl;
69}
const std::string SAVE_FOLDER_PREFIX
Definition file_io.h:45
const std::string SAVE_FILE_EXTENSION_TXT
Definition file_io.h:48
std::string playerName
Player's name.
Definition main.cpp:73
const float STOCK_PRICE_LIMIT
The upper limit of the stock price.
Definition stock.h:34

References playerName, price, SAVE_FILE_EXTENSION_TXT, SAVE_FOLDER_PREFIX, and STOCK_PRICE_LIMIT.

◆ next_round()

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.

276 {
277 /** Update the price of the stock.
278 * If the price is less than 1000, the price will increase or decrease by a random
279 * percentage. If the price is more than 1000, the price will be halved and the
280 * quantity will be doubled.
281 */
282 float price_diff = percentage_change_price(*this) / 100;
283 // Reduce all events duration by one.
284 std::list<Stock_event>::iterator event_itr = events.begin();
285 while (event_itr != events.end()) {
286 if (event_itr->duration > durationDecreaseMultiplier) {
287 event_itr->duration -= durationDecreaseMultiplier;
288 }
289 else {
290 event_itr->duration = 0;
291 }
292 event_itr++;
293 }
294 if (!(price * (1 + price_diff) >= STOCK_PRICE_LIMIT)) {
295 price *= (1 + price_diff);
296 }
297 else {
298 price /= 2;
299 quantity *= 2;
300 split_count++;
302 }
305}
void add_event(const Stock_event &event)
Add an event to the stock.
Definition stock.cpp:210
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_event setup_STOCK_SPLIT_EVENT(void)
Set up a getStockSplitEvent() with proper values.
Definition stock.cpp:269
const unsigned int durationDecreaseMultiplier
Rate of decrease of duration.
Definition events.h:73
float percentage_change_price(Stock stock)
Calculate the percentage change of the stock price.

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().

Here is the call graph for this function:

◆ num_stocks_affordable()

unsigned int Stock::num_stocks_affordable ( float balance,
float trading_fees_percent ) const
Parameters
balanceThe balance of the player.
trading_fees_percentThe trading fees percentage we charge the player.
Returns
Number of stocks that the player can afford with the balance.

Definition at line 166 of file stock.cpp.

167 {
168 float value = balance / (price * (1 + trading_fees_percent));
169 return value < 0 ? 0 : static_cast<unsigned int>(value);
170}

References balance, price, and trading_fees_percent.

◆ purchase()

float Stock::purchase ( float & balance,
unsigned int amount,
float trading_fees_percent )

Purchase a given number of stocks.

Parameters
balanceThe balance ($) of the player. Pass-by-reference.
amountThe number of stocks to purchase.
trading_fees_percentThe trading fees percentage we charge the player.
Returns
Successful: Total cost of the purchase. Failed: -1 if the player does not have enough balance to buy the stock.

Definition at line 139 of file stock.cpp.

140 {
141 float total_cost = price * amount * (1 + trading_fees_percent);
142 // Check if the player has enough balance to buy the stock
143 if (total_cost > balance && price <= 0) {
144 return INVALID_OPERATION;
145 }
146 // Update the balance, quantity, and money_spent
147 balance -= total_cost;
148 quantity += amount;
149 return total_cost;
150}
const int INVALID_OPERATION
Definition stock.cpp:27

References balance, INVALID_OPERATION, price, quantity, and trading_fees_percent.

◆ remove_obselete_event()

void Stock::remove_obselete_event ( void )
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.

247 {
248 std::list<Stock_event>::iterator event_itr = events.begin();
249 while (event_itr != events.end()) {
250 if (event_itr->duration <= 0) {
251 event_itr = events.erase(event_itr);
252 }
253 else {
254 event_itr++;
255 }
256 }
257}

References events.

Referenced by next_round().

Here is the caller graph for this function:

◆ return_most_recent_history()

std::vector< float > Stock::return_most_recent_history ( unsigned int rounds)

Get the stock price of recent rounds.

Parameters
roundsThe number of rounds to look back. 5 means the last 5 rounds.
Returns
A vector of stock prices. If the number of rounds is greater than the history size, return the whole history. Otherwise, return the most recent rounds. If the history is empty, return an empty vector.

Definition at line 177 of file stock.cpp.

177 {
178 std::vector<float> recent_history;
179 if (rounds >= history.size()) {
180 return history;
181 }
182 for (unsigned int i = history.size() - rounds; i < history.size(); i++) {
183 recent_history.emplace_back(history[i]);
184 }
185 return recent_history;
186}

References history.

◆ save()

void Stock::save ( const std::string & playerName,
int i )

Save the stock from a file.

Parameters
playerNameThe name of the player.
iThe index of the stock.

Definition at line 47 of file stock.cpp.

47 {
48 std::string filesave;
49 std::ofstream fout;
50 filesave = SAVE_FOLDER_PREFIX + playerName + "/" + std::to_string(i) + "" +
51 SAVE_FILE_EXTENSION_TXT; // creating the file path
52 fout.open(filesave.c_str());
53 fout << *this; // use operator<< to save the Stock object
54 fout.close();
55}

References playerName, SAVE_FILE_EXTENSION_TXT, and SAVE_FOLDER_PREFIX.

◆ sell()

float Stock::sell ( float & balance,
unsigned int amount,
float trading_fees_percent )

Sell a given number of stocks.

Parameters
balanceThe balance ($) of the player. Pass-by-reference.
amountThe number of stocks to sell.
trading_fees_percentThe trading fees percentage we charge the player.
Returns
Successful: Amount of money the player receive. Failed: -1 if the player does not have enough stocks to sell.

Definition at line 152 of file stock.cpp.

152 {
153 // Check if the player has enough stocks to sell
154 if (quantity < amount && price <= 0) {
155 return INVALID_OPERATION;
156 }
157 // Calculate the total revenue
158 float total_revenue = price * amount * (1 - trading_fees_percent);
159 balance += total_revenue;
160 quantity -= amount;
161 return total_revenue;
162}

References balance, INVALID_OPERATION, price, quantity, and trading_fees_percent.

◆ setup_STOCK_SPLIT_EVENT()

Stock_event Stock::setup_STOCK_SPLIT_EVENT ( void )

Set up a getStockSplitEvent() with proper values.

Definition at line 269 of file stock.cpp.

269 {
270 Stock_event event_copy = getStockSplitEvent();
271 event_copy.text = name + event_copy.text;
272 event_copy.category = category;
273 return event_copy;
274}
Stock_event getStockSplitEvent(void)
Definition events.cpp:1453
The data structure of an event that will be applied to the stocks.
Definition events.h:104
unsigned int category
Definition events.h:129
std::string text
The text that will be displayed to the player.
Definition events.h:116

References category, Stock_event::category, getStockSplitEvent(), name, and Stock_event::text.

Referenced by next_round().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_history()

void Stock::update_history ( void )
private

Update the history array with the current price.

We use vector now!

Definition at line 172 of file stock.cpp.

172 {
173 /** We use vector now! */
174 history.emplace_back(price);
175}

References history, and price.

Referenced by Stock(), and next_round().

Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & fout,
const Stock & stock )
friend

Definition at line 71 of file stock.cpp.

71 {
72 fout << stock.category
73 << std::endl; // literally load everything into class into file
74 fout << stock.name << std::endl;
75 for (unsigned int index = 0; index < stock.history.size(); index++) {
76 fout << stock.history[index] << " ";
77 }
78 fout << -1 << std::endl; // -1 is the stop code for vector<float> history in
79 // filesave
80 fout << stock.quantity << std::endl;
81 fout << stock.attributes.at(standard_deviation) << " ";
82 fout << stock.attributes.at(mean) << " ";
83 fout << stock.attributes.at(lower_limit) << " ";
84 fout << stock.attributes.at(upper_limit) << std::endl;
85 fout << stock.split_count << std::endl << std::endl;
86
87 // Save the ongoing events, separated by std::endl
88 for (Stock_event event : stock.events) {
89 fout << event << std::endl;
90 }
91 return fout;
92}

◆ operator>>

std::istream & operator>> ( std::istream & fin,
Stock & stock )
friend

Definition at line 94 of file stock.cpp.

94 {
95 fin >> stock.category; // line 1
96 assert(stock.category < category_list_size && "Invalid category");
97 // line 2 is entirely the stock name
98 std::getline(fin >> std::ws, stock.name);
99 float loadedPrice;
100 fin >> loadedPrice;
101 // Erase the history vector and load the new history
102 stock.history.clear();
103 while (loadedPrice != -1) {
104 stock.history.emplace_back(loadedPrice);
105 fin >> loadedPrice; // line 3
106 }
107 // Set the price
108 stock.price = stock.history.back();
109 fin >> stock.quantity; // line 4
110 fin >> stock.attributes[standard_deviation]; // line 5
111 fin >> stock.attributes[mean];
112 fin >> stock.attributes[lower_limit];
113 fin >> stock.attributes[upper_limit];
114 fin >> stock.split_count; // line 6
115 // Clear the events list
116 stock.events.clear();
117 // Skip 2 empty lines
118 std::string emptyLine;
119 std::getline(fin >> std::ws, emptyLine);
120 std::getline(fin >> std::ws, emptyLine);
121 std::string loadedEventString;
122 while (std::getline(fin, loadedEventString)) {
123 Stock_event loadedEvent;
124 std::istringstream(loadedEventString) >> loadedEvent;
125 // Check the loaded event is valid
126 if (loadedEvent.event_id == getStockSplitEvent().event_id) {
127 stock.add_event(loadedEvent);
128 continue;
129 }
130 assert(
131 loadedEvent.event_id < all_stock_events.size() && "Invalid event loaded");
132 Stock_event comparedEvent = all_stock_events[loadedEvent.event_id];
133 assert(loadedEvent == comparedEvent && "Invalid event loaded");
134 stock.add_event(loadedEvent);
135 }
136 return fin;
137}
const std::vector< Stock_event > all_stock_events
The list of all events that can be applied to the stocks.
Definition events.cpp:49

Member Data Documentation

◆ attributes

std::map<stock_modifiers, float> Stock::attributes
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().

◆ category

unsigned int Stock::category
private

Use numbers to represent the category of the stock.

Note
The range of the numbers should be [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().

◆ events

std::list<Stock_event> Stock::events
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().

◆ history

std::vector<float> Stock::history
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().

◆ name

std::string Stock::name
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().

◆ price

float Stock::price
private

◆ quantity

unsigned int Stock::quantity
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().

◆ split_count

unsigned int Stock::split_count
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().


The documentation for this class was generated from the following files: