So basically, the take-profit function on long trades works flawlessly, but it's not working on the short trades. It's my first time dealing with pine-script, so I've no idea what I am doing wrong.
entrySize = 2
trimSize = entrySize / 2
nytp = 0.003
ontp = 0.0015
//Calculating Time
nySession = time(timeframe.period, "0930-1600")
onSession = time(timeframe.period, "1800-0930")
//NY Session Take Profit
nyLongTP = strategy.position_avg_price * (1 + nytp)
nyShortTP = strategy.position_avg_price * (1 - nytp)
//ON Session Take Profit
onLongTP = strategy.position_avg_price * (1 + ontp)
onShortTP = strategy.position_avg_price * (1 - ontp)
//Take Profit Long
if (strategy.position_avg_price > 0)
if (nySession)
strategy.order(id = "Long", direction = strategy.short, qty = trimSize, limit = nyLongTP, comment = "LTP")
if (onSession)
strategy.order(id = "Long", direction = strategy.short, qty = trimSize, limit = onLongTP, comment = "LTP")
//Take Profit Short
if (strategy.position_avg_price < 0)
if (nySession)
strategy.order(id = "Short", direction = strategy.long, qty = trimSize, limit = nyShortTP, comment = "STP")
if (onSession)
strategy.order(id = "Short", direction = strategy.long, qty = trimSize, limit = onShortTP, comment = "STP")
I'm expecting the short trade to close 50% of the quantity at the limit price that is provided by "nyShortTP" & "onShortTP" but the limit order is not getting placed.