Pinescript 2 Orders at the same time but close with different parameters, help needed

77 Views Asked by At

I'm new to pinescript and am looking to create a strategy that opens 2 trades at the same time, the first will close when either the take profit of 1 ATR is met or hit the stoploss at 1.5 ATR, the second trade will continue until either the exit indicator shows a signal to do so or the price hits the trailing stoploss. The trades aren't being taken when they should and are not stopping either, I've tried many 'solutions' and have spend days trying to sort it but I'm out of ideas, any help would be greatly appreciated.

//@version=5
strategy("Same Time Exit x2 trades FSL TSL Complied Indicator Backtest Strategy",         overlay=true, pyramiding=2, initial_capital = 100000, currency = currency.GBP, default_qty_type = strategy.percent_of_equity, default_qty_value=1, close_entries_rule="ANY")

var float TradeSize = riskPercentage
var float trailingStopLong = close - 1.5 * atrValue
var float trailingStopShort = close + 1.5 * atrValue


var bool takeProfitHitLong = na
var bool takeProfitHitShort = na
var bool secondTradeClosed = na
var float trailPrice = na


if enterLongCondition
strategy.entry('Long', strategy.long)
strategy.entry('Long 2', strategy.long)
strategy.exit('Exit Long 1', from_entry="Long", stop=close - stopLossAmount, limit=close + takeProfitAmount)

if strategy.position_size > 0
trailingStopLong := close - 1.5 * atrValue // Calculate trailing stop
strategy.exit('Exit Long 2', from_entry="Long 2", trail_offset = trailingStopLong, trail_price=trailPrice)

if  exitLongCondition
strategy.close("Long", "Exit Long 1")

if longExitConditions
strategy.close("Long 2", "Exit Long 2")



if enterShortCondition
strategy.entry('Short', strategy.short)
strategy.entry('Short 2', strategy.short)
strategy.exit('Exit Short 1', from_entry="Short", stop=close + stopLossAmount, limit=close - takeProfitAmount)

if strategy.position_size < 0
trailingStopShort := close + 1.5` * atrValue // Calculate trailing stop
strategy.exit('Exit Short 2', from_entry="Short 2", trail_offset=trailingStopShort, trail_price=trailPrice)

if exitShortCondition
strategy.close("Exit Short 1")

if shortExitConditions
strategy.close("Exit Short 2")

I have tried changing the strategy.entry, strategy.close and strategy.exit around but just can't get it to work as intended.

0

There are 0 best solutions below