Can anyone here please help me with my code.
I do not get anything printed on the chart when I do a buy or sell order.
My code should set a default SL and TP ( that I should be able to see on my chart)
Then it should start auto trailing when my TP target is met.
Thanks in advance.
Code here:
//@version=5
strategy("Manual Order Management with Trailing SL and TP", overlay=true)
// Parameters for SL and TP
trailingStopDistance = input.float(80, title="Trailing Stop Distance ($)")
profitThreshold = input.float(100, title="Profit Threshold ($)")
tpDistance = input.float(300, title="Take-Profit Distance ($)")
retrailDistance = input.float(200, title="Retrial Distance from TP ($)")
defaultTP = input.float(200, title="Default Take-Profit ($)")
defaultSL = input.float(50, title="Default Stop-Loss ($)")
// Variables to store prices
var float entryPrice = na
var float trailingStopPrice = na
var float takeProfitPrice = na
var bool trailingActive = false
var string positionType = na
// Check if there's an open position and set TP/SL
if (strategy.position_size > 0)
// Long position detected
positionType := "long"
entryPrice := strategy.position_avg_price
trailingStopPrice := entryPrice - defaultSL
takeProfitPrice := entryPrice + defaultTP
trailingActive := true
// Set exit conditions
strategy.exit("Exit Long", from_entry="Buy", limit=takeProfitPrice, stop=trailingStopPrice)
else if (strategy.position_size < 0)
// Short position detected
positionType := "short"
entryPrice := strategy.position_avg_price
trailingStopPrice := entryPrice + defaultSL
takeProfitPrice := entryPrice - defaultTP
trailingActive := true
// Set exit conditions
strategy.exit("Exit Short", from_entry="Sell", limit=takeProfitPrice, stop=trailingStopPrice)
else
// No position
entryPrice := na
trailingStopPrice := na
takeProfitPrice := na
trailingActive := false
positionType := na
// Update trailing stop and TP dynamically for long and short positions
if (trailingActive and positionType == "long")
if (close - entryPrice >= profitThreshold)
trailingStopPrice := math.max(trailingStopPrice, close - trailingStopDistance)
if (close >= takeProfitPrice - retrailDistance)
takeProfitPrice := close + tpDistance
strategy.exit("Trailing Exit Long", from_entry="Buy", limit=takeProfitPrice, stop=trailingStopPrice)
if (trailingActive and positionType == "short")
if (entryPrice - close >= profitThreshold)
trailingStopPrice := math.min(trailingStopPrice, close + trailingStopDistance)
if (close <= takeProfitPrice + retrailDistance)
takeProfitPrice := close - tpDistance
strategy.exit("Trailing Exit Short", from_entry="Sell", limit=takeProfitPrice, stop=trailingStopPrice)
// Plotting Entry, TP, and SL lines
plot(not na(entryPrice) ? entryPrice : na, title="Entry Price", color=color.green, linewidth=2)
plot(not na(trailingStopPrice) ? trailingStopPrice : na, title="Trailing Stop Price", color=color.red, linewidth=2)
plot(not na(takeProfitPrice) ? takeProfitPrice : na, title="Take Profit Price", color=color.blue, linewidth=2)
// Reset variables when no position is held
if (na(positionType))
entryPrice := na
trailingStopPrice := na
takeProfitPrice := na
trailingActive := false
I’ved tried executing trades from the default button in the tradingview UI and from the trade tab.
None of them looks like they execute my script.
New contributor
Claus Schou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.