Stock Predictions
Algorithms that can correctly predict stock prices can help generate millions of dollars. In this one-player game, you try to predict the rise and fall of the stock price of various stocks and buy or sell the stocks accordingly.
You start with $100. Each turn you will be given the stock prices of current day and previous 4 days. You must then choose to BUY or SELL the stocks. Your program will be run with input for 1 day at a time.
The stock prices are generated by us and may contain patterns.
Input Format
The input of each turn will consist of multiple lines. All money values are doubles to two decimal places, all other numbers are integers.
The first line contains three space separated numbers m k d.
- m - the amount of money you could spend that day.
- k - the number of different stocks available for buying or selling.
- d - the number of remaining days for trading stocks.
k lines follow, each in the following format: name owned prices
- name - the name of the stock (a string).
- owned - the number of shares you own of that stock.
- prices - 5 space separated numbers representing the stock's price for the last 5 days. These are ordered from oldest to newest, so the last number is the current stock price.
Your program will be fed the days sequentially so you can write to a file in order to store a longer history of the prices.
Output Format
The output for each turn should also contain multiple lines:
Output N for the number of transactions you wish to make. Output 0 if you are not making any transactions that day.
If you are making transactions, output N lines containing the name of the stock (case sensitive), BUY or SELL, and the number of shares you wish to buy or sell.
NOTE: Money earned from selling stocks will only become available (for buying stocks) on the following day.
Constraints
1<=k<=10
Sample Input
90 2 400
iStreet 10 4.54 5.53 6.56 5.54 7.60
HR 0 30.54 27.53 24.42 20.11 17.50
Sample Output
2
iStreet SELL 10
HR BUY 5
Explanation
You have 90 dollars and 10 iStreet stocks. You decide to sell the iStreet stocks, then purchase 5 HR stocks. After the transactions you will have 90 - 17.50 x 5 + 7.60 x 10 = 78.5 dollars and 5 HR stock for the next days trading.
You could not have bought more than 5 HR stock because you do not get to spend the money from selling iStreet stock until the next day.
Scoring
Your score is = to 5 x ln(money). You will only receive points from the hidden test case.
Task
You trade until you run out of days. Any stocks left on the last day will automatically be sold at the last day's market price. Your objective is to make as much money as possible starting from $100.
Complete the function printTransactions that takes a float m money left, an integer k number of stocks, an integer d days left, a string array name of stock names, an integer array owned of stocks owned, and an 2d integer array prices of stock prices containing k arrays of length 5 and print your output.
All indexes correspond properly (For index i, the name of the stock is name[i], the number of shares of it you hold is owned[i] and the data about it is prices[i].
Bigger data set
A bigger data set can be downloaded here. The data for the hidden test cases will be similiar.
Copyright © 2025 HackerRank. All Rights Reserved