Sure! Below is a TradingView Pine Script indicator that does exactly what you've asked for. This script:
- Draws the 5-period Exponential Moving Average (EMA5) on the chart.
- Identifies when the price is above the EMA5, and the candle low does not touch the EMA5.
- Checks when the next candle breaks the low of the previous candle (the one that didn't touch EMA5).
- Initiates a trade signal at that point.
- Places a stop line at the high of the candle that didn't touch EMA5 plus 2% of the Daily Average True Range (ATR).
- Sets a Risk Reward ratio of 1:2.5.
- Filters trades to only occur between 9:30 AM - 4:00 PM ET and 8:00 PM - 11:00 PM ET.
Here's the code:
//@version=5
indicator("EMA5 Break Strategy", overlay=true)
// Parameters
ema_length = 5
atr_length = 14
// Compute EMA5
ema5 = ta.ema(close, ema_length)
// Compute Daily ATR
daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(atr_length))
// Time filter: between 9:30 AM - 4:00 PM ET, and 8:00 PM - 11:00 PM ET
is_in_session_day = not na(time(timeframe.period, "0930-1600", "America/New_York"))
is_in_session_evening = not na(time(timeframe.period, "2000-2300", "America/New_York"))
is_in_time_ny = is_in_session_day or is_in_session_evening
// Conditions
// Previous candle low does not touch EMA5 and price is above EMA5
prev_candle_low_above_ema = low[1] > ema5[1] and close[1] > ema5[1]
// Current candle breaks low of previous candle
breaks_prev_low = low < low[1]
// Signal when previous candle low is above EMA5, and current candle breaks previous low, and time is within session
signal = prev_candle_low_above_ema and breaks_prev_low and is_in_time_ny
// Entry price is the low of previous candle
entry_price = low[1]
// Stop loss is the high of previous candle plus 2% of daily ATR
stop_loss_price = high[1] + (daily_atr * 0.02)
// Risk is stop loss price minus entry price
risk = stop_loss_price - entry_price
// Take profit price is entry_price minus (risk * 2.5)
take_profit_price = entry_price - (risk * 2.5)
// Plot EMA
plot(ema5, color=color.blue, linewidth=2)
// Plot signals
plotshape(signal, title="Short Entry", location=location.abovebar, color=color.red, size=size.small, style=shape.triangledown, text="Short", textcolor=color.white)
// Plot stop loss and take profit lines on the chart when signal occurs
plot(signal ? stop_loss_price : na, title="Stop Loss", color=color.red, linewidth=1, style=plot.style_linebr)
plot(signal ? take_profit_price : na, title="Take Profit", color=color.green, linewidth=1, style=plot.style_linebr)
Explanation:
Notes:
- This script assumes a short trade when the conditions are met, based on your description (price is above EMA5 and then the next candle breaks the low).
- If you also want to consider long trades (for when the price is below EMA5 and certain conditions are met), similar logic can be added for long entries.
- The script uses the
plotshape and plot functions to visually present the signals and levels on the chart.
- The time filters rely on the TradingView platform's ability to handle time zones and sessions. Ensure that your chart's time zone is set correctly for accurate results.
How to Use:
- Open TradingView and go to the chart of the instrument you want to analyze.
- Click on the "Pine Editor" tab at the bottom of the page.
- Paste the code provided above into the editor.
- Click on "Add to Chart."
- The indicator will now plot the EMA5 and signal possible trade entries according to your criteria.
Feel free to adjust the parameters (like ema_length, atr_length, or risk-reward ratio) to suit your specific needs.
Let me know if you have any questions or need further adjustments!