Usama Buttar
Usama Buttar

Reputation: 1

There's no error but take-profit functions on short trades aren't working

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.

Upvotes: 0

Views: 104

Answers (1)

elod008
elod008

Reputation: 1362

Well your condition seems to be wrong. I think you're looking for strategy.position_size instead of strategy.position_avg_price.
Now you're evaluating:

Average entry price of current market position. If the market position is flat, 'NaN' is returned.

reference
and this should be basically < 0. Is that even possible?
Generally, to check if you ever get into a situation, you could always print a label for yourself, like:

if strategy.position_avg_price < 0  
    label.new(bar_index, high, "Yeah condition's true")

Back to the question and the solution:
I think you are looking for something else. If it's the direction of your positions you're interested in, then use strategy.position_size. Already discussed here.

Upvotes: 0

Related Questions