Most traders choose how many shares to buy first and think about the stop later. Reversing the order is the single most useful habit in risk control.
Start from the loss
Decide what share of your account you are prepared to lose on one trade. Many rules-based traders use a small fixed percentage. Then decide where the trade is wrong: the price at which you would exit. The distance between entry and stop is your risk per share.
The arithmetic
Position size equals the amount you are willing to lose divided by the risk per share. With a $50,000 account and 1% at risk, you can lose $500. If you enter at $40 with a stop at $38, the risk is $2 a share, so the position is 250 shares, or $10,000.
Notice what the formula does. A wider stop gives a smaller position. A tighter stop gives a larger one. The loss if you are wrong stays the same.
Why it matters after losses
Losses compound against you. After a 20% drawdown you need a 25% gain to get back; after 50% you need 100%. Keeping each loss small is what keeps recovery within reach.
Inside an ATM Agent this is the job of the last two gates: engine logic suggests the size from predefined parameters, and risk governance sets the stop and drawdown thresholds before any flag is raised.