Buy sell

//@version=5 indicator("Super Trend", shorttitle="Super Trend", overlay=true) // ============================================================================= // INPUTS // ============================================================================= var GRP1 = "Trend Continuation Signals with TP & SL" src = input(hl2, title="Source", group=GRP1) multiplier = input.float(2.0, title="Sensitivity (0.5 - 5)", step=0.1, minval=0.5, maxval=5, group=GRP1) atrPeriods = input.int(10, title="ATR Length", group=GRP1) atrCalcMethod = input.string("Method 1", title="ATR Calculation Methods", options=["Method 1", "Method 2"], group=GRP1) cloud_val = input.int(10, title="Cloud Moving Average Length", minval=5, maxval=500, group=GRP1) stopLossVal = input.float(2.0, title="Stop Loss Percent (0 for Disabling)", minval=0, group=GRP1) showBuySellSignals = input.bool(true, title="Show Buy/Sell Signals", group=GRP1) showMovingAverageCloud = input.bool(true, title="Show Cloud MA", group=GRP1) // ============================================================================= // CORE TREND CALCULATION (SUPERTREND-LIKE) // ============================================================================= atr = atrCalcMethod == "Method 1" ? ta.atr(atrPeriods) : ta.sma(ta.tr, atrPeriods) up = src - (multiplier * atr) up1 = nz(up[1], up) up := close[1] > up1 ? math.max(up, up1) : up dn = src + (multiplier * atr) dn1 = nz(dn[1], dn) dn := close[1] < dn1 ? math.min(dn, dn1) : dn trend = 1 trend := nz(trend[1], trend) trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend buySignal = trend == 1 and trend[1] == -1 sellSignal = trend == -1 and trend[1] == 1 // ============================================================================= // POSITION & TRADE MANAGEMENT // ============================================================================= var pos = 0.0 // 1 for long, -1 for short, 0 for flat longCond = buySignal and pos[1] != 1 shortCond = sellSignal and pos[1] != -1 // --- Update position state based on entry signals --- if longCond pos := 1 if shortCond pos := -1 // --- Get entry prices --- entryOfLongPosition = ta.valuewhen(longCond, close, 0) entryOfShortPosition = ta.valuewhen(shortCond, close, 0) // --- Calculate SL/TP levels --- sl = stopLossVal > 0 ? stopLossVal / 100 : 9999 stopLossForLong = entryOfLongPosition * (1 - sl) stopLossForShort = entryOfShortPosition * (1 + sl) takeProfitForLong1R = entryOfLongPosition * (1 + sl) takeProfitForShort1R = entryOfShortPosition * (1 - sl) takeProfitForLong2R = entryOfLongPosition * (1 + sl * 2) takeProfitForShort2R = entryOfShortPosition * (1 - sl * 2) takeProfitForLong3R = entryOfLongPosition * (1 + sl * 3) takeProfitForShort3R = entryOfShortPosition * (1 - sl * 3) // --- Check for exit conditions --- long_sl_hit = low < stopLossForLong and pos[1] == 1 short_sl_hit = high > stopLossForShort and pos[1] == -1 // NOTE: This is the primary exit logic. It only closes the position on an SL hit or a TP3 hit. // TP1 and TP2 are visual only and DO NOT close the trade in this current logic. long_tp_final_hit = high > takeProfitForLong3R and pos[1] == 1 short_tp_final_hit = low < takeProfitForShort3R and pos[1] == -1 // --- Close position if an exit condition is met --- if long_sl_hit or short_sl_hit or long_tp_final_hit or short_tp_final_hit pos := 0 // ============================================================================= // PLOTTING // ============================================================================= // --- Plot Buy/Sell Signals --- plotshape(longCond ? up : na, title="UpTrend Begins", location=location.belowbar, style=shape.circle, size=size.tiny, color=color.new(color.teal, 50)) plotshape(longCond and showBuySellSignals, title="Buy", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 50), textcolor=color.white) plotshape(shortCond ? dn : na, title="DownTrend Begins", location=location.abovebar, style=shape.circle, size=size.tiny, color=color.new(color.red, 50)) plotshape(shortCond and showBuySellSignals, title="Sell", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 50), textcolor=color.white) // --- Plot Cloud --- emaSrcHigh = ta.ema(high, cloud_val) emaSrcLow = ta.ema(low, cloud_val) [macdLine, _, _] = ta.macd(close, 12, 26, 9) plot_high = plot(showMovingAverageCloud ? emaSrcHigh : na, color=na, editable=false) plot_low = plot(showMovingAverageCloud ? emaSrcLow : na, color=na, editable=false) cloudColor = macdLine > 0 ? color.new(color.aqua, 85) : color.new(color.red, 85) fill(plot_high, plot_low, color=showMovingAverageCloud ? cloudColor : na, title="MA Cloud") // --- Draw Lines and Labels for the CURRENT trade only --- if barstate.islast and pos != 0 // Get entry bar index entryIndex = pos > 0 ? ta.valuewhen(longCond, bar_index, 0) : ta.valuewhen(shortCond, bar_index, 0) // Define prices based on position entryPrice = pos > 0 ? entryOfLongPosition : entryOfShortPosition stopPrice = pos > 0 ? stopLossForLong : stopLossForShort tp1Price = pos > 0 ? takeProfitForLong1R : takeProfitForShort1R tp2Price = pos > 0 ? takeProfitForLong2R : takeProfitForShort2R tp3Price = pos > 0 ? takeProfitForLong3R : takeProfitForShort3R // Colors entryColor = pos > 0 ? color.blue : color.purple // Draw Lines line.new(entryIndex, entryPrice, bar_index, entryPrice, color=entryColor) line.new(entryIndex, stopPrice, bar_index, stopPrice, color=color.red) line.new(entryIndex, tp1Price, bar_index, tp1Price, color=color.green, style=line.style_dashed) line.new(entryIndex, tp2Price, bar_index, tp2Price, color=color.green, style=line.style_dashed) line.new(entryIndex, tp3Price, bar_index, tp3Price, color=color.green) // Draw Labels label.new(bar_index, entryPrice, "Entry: " + str.tostring(entryPrice, format.mintick), color=entryColor, textcolor=color.white, style=label.style_label_left) label.new(bar_index, stopPrice, "SL: " + str.tostring(stopPrice, format.mintick), color=color.red, textcolor=color.white, style=label.style_label_left) label.new(bar_index, tp1Price, "TP1: " + str.tostring(tp1Price, format.mintick), color=color.green, textcolor=color.white, style=label.style_label_left) label.new(bar_index, tp2Price, "TP2: " + str.tostring(tp2Price, format.mintick), color=color.green, textcolor=color.white, style=label.style_label_left) label.new(bar_index, tp3Price, "TP3: " + str.tostring(tp3Price, format.mintick), color=color.green, textcolor=color.white, style=label.style_label_left) // ============================================================================= // ALERTS // ============================================================================= changeCond = trend != trend[1] alertcondition(changeCond, title="Trend Direction Change", message="Trend direction has changed!") if longCond alert('{"ticker": "' + syminfo.ticker + '", "Signal": "BUY", "Entry": "' + str.tostring(entryOfLongPosition) + '", "TP1": "' + str.tostring(takeProfitForLong1R) + '", "TP2": "' + str.tostring(takeProfitForLong2R) + '", "TP3": "' + str.tostring(takeProfitForLong3R) + '", "SL": "' + str.tostring(stopLossForLong) + '"}', alert.freq_once_per_bar_close) if shortCond alert('{"ticker": "' + syminfo.ticker + '", "Signal": "SELL", "Entry": "' + str.tostring(entryOfShortPosition) + '", "TP1": "' + str.tostring(takeProfitForShort1R) + '", "TP2": "' + str.tostring(takeProfitForShort2R) + '", "TP3": "' + str.tostring(takeProfitForShort3R) + '", "SL": "' + str.tostring(stopLossForShort) + '"}', alert.freq_once_per_bar_close)

Post a Comment