Close specific amount of lot

 

Hi,

I am trying to create a function that closes a specific amount of lots.
It should loop through all orders, close by oldest order first and do partial closure if there is a order thats bigger than the specified amount to close.
I am having a hard time doing this, anyone that can help?

Reason: