#C12685. Developing a Stock Trading Bot Simulation
Developing a Stock Trading Bot Simulation
Developing a Stock Trading Bot Simulation
You are required to implement a simulation of a stock trading bot. The bot monitors a stock price and performs buy or sell actions based on specific criteria. The simulation maintains an internal state with the current stock symbol, the highest price observed so far, and the price at which a stock was bought (if any).
For a price update, the bot should:
- Update its highest price if the new price is greater than the current highest price.
- If not currently holding the stock (i.e. no buy has been made), check the buy condition: if the current price is less than 95% of the highest price (i.e. \(current\_price < 0.95 \times highest\_price\)), then execute a buy action at the current price and output "Buy: X" where X is the buy price.
- If already holding the stock, check the sell condition: if the current price is greater than 105% of the bought price (i.e. \(current\_price > 1.05 \times bought\_price\)), then execute a sell action. The bot should output "Sell: X Profit: Y" where X is the selling price and Y is the profit \(Y = current\_price - bought\_price\).
In addition, the bot must support updating the monitored stock symbol. When an update
command is received, the bot resets the highest price and bought price, updates the stock symbol, and outputs "Stock symbol updated to SYMBOL".
Note: This simulation is driven by a series of commands provided via standard input.
inputFormat
The input starts with the initial stock symbol on the first line. The second line contains an integer \(N\) representing the number of commands that follow. Each of the next \(N\) lines contains a command which is either:
- A price update: a floating-point number indicating the current stock price.
- An update command: a line starting with the word
update
followed by a new stock symbol. For example:update GOOGL
.
outputFormat
For each command that triggers an action, output a line as follows:
- If a buy action is performed, output:
Buy: X
- If a sell action is performed, output:
Sell: X Profit: Y
- If an update command is processed, output:
Stock symbol updated to SYMBOL
If a command does not trigger any action, produce no output for that command.
## sampleAAPL
6
100
94
96
105
update GOOGL
200
Buy: 94
Sell: 105 Profit: 11
Stock symbol updated to GOOGL
</p>