
Esta es una estrategia de seguimiento de tendencias que combina una media móvil indexada (EMA) y un indicador aleatorio relativamente fuerte (RSI estocástico). La estrategia identifica oportunidades de negociación de alta probabilidad mediante el análisis de tendencias de precios y estados de sobreventa y sobreventa. La estrategia utiliza la intersección de EMA 9 y EMA 21 para determinar la dirección de la tendencia, mientras que el RSI estocástico se utiliza para confirmar el estado del mercado, lo que mejora la calidad de la señal de negociación.
La lógica central de la estrategia se basa en una combinación de dos indicadores técnicos principales:
Condiciones de activación de la señal comercial:
Es una estrategia de seguimiento de tendencias que tiene una estructura clara y una lógica rigurosa. Combinando la EMA y el RSI estocástico, la estrategia tiene un buen equilibrio para identificar tendencias y estados de mercado. Aunque existen algunos riesgos inherentes, la estrategia puede mantener un rendimiento estable en una variedad de entornos de mercado mediante una optimización de parámetros y una gestión de riesgos razonables.
/*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ď. ===== //