// === Swing High and Low Detection === var float swing_high = na var float swing_low = na
if bar_index > input_length if swingHigh(input_length) swing_high := high if swingLow(input_length) swing_low := low
// === Entry Conditions === long_condition = not na(swing_high) and ta.crossover(close, swing_high) short_condition = not na(swing_low) and ta.crossunder(close, swing_low)
// === Risk Management === var float long_entry = na var float short_entry = na var float stop_loss = na var float take_profit = na
// === Plotting === // Entry Labels var label long_label = na var label short_label = na
if long_condition if na(long_label) long_label := label.new(bar_index, swing_high, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white) else label.set_xy(long_label, bar_index, swing_high)
if short_condition if na(short_label) short_label := label.new(bar_index, swing_low, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white) else label.set_xy(short_label, bar_index, swing_low)
// Stop Loss and Take Profit Lines var line stop_loss_line = na var line take_profit_line = na
if not na(stop_loss) if na(stop_loss_line) stop_loss_line := line.new(bar_index, stop_loss, bar_index + 1, stop_loss, color=color.red, width=2, style=line.style_dotted) else line.set_xy1(stop_loss_line, bar_index, stop_loss) line.set_xy2(stop_loss_line, bar_index + 1, stop_loss)
if not na(take_profit) if na(take_profit_line) take_profit_line := line.new(bar_index, take_profit, bar_index + 1, take_profit, color=color.green, width=2, style=line.style_dotted) else line.set_xy1(take_profit_line, bar_index, take_profit) line.set_xy2(take_profit_line, bar_index + 1, take_profit)
// Alerts alertcondition(long_condition, title="Long Alert", message="Buy signal detected at {{close}}") alertcondition(short_condition, title="Short Alert", message="Sell signal detected at {{close}}")