Manual Order Management with Trailing SL and TP

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật