风险控制原则
1)永远设置止损;2)单笔风险<2%;3)总仓位<80%;4)分散投资;5)严格纪律执行。
项目二:期货CTA策略
风险管理是期货交易的生命线。本节将详细介绍止损止盈、仓位管理等风险管理技术。
class RiskManagement:
"""
风险管理模块
"""
def __init__(self, config):
self.config = config
def atr_stop_loss(self, entry_price, atr, multiplier=2):
"""
ATR动态止损
"""
long_stop = entry_price - atr * multiplier
short_stop = entry_price + atr * multiplier
return long_stop, short_stop
def percentage_stop_loss(self, entry_price, percentage=0.02):
"""
百分比止损
"""
long_stop = entry_price * (1 - percentage)
short_stop = entry_price * (1 + percentage)
return long_stop, short_stop
def volatility_stop_loss(self, entry_price, volatility, multiplier=2):
"""
波动率止损
"""
return self.atr_stop_loss(entry_price, volatility, multiplier)
def trailing_stop(self, current_price, current_stop, trail_percent=0.5):
"""
移动止损
"""
new_stop_long = current_price * (1 - trail_percent)
new_stop_short = current_price * (1 + trail_percent)
if current_price > entry_price: # 多头盈利
return max(current_stop, new_stop_long)
else: # 空头盈利
return min(current_stop, new_stop_short)
def check_stop_loss(self, position, current_price):
"""
检查止损触发
"""
if position['direction'] == 'long':
return current_price <= position['stop_loss']
else:
return current_price >= position['stop_loss']
def calculate_position_size(account_value, risk_per_trade=0.02,
entry_price, stop_loss_price, lot_size=10):
"""
计算仓位大小(固定风险法)
"""
# 风险金额
risk_amount = account_value * risk_per_trade
# 单手风险
risk_per_lot = abs(entry_price - stop_loss_price) * lot_size
# 仓位大小
position_lots = risk_amount / risk_per_lot
return int(position_lots)
def kelly_position_size(win_rate, win_loss_ratio, account_value=100000):
"""
凯利公式仓位管理
"""
# 凯利比例
kelly_ratio = (win_rate * win_loss_ratio - (1 - win_rate)) / win_loss_ratio
# 限制最大仓位
kelly_ratio = min(max(kelly_ratio, 0), 0.25)
position_value = account_value * kelly_ratio
return position_value, kelly_ratio
def risk_parity_allocation(capital, risk_contributions):
"""
风险平价配置
"""
# 标准化风险贡献
total_risk = sum(risk_contributions.values())
weights = {k: v/total_risk for k, v in risk_contributions.items()}
# 分配资金
allocations = {k: capital * v for k, v in weights.items()}
return allocations, weights
| 限额类型 | 参数 | 控制措施 |
|---|---|---|
| 单品种限额 | < 30%总资产 | 超限禁止开仓 |
| 单日亏损限额 | < 5%总资产 | 触发后停止交易 |
| 最大持仓限额 | < 90%总资产 | 保留现金缓冲 |
| 波动率限额 | VaR < 5% | 降低仓位 |
1)永远设置止损;2)单笔风险<2%;3)总仓位<80%;4)分散投资;5)严格纪律执行。