OPEN-SOURCE SCRIPT

huzaifa iqbal pakistan

69
this is custum only for mazaq //version=6
strategy("UT Bot Long Only Strategy", overlay=true, pyramiding=0, initial_capital=10000, currency=currency.USD)

// Parameters for ATR-based trailing stop (UT Bot logic)
var int atrLength = 10 // ATR period length
var float atrMult = 5.0 // ATR multiplier for trailing stop

// Calculate ATR and the ATR-based stop distance (nLoss)
atrValue = ta.atr(atrLength)
nLoss = atrMult * atrValue

// ATR Trailing Stop logic (UT Bot)
// Using persistent variable 'trailingStop' to maintain state across bars
var float trailingStop = na
if na(trailingStop[1])
// Initialize trailing stop on first bar
trailingStop := close - nLoss
else if (close > trailingStop[1] and close[1] > trailingStop[1])
// Price is above trailing stop and was above it in previous bar -> uptrend continues
trailingStop := math.max(trailingStop[1], close - nLoss)
else if (close < trailingStop[1] and close[1] < trailingStop[1])
// Price is below trailing stop and was below it in previous bar -> downtrend continues
trailingStop := math.min(trailingStop[1], close + nLoss)
else if (close > trailingStop[1])
// Price crossed from below to above the trailing stop -> trend flips up (new long setup)
trailingStop := close - nLoss
else
// Price crossed from above to below the trailing stop -> trend flips down
trailingStop := close + nLoss

// Long entry signal: when price crosses above the trailing stop from below
bool longSignal = (close[1] < trailingStop[1] and close > trailingStop[1])

// Enter long position on UT Bot buy signal (price crossing above trailing stop)
if (longSignal)
strategy.entry("Long", strategy.long, comment="UT Bot Buy Signal")

// Exit conditions: take-profit at +10% and stop-loss at -0.10% from entry price
// We hold until TP or SL is hit; no early exit on UT Bot sell signals
if (strategy.position_size > 0)
// Calculate profit target and stop loss based on entry price
float entryPrice = strategy.position_avg_price
float takeProfit = entryPrice * 1.10 // +//version=6
strategy("UT Bot Long Only Strategy", overlay=true, pyramiding=0, initial_capital=10000, currency=currency.USD)

// Parameters for ATR-based trailing stop (UT Bot logic)
var int atrLength = 10 // ATR period length
var float atrMult = 5.0 // ATR multiplier for trailing stop

// Calculate ATR and the ATR-based stop distance (nLoss)
atrValue = ta.atr(atrLength)
nLoss = atrMult * atrValue

// ATR Trailing Stop logic (UT Bot)
// Using persistent variable 'trailingStop' to maintain state across bars
var float trailingStop = na
if na(trailingStop[1])
// Initialize trailing stop on first bar
trailingStop := close - nLoss
else if (close > trailingStop[1] and close[1] > trailingStop[1])
// Price is above trailing stop and was above it in previous bar -> uptrend continues
trailingStop := math.max(trailingStop[1], close - nLoss)
else if (close < trailingStop[1] and close[1] < trailingStop[1])
// Price is below trailing stop and was below it in previous bar -> downtrend continues
trailingStop := math.min(trailingStop[1], close + nLoss)
else if (close > trailingStop[1])
// Price crossed from below to above the trailing stop -> trend flips up (new long setup)
trailingStop := close - nLoss
else
// Price crossed from above to below the trailing stop -> trend flips down
trailingStop := close + nLoss

// Long entry signal: when price crosses above the trailing stop from below
bool longSignal = (close[1] < trailingStop[1] and close > trailingStop[1])

// Enter long position on UT Bot buy signal (price crossing above trailing stop)
if (longSignal)
strategy.entry("Long", strategy.long, comment="UT Bot Buy Signal")

// Exit conditions: take-profit at +10% and stop-loss at -0.10% from entry price
// We hold until TP or SL is hit; no early exit on UT Bot sell signals
if (strategy.position_size > 0)
// Calculate profit target and stop loss based on entry price
float entryPrice = strategy.position_avg_price
//version=6
strategy("UT Bot Long Only Strategy", overlay=true, pyramiding=0, initial_capital=10000, currency=currency.USD)

// Parameters for ATR-based trailing stop (UT Bot logic)
var int atrLength = 10 // ATR period length
var float atrMult = 5.0 // ATR multiplier for trailing stop

// Calculate ATR and the ATR-based stop distance (nLoss)
atrValue = ta.atr(atrLength)
nLoss = atrMult * atrValue

// ATR Trailing Stop logic (UT Bot)
// Using persistent variable 'trailingStop' to maintain state across bars
var float trailingStop = na
if na(trailingStop[1])
// Initialize trailing stop on first bar
trailingStop := close - nLoss
else if (close > trailingStop[1] and close[1] > trailingStop[1])
// Price is above trailing stop and was above it in previous bar -> uptrend continues
trailingStop := math.max(trailingStop[1], close - nLoss)
else if (close < trailingStop[1] and close[1] < trailingStop[1])
// Price is below trailing stop and was below it in previous bar -> downtrend continues
trailingStop := math.min(trailingStop[1], close + nLoss)
else if (close > trailingStop[1])
// Price crossed from below to above the trailing stop -> trend flips up (new long setup)
trailingStop := close - nLoss
else
// Price crossed from above to below the trailing stop -> trend flips down
trailingStop := close + nLoss

// Long entry signal: when price crosses above the trailing stop from below
bool longSignal = (close[1] < trailingStop[1] and close > trailingStop[1])

// Enter long position on UT Bot buy signal (price crossing above trailing stop)
if (longSignal)
strategy.entry("Long", strategy.long, comment="UT Bot Buy Signal")

// Exit conditions: take-profit at +10% and stop-loss at -0.10% from entry price
// We hold until TP or SL is hit; no early exit on UT Bot sell signals
if (strategy.position_size > 0)
// Calculate profit target and stop loss based on entry price
float entryPrice = strategy.position_avg_price
float takeProfit = entryPrice * 1.10 // +10%
float stopLoss = entryPrice * 0.999 // -0.10%
strategy.exit("ExitLong", "Long", limit=takeProfit, stop=stopLoss)

// Plot the ATR-based trailing stop for visualization (green when trend is up, red when down)
plot(trailingStop, "Trailing Stop", color = close >= trailingStop ? color.lime : color.red, style=plot.style_line)
float stopLoss = entryPrice * 0.999 // -0.10%
strategy.exit("ExitLong", "Long", limit=takeProfit, stop=stopLoss)

// Plot the ATR-based trailing stop for visualization (green when trend is up, red when down)
plot(trailingStop, "Trailing Stop", color = close >= trailingStop ? color.lime : color.red, style=plot.style_line)
10%
float stopLoss = entryPrice * 0.999 // -0.10%
strategy.exit("ExitLong", "Long", limit=takeProfit, stop=stopLoss)

// Plot the ATR-based trailing stop for visualization (green when trend is up, red when down)
plot(trailingStop, "Trailing Stop", color = close >= trailingStop ? color.lime : color.red, style=plot.style_line)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.