Simplest solution would be of course, if you know exactly how long and at what hours of day the trading session starts and ends. Then you could execute the for(all_trades) loop e.g. exactly at market close time and just make sure to hit Stop a minute or two later.