DEV Community

Market Masters
Market Masters

Posted on

Master Risk Management in Trading: Position Sizing, Stop-Losses, and Kelly Criterion with Python

Master Risk Management in Trading: Position Sizing, Stop-Losses, and Kelly Criterion with Python

Risk management separates surviving traders from blown accounts. Most retail traders lose because they bet too big on hunches. Pros treat trading like a business: every position sized for survival, stops in place, math behind bets.

This guide covers core techniques: position sizing, stop-loss placement, risk-reward ratios, and the Kelly Criterion. Includes Python code to automate it. Works for stocks, crypto, futures.

Why Risk Management Matters

Markets don't care about your predictions. Bitcoin drops 20% overnight, NVDA gaps down on earnings. Without rules, one trade wipes months of gains.

Stats: 70-90% of day traders lose money (per broker disclosures). Key reason? Overleveraging. Fixed 1-2% risk per trade keeps you alive through 50-loss streaks.

Market Masters AI screens 2500+ cryptos and S&P stocks with conviction scores from 15 indicators. But even AI needs risk rules.

Position Sizing Basics

Size positions based on account risk, not conviction. Rule: risk 1% of capital per trade.

Formula: Position size = (Account * Risk%) / (Entry - Stop)

Example: $10,000 account, risk 1% ($100). Long BTC at $60,000, stop $58,000. Distance: $2,000.

Size = 100 / 2000 = 0.05 BTC.

For stocks: $AAPL at $180, stop $175. Distance $5/share. Size = 100 / 5 = 20 shares.

Python function:

def position_size(account, risk_pct, entry, stop):
    risk_amount = account * (risk_pct / 100)
    distance = abs(entry - stop)
    size = risk_amount / distance
    return size

# Example
print(position_size(10000, 1, 60000, 58000))  # 0.05 BTC
Enter fullscreen mode Exit fullscreen mode

Stop-Loss Placement

Stops protect capital. Place below support, volatility-based (2x ATR), or recent swing low.

Crypto volatility high: BTC ATR(14) ~$3,000 daily. Stop = entry - 2*ATR.

Never move stops wider mid-trade. Trail them up on winners.

Example: ETH entry $3,000, ATR $150. Stop $3,000 - 300 = $2,700.

Market Masters detects chart patterns (16 types) and Elliott Waves for precise stops.

Risk-Reward Ratios

Target 1:2 or better. Risk $100 to make $200+.

Expectancy = (Win% * Avg Win) - (Loss% * Avg Loss)

1:2 ratio with 40% win rate: positive edge.

Skip trades below 1:1.5.

Kelly Criterion: Optimal Sizing

Kelly maximizes growth: f = (bp - q)/b

f = fraction to bet
b = odds (reward/risk, e.g. 2)
p = win probability
q = 1 - p

Example: p=0.55, b=2. f = (2*0.55 - 0.45)/2 = 0.325 or 32.5%. Too aggressive; use half-Kelly (16%).

Python:

def kelly_criterion(p_win, odds):
    q = 1 - p_win
    f = (odds * p_win - q) / odds
    return max(0, f)  # No shorts

def half_kelly(p_win, odds):
    return kelly_criterion(p_win, odds) / 2

# Backtest edge first
print(half_kelly(0.55, 2))  # 0.1625 -> 1.625% risk
Enter fullscreen mode Exit fullscreen mode

Integrate with backtests. Market Masters Orion AI gives p_win estimates from historical setups.

Practical Example: Crypto Swing Trade

BTC at $67,000 (today's price). Bull flag breakout. Support $65,000. Target $72,000.

Risk: $2,000/share equiv. 1:2.5 RR.

$50k account, 1% risk: size 0.25 BTC.

Code sizes it, sets alerts.

Stops save you. No stops? Hope-based gambling.

Portfolio Risk

Correlations matter. All-in tech stocks? One rate hike tanks all.

Diversify: max 5% per asset, 20% sector.

Market Masters portfolio tools auto-balance with risk profiles.

Common Mistakes

  • Revenge trading after losses
  • Ignoring commissions/slippage (0.1% eats edges)
  • Sizing by feel
  • No journaling

Track in spreadsheet: win%, RR, expectancy.

Tie to AI Tools

Manual math slow. Use Python bots or platforms like Market Masters AI. Orion runs 45 tools: TA, sentiment, liquidations. Free tier screens basics.

Get Started

  1. Journal last 20 trades: calculate expectancy.
  2. Code position sizer.
  3. Paper trade with rules.

Ready for edge? Try Market Masters free (no CC). Build bots, screen markets, join 10k traders.

Questions? Comment below.

Top comments (0)