How to close all orders on last bar in Pine Script?

Viewed 266

When I simulate strategies, I sometimes can not estimate profitability automatically, because on last bar strategy contains open trades.

I can of course calculate such unclosed strategy profit manually. However, it looks better idea to close strategy on last bar automatically when I debugging it.

I don't want to set process_orders_on_closed to still have realistic result. I just need to initiate close_all on penultimate bar, so on last bar it will close the order with previous bar close price.

Is there a way to do that with Pine Script, or I need more serious programming environment than Pine Script for such customization?

1 Answers

Faced the same issue: I solved it like this as barstate.islast won't close it at end as it will wait till the next bar which will never appear until it is live marked and in that case you have to use barstate.islastconfimedhistory.

For this problem, use it like this. Logic: time in script from current time difference is used to identify last to last trading day, the strategy can close on last day.

//For PineScript v5.0 isLastTradingDay = ((timenow-time)<300000000) if (isLastTradinDay) strategy.closeAll("Exit at end of script")

Related