How can I Open Multiple Trades in a Single EA? I want to open multiple trades, each with a new signal, and close them all at the same time. If I build the EA for this and set maxsell or maxbuys and maxtrades to a different number, for example 5, it opens all 5 trades at the same time with one signal. How do I fix this?
A: Whenever possible, I advise traders to manage one single trade in each EA. I understand that it’s not always possible – I’ll show below how it’s done (I’m working on a way to make this easier, so if you have any ideas, please let me know)
VTS uses the MQL magic number concept to identify trades. There is more info about magic numbers here:
Furthermore, VTS automatically creates an input parameter named MagicNumber in all VTS systems. The MagicNumber value is used (by default) throughout a VTS-generated EA. It’s used when creating an order (in the MQL function OrderSend), and it’s used when looking for and identifying open trades .
So when you create a single-trade EA, it’s best to use the MagicNumber and leave it set as the default value throughtout your EA. Note: Since MagicNumber is an input parameter, it can be set to any value when the EA is attached to a MetaTrader chart.
The advantage of using the MagicNumber value is that the same value is used consistently throughout the EA.
You want to make sure you open your trades using the same MagicNumber that you use to search for and identify your open trades.
However, to manage more than one trade, we’ll need to use more than one magic number value. We’ll have to open each trade using a unique magic number, AND more importantly, search for each trade using the correct (same) number.
This is done in two ways:
1. Change the magic number default value from MagicNumber to some unique value when opening the trade. I chose 5555 in the image below.
Note, the magic parameter is on the Advanced tab.
2, To keep from opening more than one trade, we need to check if a trade is already open. VTS does this automatically when you use the MagicNumber value, We’ll have to do it ourselves here. We’ll use the function fnGetOrderInfo to find the number of lots open for any trade with a magic number of 5555. The function fnGetOrderInfo is found in the Functions ToolBox under the Trade menu.
- Set the InfoType to INFO_LOTS
- Uncheck All, check MagicNumber and enter 5555
- You can name the function something like Lots5555
Now, before opening a trade with a MagicNumber of 5555, we first check if trade is already open, and do not open it if one exists. We use the lots value as a proxy for an open trade: you can’t have an open trade of 0 lots.
This logic checks the value:
This drawing shows how to connect the Elements. This only executes the fnOpenOrder function if the trade is not already open (lots is not greater than zero).
This can be done with any number of trades, as long as the MagicNumber is unique. So you can recreate this same logic for Magic numbers of 6666, 7777, 8888, 9999, etc.
As for closing the trades, to close all at once, use the function fnCloseAll. This will close all trades for the MetaTrader account. To close the trades individually, use the function fnCloseOrder, and set the MagicNumber appropriately.
Both of these functiohns are found in the Functions ToolBox under the Trade menu.
To get notified about the latest questions and answers, follow us!