1.5 KiB
将固定百分比止损,改为基于 ATR(平均真实波幅) 的动态止损。 这是专业交易系统的标准做法。
逻辑:ATR指标精确衡量了当前价格的平均波动幅度。将止损设置为 1.5倍至2倍的ATR值,意味着止损距离能随着市场波动自动调整。市场平静时收紧,市场疯狂时放宽,始终保持在“正常波动”之外。
示例:如果当前ATR值为价格的3%,那么:
1.5倍ATR止损 = 价格反向4.5% 止损
2倍ATR止损 = 价格反向6% 止损
开仓时实时计算:
python
1. 获取当前ATR值 (假设为价格的3%)
current_atr_percent = get_current_atr(symbol) # 返回 0.03 (3%)
2. 根据ATR倍数计算动态止损距离
stop_distance = current_atr_percent * ATR_MULTIPLIER_SL # 0.03 * 1.5 = 0.045 (4.5%)
3. 根据盈亏比计算动态止盈距离
take_profit_distance = stop_distance * RISK_REWARD_RATIO # 0.045 * 3 = 0.135 (13.5%)
4. 计算出具体的止损价和止盈价
if side == 'BUY': stop_loss_price = entry_price * (1 - stop_distance) # 做多,止损价在下 take_profit_price = entry_price * (1 + take_profit_distance) else: stop_loss_price = entry_price * (1 + stop_distance) # 做空,止损价在上 take_profit_price = entry_price * (1 - take_profit_distance)
先实施,再优化:不必追求完美。你可以先从 ATR_MULTIPLIER_SL = 1.5 和 RISK_REWARD_RATIO = 3.0 这个组合开始。这个组合意味着你允许价格反向波动“1.5倍的平均波幅”才止损,同时追求3倍于此空间的利润。