This trading strategy combines three Exponential Moving Averages (EMAs) to identify trend direction, uses RSI to signal exit conditions, and applies both a fixed percentage stop-loss and a trailing stop for risk management. It aims to capture momentum when the faster EMAs cross the slower EMA, then uses RSI thresholds, time-based exits, and stops to close trades.
Short Explanation of the Logic
Trend Detection: When the 10 EMA crosses above the 20 EMA and both are above the 100 EMA (and the current price bar closes higher), it triggers a long entry signal. The reverse happens for a short (the 10 EMA crosses below the 20 EMA and both are below the 100 EMA). RSI Exit: RSI crossing above a set threshold closes long trades; crossing below another threshold closes short trades. Time-Based Exit: If a trade is in profit after a set number of bars, the strategy closes it. Stop-Loss & Trailing Stop: A fixed stop-loss based on a percentage from the entry price guards against large drawdowns. A trailing stop dynamically tightens as the trade moves in favor, locking in potential gains.
Detailed Explanation of the Strategy Logic
Exponential Moving Average (EMA) Setup
Short EMA (out_a, length=10) Medium EMA (out_b, length=20) Long EMA (out_c, length=100) The code calculates three separate EMAs to gauge short-term, medium-term, and longer-term trend behavior. By comparing their relative positions, the strategy infers whether the market is bullish (EMAs stacked positively) or bearish (EMAs stacked negatively).
Entry Conditions
Long Entry (entryLong): Occurs when:
The short EMA (10) crosses above the medium EMA (20). Both EMAs (short and medium) are above the long EMA (100). The current bar closes higher than it opened (close > open). This suggests that momentum is shifting to the upside (short-term EMAs crossing up and price action turning bullish). If there’s an existing short position, it’s closed first before opening a new long.
Short Entry (entryShort): Occurs when:
The short EMA (10) crosses below the medium EMA (20). Both EMAs (short and medium) are below the long EMA (100). The current bar closes lower than it opened (close < open). This indicates a potential shift to the downside. If there’s an existing long position, that gets closed first before opening a new short.
Exit Signals
RSI-Based Exits:
For long trades: When RSI exceeds a specified threshold (e.g., 70 by default), it triggers a long exit. RSI > short_rsi generally means overbought conditions, so the strategy exits to lock in profits or avoid a pullback. For short trades: When RSI dips below a specified threshold (e.g., 30 by default), it triggers a short exit. RSI < long_rsi indicates oversold conditions, so the strategy closes the short to avoid a bounce. Time-Based Exit: If the trade has been open for xBars bars (configurable, e.g., 24 bars) and the trade is in profit (current price above entry for a long, or current price below entry for a short), the strategy closes the position. This helps lock in gains if the move takes too long or momentum stalls.
Stop-Loss Management
Fixed Stop-Loss (% Based): Each trade has a fixed stop-loss calculated as a percentage from the average entry price.
For long positions, the stop-loss is set below the entry price by a user-defined percentage (fixStopLossPerc). For short positions, the stop-loss is set above the entry price by the same percentage. This mechanism prevents catastrophic losses if the market moves strongly against the position.
Trailing Stop:
The strategy also sets a trail stop using trail_points (the distance in price points) and trail_offset (how quickly the stop “catches up” to price). As the market moves in favor of the trade, the trailing stop gradually tightens, allowing profits to run while still capping potential drawdowns if the price reverses. Order Execution Flow
When the conditions for a new position (long or short) are triggered, the strategy first checks if there’s an opposite position open. If there is, it closes that position before opening the new one (prevents going “both long and short” simultaneously). RSI-based and time-based exits are checked on each bar. If triggered, the position is closed. If the position remains open, the fixed stop-loss and trailing stop remain in effect until the position is exited. Why This Combination Works
Multiple EMA Cross: Combining 10, 20, and 100 EMAs balances short-term momentum detection with a longer-term trend filter. This reduces false signals that can occur if you only look at a single crossover without considering the broader trend. RSI Exits: RSI provides a momentum oscillator view—helpful for detecting overbought/oversold conditions, acting as an extra confirmation to exit. Time-Based Exit: Prevents “lingering trades.” If the position is in profit but failing to advance further, it takes profit rather than risking a trend reversal. Fixed & Trailing Stop-Loss: The fixed stop-loss is your safety net to cap worst-case losses. The trailing stop allows the strategy to lock in gains by following the trade as it moves favorably, thus maximizing profit potential while keeping risk in check. Overall, this approach tries to capture momentum from EMA crossovers, protect profits with trailing stops, and limit risk through both a fixed percentage stop-loss and exit signals from RSI/time-based logic.
Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai sumber terbuka supaya pedagang dapat memahami dan mengesahkannya. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara percuma, tetapi penggunaan semula kod dalam penerbitan ini adalah dikawal oleh Peraturan dalaman. Anda boleh menyukainya untuk menggunakannya pada carta.
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.