Orders/Positions limiter needed for an EA.

 

Attached is the .mq4 codefile for a grid system EA.

Under certain market conditions and certain settings this EA is net profitable... if you keep a constant eye on it and limit its total positions to (for example) 30 by turning it off when the sum of its open positions and pending stop orders reaches 30. Then you have to wait until all the TP and SL points are reached at either the top or the bottom of the grid... take the profit/loss... and then turn the EA back on again to start a new "cycle". Using this methodology the EA returns a net profit... incurring very few losses... which are mostly small.

However, this method defeats the object of auto-trading... when one has to watch the EA constantly whilst it is in use... and manually perform the task of limiting the orders total... and then reset the EA after each cycle. I'm virtually tied to my desk when the EA is running! Hardly automatic.

Left to its own devices the EA can open so many positions that it creates a margin call every few days... and wipes my equity (or a fair chunk of it) out. Manually limiting the positions/pending orders prevents this. 

Can anyone assist me by adding a little code which would enable me (or anyone else) to define a limit for the number of pending orders (or open positions) the EA can commit to?

This would enable the EA to run fully automatically without the risk of cleaning me out... and make a small net profit return for me.

I'm happy to define the limit variable inside the code file itself if necessary... but an input parameter in the EA's "control box" would be a real bonus if it's not too much hassle.

I have tried to do this myself... but I keep getting error messages on debug. I've also tried using a script... but even if that worked I'd still have to load it to the chart again and again manually.

I'm just not at a level where I can get this limiting issue resolved inside the EA itself. Can anyone out there assist me by appending some code please?

Thanks in advance if you can.

David

Files:
Reason: