//@version=6indicator("Swing Trading MA Touch", shorttitle="MA Touch", overlay=true) // Input parametersma_length = input.int(8, title="Moving Average Length", minval=1)trend_ma_length = input.int(20, title="Trend MA Length (for trend direction)", minval=1)tp_percent = input.float(10.0, title="Take Profit %", minval=0.1, maxval=50.0)sl_percent = input.float(5.0, title="Stop Loss %", minval=0.1, maxval=20.0)touch_tolerance = input.float(0.5, title="Touch Tolerance %", minval=0.1, maxval=2.0)show_levels = input.bool(true, title="Show TP/SL Levels") // Calculate moving averagesma_8 = ta.sma(close, ma_length)trend_ma = ta.sma(close, trend_ma_length) // Position tracking variables (must be declared early)var float entry_price = navar float tp_level = navar float sl_level = navar bool position_active = false // Plot moving averagesplot(ma_8, color=color.blue, linewidth=2, title="8-Day MA")plot(trend_ma, color=color.orange, linewidth=1, title="Trend MA") // Determine bullish trendbullish_trend = close > trend_ma and ma_8 > trend_ma // Calculate touch conditiontouch_range = ma_8 * (touch_tolerance / 100)price_touching_ma = math.abs(close - ma_8) <= touch_range or math.abs(low - ma_8) <= touch_range // Buy signal condition (only when no position is active)buy_signal = bullish_trend and price_touching_ma and close > ma_8[1] and not position_active // Plot buy signalsplotshape(buy_signal, style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.normal, title="Buy Signal") // Add buy signal labelif buy_signal label.new(bar_index, low * 0.995, text="BUY\nEntry: " + str.tostring(close, "#.##") + "\nTP: " + str.tostring(close * 1.10, "#.##") + "\nSL: " + str.tostring(close * 0.95, "#.##"), style=label.style_label_up, color=color.green, textcolor=color.white, size=size.normal) // Update position trackingif buy_signal entry_price := close tp_level := entry_price * (1 + tp_percent / 100) sl_level := entry_price * (1 - sl_percent / 100) position_active := true // Check if TP or SL is hittp_hit = position_active and high >= tp_levelsl_hit = position_active and low <= sl_level if position_active and (tp_hit or sl_hit) // Add exit label exit_text = tp_hit ? "TP HIT\n+10%" : "SL HIT\n-5%" exit_color = tp_hit ? color.green : color.red label.new(bar_index, tp_hit ? high * 1.005 : low * 0.995, text=exit_text, style=tp_hit ? label.style_label_down : label.style_label_up, color=exit_color, textcolor=color.white, size=size.normal) // Reset position - ready for new entry signals position_active := false entry_price := na tp_level := na sl_level := na // Plot TP and SL levelsplot(show_levels and position_active ? tp_level : na, color=color.green, linewidth=2, style=plot.style_stepline, title="Take Profit")plot(show_levels and position_active ? sl_level : na, color=color.red, linewidth=2, style=plot.style_stepline, title="Stop Loss")plot(show_levels and position_active ? entry_price : na, color=color.yellow, linewidth=1, style=plot.style_stepline, title="Entry Price") // Background color for trendbgcolor(bullish_trend ? color.new(color.green, 97) : color.new(color.gray, 98), title="Trend Background") // Create table for signal informationvar table info_table = table.new(position.top_right, 2, 4, bgcolor=color.white, border_width=1) if barstate.islast table.cell(info_table, 0, 0, "Status", text_color=color.black, bgcolor=color.gray) table.cell(info_table, 1, 0, position_active ? "Active Position" : "No Position", text_color=color.black, bgcolor=position_active ? color.yellow : color.white) if position_active table.cell(info_table, 0, 1, "Entry", text_color=color.black) table.cell(info_table, 1, 1, str.tostring(entry_price, "#.##"), text_color=color.black) table.cell(info_table, 0, 2, "TP (+10%)", text_color=color.black) table.cell(info_table, 1, 2, str.tostring(tp_level, "#.##"), text_color=color.green) table.cell(info_table, 0, 3, "SL (-5%)", text_color=color.black) table.cell(info_table, 1, 3, str.tostring(sl_level, "#.##"), text_color=color.red) // Alertsalertcondition(buy_signal, title="Buy Signal", message="Swing Trading Buy Signal: Price touched 8-day MA in bullish trend")alertcondition(position_active and high >= tp_level, title="Take Profit Hit", message="Take Profit level reached (+10%)")alertcondition(position_active and low <= sl_level, title="Stop Loss Hit", message="Stop Loss level hit (-5%)")