Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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, or 0.25.
  • D: the number of digits after the point in tick_size, trailing zeros included: 2 for 0.01 and 0.50, 3 for 0.005, 0 for 5.

What to implement

  • process_commands(tick_size, commands) returns one output line per command, in order.
  • On stdin: tick_size on 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.
CommandMeaningPrints
TICKS priceConvert a price to a tick countthe tick count, or OFF_TICK
PRICE ticksConvert a tick count to a pricethe price, with D decimals
ROUND price modeRound 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, and 190.100000 are all valid; trailing zeros after the point don't change the value.
  • No leading zeros before the point, except a single 0 below 1, as in 0.29. The tick size uses the same format.
  • A tick count in PRICE is a plain integer. Every input is well formed.
  • A printed price is the whole part without leading zeros (a single 0 below 1), the point, and exactly D digits, zero-padded on the right. When D is 0, there is no point.
tick_sizeTicksPrints
0.0150.05
0.011001.00
538190

Integers only

  • No double or float for a price or the tick size, and no decimal or big-number library such as Python's decimal module.
  • As Floating-Point Representation showed, most decimal prices have no exact double: in both C++ and Python, 0.29 / 0.01 evaluates to 28.999999999999996. The tests include such prices, and a floating-point solution fails some of them.

Part 1: Ticks and prices

  1. A price is on tick when it equals a whole number n of ticks, price = n × tick_size. TICKS price prints n, or OFF_TICK otherwise.
  2. A price may carry more digits after the point than the tick size does.
  3. PRICE ticks prints the price of ticks ticks, ticks × tick_size, with D decimals.

With tick_size = 0.005:

CommandPrints
TICKS 190.10538021
TICKS 190.138020
TICKS 190.104OFF_TICK: it lies between tick 38020 (190.100) and tick 38021 (190.105)
PRICE 38021190.105
PRICE 2001.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.

  1. lower is the largest tick count whose price is at most the given price.
  2. An off-tick price lies strictly between tick lower and tick lower + 1. An on-tick price equals tick lower.
  3. ROUND price mode picks one of the two by mode. Every mode returns an on-tick price unchanged.
modeResult
DOWNlower, which rounds toward minus infinity
UPlower if the price is on tick, otherwise lower + 1, which rounds toward plus infinity
NEAREST_EVENwhichever of lower and lower + 1 is nearer; at exactly halfway, whichever is even
BUYthe same as DOWN
SELLthe same as UP

Output: one line.

ticks price
  • ticks = the resulting tick count.
  • price = the price of that many ticks, formatted as in PRICE.

Notes on the modes

  • NEAREST_EVEN is 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.125 is 2.5 ticks and rounds to 2 ticks, 0.10; 0.175 is 3.5 ticks and rounds to 4 ticks, 0.20.
  • BUY and SELL follow 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 BUY prints 19010 190.10, and ROUND 190.104 SELL prints 19011 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, and PRICE may 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:

  1. DOWN and BUY still round toward minus infinity. ROUND -190.104 DOWN prints -19011 -190.11, while ROUND -190.104 UP prints -19010 -190.10.
  2. A negative price prints as a minus sign followed by its magnitude, formatted as before: PRICE -3 prints -0.03.
  3. Zero never prints a sign: TICKS -0.00 prints 0, and ROUND -0.004 UP prints 0 0.00.
  4. NEAREST_EVEN uses the tick count's parity even when the count is negative: -0.015 is -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.

Example
Input
0.01
6
TICKS 190.10
TICKS 190.1
TICKS 190.105
TICKS 0.29
PRICE 19010
PRICE 5
Output
19010
19010
OFF_TICK
29
190.10
0.05
Explanation

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.