Position sizing answers one question: how big can this trade be if I accept to lose a fixed share of my account when my stop is hit?
It is a small formula, and a good exercise in input validation and floating point. Here is a version in TypeScript.
Disclosure: I am the founder of copy.top. We publish a free position size calculator , and this post explains the math behind that kind of tool. The code below is a minimal version written for this article.
The formula
Four inputs:
account : the size of your account
riskPct : the share of the account you accept to lose on one trade, in percent
entry : your entry price
stop : the price where you exit if you are wrong
Then:
riskAmount = account * riskPct / 100 lossPerCoin = |entry - stop| quantity = riskAmount / lossPerCoin notional = quantity * entry
Enter fullscreen mode
Exit fullscreen mode
The idea is that the stop distance decides the size.…