资源加载中... loading...

Quantitative Trading Based on Moving Average Envelope and ATR Trailing Stop

Author: ChaoZhang, Date: 2023-12-08 15:53:22
Tags:

img

Overview

The strategy is named “MADEFlex”, which stands for “Flexible Quantitative Trading Strategy Based on Moving Average Envelope and ATR Trailing Stop”. It combines the Moving Average Envelope indicator and ATR trailing stop mechanism to implement a flexible and controllable quantitative trading solution.

Principles

The core of this strategy is the Moving Average Displaced Envelope (MADE) indicator. MADE forms upper and lower bands by multiplying percentage factors with displaced exponential moving average (EMA). Sell signals are generated when prices break above the upper band, and buy signals are generated when prices break below the lower band. This strategy incorporates the MADE indicator with Average True Range (ATR) trailing stop mechanism. The ATR trailing stop sets stop loss based on several multiples of ATR values, enabling flexible tracking of stop loss lines. This avoids unnecessary stop loss while also locking in existing profits.

Specifically, the MADE indicator contains 3 parameters: Period, percentage above perAb, and percentage below perBl. The Period parameter determines the period of EMA. The upper and lower bands’ distance from EMA is controlled by percentage factors. The ATR trailing stop part is mainly set by the ATR period nATRPeriod and ATR multiples nATRMultip. When the price exceeds the stop loss line of the previous bar, the stop loss line is adjusted to the price minus the fixed ATR loss. When the price falls below the previous bar’s stop loss line, the stop loss line is adjusted to the price plus the fixed ATR loss.

Finally, buy and sell signals are generated by combining the MADE indicator signals and ATR trailing stop conditions. Reverse trading can be achieved via the reverse input switch.

Advantage Analysis

The MADEFlex strategy has the following advantages:

  1. More reliable combined signals and stop loss. MADE itself tends to generate false signals. Incorporating ATR trailing stop can effectively filter out some noise.

  2. Rich adjustable parameters and flexible control. MADE parameters and ATR parameters can be adjusted to control signal quantity and quality.

  3. Support for reverse trading. Reverse trading enriches application scenarios.

  4. Intuitive visualized stopper. Judging stop effect visually via plotted stop lines.

Risk Analysis

The MADEFlex strategy also has the following risks:

  1. Improper MADE parameters may generate excessive false signals. Proper parameters need to be carefully tested.

  2. Excessively loose ATR stop may miss stop opportunities. Appropriate ATR multiples need to be tested.

  3. Higher risk with reverse trading. Especially in high volatility situations, reverse trading may increase loss risk. Needs to be used cautiously.

  4. Potential higher loss without stop loss. In extreme market conditions, the lack of stop loss protection may lead to higher losses.

Optimization Directions

The MADEFlex strategy can be optimized in the following directions:

  1. Optimize MADE parameters to improve signal quality. Different periods and percentage combinations can be tested to find more reliable parameter sets.

  2. Optimize ATR trailing stop parameters for better stop effect. ATR periods and multiples can be tested to determine more appropriate combinations.

  3. Add other filters to further reduce false signals, e.g. combining volatility indicators for filtering.

  4. Add profit taking mechanism to exit at certain profit levels. This locks in profits and controls risks.

  5. Utilize machine learning methods to dynamically optimize parameters, e.g. reinforcement learning. This adds flexibility to the strategy.

Conclusion

The MADEFlex strategy successfully combines the trading signals from Moving Average Envelope indicator and the ATR trailing stop methodology. Through adjustable parameters, it implements a flexible and controllable quantitative trading solution. The strategy has relatively high reliability and strong control capability. It is suitable for users with some quantitative basis to use and optimize. With further optimizations, more significant strategy performance can be expected.


/*backtest
start: 2022-12-01 00:00:00
end: 2023-12-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 27/09/2022
// Moving Average Displaced Envelope. These envelopes are calculated 
// by multiplying percentage factors with their displaced expotential 
// moving average (EMA) core.
// How To Trade Using:
// Adjust the envelopes percentage factors to control the quantity and 
// quality of the signals. If a previous high goes above the envelope 
// a sell signal is generated. Conversely, if the previous low goes below 
// the envelope a buy signal is given.
//
// Average True Range Trailing Stops Strategy, by Sylvain Vervoort 
// The related article is copyrighted material from Stocks & Commodities Jun 2009 
//
// ATR TS used by filter for MADE signals.
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title='Moving Average Displaced Envelope & ATRTS', shorttitle='MADE+ATR', overlay=true)
tradeDirection = input.string('Both', title='Trade Direction', options=['Both', 'Long', 'Short'])
Price = input(title='Source', defval=close)
Period = input.int(defval=9, minval=1)
perAb = input.float(title='Percent above', defval=.5, minval=0.01, step=0.1)
perBl = input.float(title='Percent below', defval=.5, minval=0.01, step=0.1)
disp = input.int(title='Displacement', defval=13, minval=1)

nATRPeriod = input(15)
nATRMultip = input(2)
useATR = input(false, title='ATR Filter')
reverse = input(false, title='Trade reverse')

longAllowed = tradeDirection == 'Long' or tradeDirection == 'Both'
shortAllowed = tradeDirection == 'Short' or tradeDirection == 'Both'
pos = 0
sEMA = ta.ema(Price, Period)
top = sEMA[disp] * ((100 + perAb) / 100)
bott = sEMA[disp] * ((100 - perBl) / 100)

xATR = ta.atr(nATRPeriod)
xHHs =ta.sma(ta.highest(nATRPeriod), nATRPeriod)
xLLs =ta.sma(ta.lowest(nATRPeriod),nATRPeriod)
nSpread = (xHHs - xLLs) / 2
nLoss = nATRMultip * xATR
var xATRTrailingStop = 0.0
xATRTrailingStop := close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), close - nLoss) :
     close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), close + nLoss) : 
     close > nz(xATRTrailingStop[1], 0) ? close - nLoss : close + nLoss

ATRLong = close > xATRTrailingStop ? true : false
ATRShort = close < xATRTrailingStop ? true : false

iff_1 = close > top ? 1 : pos[1]
pos := close < bott ? -1 : iff_1
iff_2 = reverse and pos == -1 ? 1 : pos
possig = reverse and pos == 1 ? -1 : iff_2
clr = strategy.position_size
if possig == 1 
    if longAllowed and ATRLong
        strategy.entry('Long', strategy.long)
    else
        if ATRLong or strategy.position_size > 0
            strategy.close_all()
if possig == -1 
    if shortAllowed and ATRShort
        strategy.entry('Short', strategy.short)
    else    
        if ATRShort or strategy.position_size < 0
            strategy.close_all()
if possig == 0
    strategy.close_all()
    
plot(xATRTrailingStop[1], color=color.blue, title='ATR Trailing Stop')
barcolor(clr < 0 ? #b50404 : clr > 0 ? #079605 : #0536b3)



More