Ok, for an entry stop your code is correct.
Programmers do not think in source code. They just think how to describe their task as clear as possible. Then the source code appears automatically. If you want to exit all trades that are in profit at the next bar's open, the exit code snippet would look like this:
for(open_trades)
if(TradeIsOpen and TradeResult > 0)
exitTrade(ThisTrade);
This would of course cause a margin call sooner or later when a trade does not reach the profit zone.