The Comprehensive Automated Trading Moving Average Rainbow Strategy is a typical multi-timeframe moving average combination strategy. It uses 12 moving averages with different periods to determine the trend direction and define entry, stop loss and take profit conditions to implement automated trading. This strategy can automatically identify trends and has a complete stop loss mechanism to control risks.
## Principles
This strategy uses 12 moving averages, including periods of 3, 5, 8 all the way to 55. The moving average type can be selected from EMA, SMA, RMA, etc. The strategy first judges the arrangement relationship between the short and long period moving averages (1-4 period Lines and 5-8 period Lines) to determine if it is an uptrend or downtrend environment.
In an uptrend, if the price breaks through the moving average corresponding to the previous low point, it is determined as a signal to go long. The stop loss is placed at the moving average corresponding to the previous low point, and the take profit is 1.6 times the stop loss. In a downtrend, if the price breaks through the moving average corresponding to the previous high point, it is determined as a signal to go short. The stop loss is placed at the moving average corresponding to the previous high point, and the take profit is 1.6 times the stop loss.
This strategy also has a trend reversal detection feature. During the holding period, if the short period moving average arrangement changes, and the price exceeds the most recent high or low point, it is determined that a trend reversal may have occurred. At this point it will exit the current position and enter a position in the opposite direction, using the new high or low point as the stop loss and take profit.
## Advantages
1. This strategy comprehensively applies multi-timeframe analysis to better determine the trend direction.
2. The addition of moving average sequential/reverse arrangement helps avoid being misled by ranging markets.
3. The strategy has a complete stop loss mechanism to effectively control the risk of each trade.
4. The strategy has trend reversal detection to capture reversal opportunities and reduce systemic risks.
5. The strategy has flexible parameter settings where moving average periods and types can be customized.
6. The strategy uses a trailing stop loss to lock in maximum profits.
## Risks
1. Multi-moving average combination strategies, parameter settings will affect performance and need optimization testing.
2. Moving averages may give false signals in ranging markets, parameters should be adjusted or stop trading temporarily.
3. There is some lag, risks of missing opportunities around trend turning points.
4. Need to watch other technical indicators, avoid going short near important support levels.
5. Systemic risks need monitoring, reversal detection cannot completely avoid such risks.
6. Drawdown control needs additional mechanisms, consider dynamic position sizing.
## Optimization Directions
1. Test different moving average types and parameter settings to find the optimal combination.
2. Optimize the reversal detection mechanism, set more precise reversal trigger conditions.
3. Add dynamic position sizing to reduce position size when drawdown gets too high.
4. Consider incorporating machine learning algorithms, using big data to train judgement of key points.
5. Incorporate other indicator signals for combined judgement to improve accuracy.
6. Build a multi-instrument trading portfolio to diversify risks using low correlations.
## Summary
The Comprehensive Automated Trading Moving Average Rainbow Strategy is overall a solid trend following strategy, with strong abilities in trend identification and risk control. With further optimizations such as parameter tuning, adding dynamic position sizing etc., it can become a very practical quantitative trading strategy. The strategy logic is clear and easy to understand, while also having certain flexibility, making it worthwhile for in-depth research, usage and continuous improvement.
[/trans]
> Strategy Arguments
|Argument|Default|Description|
|----|----|----|
|v_input_string_1|0|(?Moving Averages/Médias Móveis)Moving Average Type/Tipo de Média Móvel: EMA|SMA|RMA|WMA|HMA|VWMA|
|v_input_int_1|3|MA #1|
|v_input_int_2|5|MA #2|
|v_input_int_3|8|MA #3|
|v_input_int_4|13|MA #4|
|v_input_int_5|20|MA #5|
|v_input_int_6|25|MA #6|
|v_input_int_7|30|MA #7|
|v_input_int_8|35|MA #8|
|v_input_int_9|40|MA #9|
|v_input_int_10|45|MA #10|
|v_input_int_11|50|MA #11|
|v_input_int_12|55|MA #12|
|v_input_float_1|1.6|(?Risk Management/Gerenciamento de Risco)Target Take Profit/Objetivo de Lucro Alvo|
|v_input_bool_1|true|(?Turnover Trend/Rotatividade Tendência)Verify Turnover Trend/Verificar Tendência de Rotatividade|
|v_input_bool_2|false|(?Turnover Signal/Rotatividade Sinal)Verify Turnover Signal/Verificar Sinal de Rotatividade|
|v_input_bool_3|false|Verify Price Exit Turnover Signal/Verificar Saída de Preço Sinal de Rotatividade|
strategy('Moving Average Rainbow (Stormer)', overlay=true)
maType = input.string('EMA', title='Moving Average Type/Tipo de Média Móvel', options=['EMA', 'SMA', 'RMA', 'WMA', 'HMA', 'VWMA'], tooltip='This option is to select the type of Moving Average that the Rainbow will use./Esta opção é para selecionar o tipo de Média Móvel que o Rainbow utilizará.', group='Moving Averages/Médias Móveis')
maLengthFirst = input.int(3, title='MA #1', minval=1, step=1, tooltip='First MA length./Comprimento da primeira MA.', group='Moving Averages/Médias Móveis')
maLengthSecond = input.int(5, title='MA #2', minval=1, step=1, tooltip='Second MA length./Comprimento da segunda MA.', group='Moving Averages/Médias Móveis')
maLengthThird = input.int(8, title='MA #3', minval=1, step=1, tooltip='Third MA length./Comprimento da terceira MA.', group='Moving Averages/Médias Móveis')
maLengthFourth = input.int(13, title='MA #4', minval=1, step=1, tooltip='Fourth MA length./Comprimento da quarta MA.', group='Moving Averages/Médias Móveis')
maLengthFifth = input.int(20, title='MA #5', minval=1, step=1, tooltip='Fifth MA length./Comprimento da quinta MA.', group='Moving Averages/Médias Móveis')
maLengthSixth = input.int(25, title='MA #6', minval=1, step=1, tooltip='Sixth MA length./Comprimento da sexta MA.', group='Moving Averages/Médias Móveis')
maLengthSeventh = input.int(30, title='MA #7', minval=1, step=1, tooltip='Seventh MA length./Comprimento da sétima MA.', group='Moving Averages/Médias Móveis')
maLengthEighth = input.int(35, title='MA #8', minval=1, step=1, tooltip='Eighth MA length./Comprimento da oitava MA.', group='Moving Averages/Médias Móveis')
maLengthNineth = input.int(40, title='MA #9', minval=1, step=1, tooltip='Nineth MA length./Comprimento da nona MA.', group='Moving Averages/Médias Móveis')
maLengthTenth = input.int(45, title='MA #10', minval=1, step=1, tooltip='Tenth MA length./Comprimento da décima MA.', group='Moving Averages/Médias Móveis')
maLengthEleventh = input.int(50, title='MA #11', minval=1, step=1, tooltip='Eleventh MA length./Comprimento da décima primeira MA.', group='Moving Averages/Médias Móveis')
maLengthTwelveth = input.int(55, title='MA #12', minval=1, step=1, tooltip='Twelveth MA length./Comprimento da décima segunda MA.', group='Moving Averages/Médias Móveis')
targetFactor = input.float(1.6, title='Target Take Profit/Objetivo de Lucro Alvo', minval=0.1, step=0.1, tooltip='Calculate the take profit factor when entry position./Calcula o fator do alvo lucro ao entrar na posição.', group='Risk Management/Gerenciamento de Risco')
verifyTurnoverTrend = input.bool(true, title='Verify Turnover Trend/Verificar Tendência de Rotatividade', tooltip='This option checks for a supposedly turnover trend and setup new target (for long is the highest high and for short is the lowest low identified)./Esta opção verifica uma suposta tendência de rotatividade e estabelece um novo objetivo (para long é a máxima mais alta, para short é a mínima mais baixa identificados).', group='Turnover Trend/Rotatividade Tendência')
verifyTurnoverSignal = input.bool(false, title='Verify Turnover Signal/Verificar Sinal de Rotatividade', tooltip='This option checks for a supposedly turnover signal, closing the current position and opening a new one (for long it will close and open a new for short, for short it will close and open a new for long)./Essa opção verifica um sinal de possível reversão, fechando a posição atual e abrindo uma nova (para long fechará e abrirá uma nova para short, para short fechará e abrirá uma nova para long).', group='Turnover Signal/Rotatividade Sinal')
verifyTurnoverSignalPriceExit = input.bool(false, title='Verify Price Exit Turnover Signal/Verificar Saída de Preço Sinal de Rotatividade', tooltip='This option complements "turnover signal" by veryfing the price if profitable before exiting the current position./Esta opção complementa o "sinal de rotatividade" verificando o preço do lucro antes de sair da posição atual.', group='Turnover Signal/Rotatividade Sinal')
isPositionLongClose = na(positionEntryPrice) and not positionIsEntryLong ? 1 : 0
isPositionShortClose = na(positionEntryPrice) and not positionIsEntryShort ? 1 : 0
isLongCondition = isMaUptrend and isCurHighGreaterPrevHigh and isPrevTouchedPriceUptrend ? 1 : 0
isShortCondition = isMaDowntrend and isCurLowLesserPrevLow and isPrevTouchedPriceDowntrend ? 1 : 0
longTurnoverExit = verifyTurnoverSignal and verifyTurnoverSignalPriceExit ? (verifyTurnoverSignal and isLongCondition and positionIsEntryShort and close < positionEntryPrice) : verifyTurnoverSignal ? (verifyTurnoverSignal and isLongCondition and positionIsEntryShort) : na
shortTurnoverExit = verifyTurnoverSignal and verifyTurnoverSignalPriceExit ? (verifyTurnoverSignal and isShortCondition and positionIsEntryLong and close > positionEntryPrice) : verifyTurnoverSignal ? (verifyTurnoverSignal and isShortCondition and positionIsEntryLong) : na
if (isPositionFlat)
positionEntryPrice := na
positionIsEntryLong := false
positionIsEntryShort := false
stopLossLong := na
targetLong := na
stopLossShort := na
targetShort := na
isTurnoverTrendLongTrigger := na
isTurnoverTrendShortTrigger := na
if ((isLongCondition and isPositionLongClose) or longTurnoverExit)
table.cell(table_id=tableInfoTrade, column=0, row=6, text=verifyTurnoverTrend and positionIsEntryLong and isTurnoverTrendLongTrigger ? str.tostring(longPositionHighestHigh) : verifyTurnoverTrend and positionIsEntryShort and isTurnoverTrendShortTrigger ? str.tostring(shortPositionLowestLow) : 'NONE/NENHUM', text_color=color.green)
table.cell(table_id=tableInfoTrade, column=1, row=5, text='Possible Market Turnover/Possível Virada do Mercado', text_color=color.white)
table.cell(table_id=tableInfoTrade, column=1, row=6, text=verifyTurnoverTrend and positionIsEntryLong and isTurnoverTrendLongTrigger ? 'YES/SIM (Possible long going short/Possível long indo short)' : verifyTurnoverTrend and positionIsEntryShort and isTurnoverTrendShortTrigger ? 'YES/SIM (Possible short going long/Possível short indo long)' : 'NONE/NENHUM', text_color=color.red)