You are writing a broker program that interfaces with an exchange via a stream of commands on stdin. You must process commands in order and write required outputs to stdout.
Timestamps are integers (seconds since market open) and are non-decreasing across all input lines.
Security names and client names are unquoted strings with no spaces. All numeric values fit in 32-bit signed integers.
A line of the form:
print <timestamp> <security> <quantity> <price>
quantity
and
price
are positive integers.
A line of the form:
volume-check <timestamp> <security>
For each volume-check, output one line containing the trailing-minute market volume for that security at that timestamp.
quantity
from all
market trade prints
(
print ...
) for that security with timestamps in the last 60 seconds ending at the query time.
t-60 < trade_timestamp <= t
.
Output format (Part 1):
<volume>
In addition to the above, the input may contain client orders:
order <timestamp> <security> <client> <goal_shares> <participation_rate>
goal_shares
is a positive integer (total shares the client wants to trade).
participation_rate
is an integer percentage (e.g.,
20
means 20%).
When processing the stream, you may generate your own trades to fill active orders.
print
line; if no price has ever been printed for that security yet, you cannot trade it).
t
, the client’s cumulative executed shares for that security in the trailing minute must be at most:
floor( market_volume(t) * participation_rate / 100 )
where
market_volume(t)
is the trailing-minute market volume defined above (from
exchange print lines only
, not including your own executions).
goal_shares
are fully executed.
For each executed trade you generate, output a line:
print <timestamp> <security> <num_shares> <price>
timestamp
is the time at which you execute it (i.e., the timestamp of the currently processed input line that enabled the trade).
Input:
print 122 GOLD 100 10
print 123 GOLD 100 10
order 123 GOLD BOB 100 20
print 124 GOLD 100 10
Output:
print 123 GOLD 40 10
print 124 GOLD 20 10
Explanation (informal): at t=123, trailing-minute market volume is 200, so max allowed is floor(200*20/100)=40. After the next market print at t=124, trailing-minute market volume is 300, so max allowed becomes 60, allowing 20 more shares.
Implement this streaming processor.
t
before reading the next input line).