Job finished


Specification
My current EA (I will refer to it as OP) opens opposite pending orders based on existing open trades in the terminal.
Example of how OP works:
1) Let's say I manually or automatically (through a different EA) open a buy trade for EURUSD with open price of 1.10000, SL of 1.00000, and TP of 1.20000
(magic number is 100, order comment is "euro breakout", and symbol is of course EURUSD)
2) OP will then identify open trades to act on based on magic number, order comment, or symbol. There are three different entry fields in the settings, for the user to type in the magic number, order comment, or symbol of the open trade that they want OP to identify and base its trade on. For this example, we will put EURUSD into the "symbol" field, so that OP looks for an open EURUSD trade in the terminal.
3) OP identifies the open EURUSD buy trade (Trade A), and places a pending order in the opposite direction (Trade B) with an entry price at X% of the SL level of Trade A. For example, if the entry price setting is 0.65, then the entry price of Trade B would be at 65% of the distance between the entry price for Trade A (1.10000) and the SL for Trade A (1.00000). So in this example, the entry price of Trade B (EURUSD sell trade) would be 1.06500.
4) The order for Trade B is automatically canceled if it is not yet filled by the time Trade A is closed. In other words, the closure of Trade A causes the cancellation of the pending order for Trade B.
5) in the settings for OP, the user can customize the Volume, SL, TP, and Trailing Stop on Profit for Trade B
What I need modified, your key actions highlighted in bold:
1) When OP gets a signal to open an order, I am getting the following error: “invalid pointer access in ‘OP.MT5’ (128,16)”. The pending order fails to open, and OP removes itself from the chart it is attached to. Fix this error so that the order for Trade B is always successfully placed.
2) Limit the maximum number of Trade B orders to one per each Trade A
Continuing on the previous example, if I set the entry price percentage for Trade B at 0.4, OP will place a pending order for EURUSD sell at 1.04000. The user has set the SL and TP for Trade B at 4000 points (1.08000) and 4000 points (1.00000), respectively. If Trade B is filled, and then the price goes up to 1.08000, it hits SL and Trade B is closes. But then another pending order is placed with entry price of 1.04000, because Trade A is still open (until it hits its own SL or TP of 1.00000 or 1.20000), and OP is still looking to open a new Trade B based on any existing trade that fits the linkage (symbol EURUSD). So if the price goes up to 1.08000, hits Trade B SL, and then quickly goes back down to 1.04000, without ever touching the SL or TP for Trade A, then another Trade B is opened at the price of 1.04000.
This means that one Trade A can have multiple Trade Bs, which is not good. The code should be fixed such that for each Trade A, there is maximum one deal for Trade B, until Trade A is closed.
3) Ensure that a Trade B is filled even if Trade A opens and closes in a loss very quickly
For example, if Trade A has a tight SL or if market movements are very strong and fast, Trade A can open and close in a loss in 1 second or less, not giving enough time for the pending order for Trade B to be opened and filled before being cancelled due to Trade A being closed. I believe the way the logic is now, the pending order for Trade B is automatically closed if it is not filled by the time Trade A is closed, regardless of whether Trade A closed in profit or in loss. The code should be fixed such that the pending order for Trade B is only cancelled if Trade A closes in profit.
And sometimes the issue may be that OP does not have enough time to place the pending order for Trade B by the time Trade A closes (due to slow server speeds, etc.) The code should ensure that there is always a filled order for Trade B if Trade A hits SL and closes in loss. This may require adding a “safety net” in the code so that if Trade A closes in loss before the pending order for Trade B can be placed, OP will immediately open a market order instead. The priority is to always open Trade B with pending orders, but in the rare cases that fails due to extremely fast Trade A closure, only then should a market order be used to open Trade B.
4) Ensure that once Trade B is filled, it is no longer affected by Trade A. Review the code to ensure that any actions with Trade A have no impact on Trade B once Trade B is already filled.
5) Ensure that the SL and TP of Trade B are modified based on the actual fill price after slippage. For example, if the entry price for Trade B is 1.04000 but it actually fills at 1.03800 due to slippage, ensure that the SL and TP are adjusted to be based on the actual fill price. So if the user settings for Trade B’s SL and TP are 1000 points, then the SL and TP of Trade B should be 1.04800 and 1.02800 instead of 1.05000 and 1.03000, because even though the pending order had an entry price of 1.04000, the actual fill price after slippage was 1.03800.
6) Add option to determine the lot volume of Trade B based on a percentage of the lot volume of Trade A. Insert code that will create a user input to calculate Trade B lot volume as a percentage of the lot volume of Trade A (for example 0.10 for 10%, 2.00 for 200%, 3.55 for 355%). So if the user setting for this 1.5, and Trade A has a volume 0.1 lots, then Trade B will have a volume of 0.15 lots. The code should ensure that Trade B lot is rounded up to the broker minimum lot. This input should only be active if the user input for fixed lot volume is 0.
7) Overall review of the code to identify and fix errors and optimize the speed of execution.