Scancieitor
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: December 26th, 2023
Contact: TradingView Profile

Multiple position closing in the same bar

Hi Pine Scripters,

I have an strategy nearly finished but still have a problem in the moment that I want to close a part of an open position, because, once the condition cierralong or cierrashort is activated , the opened long/short position is closed partially but this closing repeats several times during the same bar.

I have added a bar control to the condition but still the same problem.
That I am looking for is to activate only once when condition is on.
Thanks in advance, solving this issue would be a big advance for my strategy.


if barstate.isnew
barra:=nz(barra[1])



for i=0 to strategy.opentrades-1
entrada = strategy.opentrades.entry_price(i)
id=str.tostring(strategy.opentrades.entry_id(i))
ganado=str.tostring(strategy.opentrades.profit(i),format.volume)
capital=str.tostring(strategy.opentrades.size(i),format.volume)
if str.contains(strategy.opentrades.entry_id(i), "Long")
colorentrada:=#D0F0C0
beneficio:= margin*(close-entrada)/entrada
if cierralong and strategy.opentrades.profit(i)>0 and bar_index>barra
barra:=bar_index
strategy.close(id, qty_percent = qtycierre)

else
colorentrada:=#FDBCB4
beneficio:= margin*(entrada-close)/entrada
if cierrashort and strategy.opentrades.profit(i)>0 and bar_index>barra
barra:=bar_index
strategy.close(id, qty_percent = qtycierre)

Return to “Pine Script Q&A”