Key Features
1. EMA Calculation:
* Calculates two EMAs:
* emaShort: 9-period EMA.
* emaLong: 21-period EMA.
* Plots these EMAs on the chart with transparency:
* EMA 9: Yellow with 50% transparency.
* EMA 21: Orange with 30% transparency.
2. Crossover Detection:
* Detects bullish crossovers (EMA 9 crosses above EMA 21).
* Detects bearish crossovers (EMA 9 crosses below EMA 21).
3. Custom Timeframe Opposite Candle:
* Allows the user to select a custom timeframe (1m, 2m, or 3m) for detecting an opposite candle after a crossover.
* An opposite candle is defined as:
* A red candle (close < open) for bullish crossovers.
* A green candle (close > open) for bearish crossovers.
4. State Tracking:
* Tracks whether the script is waiting for a red or green candle after a crossover:
* waitingForRed: Set to true after a bullish crossover.
* waitingForGreen: Set to true after a bearish crossover.
5. Alert Conditions:
* Triggers an alert when:
* A bullish crossover is followed by a red candleon the custom timeframe.
* A bearish crossover is followed by a green candle on the custom timeframe.
* Alerts are reset after being triggered.
6. Visual Markers:
* Plots shapes on the chart to indicate:
* Bullish crossovers (green circle below the bar).
* Bearish crossovers (red circle above the bar).
* Opposite candle alerts (crosses on the bar where the alert is triggered).
2. User Customization
* Custom Timeframe Selection:
* Users can choose between 1-minute, 2-minute, or 3-minute timeframes for detecting the opposite candle.
* This is done via the input.string function with options ["1", "2", "3"].
3. How It Works
1. EMA Crossovers:
* The script detects when the 9-period EMA crosses above or below the 21-period EMA on the 5-minute chart.
2. Opposite Candle Detection:
* After a crossover, the script waits for the first opposite candle on the selected custom timeframe:
* For a bullish crossover, it waits for a red candle.
* For a bearish crossover, it waits for a green candle.
3. Alerts:
* Alerts are triggered when the opposite candle is detected on the custom timeframe.
* The alert message specifies the type of crossover and the opposite candle condition.
4. Visual Feedback:
* The script provides visual feedback on the chart:
* Circles mark the crossover points.
* Crosses mark the opposite candle alerts.
4. Use Case
This script is ideal for traders who:
* Use EMA crossovers as a trend-following strategy.
* Want to confirm crossovers with price action on a different timeframe.
* Prefer to wait for an opposite candle (e.g., a pullback or continuation signal) before entering a trade.
5. Example Scenarios
1. Bullish Crossover:
* EMA 9 crosses above EMA 21 on the 5-minute chart.
* The script waits for a red candle on the selected custom timeframe (e.g., 2-minute chart).
* Once the red candle closes, a bullish alert is triggered.
2. Bearish Crossover:
* EMA 9 crosses below EMA 21 on the 5-minute chart.
* The script waits for a green candle on the selected custom timeframe (e.g., 2-minute chart).
* Once the green candle closes, a bearish alert is triggered.
6. Visual Markers
* Bullish Cross: Green circle below the bar.
* Bearish Cross: Red circle above the bar.
* Bullish Alert: Red cross below the bar.
* Bearish Alert: Green cross above the bar.
7. Summary
This script combines EMA crossovers with opposite candle confirmation on a custom timeframe to provide a more robust trading signal. It is particularly useful for traders who want to:
* Avoid false signals by waiting for price confirmation.
* Use multiple timeframes for better decision-making.
* Receive real-time alerts when conditions are met.