RSI Moving Average Trailing Stop estratégia

Autora:ChaoZhang, Data: 2023-09-13 14:26:43
Tags:

Esta estratégia combina RSI e médias móveis para o viés da tendência e adiciona trailing stops para a gestão do risco.

Estratégia lógica:

  1. Calcule o RSI para julgar os níveis de sobrecompra/supervenda.

  2. Calcule médias móveis rápidas e lentas, cruz de ouro sinaliza tendência de alta.

  3. A elevação constante do RSI também sinaliza entrada longa.

  4. Após a entrada, defina o stop loss final e pegue as linhas de lucro.

  5. Parar a trilha da perda abaixo do preço, tomar a trilha do lucro acima.

  6. Saia quando o preço chegar ao ponto de paragem ou tire lucro.

Vantagens:

  1. O RSI evita perseguir os altos e os baixos.

  2. As médias móveis identificam a direcção da tendência.

  3. As paradas/lucros se ajustam dinamicamente ao preço.

Riscos:

  1. RSI e MAs propensos a sinais falsos em mercados variados.

  2. A largura do travão requer uma calibração prudente, sendo problemática a largura ou a estreiteza.

  3. Incapaz de limitar o tamanho das perdas, corre o risco de grandes perdas de negócios.

Em resumo, esta estratégia combina RSI e MAs, em seguida, usa trailing stops para gestão de riscos.


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

//@version=5
strategy("RSI and MA Strategy with Trailing Stop Loss and Take Profit",
         overlay=true,
         initial_capital=1000,
         process_orders_on_close=true,
         default_qty_type=strategy.percent_of_equity,
         default_qty_value=100,
         commission_type=strategy.commission.percent,
         commission_value=0.1)

showDate = input(defval=true, title='Show Date Range')
timePeriod = time >= timestamp(syminfo.timezone, 2022, 1, 1, 0, 0)
notInTrade = strategy.position_size <= 0

//==================================Buy Conditions============================================

//RSI
length = input(14)
rsi = ta.rsi(close, length)
buyCondition1 = rsi > 50

//MA
SMA9 = ta.sma(close, 9)
SMA50 = ta.sma(close, 50)
SMA100 = ta.sma(close, 100)
plot(SMA9, color = color.green)
plot(SMA50, color = color.orange)
plot(SMA100, color = color.blue)
buyCondition2 = SMA9 > SMA50//ta.crossover(SMA9, SMA100)

//RSI Increase
increase = 5
buyCondition3 = (rsi > rsi[1] + increase)


if (buyCondition1 and buyCondition2 and buyCondition3 and timePeriod) //and buyCondition
    strategy.entry("Long", strategy.long)

//==================================Sell Conditions============================================

//Trailing Stop Loss and Take Profit
longTrailPerc = input.float(title='Trail Long Loss (%)', minval=0.0, step=0.1, defval=2) * 0.01
shortTrailPerc = input.float(title='Trail Short Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01

longStopPrice = 0.0
shortStopPrice = 0.0

longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - longTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + shortTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    999999


strategy.exit(id="Exit", stop = longStopPrice, limit = shortStopPrice)

Mais.