
Esta é uma estratégia de acompanhamento de tendências que combina uma média móvel indexada (EMA) e um indicador aleatório relativamente forte (RSI estocástico). A estratégia identifica oportunidades de negociação de alta probabilidade através da análise de tendências de preços e estados de sobrecompra e sobrevenda. A estratégia usa o cruzamento de EMA 9 e EMA 21 para determinar a direção da tendência, enquanto usa o RSI estocástico para confirmar o estado do mercado, aumentando a qualidade do sinal de negociação.
A lógica central da estratégia é baseada em uma combinação de dois indicadores técnicos principais:
Condições de ativação do sinal de negociação:
É uma estratégia de acompanhamento de tendências com uma estrutura clara e rigorosa. Combinando a EMA e o RSI estocástico, a estratégia possui um bom equilíbrio na identificação de tendências e estado de mercado. Embora haja alguns riscos inerentes, a estratégia pode manter um desempenho estável em vários ambientes de mercado com a otimização e o gerenciamento de riscos de parâmetros razoáveis.
/*backtest
start: 2025-01-10 00:00:00
end: 2025-02-09 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA 9/21 + Stoch RSI Strategy", shorttitle="EMA+StochRSI", overlay=true)
// ===== Užívateľské vstupy ===== //
emaFastLen = input.int(9, "Rýchla EMA (9)")
emaSlowLen = input.int(21, "Pomalá EMA (21)")
rsiLen = input.int(14, "RSI Length")
stochRsiLen = input.int(14, "Stoch RSI Length") // úsek, z ktorého berieme min/max RSI
stochSignalLen = input.int(3, "Stoch RSI K/D Smoothing")
overSold = input.int(20, "Stoch RSI Oversold (%)")
overBought = input.int(80, "Stoch RSI Overbought (%)")
// ===== Výpočet EMA(9) a EMA(21) ===== //
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
// ===== Výpočet RSI a Stoch RSI ===== //
// 1) Klasické RSI
rsiValue = ta.rsi(close, rsiLen)
// 2) Prevod RSI -> Stoch RSI:
// (rsiValue - min(rsiValue, stochRsiLen)) / (max(rsiValue, stochRsiLen) - min(rsiValue, stochRsiLen)) * 100
// Následne vyhladíme K a D (podobne ako pri bežnom Stochastic)
rsiLowest = ta.lowest(rsiValue, stochRsiLen)
rsiHighest = ta.highest(rsiValue, stochRsiLen)
stochRaw = (rsiValue - rsiLowest) / math.max(rsiHighest - rsiLowest, 1e-10) * 100.0
stochK = ta.sma(stochRaw, stochSignalLen)
stochD = ta.sma(stochK, stochSignalLen)
// ===== Podmienky pre LONG / SHORT ===== //
// LONG, ak:
// - EMA(9) prekríži EMA(21) smerom nahor
// - Stoch RSI je v prepredanej zóne (t.j. stochK < overSold)
longCondition = ta.crossover(emaFast, emaSlow) and (stochK < overSold)
// SHORT, ak:
// - EMA(9) prekríži EMA(21) smerom nadol
// - Stoch RSI je v prekúpenej zóne (stochK > overBought)
shortCondition = ta.crossunder(emaFast, emaSlow) and (stochK > overBought)
// ===== Vstup do pozícií ===== //
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
// ===== Výstup z pozície pri opačnom signáli (okamžite na trhu) ===== //
if strategy.position_size > 0 and shortCondition
// Ak držíme LONG a príde signál na SHORT, zavrieme LONG
strategy.close("Long", comment="Exit Long")
if strategy.position_size < 0 and longCondition
// Ak držíme SHORT a príde signál na LONG, zavrieme SHORT
strategy.close("Short", comment="Exit Short")
// ===== (Nepovinné) Môžeš pridať stop-loss, take-profit, trailing stop atď. ===== //