Skip to content

Latest commit

 

History

History
247 lines (159 loc) · 12.1 KB

双均线反转与ATR-trailing-stop组合策略Combination-Strategy-of-Dual-Moving-Average-Reversal-and-ATR-Trailing-Stop.md

File metadata and controls

247 lines (159 loc) · 12.1 KB

Name

双均线反转与ATR-trailing-stop组合策略Combination-Strategy-of-Dual-Moving-Average-Reversal-and-ATR-Trailing-Stop

Author

ChaoZhang

Strategy Description

IMG

[trans]

概述

双均线反转与ATR trailing stop组合策略是一个非常实用的量化交易策略。该策略首先利用双均线形成的死叉和Golden交叉来判断市场趋势和反转点。同时,策略还会结合平均真实波幅来设置trail stop,在保证获利的同时控制风险。

策略原理

双均线反转策略

双均线反转策略利用快线和慢线的交叉来判断市场趋势。当快线从上向下穿过慢线时,形成死叉,表示市场由涨转为跌;当快线从下向上穿过慢线时,形成Golden交叉,表示市场由跌转为涨。策略在死叉时做空,在Golden交叉时做多。

具体来说,策略选用9日STOCH指标的快线作为快线,3日EMA作为慢线。当close低于前一日close,且快线高于50而上穿慢线时清仓做空;当close高于前一日close,且快线低于50而下穿慢线时清仓做多。

ATR Trailing Stop策略

ATR Trailing Stop策略利用平均真实波幅来设置止损点。ATR指标可以有效反映市场的短期波动性。策略根据ATR的值来设置trail stop,当价格走势反转时止损退出。

具体来说,策略选用5日ATR,止损点设为close减去ATR的3.5倍。当价格达到该止损点时平仓止损。

优势分析

双均线反转与ATR trailing stop组合策略结合了均线策略判断趋势和反转的优势,以及ATR trail stop策略控制风险的优势,使其成为一个非常实用的策略。

具体来说,该策略有以下优势:

  1. 利用双均线形成的死叉和Golden交叉来判断市场趋势转折点,精准判断反转信号。

  2. 结合STOCH指标来确认反转信号,避免错误信号。

  3. ATR trailing stop根据市场波动灵活设置止损点,最大程度锁定利润。

  4. 该策略融合了多种指标和技术分析方法,进行组合使用,使策略更加稳健。

  5. 策略思路清晰易理解,参数调整灵活,容易实盘操作。

风险分析

虽然该策略有许多优势,但也存在一定的风险需要注意:

  1. 双均线生成的信号可能存在滞后,无法在反转点前后准确买入和卖出。可以适当缩短均线周期或结合其他指标进行优化。

  2. ATR指标对市场大幅波动不敏感,无法及时更新止损点。可以考虑结合动量指标或波动率指标进行调整。

  3. 多种参数和条件的组合使用增加了策略复杂度。参数不当可能造成过于激进的交易而增加风险。需谨慎评估并逐步调整参数。

优化方向

根据上述风险分析,该策略可以从以下几个方面进行优化:

  1. 调整均线周期参数,缩短周期以提前捕捉反转机会。

  2. 添加其他指标判断反转信号,如MACD,KD等,形成多重确认。

  3. 动态调整ATR周期或引入市场波动率,实时更新止损点。

  4. 评估股票和期货市场的差异,分别调整参数,使之更符合两种市场的特点。

  5. 在回测时加入交易成本和滑点的考量,使策略更加接近实盘交易环境。

  6. 可以考虑加入机器学习模型来动态优化多个参数。

总结

双均线反转与ATR trailing stop组合策略是一个高效实用的量化策略。它融合了均线判断市场反转和ATR setting trail stop控制风险的双重优势。在确保获利的同时可以减少不必要的损失。该策略参数调整灵活,易于实盘操作。同时也可以从多个角度进行扩展与优化,使其适用于更广泛的市场环境。总体来说,该策略为量化交易提供了一个非常出色的策略框架。

||

Overview

The combination strategy of dual moving average reversal and ATR trailing stop is a very practical quantitative trading strategy. The strategy first uses the death cross and golden cross formed by the dual moving averages to determine market trends and reversal points. At the same time, the strategy also combines the average true range to set trail stops to ensure profit while controlling risks.

Strategy Principle

Dual Moving Average Reversal Strategy

The dual moving average reversal strategy uses the crossover of fast and slow lines to determine market trends. When the fast line crosses below the slow line from top to bottom, it forms a death cross, indicating the market trend is changing from rising to falling. When the fast line crosses above the slow line from bottom to top, it forms a golden cross, indicating the market trend is changing from falling to rising. The strategy goes short on death cross and goes long on golden cross.

Specifically, the strategy uses the 9-day STOCH fast line as the fast line and 3-day EMA as the slow line. When close is lower than previous close and fast line crosses above 50 to cross above slow line, it clears position to go short. When close is higher than previous close and fast line crosses below 50 to cross below slow line, it clears position to go long.

ATR Trailing Stop Strategy

The ATR trailing stop strategy uses the average true range to set stop loss points. The ATR indicator can effectively reflect the short-term volatility of the market. The strategy sets trail stop based on the value of ATR to exit when price trend reverses.

Specifically, the strategy uses 5-day ATR and sets stop loss point at close minus 3.5 times ATR. When price hits the stop loss point, it closes position for stop loss.

Advantage Analysis

