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
events.h
Go to the documentation of this file.
1/// @file events.h
2/// Implements event-related data structures and (declarations of) such functions.
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
16#ifndef EVENTS_H
17#define EVENTS_H
18
19#include <istream>
20#include <map>
21#include <sstream>
22#include <string>
23#include <vector>
24
25/**
26 * @enum stock_modifiers
27 * @brief The attributes of a stock that Events will modify are hardcoded here.
28 * @note Please store them as `std::map<stock_modifiers, float>`.
29 */
31 /** Amount of variation of the stock price percentage change. */
33 /** The expectation of the stock price percentage change.
34 * For a normal distribution, this is the mean, median and mode.
35 */
37 /** The lower limit of the stock price percentage change.
38 * For example, if lower_limit is -10, then the percentage change will not go below
39 * -10%. Note that stock initially has a lower limit of -40%. This value adds to
40 * that. The value should be `[-100, 0]`.
41 */
43 /** The upper limit of the stock price percentage change.
44 * For example, if upper_limit is 10, then the percentage change will not go over
45 * +10%. Note that stock initially has a upper limit of +40%. This value adds to
46 * that. The value should be `[0, 100]`.
47 */
49};
50
51/// @brief Multiplier for mean
52constexpr float meanMultiplier = 0.6f;
53
54/// @brief Multiplier for standard deviation
55const float sdMultiplier = 7.5f;
56
57/// @brief Lower limit multiplier
58const float lowerLimitMultiplier = 0.2;
59
60/// @brief Upper limit multiplier
61const float upperLimitMultiplier = 0.2;
62
63/// @brief Default lower limit
64const float defaultLowerLimit = -5;
65
66/// @brief Default upper limit
67const float defaultUpperLimit = 5;
68
69/// @brief Default mean
70constexpr float defaultMean = 0.5f / meanMultiplier;
71
72/// @brief Rate of decrease of duration
73const unsigned int durationDecreaseMultiplier = 2;
74
75/** @enum event_type
76 * @brief The type of event that will be applied to the stocks
77 * @note These objects does not have any values, they are used to specify the type of
78 * event.
79 * @code {.cpp}
80 * if (event.type_of_event == all_stocks) {
81 * // Apply the event to all stocks
82 * for (auto &stock : stocks) {
83 * stock.add_event(event);
84 * }
85 * } else if (event.type_of_event == category) { ...
86 * @endcode
87 */
89 /** This event will apply to all stocks. */
91 /** This event will apply to stocks within the specified category. */
93 /** This event will apply to a randomly selected stock from all of the stocks
94 available currently. */
96};
97
98/**
99 * @struct Stock_event events.h "events.h"
100 * @brief The data structure of an event that will be applied to the stocks.
101 * @note <b> For more information about how to write your own events,
102 * visit the documentation of the @ref events.h "events.h" file. </b>
103 */
105 public:
106 /** @brief The id of the event.
107 * This is still required for checking, despite the fact that we are using a
108 * vector.
109 */
110 unsigned int event_id;
111
112 /** @brief A list of event_ids that this event is mutually exclusive with. */
113 std::vector<unsigned int> mutually_exclusive_events;
114
115 /** @brief The text that will be displayed to the player. */
116 std::string text;
117
118 /** @brief Number of rounds the event will last. If the event has duration <= 0
119 * then it will be removed. */
120 unsigned int duration;
121
122 /** @brief 0 to 1000, so 114 means 11.4% */
124
125 /** @brief The type of event: Apply to all stocks, in one category or randomly?
126 */
128
129 unsigned int category;
130
131 /** @brief Stores the stock_modifiers that the event applies. */
132 std::map<stock_modifiers, double> modifiers;
133
134 /** @brief Overload the == operator to compare two Stock_event */
135 bool operator==(const Stock_event & other) const {
136 return event_id == other.event_id && text == other.text &&
138 type_of_event == other.type_of_event && category == other.category &&
139 modifiers == other.modifiers;
140 }
141
142 /**
143 * @brief Serialize the event as std::ostream object.
144 * @param outputstream The std::ostream object to write the data.
145 * @param event The event object to get the data from.
146 * @return A std:ostream object contains all the data of the event.
147 * @code
148 * // Create a event object by calling the constructor
149 * Stock_event event;
150 * // Print the data of the event.
151 * std::cout << event << std::endl;
152 * @endcode
153 */
154 friend std::ostream & operator<<(
155 std::ostream & outputstream, const Stock_event & event) {
156 outputstream << event.event_id << " ";
157 for (unsigned int i = 0; i < event.mutually_exclusive_events.size(); i++) {
158 outputstream << event.mutually_exclusive_events[i] << " ";
159 }
160 outputstream << ";" << event.text << ";" << event.duration << " "
161 << event.probability_permille << " " << event.type_of_event
162 << " " << event.category << " ";
163 for (const auto & modifier : event.modifiers) {
164 outputstream << modifier.second << " ";
165 }
166 return outputstream;
167 }
168
169 /**
170 * @brief Deserialize the event from a std::istream object.
171 * @param inputstream The std::istream object to read the data.
172 * @param event The event object to store the data.
173 * @return A std:istream object contains all the data of the event.
174 */
175 friend std::istream & operator>>(
176 std::istream & inputstream, Stock_event & event) {
177 // fix the bug that mutually_exclusive_events is not read correctly
178 inputstream >> event.event_id;
179 std::string _mutually_exclusive_events;
180 std::getline(inputstream, _mutually_exclusive_events, ';');
181 std::istringstream mutually_exclusive_events_stream(
182 _mutually_exclusive_events);
183 unsigned int temp_id;
184 while (mutually_exclusive_events_stream >> temp_id) {
185 event.mutually_exclusive_events.emplace_back(temp_id);
186 }
187 std::getline(inputstream, event.text, ';');
188 unsigned int temp_type;
189 inputstream >> event.duration >> event.probability_permille >> temp_type >>
190 event.category;
191 event.type_of_event = static_cast<event_type>(temp_type);
192 inputstream >> event.modifiers[standard_deviation] >>
193 event.modifiers[mean] >> event.modifiers[lower_limit] >>
194 event.modifiers[upper_limit];
195 return inputstream;
196 }
197};
198
199/** @brief The list of all events that will be applied to the stocks. */
200extern const std::vector<Stock_event> all_stock_events;
201
202typedef std::map<decltype(Stock_event::event_id),
203 std::vector<decltype(Stock_event::event_id)>>
205
206/**
207 * @brief Pick a random event from the list of events
208 * @param all_events The list of all events
209 * @param num_events The number of events to pick
210 * @return A vector of Stock_event
211 */
212std::vector<Stock_event> pick_events(
213 const std::vector<Stock_event> & all_events, unsigned int num_events);
214
216 const std::vector<Stock_event> & all_events);
217
218/**
219 * @brief If A is mutually exclusive with B, then B is mutually exclusive with A.
220 * Check if these two events specifies each other as mutually exclusive in
221 * mutually_exclusive_events.
222 * @param all_events The list of all events
223 * @return A map of event_id to a vector of mutually exclusive event_ids that does not
224 * exist but should.
225 */
227 const std::vector<Stock_event> & all_events);
228
229/**
230 * @brief Check if there are mutual exclusivity violations in all_stock_events.
231 * @return True if the events are mutually exclusive with each other.
232 */
234
235/** @brief Print a map to std::cout.
236 * @param map The std::map object you want to print.
237 */
238void print_map(const std::map<unsigned int, std::vector<unsigned int>> & map);
239
241
242/**
243 * @brief Filter out the events that are mutually exclusive with each other,
244 * or events that are identical (except for event_type::pick_random_stock).
245 * @param picked_events The list of events to filter.
246 * @param all_events The list of all events to consider.
247 * @return A list of events that are mutually exclusive with each other.
248 */
249std::vector<Stock_event> uniq_events(std::vector<Stock_event> picked_events,
250 const std::vector<Stock_event> & all_events);
251
253
254#endif
mutuallyExclusiveMap build_mutual_exclusivity_map(const std::vector< Stock_event > &all_events)
Definition events.cpp:1293
constexpr float meanMultiplier
Multiplier for mean.
Definition events.h:52
const float sdMultiplier
Multiplier for standard deviation.
Definition events.h:55
std::map< decltype(Stock_event::event_id), std::vector< decltype(Stock_event::event_id)> > mutuallyExclusiveMap
Definition events.h:204
stock_modifiers
The attributes of a stock that Events will modify are hardcoded here.
Definition events.h:30
@ upper_limit
The upper limit of the stock price percentage change.
Definition events.h:48
@ 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
void print_map(const std::map< unsigned int, std::vector< unsigned int > > &map)
Print a map to std::cout.
Definition events.cpp:1269
const float defaultUpperLimit
Default upper limit.
Definition events.h:67
const float defaultLowerLimit
Default lower limit.
Definition events.h:64
event_type
The type of event that will be applied to the stocks.
Definition events.h:88
@ pick_random_stock
This event will apply to a randomly selected stock from all of the stocks available currently.
Definition events.h:95
@ category
This event will apply to stocks within the specified category.
Definition events.h:92
@ all_stocks
This event will apply to all stocks.
Definition events.h:90
std::vector< Stock_event > uniq_events(std::vector< Stock_event > picked_events, const std::vector< Stock_event > &all_events)
Filter out the events that are mutually exclusive with each other, or events that are identical (exce...
Definition events.cpp:1380
Stock_event getStockSplitEvent(void)
Definition events.cpp:1453
const std::vector< Stock_event > all_stock_events
The list of all events that will be applied to the stocks.
Definition events.cpp:49
bool assertion_check_mutual_exclusivity(void)
Check if there are mutual exclusivity violations in all_stock_events.
Definition events.cpp:1331
mutuallyExclusiveMap check_mutual_exclusivity(const std::vector< Stock_event > &all_events)
If A is mutually exclusive with B, then B is mutually exclusive with A.
Definition events.cpp:1307
constexpr float defaultMean
Default mean.
Definition events.h:70
const unsigned int durationDecreaseMultiplier
Rate of decrease of duration.
Definition events.h:73
const float upperLimitMultiplier
Upper limit multiplier.
Definition events.h:61
std::vector< Stock_event > pick_events(const std::vector< Stock_event > &all_events, unsigned int num_events)
Pick a random event from the list of events.
Definition events.cpp:1348
void assertion_check_uniq_events(void)
Definition events.cpp:1416
const float lowerLimitMultiplier
Lower limit multiplier.
Definition events.h:58
The data structure of an event that will be applied to the stocks.
Definition events.h:104
event_type type_of_event
The type of event: Apply to all stocks, in one category or randomly?
Definition events.h:127
bool operator==(const Stock_event &other) const
Overload the == operator to compare two Stock_event.
Definition events.h:135
friend std::ostream & operator<<(std::ostream &outputstream, const Stock_event &event)
Serialize the event as std::ostream object.
Definition events.h:154
friend std::istream & operator>>(std::istream &inputstream, Stock_event &event)
Deserialize the event from a std::istream object.
Definition events.h:175
unsigned int probability_permille
0 to 1000, so 114 means 11.4%
Definition events.h:123
unsigned int duration
Number of rounds the event will last.
Definition events.h:120
unsigned int category
Definition events.h:129
std::map< stock_modifiers, double > modifiers
Stores the stock_modifiers that the event applies.
Definition events.h:132
std::string text
The text that will be displayed to the player.
Definition events.h:116
std::vector< unsigned int > mutually_exclusive_events
A list of event_ids that this event is mutually exclusive with.
Definition events.h:113
unsigned int event_id
The id of the event.
Definition events.h:110