LIVE
connecting to the live quote stream…
--:--:--UTC
easyTrading○
#43

Position size from stop distance

easyTradingacceptance —~10 min

Implement position_size(equity, risk_frac, entry, stop) — the number of WHOLE shares to buy so that being stopped out loses at most equity·risk_frac: floor(equity·risk_frac / |entry - stop|).

Risk a fixed fraction per trade and let the stop set the size — the rule the safety settings on this site enforce for you. Return an integer.

example 1
input
equity=10000.0, risk_frac=0.01, entry=50.0, stop=48.0
output
50
note

Risking $100 at $2 per share buys 50 shares.

example 2
input
equity=10000.0, risk_frac=0.01, entry=50.0, stop=47.6
output
41
constraints
  • equity > 0
  • 0 < risk_frac ≤ 0.05
  • entry != stop
teacher · 2 hints · the first is free
↗ read the theory
the chapter this problem is cut from
▶ open in the workbench
run the same math on a real tape
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to position_size()
equity=10000.0, risk_frac=0.01, entry=50.0, stop=48.0
expected
50

the judge calls position_size() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice