
Il s’agit d’une stratégie de suivi de tendance combinant une moyenne mobile indicielle (EMA) et un indicateur stochastique relativement faible (RSI) au hasard. Cette stratégie identifie des opportunités de trading à forte probabilité en analysant les tendances des prix et les conditions de survente et de survente. La stratégie utilise les croisements d’EMA 9 et d’EMA 21 pour déterminer la direction de la tendance, tout en utilisant le RSI stochastique pour confirmer l’état du marché, ce qui améliore la qualité du signal de trading.
La logique de base de la stratégie est basée sur une combinaison de deux indicateurs techniques principaux:
Conditions de déclenchement du signal de trading :
Il s’agit d’une stratégie de suivi de tendance structurée avec une clarté et une rigueur logiques. En combinant l’EMA et le RSI stochastique, la stratégie est mieux équilibrée pour identifier les tendances et l’état du marché. Bien qu’il y ait des risques inhérents, la stratégie peut maintenir une performance stable dans une variété d’environnements de marché grâce à une optimisation des paramètres et une gestion des risques raisonnables.
/*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ď. ===== //