Price Ticks Converter
Prerequisites: Integer Ticks and Price Representation, Floating-Point Representation
Prices arrive as decimal text, such as 190.105, from feed messages, config files, and operators. Convert that text to integer tick counts and back, for one instrument.
- Tick count: a price stored as a whole number of ticks in an
std::int64_t. It becomes text only at the display edge. - Tick size: the price of one tick, a decimal string such as
0.01(most US stocks),0.005, or0.25. D: the number of digits after the point intick_size, trailing zeros included: 2 for0.01and0.50, 3 for0.005, 0 for5.
What to implement
process_commands(tick_size, commands)returns one output line per command, in order.- On stdin:
tick_sizeon the first line, the number of commands on the second, then one command per line. - There are three parts, each adding to the last. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
TICKS price | Convert a price to a tick count | the tick count, or OFF_TICK |
PRICE ticks | Convert a tick count to a price | the price, with D decimals |
ROUND price mode | Round a price to a tick (Part 2) | ticks price |
Number formats
- An input price is digits, optionally followed by a point and at least one more digit.
190,190.1, and190.100000are all valid; trailing zeros after the point don't change the value. - No leading zeros before the point, except a single
0below 1, as in0.29. The tick size uses the same format. - A tick count in
PRICEis a plain integer. Every input is well formed. - A printed price is the whole part without leading zeros (a single
0below 1), the point, and exactlyDdigits, zero-padded on the right. WhenDis 0, there is no point.
tick_size | Ticks | Prints |
|---|---|---|
0.01 | 5 | 0.05 |
0.01 | 100 | 1.00 |
5 | 38 | 190 |
Integers only
- No
doubleorfloatfor a price or the tick size, and no decimal or big-number library such as Python'sdecimalmodule. - As Floating-Point Representation showed, most decimal prices have no exact
double: in both C++ and Python,0.29 / 0.01evaluates to28.999999999999996. The tests include such prices, and a floating-point solution fails some of them.
Part 1: Ticks and prices
- A price is on tick when it equals a whole number
nof ticks,price = n × tick_size.TICKS priceprintsn, orOFF_TICKotherwise. - A price may carry more digits after the point than the tick size does.
PRICE ticksprints the price ofticksticks,ticks × tick_size, withDdecimals.
With tick_size = 0.005:
| Command | Prints |
|---|---|
TICKS 190.105 | 38021 |
TICKS 190.1 | 38020 |
TICKS 190.104 | OFF_TICK: it lies between tick 38020 (190.100) and tick 38021 (190.105) |
PRICE 38021 | 190.105 |
PRICE 200 | 1.000 |
Limits in Parts 1 and 2
- Every price is non-negative, with at most 6 digits before the point and 6 after it. Printed prices obey the same bounds.
- The tick size is positive, at most
1000, with at most 6 digits after the point. - There are at most 105 commands.
Tests 1–4 cover this part. Test 4 uses tick_size = 0.000001, where a tick count such as 123456789012 is far above the 32-bit int limit of about 2.1 × 109.
Part 2: Round off-tick prices
An exchange rejects an off-tick limit price, so a gateway rounds such prices before sending them.
loweris the largest tick count whose price is at most the given price.- An off-tick price lies strictly between tick
lowerand ticklower + 1. An on-tick price equals ticklower. ROUND price modepicks one of the two bymode. Every mode returns an on-tick price unchanged.
mode | Result |
|---|---|
DOWN | lower, which rounds toward minus infinity |
UP | lower if the price is on tick, otherwise lower + 1, which rounds toward plus infinity |
NEAREST_EVEN | whichever of lower and lower + 1 is nearer; at exactly halfway, whichever is even |
BUY | the same as DOWN |
SELL | the same as UP |
Output: one line.
ticks price
ticks= the resulting tick count.price= the price of that many ticks, formatted as inPRICE.
Notes on the modes
NEAREST_EVENis also called banker's rounding. Rounding every tie up would push a large set of rounded prices upward on average; the even neighbor sends about half the ties each way.- Its parity is the tick count's, not the price's last digit. With
tick_size = 0.05,0.125is 2.5 ticks and rounds to 2 ticks,0.10;0.175is 3.5 ticks and rounds to 4 ticks,0.20. BUYandSELLfollow a common limit-price convention: rounding must not move a price against its sender. A buy rounds down, so it never pays more than its given price; a sell rounds up, so it never sells for less.- With
tick_size = 0.01,ROUND 190.104 BUYprints19010 190.10, andROUND 190.104 SELLprints19011 190.11.
Tests 5–7 cover this part.
Part 3: Negative spreads at full precision
A spread, the price of one instrument minus the price of another, can be negative. Some instruments are priced to nine decimal places. Part 3 lifts both limits, and every rule above applies unchanged.
New limits
- A price may begin with a minus sign, as in
-0.05, andPRICEmay receive a negative tick count. - A price may have up to 9 digits before the point and 9 after it. Printed prices obey the same bounds.
- The tick size may have up to 9 digits after the point, so it can be as small as
0.000000001.
Signs, with tick_size = 0.01:
DOWNandBUYstill round toward minus infinity.ROUND -190.104 DOWNprints-19011 -190.11, whileROUND -190.104 UPprints-19010 -190.10.- A negative price prints as a minus sign followed by its magnitude, formatted as before:
PRICE -3prints-0.03. - Zero never prints a sign:
TICKS -0.00prints0, andROUND -0.004 UPprints0 0.00. NEAREST_EVENuses the tick count's parity even when the count is negative:-0.015is -1.5 ticks and rounds to -2, printing-2 -0.02.
18 digits: with tick_size = 0.000000001, the price 123456789.123456789 is 123456789123456789 ticks. That is below the int64 limit of about 9.2 × 1018, so an std::int64_t holds it exactly. It is above 253, past which a double no longer holds every integer.
Tests 8–10 cover this part.
0.01 6 TICKS 190.10 TICKS 190.1 TICKS 190.105 TICKS 0.29 PRICE 19010 PRICE 5
19010 19010 OFF_TICK 29 190.10 0.05
The tick size is one cent, so D is 2 and every printed price has two decimals.
190.10 and 190.1 are the same price, 19010 cents. 190.105 sits half a cent above tick 19010, so it is OFF_TICK.
0.29 is exactly 29 ticks, although dividing the doubles 0.29 / 0.01 gives 28.999999999999996, which truncates to 28.
PRICE 19010 prints 190.10, keeping the trailing zero that D = 2 requires. PRICE 5 prints 0.05, with a leading 0 and the fraction padded to two digits.