0% found this document useful (0 votes)
2 views

well done

The document outlines a Pine Script strategy for a SuperTrend Long Only trading system, which includes user-defined inputs for ATR period, multipliers, risk management, and take profit levels. It calculates long stop levels, generates buy signals, and manages trade entries and exits based on price movements relative to these levels. The strategy also features plotting for buy signals, take profit, and stop loss levels on the trading chart.

Uploaded by

zooman4566
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

well done

The document outlines a Pine Script strategy for a SuperTrend Long Only trading system, which includes user-defined inputs for ATR period, multipliers, risk management, and take profit levels. It calculates long stop levels, generates buy signals, and manages trade entries and exits based on price movements relative to these levels. The strategy also features plotting for buy signals, take profit, and stop loss levels on the trading chart.

Uploaded by

zooman4566
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// @version=4

strategy("SuperTrend Long Only Strategy", overlay=true,


default_qty_type=strategy.cash, default_qty_value=100)

// SuperTrend inputs
length = input(22, title="ATR Period")
mult = input(3.0, title="ATR Multiplier", step=0.1)
src = input(hl2, title="Source")
wicks = input(true, title="Take Wicks into Account?")
showLabels = input(true, title="Show Buy Labels?")
highlightState = input(true, title="Highlight State?")

// Risk Management inputs


riskPercent = input(1.5, title="Risk % per Trade", step=0.1)
rewardPercent1 = input(1.0, title="Take Profit 1 (%)", step=0.1)
rewardPercent2 = input(2.0, title="Take Profit 2 (%)", step=0.1)
rewardPercent3 = input(3.0, title="Take Profit 3 (%)", step=0.1)
rewardPercent4 = input(4.0, title="Take Profit 4 (%)", step=0.1)
rewardPercent5 = input(5.0, title="Take Profit 5 (%)", step=0.1)
rewardPercent6 = input(6.0, title="Take Profit 6 (%)", step=0.1)
rewardPercent7 = input(7.0, title="Take Profit 7 (%)", step=0.1)
rewardPercent8 = input(8.0, title="Take Profit 8 (%)", step=0.1)
rewardPercent9 = input(9.0, title="Take Profit 9 (%)", step=0.1)
tradeSizeUSD = input(100, title="Trade Size in USDT")

// SuperTrend Calculation
atrValue = atr(length)
atrCalc = mult * atrValue

highPrice = wicks ? high : close


lowPrice = wicks ? low : close

doji4price = (open == close) and (open == low) and (open == high)

// Calculate Long Stop


longStop = src - atrCalc
longStopPrev = nz(longStop[1], longStop)
if longStop > 0
if doji4price
longStop := longStopPrev
else
longStop := (lowPrice[1] > longStopPrev ? max(longStop, longStopPrev) :
longStop)
else
longStop := longStopPrev

// Determine direction (1 = long, 0 = neutral)


var int dir = 0
if (dir == 0 and lowPrice > longStopPrev)
dir := 1
else if (dir == 1 and lowPrice < longStopPrev)
dir := 0

// Plot SuperTrend stop


plot(dir == 1 ? longStop : na, title="Long Stop", color=color.green, linewidth=2)

// Buy Signal
buySignal = (dir == 1 and dir[1] == 0)
// Calculate TP and SL levels
var float tradeEntry = na
var float tp1 = na
var float tp2 = na
var float tp3 = na
var float tp4 = na
var float tp5 = na
var float tp6 = na
var float tp7 = na
var float tp8 = na
var float tp9 = na
var float sl = na
var float stepwiseSL = na
var int tpHit = 0

// Reset TP and SL states on new trade


if buySignal
tpHit := 0
stepwiseSL := na

// Set TP/SL for Long Trade


if buySignal
tradeEntry := close
tp1 := tradeEntry * (1 + rewardPercent1 / 100)
tp2 := tradeEntry * (1 + rewardPercent2 / 100)
tp3 := tradeEntry * (1 + rewardPercent3 / 100)
tp4 := tradeEntry * (1 + rewardPercent4 / 100)
tp5 := tradeEntry * (1 + rewardPercent5 / 100)
tp6 := tradeEntry * (1 + rewardPercent6 / 100)
tp7 := tradeEntry * (1 + rewardPercent7 / 100)
tp8 := tradeEntry * (1 + rewardPercent8 / 100)
tp9 := tradeEntry * (1 + rewardPercent9 / 100)
sl := tradeEntry * (1 - riskPercent / 100)
stepwiseSL := sl

