Skip to content

Instantly share code, notes, and snippets.

@sivaprabug
Created November 12, 2025 23:54
Show Gist options
  • Select an option

  • Save sivaprabug/fb446f735969feba37b99965f0f01344 to your computer and use it in GitHub Desktop.

Select an option

Save sivaprabug/fb446f735969feba37b99965f0f01344 to your computer and use it in GitHub Desktop.
//@version=5
indicator("Targets + SL + Purchase Date DD/MM/YYYY (Stable)", overlay=true, max_labels_count=500)
// INPUTS
buy_price = input.float(25.0, "Buy Price", step=0.01)
qty = input.int(100, "Total Quantity")
purchase_date_str = input.string("01/01/2024", "Purchase Date (DD/MM/YYYY)")
// PARSE DD/MM/YYYY
parts = str.split(purchase_date_str, "/")
pday = str.tonumber(array.get(parts, 0))
pmo = str.tonumber(array.get(parts, 1))
pyr = str.tonumber(array.get(parts, 2))
purchase_date_int = pyr * 10000 + pmo * 100 + pday
cur_date_int = year(time)*10000 + month(time)*100 + dayofmonth(time)
// TARGETS
tp25 = buy_price * 1.25
tp50 = buy_price * 1.50
tp75 = buy_price * 1.75
tp100 = buy_price * 2.00
stop_loss = buy_price * 0.80
// QTY SPLIT
q_each = qty / 4.0
q_floor = math.floor(q_each)
q_remain = qty - q_floor * 3
// PURCHASE BAR
var int purchase_bar = na
if na(purchase_bar) and cur_date_int >= purchase_date_int
purchase_bar := bar_index
// LINES
var bool created = false
var line l25 = na
var line l50 = na
var line l75 = na
var line l100 = na
var line lSL = na
if not created and not na(purchase_bar)
l25 := line.new(purchase_bar, tp25, purchase_bar+1, tp25, xloc=xloc.bar_index, extend=extend.right, color=color.green)
l50 := line.new(purchase_bar, tp50, purchase_bar+1, tp50, xloc=xloc.bar_index, extend=extend.right, color=color.blue)
l75 := line.new(purchase_bar, tp75, purchase_bar+1, tp75, xloc=xloc.bar_index, extend=extend.right, color=color.orange)
l100 := line.new(purchase_bar, tp100, purchase_bar+1, tp100, xloc=xloc.bar_index, extend=extend.right, color=color.red)
lSL := line.new(purchase_bar, stop_loss, purchase_bar+1, stop_loss, xloc=xloc.bar_index, extend=extend.right, color=color.maroon)
created := true
if created
line.set_y1(l25, tp25)
line.set_y2(l25, tp25)
line.set_y1(l50, tp50)
line.set_y2(l50, tp50)
line.set_y1(l75, tp75)
line.set_y2(l75, tp75)
line.set_y1(l100, tp100)
line.set_y2(l100, tp100)
line.set_y1(lSL, stop_loss)
line.set_y2(lSL, stop_loss)
// LABELS — ALL SINGLE-LINE, NO MULTILINE CALLS
if barstate.islast
label.new(bar_index+1, tp25, "Exit1: Sell " + str.tostring(q_floor) + " @ " + str.tostring(tp25), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(color.green,0), textcolor=color.white, size=size.small)
label.new(bar_index+1, tp50, "Exit2: Sell " + str.tostring(q_floor) + " @ " + str.tostring(tp50), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(color.blue,0), textcolor=color.white, size=size.small)
label.new(bar_index+1, tp75, "Exit3: Sell " + str.tostring(q_floor) + " @ " + str.tostring(tp75), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(color.orange,0), textcolor=color.white, size=size.small)
label.new(bar_index+1, tp100, "Exit4: Sell " + str.tostring(q_remain) + " @ " + str.tostring(tp100), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(color.red,0), textcolor=color.white, size=size.small)
label.new(bar_index+1, stop_loss, "STOP LOSS: Exit All @ " + str.tostring(stop_loss), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(color.maroon,0), textcolor=color.white, size=size.small)
// ALERTS
alertcondition(close <= stop_loss, "Stop Loss Hit", "Exit all!")
alertcondition(close >= tp25, "Target 1 Hit", "Exit 25%")
alertcondition(close >= tp50, "Target 2 Hit", "Exit 25%")
alertcondition(close >= tp75, "Target 3 Hit", "Exit 25%")
alertcondition(close >= tp100, "Target 4 Hit", "Final exit")
@sivaprabug
Copy link
Author

📘 Targets & Stop Loss Visual Planner (TradingView Script)

This script helps traders plan their exits clearly by displaying target levels, stop loss, and quantity-based exits directly on the TradingView chart.

Enter just three inputs:

  • Buy Price

  • Total Quantity

  • Purchase Date (DD/MM/YYYY)

The script automatically calculates and draws:

  • 25% Target

  • 50% Target

  • 75% Target

  • 100% Target

  • Stop Loss (–20%)

Each level is plotted as a clean horizontal line starting from your purchase date, and clearly labeled on the right side of the chart.


🎯 Target & Stop-Loss Calculations

Level | Formula | Meaning -- | -- | -- Target 1 | Buy Price × 1.25 | +25% profit Target 2 | Buy Price × 1.50 | +50% profit Target 3 | Buy Price × 1.75 | +75% profit Target 4 | Buy Price × 2.00 | +100% profit Stop Loss | Buy Price × 0.80 | –20% protection

📦 Quantity Allocation

Your total quantity is automatically split:

  • 25% at Target 1

  • 25% at Target 2

  • 25% at Target 3

  • Remaining shares at Target 4

This ensures you follow a clean, structured exit strategy.


📊 What the Script Draws on Chart

  • Colored horizontal lines for each target and stop loss

  • Professional right-side labels showing:

    • Target name

    • Quantity to sell

    • Exact exit price

  • Lines start only after your purchase date, keeping the chart clean

No signals, no indicators — just exit planning visualized.


🛠️ Inputs Explained

1️⃣ Buy Price

Enter the price at which you purchased the stock.

2️⃣ Total Quantity

Number of shares bought.

3️⃣ Purchase Date (DD/MM/YYYY)

Used to start drawing lines from that bar onward.


🧩 Ideal Use Cases

  • Swing trading

  • Positional trading

  • Delivery/long-term investments

  • Traders who want discipline and clarity

  • Exit planning without emotions


🚨 Alerts

The script includes alerts for:

  • Stop Loss Hit

  • Target 1 Hit

  • Target 2 Hit

  • Target 3 Hit

  • Target 4 Hit

You can enable them from TradingView’s Alerts menu.


✔️ Summary

A simple, effective visual tool that helps you:

  • Stick to your trading plan

  • Avoid emotional exits

  • See all target levels clearly

  • Manage risk properly

Easy to use, perfect for traders who want structured and disciplined exit strategies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment