Decide what you are prepared to lose if your stop fills, and get the position size that keeps the loss there.
Add an entry, a stop, a risk amount and a cost allowance.
What the size does and does not cover
Units = risk amount ÷ (distance from entry to stop + entry × cost allowance). The result is rounded down to six decimals, and a venue's size rules can trim it further.
Margin is the position size divided by leverage. It leaves out the venue's maintenance margin and anything else you already hold. Funding is not modelled.