Discussion of article "Library for easy and quick development of MetaTrader programs (part XXXIV): Pending trading requests - removing and modifying orders and positions under certain conditions"

 

New article Library for easy and quick development of MetaTrader programs (part XXXIV): Pending trading requests - removing and modifying orders and positions under certain conditions has been published:

In this article, we will complete the description of the pending request trading concept and create the functionality for removing pending orders, as well as modifying orders and positions under certain conditions. Thus, we are going to have the entire functionality enabling us to develop simple custom strategies, or rather EA behavior logic activated upon user-defined conditions.

Compile the EA and launch it in the tester in the visualization mode. To check order removal as well as order and position modification, open two sell positions and place a sell pending order without StopLoss and TakeProfit levels. Next, create pending requests to modify stop levels of orders and positions by price value. Wait for activation of pending requests and placing specified stop levels, and remove orders and positions.

Then open two buy positions and place a buy pending order. After that, create pending requests to remove orders and close positions by time.


As we can see, stop levels were set at the intersection of a given pending request activation price level. The positions were closed after a specified time and the order was removed.

Author: Artyom Trishkin

 

Hello @Artyom Trishkin, thank you for this code.

I tried to run modified version in MT4 with no testing buttons to listen to Account/Trade events. but I see wrong result when I opened and closed 3 trades:

01:17:13.849 removed

01:17:13.733 uninit reason 4

01:15:36.860 OnDoEasyEvent: Position closed

01:15:36.860 - Position closed: 2020.08.20 23:45:29.000 -EURUSD Closed Sell #573015195 at price 1.18610, Profit -0.13 USD

01:15:35.489 CEventsCollection::CreateNewEvent, Line 768: This event already in the list

01:15:35.019 OnDoEasyEvent: Position closed

01:15:35.019 - Position closed: 2020.08.20 23:45:27.000 -EURUSD Closed Buy #573015193 at price 1.18623, Profit -0.13 USD

01:15:30.144 OnDoEasyEvent: Position opened

01:15:30.144 - Position opened: 2020.08.20 23:45:29.000 -EURUSD Opened 0.01 Sell #573015195 [0.01 Market-order Sell #573015195] at price 1.18610

01:15:28.724 OnDoEasyEvent: Position opened

01:15:28.724 - Position opened: 2020.08.20 23:45:28.000 -EURUSD Opened 0.01 Buy #573015194 [0.01 Market-order Buy #573015194] at price 1.18623

01:15:28.263 OnDoEasyEvent: Position opened

01:15:28.257 - Position opened: 2020.08.20 23:45:27.000 -EURUSD Opened 0.01 Buy #573015193 [0.01 Market-order Buy #573015193] at price 1.18623

01:14:49.639 initialized

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 Account ****: **** (****) **** USD, 1:100, Hedge, Demo account MetaTrader 4

01:14:20.357 loaded successfully


What is probably wrong in my testing code?

 
Mohammad Bazrkar :

Hello @Artyom Trishkin , thank you for this code.

I tried to run modified version in MT4 with no testing buttons to listen to Account/Trade events. but I see wrong result when I opened and closed 3 trades:

01:17:13.849 removed

01:17:13.733 uninit reason 4

01:15:36.860 OnDoEasyEvent: Position closed

01:15:36.860 - Position closed: 2020.08.20 23:45:29.000 -EURUSD Closed Sell #573015195 at price 1.18610, Profit -0.13 USD

01:15:35.489 CEventsCollection::CreateNewEvent, Line 768: This event already in the list

01:15:35.019 OnDoEasyEvent: Position closed

01:15:35.019 - Position closed: 2020.08.20 23:45:27.000 -EURUSD Closed Buy #573015193 at price 1.18623, Profit -0.13 USD

01:15:30.144 OnDoEasyEvent: Position opened

01:15:30.144 - Position opened: 2020.08.20 23:45:29.000 -EURUSD Opened 0.01 Sell #573015195 [0.01 Market-order Sell #573015195] at price 1.18610

01:15:28.724 OnDoEasyEvent: Position opened

01:15:28.724 - Position opened: 2020.08.20 23:45:28.000 -EURUSD Opened 0.01 Buy #573015194 [0.01 Market-order Buy #573015194] at price 1.18623

01:15:28.263 OnDoEasyEvent: Position opened

01:15:28.257 - Position opened: 2020.08.20 23:45:27.000 -EURUSD Opened 0.01 Buy #573015193 [0.01 Market-order Buy #573015193] at price 1.18623

01:14:49.639 initialized

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 Account ****: **** (****) **** USD, 1:100, Hedge, Demo account MetaTrader 4

01:14:20.357 loaded successfully


What is probably wrong in my testing code?

I don't know what you changed there ...

 

I cut some codes related to buttons, but it is possible to removed some necessary codes. I attach it now.

But, do you agree that error for not capturing SECOND Closing Buy position should not relate to my changes ?

Files:
mhdbzr-34.mq4  46 kb
 
Mohammad Bazrkar :

I cut some codes related to buttons, but it is possible to removed some necessary codes. I attach it now.

But, do you agree that error for not capturing SECOND Closing Buy position should not relate to my changes ?

Does the test advisor work without errors?

 
Artyom Trishkin:

Does the test advisor work without errors?

I run the modified EA to watch my events, then manually place some trades and close them as fast as possible.
I don't use testing EA to open/close position.


those `CAccount` errors appear in default TestDoEasyPart34.ex4

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

 
Mohammad Bazrkar:
I run the modified EA to watch my events, then manually place some trades and close them as fast as possible.
I don't use testing EA to open/close position.


