Hi, I am still quite new to MQL5 programming and I am stuck writing the code for a grid EA. Basically, I want the EA to do the following:
Step 1. If there are no open positions and pending orders, the EA should call the OpenNewTrade() function
Step 2. While there are open positions or pending orders, the EA should close all other sell positions and delete all buy limit orders whenever a sell position hits take profit. Also, it should close all other buy positions and delete all sell limit orders whenever a buy position hits take profit.
Step 3. If step 2 above is properly executed, there will be no more open positions and pending orders, therefore, step 1 gets called again in a loop.
PROBLEM
==========
All functions work fine except that the function to close positions as in step 2 above isn't working properly so I don't know if there is an error in the logic of the code. For instance, whenever a sell position hits take profit, the buy limit orders are deleted correctly but the other sell position(s) gets deleted along with buy position(s). this is not supposed to be so as I want the EA to function strictly according to step 2. Below is the code for the EA:
Please help correct this code to function as described in step 2 above. Thank you very much in advance.
Also I don't know how to have the EA set its name as trade comments. It will be nice if you could help me with it as I will like the trades from this EA differentiated from trades placed by other EAs. Thank you.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I am still quite new to MQL5 programming and I am stuck writing the code for a grid EA. Basically, I want the EA to do the following:
Step 1. If there are no open positions and pending orders, the EA should call the OpenNewTrade() function
Step 2. While there are open positions or pending orders, the EA should close all other sell positions and delete all buy limit orders whenever a sell position hits take profit. Also, it should close all other buy positions and delete all sell limit orders whenever a buy position hits take profit.
Step 3. If step 2 above is properly executed, there will be no more open positions and pending orders, therefore, step 1 gets called again in a loop.
PROBLEM
==========
All functions work fine except that the function to close positions as in step 2 above isn't working properly so I don't know if there is an error in the logic of the code. For instance, whenever a sell position hits take profit, the buy limit orders are deleted correctly but the other sell position(s) gets deleted along with buy position(s). this is not supposed to be so as I want the EA to function strictly according to step 2. Below is the code for the EA: