9.8 KiB
Name
基于ATR和资金管理的SSL通道回测策略SSL-Channel-Backtester-Strategy-With-ATR-and-Money-Management
Author
ChaoZhang
Strategy Description
[trans]
概述
本策略是基于SSL通道指标的回测策略,同时结合了ATR止损、ATR止盈和资金管理等功能,可以更全面地测试SSL通道策略的效果。
策略原理
SSL通道指标
SSL通道指标由通道中线和通道带组成。通道中线是简单移动平均线,分为上轨和下轨,通常取高点期间的简单移动平均线作为上轨,低点期间的简单移动平均线作为下轨。通道带则由上轨和下轨之间的区域构成。
当价格接近通道上轨时视为超买,当价格接近通道下轨时视为超卖。价格突破通道带的时候,表示趋势发生转变的信号。
本策略中的SSL通道指标参数设置为:ssl_period=16。
ATR止损止盈
ATR指平均真实波幅。它可以用来评估市场的波动性和确定止损止盈位置。
本策略使用了参数atr_period=14的ATR指标,并结合atr_stop_factor=1.5和atr_target_factor=1.0作为止损和止盈的动态倍数,实现了基于市场波动率的止损止盈。
此外,为了适应不同品种,本策略还加入了two_digit参数判断合约为2位精度的品种(如黄金、日元),从而可灵活调整止损止盈位。
资金管理
资金管理主要通过参数position_size(固定仓位)和risk(百分比风险敞口)实现。当use_mm=true时会启用资金管理模块。
资金管理的主要目标是控制每次开仓的头寸大小。当采用固定百分比风险模式时,会根据账户权益计算出风险敞口后转化为合约数,从而实现抑制单笔损失的效果。
优势分析
- 使用SSL通道判断趋势方向,对于捕捉趋势转换具有一定效果
- 应用ATR动态计算止损止盈位置,可以自适应市场波动率
- 利用资金管理原则,有助于从长期角度控制风险
风险分析
- SSL通道虽可判断趋势转折,但并不是百分之百可靠,可能出现错误信号
- ATR跟随市场波动率设置止损止盈,可能会过于宽松或过于僵硬
- 资金管理参数设置不当也会导致仓位过大或效率过低
这些风险可以通过以下方法加以改善:
- 结合其他指标进行确认,避免出现错误信号
- 适当调整ATR周期参数,使止损止盈水平达到最佳平衡
- 测试不同资金管理参数,找到最优仓位
优化方向
本策略可以从以下几个方面进行优化:
- 优化SSL通道参数,寻找最佳参数组合
- 优化或替换ATR止损止盈机制,使其更加完善
- 增加其他过滤指标,避免不必要的交易
- 增加仓位控制模块,实现损益最大化
- 针对不同品种进行参数微调,提高策略适应性
- 加入量化工具,实现更全面的回测和优化
通过系统的测试和优化,本策略可以成为一个可靠和稳定的量化交易系统。
总结
本策略整合了SSL通道指标判断趋势、ATR设定止损止盈和资金管理控制风险三种机制。通过全面的回测可以检验该策略的效果,并且可以作为量化交易策略优化的基础框架。与此同时,本策略也有可以改进的空间,如加入其他过滤指标、优化参数以及扩充功能等。总的来说,本策略为搭建自动化交易系统奠定了坚实的基石。 ||
Overview
This is a backtesting strategy based on the SSL channel indicator, integrated with functions like ATR stop loss, ATR take profit and money management to facilitate a more comprehensive test on the SSL channel strategy.
Strategy Logic
SSL Channel Indicator
The SSL channel indicator consists of the channel midline and channel bands. The channel midline contains an upper track and a lower track, which are usually simple moving averages of high and low prices over a lookback period. The channel bands are formed between the upper and lower tracks.
When price approaches the upper band, it indicates overbought conditions; when price approaches the lower band, it signals oversold conditions. A breakout of the channel bands implies a trend reversal.
The SSL channel parameter is set to ssl_period=16 in this strategy.
ATR Stop Loss/Take Profit
The Average True Range (ATR) measures market volatility and can be used to determine stop loss and take profit levels.
This strategy utilizes a 14-period ATR (atr_period=14) and dynamic multipliers atr_stop_factor=1.5 and atr_target_factor=1.0 to set adaptive stop loss and take profit based on volatility.
It also checks if the instrument has 2-decimal precision (two_digit) to adjust the stop and target accordingly for pairs like gold and JPY.
Money Management
Money management is achieved through position_size (fixed position sizing) and risk (risk percentage per trade) parameters. The money management module will be enabled when use_mm=true.
The goal is to determine the optimal position size for each trade. By using fixed risk % per trade, the allowable position size will be calculated dynamically based on the account equity to limit the loss on every single trade.
Advantage Analysis
- SSL channel is effective in capturing trend reversal signals
- ATR-based stops adjust automatically based on volatility
- Money management helps control risk across all trades
Risk Analysis
- SSL channel signals may not be completely reliable, false signals can occur
- ATR stops may end up too wide or too tight
- Improper money management settings can lead to oversized positions or low efficiency
These risks can be mitigated by:
- Adding filters to confirm signals and avoid false entries
- Tuning ATR period parameter for optimal stop loss/take profit levels
- Testing different money management parameters for ideal position sizing
Optimization Directions
The strategy can be improved in the following aspects:
- Optimize SSL channel parameters for best performance
- Enhance or replace the ATR stop mechanism
- Add filtering indicators to avoid unnecessary trades
- Incorporate position sizing to maximize risk-adjusted returns
- Fine-tune parameters for different instruments
- Add quantitative tools for more comprehensive testing
With systematic optimization, this strategy can become a robust algorithmic trading system.
Conclusion
This strategy combines the SSL channel for trend, ATR for risk control, and money management for position sizing. Comprehensive backtesting facilitates evaluating and enhancing the strategy into an automated trading system. There is also room for improvements like adding filters, optimizing parameters and expanding functionality. Overall, this forms a solid foundation for building algorithmic trading strategies.
[/trans]
Strategy Arguments
| Argument | Default | Description |
|---|---|---|
| v_input_1 | false | Check this for 2-digit pairs (JPY, Gold, Etc) |
| v_input_2 | 16 | SSL Period |
| v_input_3 | 14 | ATR Period |
| v_input_4 | 1.5 | ATR Stop Loss Factor |
| v_input_5 | true | ATR Target Factor |
| v_input_6 | true | Check this to use Money Management |
| v_input_7 | 1000 | Position size (for Fixed Risk) |
| v_input_8 | 0.01 | Risk % in Decimal Form |
Source (PineScript)
/*backtest
start: 2023-10-23 00:00:00
end: 2023-11-22 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © comiclysm
//@version=4
strategy("SSL Backtester", overlay=false)
//--This strategy will simply test the effectiveness of the SSL using
//--money management and an ATR-derived stop loss
//--USER INPUTS
two_digit = input(false, "Check this for 2-digit pairs (JPY, Gold, Etc)")
ssl_period = input(16, "SSL Period")
atr_period = input(14, "ATR Period")
atr_stop_factor = input(1.5, "ATR Stop Loss Factor")
atr_target_factor = input(1.0, "ATR Target Factor")
use_mm = input(true, "Check this to use Money Management")
position_size = input(1000, "Position size (for Fixed Risk)")
risk = input(0.01, "Risk % in Decimal Form")
//--INDICATORS------------------------------------------------------------
//--SSL
sma_high = sma(high, ssl_period)
sma_low = sma(low, ssl_period)
ssl_value = 0
ssl_value := close > sma_high ? 1 : close < sma_low ? -1 : ssl_value[1]
ssl_low = ssl_value < 0 ? sma_high : sma_low
ssl_high = ssl_value < 0 ? sma_low : sma_high
//--Average True Range
atr = atr(atr_period)
//--TRADE LOGIC----------------------------------------------------------
signal_long = ssl_value > 0 and ssl_value[1] < 0
signal_short = ssl_value < 0 and ssl_value[1] > 0
//--RISK MANAGMENT-------------------------------------------------------
strategy.initial_capital = 50000
balance = strategy.netprofit + strategy.initial_capital
risk_pips = atr*10000*atr_stop_factor
if(two_digit)
risk_pips := risk_pips / 100
risk_in_value = balance * risk
point_value = syminfo.pointvalue
risk_lots = risk_in_value / point_value / risk_pips
final_risk = use_mm ? risk_lots * 10000 : position_size
//--TRADE EXECUTION-----------------------------------------------------
if (signal_long)
stop_loss = close - atr * atr_stop_factor
target = close + atr * atr_target_factor
strategy.entry("Long", strategy.long, final_risk)
strategy.exit("X", "Long", stop=stop_loss, limit=target)
if (signal_short)
stop_loss = close + atr * atr_stop_factor
target = close - atr * atr_target_factor
strategy.entry("Short", strategy.short, final_risk)
strategy.exit("X", "Short", stop=stop_loss, limit=target)
//--PLOTTING-----------------------------------------------------------
plot(ssl_low, "SSL", color.red, linewidth=1)
plot(ssl_high, "SSL", color.lime, linewidth=1)
Detail
https://www.fmz.com/strategy/432965
Last Modified
2023-11-23 10:26:58
