use the following code for trading view, allow alerts and move short and long entries so…
- User
use the following code for trading view, allow alerts and move short and long entries so it aligns with the sell and buy signals
//@version=5 strategy("First 15-Minute Candle NY Session Breakout Strategy [First 4 Signals Only, TP at +150% Profit]", overlay=true)
// Define the New York timezone ny_timezone = "America/New_York"
// Variables to store the high and low of the session candle var float session_candle_high = na var float session_candle_low = na
// Initialize signal counter var int signal_counter = 0
// Get the bar's opening time in New York time bar_time_ny = time(timeframe.period, ny_timezone) hour_ny = hour(bar_time_ny) minute_ny = minute(bar_time_ny)
// Detect if the current bar is the session candle (9:30 AM New York time) is_session_candle = (hour_ny == 9 and minute_ny == 30)
// On the session candle, update the high and low and reset the signal counter if is_session_candle session_candle_high := high session_candle_low := low signal_counter := 0 // Reset signal counter at the start of each session
// Plot the high and low levels on the chart plot(session_candle_high, title="Session Candle High", color=color.green, linewidth=2) plot(session_candle_low, title="Session Candle Low", color=color.red, linewidth=2)
// Generate buy and sell signals when the entire candle is above/below the levels buy_signal = not is_session_candle and not na(session_candle_high) and low > session_candle_high sell_signal = not is_session_candle and not na(session_candle_low) and high < session_candle_low
// Initialize variables to determine whether to plot signals and execute trades enter_long = false enter_short = false
// Only consider the first 4 signals if signal_counter < 4 // If buy signal occurs and we're not already in a long position if buy_signal and (strategy.position_size <= 0) enter_long := true signal_counter += 1 // If sell signal occurs and we're not already in a short position else if sell_signal and (strategy.position_size >= 0) enter_short := true signal_counter += 1
// Execute trades and set take-profit levels if enter_long if strategy.position_size < 0 strategy.close("Short Entry") strategy.entry("Long Entry", strategy.long) // Set take-profit at +150% profit level tp_price_long = strategy.position_avg_price * 2.5 strategy.exit("Long TP", "Long Entry", limit=tp_price_long) if enter_short if strategy.position_size > 0 strategy.close("Long Entry") strategy.entry("Short Entry", strategy.short) // Set take-profit to a minimal price since price can't go below zero tp_price_short = 0.01 // Minimal price to simulate maximum profit strategy.exit("Short TP", "Short Entry", limit=tp_price_short)
// Plot buy and sell signals on the chart plotshape(enter_long, title="Buy Signal", location=location.belowbar, color=color.new(color.green, 0), style=shape.triangleup, size=size.small, text="BUY", textcolor=color.white) plotshape(enter_short, title="Sell Signal", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small, text="SELL", textcolor=color.white)
- Assistant