if showLabels
label.new(bar_index, tradeEntry, "BUY\nUSDT: " + tostring(tradeSizeUSD) +
"\nPrice: " + tostring(tradeEntry), color=color.green, textcolor=color.white,
style=label.style_label_up)

// Update tpHit and stepwiseSL based on price reaching TP levels


if dir == 1 and close >= tp9
tpHit := 9
stepwiseSL := tp8
else if dir == 1 and close >= tp8
tpHit := 8
stepwiseSL := tp7
else if dir == 1 and close >= tp7
tpHit := 7
stepwiseSL := tp6
else if dir == 1 and close >= tp6
tpHit := 6
stepwiseSL := tp5
else if dir == 1 and close >= tp5
tpHit := 5
stepwiseSL := tp4
else if dir == 1 and close >= tp4
tpHit := 4
stepwiseSL := tp3
else if dir == 1 and close >= tp3
tpHit := 3
stepwiseSL := tp2
else if dir == 1 and close >= tp2
tpHit := 2
stepwiseSL := tp1
else if dir == 1 and close >= tp1
tpHit := 1
stepwiseSL := tradeEntry
else
tpHit := 0

// Stop Loss Condition (initial SL or stepwise SL)


slHit = dir == 1 and close < stepwiseSL

// Strategy Entry
if buySignal
strategy.entry("Long", strategy.long, qty=tradeSizeUSD/close)

// Strategy Exit Conditions


if slHit
strategy.close_all(comment="Stop Loss")

// Partial Exits on Take Profit Levels


qtyPerTP = (tradeSizeUSD / tradeEntry) * 0.10 // 10% of position
if tpHit >= 1 and tpHit[1] < 1
strategy.order("Sell_TP1", strategy.short, qty=qtyPerTP, limit=tradeEntry,
comment="TP1")
if tpHit >= 2 and tpHit[1] < 2
strategy.order("Sell_TP2", strategy.short, qty=qtyPerTP, limit=tp1,
comment="TP2")
if tpHit >= 3 and tpHit[1] < 3
strategy.order("Sell_TP3", strategy.short, qty=qtyPerTP, limit=tp2,
comment="TP3")
if tpHit >= 4 and tpHit[1] < 4
strategy.order("Sell_TP4", strategy.short, qty=qtyPerTP, limit=tp3,
comment="TP4")
if tpHit >= 5 and tpHit[1] < 5
strategy.order("Sell_TP5", strategy.short, qty=qtyPerTP, limit=tp4,
comment="TP5")
if tpHit >= 6 and tpHit[1] < 6
strategy.order("Sell_TP6", strategy.short, qty=qtyPerTP, limit=tp5,
comment="TP6")
if tpHit >= 7 and tpHit[1] < 7
strategy.order("Sell_TP7", strategy.short, qty=qtyPerTP, limit=tp6,
comment="TP7")
if tpHit >= 8 and tpHit[1] < 8
strategy.order("Sell_TP8", strategy.short, qty=qtyPerTP, limit=tp7,
comment="TP8")
if tpHit >= 9 and tpHit[1] < 9
strategy.order("Sell_TP9", strategy.short, qty=qtyPerTP, limit=tp8,
comment="TP9")

// Plot Buy Signal


plotshape(buySignal, style=shape.labelup, location=location.belowbar,
color=color.green, size=size.small, title="Buy Signal", text="Buy")

// Plot TP and SL levels


plot(tp1, color=color.blue, title="Take Profit 1", linewidth=1,
style=plot.style_stepline)
plot(tp2, color=color.blue, title="Take Profit 2", linewidth=1,
style=plot.style_stepline)
plot(tp3, color=color.blue, title="Take Profit 3", linewidth=1,
style=plot.style_stepline)
plot(tp4, color=color.blue, title="Take Profit 4", linewidth=1,
style=plot.style_stepline)
plot(tp5, color=color.blue, title="Take Profit 5", linewidth=1,
style=plot.style_stepline)
plot(tp6, color=color.blue, title="Take Profit 6", linewidth=1,
style=plot.style_stepline)
plot(tp7, color=color.blue, title="Take Profit 7", linewidth=1,
style=plot.style_stepline)
plot(tp8, color=color.blue, title="Take Profit 8", linewidth=1,
style=plot.style_stepline)
plot(tp9, color=color.blue, title="Take Profit 9", linewidth=1,
style=plot.style_stepline)
plot(sl, color=color.orange, title="Initial Stop Loss", linewidth=1,
style=plot.style_stepline)
plot(stepwiseSL, color=color.red, title="Stepwise Stop Loss", linewidth=1,
style=plot.style_stepline)

You might also like