EvilEddie
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: February 4th, 2022

Close Long on Short Signal

Hi Guys, I’m stuck on the Strategy Order lesson in the mastery course. How do I set my Take Profit target to take 50% of profits and let the rest ride til I get an an opposing entry signal?

i.e. Go ‘Long', take 50% at 1:1 the close the rest on the next ‘Short’ signal.

Cheers. Eddie.

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Close Long on Short Signal

Hey

I like to do it as follows:
When there is a need to go Long , i mark isLong as true and for short isShort as true.
Also i mark takeProfitBuy as true on meeting certain conditions and takeProfitSell as true when certain other conditions meet for short. You can set these to be set at 1:1 profit or any other condition.

I than have the following code in sequence.

Code: Select all

strategy.entry("LONG", strategy.long,  when=isLong)
strategy.entry("SHORT", strategy.short, when=isShort)
strategy.entry("LongHalfClose", strategy.short, math.abs(strategy.position_size/2), when = takeProfitBuy and strategy.position_size > 0)
strategy.entry("ShortHalfClose", strategy.long, math.abs(strategy.position_size/2),  when = takeProfitSell and strategy.position_size < 0)

Return to “Request Scripts”