function needed

 

I need a function that will close the losing trades (one at a time) when the margin level is equal or below 100%, until the margin level is above 100%. - closing the the biggest losing order first.

Reason: