position-sizer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Position Sizer

仓位计算器

Overview

概述

Calculate the optimal number of shares to buy for a long stock trade based on risk management principles. Supports three sizing methods:
  • Fixed Fractional: Risk a fixed percentage of account equity per trade (default: 1%)
  • ATR-Based: Use Average True Range to set volatility-adjusted stop distances
  • Kelly Criterion: Calculate mathematically optimal risk allocation from historical win/loss statistics
All methods apply portfolio constraints (max position %, max sector %) and output a final recommended share count with full risk breakdown.
基于风险管理原则,计算多头股票交易的最优购买股数。支持三种仓位计算方法:
  • 固定比例法:每笔交易承担账户净资产的固定比例风险(默认:1%)
  • 基于ATR的方法:使用Average True Range(ATR)设置经波动率调整的止损距离
  • Kelly Criterion:根据历史盈亏统计数据计算数学上最优的风险分配
所有方法均应用投资组合约束条件(最大仓位占比、最大行业占比),并输出最终推荐的股数及完整的风险明细。

When to Use

适用场景

  • User asks "how many shares should I buy?"
  • User wants to calculate position size for a specific trade setup
  • User mentions risk per trade, stop-loss sizing, or portfolio allocation
  • User asks about Kelly Criterion or ATR-based position sizing
  • User wants to check if a position fits within portfolio concentration limits
  • 用户询问“我应该买多少股?”
  • 用户希望为特定交易策略计算仓位规模
  • 用户提及每笔交易风险、止损仓位设置或投资组合分配
  • 用户询问Kelly Criterion或基于ATR的仓位计算
  • 用户希望检查某一仓位是否符合投资组合集中度限制

Prerequisites

前提条件

  • No API keys required
  • Python 3.9+ with standard library only
  • 无需API密钥
  • 仅需Python 3.9+及标准库

Workflow

工作流程

Step 1: Gather Trade Parameters

步骤1:收集交易参数

Collect from the user:
  • Required: Account size (total equity)
  • Mode A (Fixed Fractional): Entry price, stop price, risk percentage (default 1%)
  • Mode B (ATR-Based): Entry price, ATR value, ATR multiplier (default 2.0x), risk percentage
  • Mode C (Kelly Criterion): Win rate, average win, average loss; optionally entry and stop for share calculation
  • Optional constraints: Max position % of account, max sector %, current sector exposure
If the user provides a stock ticker but not specific prices, use available tools to look up the current price and suggest entry/stop levels based on technical analysis.
向用户收集以下信息:
  • 必填项:账户规模(总净资产)
  • 模式A(固定比例法):入场价格、止损价格、风险比例(默认1%)
  • 模式B(基于ATR的方法):入场价格、ATR值、ATR乘数(默认2.0倍)、风险比例
  • 模式C(Kelly Criterion):胜率、平均盈利、平均亏损;可选提供入场和止损价格以计算股数
  • 可选约束条件:账户最大仓位占比、行业最大占比、当前行业持仓占比
如果用户提供股票代码但未指定具体价格,可使用可用工具查询当前价格,并基于技术分析建议入场/止损价位。

Step 2: Execute Position Sizer Script

步骤2:执行仓位计算器脚本

Run the position sizing calculation:
bash
undefined
运行仓位计算脚本:
bash
undefined

Fixed Fractional (most common)

固定比例法(最常用)

python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--stop 148.50
--risk-pct 1.0
--output-dir reports/
python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--stop 148.50
--risk-pct 1.0
--output-dir reports/

ATR-Based

基于ATR的方法

python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--atr 3.20
--atr-multiplier 2.0
--risk-pct 1.0
--output-dir reports/
python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--atr 3.20
--atr-multiplier 2.0
--risk-pct 1.0
--output-dir reports/

Kelly Criterion (budget mode - no entry)

Kelly Criterion(预算模式 - 无需入场价)

python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--win-rate 0.55
--avg-win 2.5
--avg-loss 1.0
--output-dir reports/
python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--win-rate 0.55
--avg-win 2.5
--avg-loss 1.0
--output-dir reports/

Kelly Criterion (shares mode - with entry/stop)

Kelly Criterion(股数模式 - 含入场/止损价)

python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--stop 148.50
--win-rate 0.55
--avg-win 2.5
--avg-loss 1.0
--output-dir reports/
undefined
python3 skills/position-sizer/scripts/position_sizer.py
--account-size 100000
--entry 155
--stop 148.50
--win-rate 0.55
--avg-win 2.5
--avg-loss 1.0
--output-dir reports/
undefined

Step 3: Load Methodology Reference

步骤3:查阅方法参考文档

Read
references/sizing_methodologies.md
to provide context on the chosen method, risk guidelines, and portfolio constraint best practices.
阅读
references/sizing_methodologies.md
,了解所选方法的背景、风险准则及投资组合约束的最佳实践。

Step 4: Calculate Multiple Scenarios

步骤4:计算多种场景

If the user has not specified a single method, run multiple scenarios for comparison:
  • Fixed Fractional at 0.5%, 1.0%, and 1.5% risk
  • ATR-based at 1.5x, 2.0x, and 3.0x multipliers
  • Present a comparison table showing shares, position value, and dollar risk for each
如果用户未指定单一方法,可运行多种场景进行比较:
  • 固定比例法:0.5%、1.0%和1.5%风险比例
  • 基于ATR的方法:1.5倍、2.0倍和3.0倍乘数
  • 展示对比表格,包含每种场景的股数、仓位价值及美元风险

