Sub topic 3
Investing by dollar amount: orders and rounding
This chapter explains what happens between "I want to invest $50" and "I own 0.166666 shares." It covers how a dollar-based order is turned into a share quantity, where rounding enters, and what the leftover amounts mean for your records. The chapter assumes you know:
- what a share and an ETF are
- how to compute averages and percentages
- the idea of a market order, which is an order to trade at the best available price
You don't need a brokerage account to follow the arithmetic.
Two ways to specify an order
Brokers that support fractional shares usually let you describe a purchase in one of two ways.
- Share-based order. You say how many shares to buy, such as 2 shares or 0.5 shares. The cost depends on the price at which the order fills.
- Dollar-based order. You say how much money to spend, such as $50. The share count depends on the price at which the order fills.
Both orders are uncertain in one quantity. A share-based order fixes the shares and leaves the cost open. A dollar-based order fixes the cost and leaves the shares open. Recurring investing mostly uses dollar-based orders, because your budget is a fixed amount of cash per period.
| Order type | You fix | The market decides | Typical use |
|---|---|---|---|
| Share-based | Number of shares | Total cost | Buying a target position size |
| Dollar-based | Cash to spend | Number of shares | Recurring contributions |
Many brokers restrict fractional orders. Common restrictions are market orders only, or no stop orders. Some brokers also allow fractional trading only in a list of eligible securities. These rules differ between brokers, so check yours before you design a plan around a specific order type.
From dollars to shares
The core formula is simple:
shares = dollar amount / price per shareTwo details make it harder in practice.
- The price is not known when you submit the order. It is the price at which the order fills. For a market order this can differ slightly from the last price you saw on screen.
- The result is rarely a tidy number. $50 at $300 per share is 0.1666666... shares, and the decimals never end. The broker has to cut the number off somewhere.
The broker therefore applies a precision rule, which is a fixed number of decimal places. Six places is a common illustration, but your broker may use fewer or more. Many brokers truncate, meaning they round down, so they never fill you for more shares than your dollars can pay for. Others round to the nearest unit. The tutorial's example truncates.
from decimal import Decimal, ROUND_DOWN
amount = Decimal("50.00")
price = Decimal("300.00")
shares = (amount / price).quantize(Decimal("0.000001"), rounding=ROUND_DOWN)
print(shares)
# 0.166666quantize forces the result to the given number of decimal places. ROUND_DOWN drops the extra digits instead of rounding them up. The result is 0.166666 shares, and the dropped part is the leftover.
Why the example uses `Decimal`
Python's float stores numbers in binary, and many decimal fractions cannot be stored exactly. The classic case is 0.1 + 0.2, which gives 0.30000000000000004. For one calculation the error is tiny. If you store, sum, and compare thousands of lots, the errors can make totals disagree by a cent or fail an equality check.
Decimal stores decimal digits exactly, and you choose the rounding mode explicitly. Use it whenever your code handles money or share counts. The cost is that you must pass numbers in as strings, such as Decimal("0.1"). Decimal(0.1) copies the float's binary error into the result.
The leftover amount
Truncation means the filled shares are worth slightly less than the dollars you committed. The next example computes that gap for three prices.
from decimal import Decimal, ROUND_DOWN
def dollars_to_shares(amount, price, places=6):
step = Decimal(1).scaleb(-places)
shares = (Decimal(amount) / Decimal(price)).quantize(step, rounding=ROUND_DOWN)
cost = shares * Decimal(price)
return shares, cost, Decimal(amount) - cost
for price in ["300.00", "187.43", "3.17"]:
shares, cost, left = dollars_to_shares("50.00", price)
print(price, shares, cost, left)
# 300.00 0.166666 49.99980000 0.00020000
# 187.43 0.266766 49.99995138 0.00004862
# 3.17 15.772870 49.99999790 0.00000210scaleb(-places) builds the step size 1E-6, which is one millionth of a share. The function returns the shares, their exact cost, and the amount left over.
The leftover is a few hundredths of a cent. That is why precision matters little for one order of $50. In practice your account balance is in cents, so the broker settles the trade to the nearest cent. How a broker handles that sub-cent remainder is a platform detail you may never see, but it is why your statement can show a cost that differs by a cent from your own calculation.
The lesson is about size, not secrecy. Truncation error at six places is at most one millionth of a share per order. At $300 per share that is at most $0.0003. Over 1,000 orders it would still be under 30 cents. If a broker only supports two decimal places for shares, the error is much larger. At $300 per share, one hundredth of a share is $3.
Order lifecycle
A dollar-based order passes through several steps. The exact flow varies, but the steps are broadly like this.
Two points in this flow often surprise new investors.
- Execution timing. Some brokers execute fractional orders in batches at set times instead of the instant you submit. Your fill price then comes from the batch, not from the moment you tapped the button. If you submit outside market hours, the order typically waits until the next session.
- The conversion happens after pricing. You do not lock in a share count when you submit. You only learn it from the confirmation.
Edge cases worth knowing
Minimum order size. Brokers often set a floor, such as $1 or $5, for a dollar-based order. Plan contributions that are comfortably above it. A very small contribution is also more likely to be affected by fees. Costs and minimums are covered in a later chapter.
Very high-priced shares. If a share costs $3,000 and you invest $50, you receive about 0.0166 shares. If the broker's precision is only a few decimal places, the truncated amount is a larger share of your order. At four places, truncation costs at most 0.0001 shares, or about $0.30 at that price.
Very low-priced shares. For a $3.17 share, $50 buys more than 15 shares, so rounding barely matters. The share count is still fractional, and your position will not be a whole number.
Selling. Many brokers let you sell fractional positions. Some let you sell only the whole position when it is below one share. Check how your broker treats a partial sale so that you can exit when you want to.
Transfers. If you move an account to another broker, fractional shares may not transfer in kind. The old broker may sell them for cash first. That can trigger a taxable sale, which a later chapter covers.
Price changes between submission and fill. A market order has no guaranteed price. For a liquid ETF the difference is usually small. For a thinly traded stock it can be larger. This is a reason to prefer broad, liquid funds for automated purchases.
Dividend-sized remainders. Dividends are also paid in cash amounts, so they hit the same conversion step. A later chapter covers fractional reinvestment.
Checking your own orders
A short routine will catch most surprises.
- Record the dollar amount you ordered.
- Read the fill price and the share count from the confirmation.
- Multiply shares by fill price. The result should equal your dollars within a cent, minus any fee.
- Record the share count with its full precision, not a rounded display value.
The fourth step matters because the display in an app may show only two or four decimals, while the broker holds more. If you keep your own records, such as a spreadsheet, use the confirmation values. If you keep the rounded display, a small error enters every row and grows as you add rows.
Exercise
You place a dollar-based order for $20.00 of a fund that fills at $70.00 per share. Your broker truncates shares to four decimal places. How many shares do you receive, what is their exact cost, and how much of your $20.00 is left over?
Show answer
Shares = 20 / 70 = 0.285714..., which truncates to 0.2857. Exact cost = 0.2857 × 70 = $19.999. Leftover = 20.00 − 19.999 = $0.001, which is a tenth of a cent. The broker settles to the nearest cent, so you would normally see a cost of $20.00 on your statement.
Exercise
Why does the code in this chapter pass strings such as "0.000001" to Decimal instead of the number 0.000001?
Show answer
A float such as 0.000001 is stored in binary and is not exactly one millionth. Passing it to Decimal would copy that tiny binary error into the result. A string gives Decimal the exact decimal digits, so the rounding step and the leftover calculation are exact.