The combination strategy of dual moving average reversal and ATR trailing stop combines the advantage of moving average strategy in determining trends and reversals and the advantage of ATR trail stop strategy in controlling risks, making it a very practical strategy.

Specifically, the strategy has the following advantages:

  1. Use the death cross and golden cross formed by dual moving averages to determine market trend reversal points and accurately identify reversal signals.

  2. Combine STOCH indicator to confirm reversal signals and avoid false signals.

  3. ATR trailing stop flexibly sets stop loss points based on market volatility to maximize profit locking.

  4. The strategy integrates multiple indicators and technical analysis methods to make the strategy more robust.

  5. The strategy idea is clear and easy to understand, parameters are flexible for adjustment, and it is easy to operate in live trading.

Risk Analysis

Although the strategy has many advantages, there are still some risks to note:

  1. The signals generated by dual moving averages may lag and unable to accurately buy and sell at reversal points. Periods can be moderately shortened or combined with other indicators for optimization.

  2. ATR indicator is not sensitive to large market fluctuations and cannot update stop loss in time. Consider combining momentum indicators or volatility indicators for adjustment.

  3. The combination of multiple parameters and conditions increases the complexity of the strategy. Improper parameters may cause overly aggressive trading and increase risks. Parameters need to be carefully evaluated and adjusted gradually.

Optimization Directions

According to the above risk analysis, the strategy can be optimized in the following aspects:

  1. Adjust moving average period parameters to shorten periods for early capturing reversal opportunities.

  2. Add other indicators to determine reversal signals, such as MACD, KD, etc. to form multiple confirmations.

  3. Dynamically adjust ATR periods or introduce market volatility to update stop loss in real time.

  4. Evaluate the differences between stock and futures markets, and adjust parameters respectively to make them more suited to both markets.

  5. Add trading costs and slippage in backtesting to make the strategy closer to the live trading environment.

  6. Consider adding machine learning models to dynamically optimize multiple parameters.

Summary

The combination strategy of dual moving average reversal and ATR trailing stop is an efficient and practical quantitative strategy. It combines the dual advantages of determining market reversal with moving averages and controlling risks by setting ATR trail stops. It ensures profit while reducing unnecessary losses. The strategy has flexible parameter adjustment and is easy to operate in live trading. At the same time, it can also be extended and optimized in multiple aspects to adapt to more extensive market environments. Overall, the strategy provides an excellent strategic framework for quantitative trading.
[/trans]

Strategy Arguments

Argument Default Description
v_input_1 14 Length
v_input_2 true KSmoothing
v_input_3 3 DLength
v_input_4 50 Level
v_input_5 5 nATRPeriod
v_input_6 3.5 nATRMultip
v_input_7 false Trade reverse

Source (PineScript)

/*backtest
start: 2023-12-26 00:00:00
end: 2024-01-25 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 17/05/2019
// This is combo strategies for get 
// a cumulative signal. Result signal will return 1 if two strategies 
// is long, -1 if all strategies is short and 0 if signals of strategies is not equal.
//
// First strategy
// This System was created from the Book "How I Tripled My Money In The 
// Futures Market" by Ulf Jensen, Page 183. This is reverse type of strategies.
// The strategy buys at market, if close price is higher than the previous close 
// during 2 days and the meaning of 9-days Stochastic Slow Oscillator is lower than 50. 
// The strategy sells at market, if close price is lower than the previous close price 
// during 2 days and the meaning of 9-days Stochastic Fast Oscillator is higher than 50.
//
// Secon strategy
// Average True Range Trailing Stops Strategy, by Sylvain Vervoort 
// The related article is copyrighted material from Stocks & Commodities Jun 2009 
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
Reversal123(Length, KSmoothing, DLength, Level) =>
    vFast = sma(stoch(close, high, low, Length), KSmoothing) 
    vSlow = sma(vFast, DLength)
    pos = 0.0
    pos := iff(close[2] < close[1] and close > close[1] and vFast < vSlow and vFast > Level, 1,
	         iff(close[2] > close[1] and close < close[1] and vFast > vSlow and vFast < Level, -1, nz(pos[1], 0))) 
	pos


ATR_TrailingStop(nATRPeriod, nATRMultip) =>
    xATR = atr(nATRPeriod)
    nLoss = nATRMultip * xATR
    pos = 0.0
    xATRTrailingStop = 0.0
    xATRTrailingStop := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
                         iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), 
                           iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
    pos := iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
    	     iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) 

    pos

strategy(title="Combo Backtest 123 Reversal & Average True Range Trailing Stops", shorttitle="Combo", overlay = true)
Length = input(14, minval=1)
KSmoothing = input(1, minval=1)
DLength = input(3, minval=1)
Level = input(50, minval=1)
nATRPeriod = input(5)
nATRMultip = input(3.5)
reverse = input(false, title="Trade reverse")
posReversal123 = Reversal123(Length, KSmoothing, DLength, Level)
posATR_TrailingStop = ATR_TrailingStop(nATRPeriod, nATRMultip)
pos = iff(posReversal123 == 1 and posATR_TrailingStop == 1 , 1,
	   iff(posReversal123 == -1 and posATR_TrailingStop == -1, -1, 0)) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1, 1, pos))	   
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)	 
if (possig == 0) 
    strategy.close_all()
barcolor(possig == -1 ? red: possig == 1 ? green : blue ) 

Detail

https://www.fmz.com/strategy/440062

Last Modified

2024-01-26 11:26:40