those `CAccount` errors appear in default TestDoEasyPart34.ex4

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

01:14:22.120 CAccount::Load: Could not create object structure from uchar array

Yes, they will. Because there the approach to storing your account data has been changed. This is written at the end of the article as a warning:
-----
For your information:
since we changed the structure of the account object (changed the size of the uchar arrays to store the string properties of the account and added one more integer property), all previously saved files of the account objects will no longer be loaded correctly. If they are in the common folder of terminals in the \Files\DoEasy\Accounts\ directory, then they must all be deleted before starting this test advisor - they will be recreated when switching from one account to another with a new size of the object structure.
-----
 
Artyom Trishkin:
Yes, they will. Because there the approach to storing your account data has been changed. This is written at the end of the article as a warning:
-----
For your information:
since we changed the structure of the account object (changed the size of the uchar arrays to store the string properties of the account and added one more integer property), all previously saved files of the account objects will no longer be loaded correctly. If they are in the common folder of terminals in the \Files\DoEasy\Accounts\ directory, then they must all be deleted before starting this test advisor - they will be recreated when switching from one account to another with a new size of the object structure.
-----

I delete all Account BIN files in the common folder to be sure your code load completely. it loads with no error about `CAccount::Load` (but main problem was wrong event ignoring and still exists)

Then I opened some trades manually and close them by a script as fast as possible. I got this error again:

CEventsCollection::CreateNewEvent, Line 768: This event already in the list

we know those events were from different position closing in MT4, but `engine` code can not understand it. Is the engine able to understand multiple trade close / events in a short time at all?


Here is all log messages:

2020.08.21 21:23:50.604 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:50.600 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:47.000 -

AUDCAD Closed Buy #573142748 at price 0.94405, Profit -0.22 USD

2020.08.21 21:23:50.368 Script CloseTradesFast4Test AUDCAD,H1: removed

2020.08.21 21:23:50.364 CloseTradesFast4Test AUDCAD,H1: uninit reason 0

2020.08.21 21:23:50.364 CloseTradesFast4Test AUDCAD,H1: close #573142747 buy 0.01 AUDCAD at 0.94404 at price 0.94375

2020.08.21 21:23:50.148 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:50.146 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:47.000 -

AUDCAD Closed Buy #573142748 at price 0.94405, Profit -0.22 USD

2020.08.21 21:23:49.942 CloseTradesFast4Test AUDCAD,H1: close #573142748 buy 0.01 AUDCAD at 0.94405 at price 0.94376

2020.08.21 21:23:49.725 mhdbzr-34 AUDCAD,H1: CEventsCollection::CreateNewEvent, Line 768: This event already in the list

2020.08.21 21:23:49.520 CloseTradesFast4Test AUDCAD,H1: close #573142752 buy 0.01 AUDCAD at 0.94406 at price 0.94376

2020.08.21 21:23:49.257 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:49.255 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:52.000 -

AUDCAD Closed Sell #573142755 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:49.086 CloseTradesFast4Test AUDCAD,H1: close #573142755 sell 0.01 AUDCAD at 0.94366 at price 0.94414

2020.08.21 21:23:48.815 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:48.812 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:53.000 -

AUDCAD Closed Sell #573142757 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:48.661 CloseTradesFast4Test AUDCAD,H1: close #573142756 sell 0.01 AUDCAD at 0.94366 at price 0.94415

2020.08.21 21:23:48.352 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:48.350 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:53.000 -

AUDCAD Closed Sell #573142757 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:48.237 CloseTradesFast4Test AUDCAD,H1: close #573142757 sell 0.01 AUDCAD at 0.94366 at price 0.94415

2020.08.21 21:23:47.537 CloseTradesFast4Test AUDCAD,H1: initialized

2020.08.21 21:23:47.520 Script CloseTradesFast4Test AUDCAD,H1: loaded successfully

2020.08.21 21:23:31.762 Automated trading enabled

 
Mohammad Bazrkar :

I delete all Account BIN files in the common folder to be sure your code load completely. it loads with no error about `CAccount::Load` (but main problem was wrong event ignoring and still exists)

Then I opened some trades manually and close them by a script as fast as possible. I got this error again:

CEventsCollection::CreateNewEvent, Line 768: This event already in the list

we know those events were from different position closing in MT4, but `engine` code can not understand it. Is the engine able to understand multiple trade close / events in a short time at all?

Please describe step by step what you are doing to get this error.

 
Artyom Trishkin:

Please describe step by step what you are doing to get this error.

 I modified your EA code to only get events. (I attached the modified version in previous messages, it is named `mhdbzr-34`)

  1. run the EA to start watching.
  2. open some buy/sell trades in demo account with same lots, fast. (by clicking fast on one-click buy/sell button on the chart)
  3. run a script to close those trades as fast as possible. (my script name is `CloseTradesFast4Test`)
  4. check log messages, you see the engine missed some position closing probably with that red error message I wrote here.
 
Mohammad Bazrkar :

 I modified your EA code to only get events. (I attached the modified version in previous messages, it is named `mhdbzr-34`)

  1. run the EA to start watching.
  2. open some buy/sell trades in demo account with same lots, fast. (by clicking fast on one-click buy/sell button on the chart)
  3. run a script to close those trades as fast as possible. (my script name is ` CloseTradesFast4Test `)
  4. check log messages, you see the engine missed some position closing probably with that red error message I wrote here.

Give your script to close positions.