Step 5: Apply Portfolio Constraints and Determine Final Size

步骤5:应用投资组合约束并确定最终仓位

Add constraints if the user has portfolio context:
bash
python3 skills/position-sizer/scripts/position_sizer.py \
  --account-size 100000 \
  --entry 155 \
  --stop 148.50 \
  --risk-pct 1.0 \
  --max-position-pct 10 \
  --max-sector-pct 30 \
  --current-sector-exposure 22 \
  --output-dir reports/
Explain which constraint is binding and why it limits the position.
若用户提供投资组合相关信息,添加约束条件:
bash
python3 skills/position-sizer/scripts/position_sizer.py \
  --account-size 100000 \
  --entry 155 \
  --stop 148.50 \
  --risk-pct 1.0 \
  --max-position-pct 10 \
  --max-sector-pct 30 \
  --current-sector-exposure 22 \
  --output-dir reports/
解释哪项约束条件生效,以及为何限制了仓位规模。

Step 6: Generate Position Report

步骤6:生成仓位报告

Present the final recommendation including:
  • Method used and rationale
  • Exact share count and position value
  • Dollar risk and percentage of account
  • Stop-loss price
  • Any binding constraints
  • Risk management reminders (portfolio heat, loss-cutting discipline)
呈现最终推荐内容,包括:
  • 使用的方法及理由
  • 准确的股数和仓位价值
  • 美元风险及占账户的比例
  • 止损价格
  • 任何生效的约束条件
  • 风险管理提醒(投资组合风险热度、止损纪律)

Output Format

输出格式

JSON Report

JSON报告

json
{
  "schema_version": "1.0",
  "mode": "shares",
  "parameters": {
    "entry_price": 155.0,
    "account_size": 100000,
    "stop_price": 148.50,
    "risk_pct": 1.0
  },
  "calculations": {
    "fixed_fractional": {
      "method": "fixed_fractional",
      "shares": 153,
      "risk_per_share": 6.50,
      "dollar_risk": 1000.0,
      "stop_price": 148.50
    },
    "atr_based": null,
    "kelly": null
  },
  "constraints_applied": [],
  "final_recommended_shares": 153,
  "final_position_value": 23715.0,
  "final_risk_dollars": 994.50,
  "final_risk_pct": 0.99,
  "binding_constraint": null
}
json
{
  "schema_version": "1.0",
  "mode": "shares",
  "parameters": {
    "entry_price": 155.0,
    "account_size": 100000,
    "stop_price": 148.50,
    "risk_pct": 1.0
  },
  "calculations": {
    "fixed_fractional": {
      "method": "fixed_fractional",
      "shares": 153,
      "risk_per_share": 6.50,
      "dollar_risk": 1000.0,
      "stop_price": 148.50
    },
    "atr_based": null,
    "kelly": null
  },
  "constraints_applied": [],
  "final_recommended_shares": 153,
  "final_position_value": 23715.0,
  "final_risk_dollars": 994.50,
  "final_risk_pct": 0.99,
  "binding_constraint": null
}

Markdown Report

Markdown报告

Generated automatically alongside the JSON report. Contains:
  • Parameters summary
  • Calculation details for the active method
  • Constraints analysis (if any)
  • Final recommendation with shares, value, and risk
Reports are saved to
reports/
with filenames
position_sizer_YYYY-MM-DD_HHMMSS.json
and
.md
.
会与JSON报告自动生成。包含:
  • 参数摘要
  • 当前使用方法的计算细节
  • 约束条件分析(如有)
  • 最终推荐的股数、价值及风险
报告将保存至
reports/
目录,文件名为
position_sizer_YYYY-MM-DD_HHMMSS.json
.md

Resources

资源

  • references/sizing_methodologies.md
    : Comprehensive guide to Fixed Fractional, ATR-based, and Kelly Criterion methods with examples, comparison table, and risk management principles
  • scripts/position_sizer.py
    : Main calculation script (CLI interface)
  • references/sizing_methodologies.md
    :固定比例法、基于ATR的方法和Kelly Criterion的综合指南,包含示例、对比表格及风险管理原则
  • scripts/position_sizer.py
    主计算脚本(CLI界面)

Key Principles

核心原则

  1. Survival first: Position sizing is about surviving losing streaks, not maximizing winners
  2. The 1% rule: Default to 1% risk per trade; never exceed 2% without exceptional reason
  3. Round down: Always round shares down to whole numbers (never round up)
  4. Strictest constraint wins: When multiple limits apply, the tightest one determines final size
  5. Half Kelly: Never use full Kelly in practice; half Kelly captures 75% of growth with far less risk
  6. Portfolio heat: Total open risk should not exceed 6-8% of account equity
  7. Asymmetry of losses: A 50% loss requires a 100% gain to recover; size accordingly
  1. 生存优先:仓位规模的核心是挺过亏损期,而非最大化盈利
  2. 1%规则默认每笔交易承担1%的风险;无特殊理由绝不超过2%
  3. 向下取整:股数始终向下取整为整数(绝不向上取整)
  4. 最严格约束优先:当存在多个限制条件时,最严格的条件决定最终仓位规模
  5. 半凯利准则:实际操作中绝不使用全凯利准则;半凯利准则可在风险远更低的情况下获取75%的增长收益
  6. 投资组合风险热度:总未平仓风险不应超过账户净资产的6-8%
  7. 亏损不对称性:50%的亏损需要100%的收益才能回本;据此合理设置仓位规模