I want to create a strategy that enter position everyday at a specific hour and when a specific condition is met. After entering position, the stoploss order is set and a take profit 1 order is set as well that closes half of the position. If the take profit 1 order is filled, i want to place a second take profit order and place the stoploss to entry price (be even).
I use the variable entry to keep track of whether the position was totally closed, half closed or full. The problems I encounter is that the SL (stoploss) order is not getting filled when the price gets to stopLossPriceLong.
entry=0
if isTenAM()
if isUptrend and entry==0
strategy.entry('Buy', strategy.long)
entry:=2
// else if isDowntrend
// strategy.entry('Sell', strategy.short)
if entry==2 and strategy.position_size > 0
strategy.order('TP1 Long', strategy.short, limit=takeProfitPriceLong, qty=strategy.position_size * 0.5)
entry:=1
if entry==1 and strategy.position_size > 0
strategy.order('TP2 Long', strategy.short, limit=takeProfitPriceLong2, qty=strategy.position_size)
strategy.exit('SLBE Long', 'TP2 Short', stop=entryPrice, qty=strategy.position_size)
entry:=0
if entry==2 and strategy.position_size > 0
strategy.order('SL Long', strategy.short, stop=stopLossPriceLong, qty=strategy.position_size)
entry:=0
The script will only executes TP1 long each time the price get to the takeProfitPriceLong. I couldnt find a way to prevent some take profit orders or stoploss running all over again on the same transaction. And I also had issues placing the stoplosses and changing it according to the execution of the other orders or position size.
Im new to this and any suggestion would help. Thanks!