EA study book MT5 Help Andrew R. Young "Closing positions"

 

For those whove read it im trying to use the Trade.mqh include file.

In the Trade.mqh theres a segment for closing a position "CTrade::Close"

Ive been trying to use it but its not working. Heres my attempt

 


 

  void OnTimer()
{
//+------------------------------------------------------------------+
//|Close Timer                                                       |
//+------------------------------------------------------------------+
Print("OnTimer Initiated");





do Sleep(100); while(PositionSelect(_Symbol) == false);
Trade.Close(_Symbol);







EventKillTimer();


}

 

The aim of this was to close a position "X" ammount of time after opening it. 

 

 

[UPDATE] 

The OnTimer part works. Here is segment of log where trade is failed :

 

RR      0       20:31:16.753    Core 1  2016.01.04 00:05:01   instant buy 0.01 EURUSD at 1.0876 (1.0873 / 1.0876 / 1.0873)
II      0       20:31:16.753    Core 1  2016.01.04 00:05:01   deal #2 buy 0.01 EURUSD at 1.0876 done (based on order #2)
KK      0       20:31:16.753    Core 1  2016.01.04 00:05:01   deal performed [#2 buy 0.01 EURUSD at 1.0876]
ON      0       20:31:16.753    Core 1  2016.01.04 00:05:01   order performed buy 0.01 at 1.0876 [#2 buy 0.01 EURUSD at 1.0876]
ES      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Open buy order #2: 10009 - Request is completed, Volume: 0.01, Price: 1.0876, Bid: 1.0873, Ask: 1.0876
JD      0       20:31:16.753    Core 1  2016.01.04 00:05:01   position modified [#2 buy 0.01 EURUSD 1.0876 sl: 1.0676 tp: 1.1076]
RP      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Modify position: 10009 - Request is completed, SL: 1.0676, TP: 1.1076, Bid: 1.0873, Ask: 1.0876, Stop Level: 3
OE      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Buy Placed
EQ      0       20:31:16.753    Core 1  2016.01.04 00:10:01   OnTimer Initiated
FD      2       20:31:16.753    Core 1  2016.01.04 00:10:01   failed instant sell 0.01 EURUSD at 1.0872 sl: 1.0676 tp: 1.1076 [Invalid stops]
CF      0       20:31:16.753    Core 1  2016.01.04 00:10:01   Alert: Close position: Error 10016 - Invalid stops in the request
QE      0       20:31:16.753    Core 1  2016.01.04 00:10:01   Close Buy position #0: 10016 - Invalid stops in the request, Volume: 0.0, Price: 0.0, Bid: 0.0, Ask: 0.0


 Invalid stops.... hmm....

How do I set stoploss and take profit to nothing? just "NULL"? 

 
Ah sweet all sorted thanks me 
 
ReillyFox:

For those whove read it im trying to use the Trade.mqh include file.

In the Trade.mqh theres a segment for closing a position "CTrade::Close"

Where it comes from ? I don't see such method.

To close a position you have to use trade.PositionClose() function.

 
Alain Verleyen:

Where it comes from ? I don't see such method.

To close a position you have to use trade.PositionClose() function.

It is from a commercial book. You can download the source from here by the looks of things.
 
RR      0       20:31:16.753    Core 1  2016.01.04 00:05:01   instant buy 0.01 EURUSD at 1.0876 (1.0873 / 1.0876 / 1.0873)
II      0       20:31:16.753    Core 1  2016.01.04 00:05:01   deal #2 buy 0.01 EURUSD at 1.0876 done (based on order #2)
KK      0       20:31:16.753    Core 1  2016.01.04 00:05:01   deal performed [#2 buy 0.01 EURUSD at 1.0876]
ON      0       20:31:16.753    Core 1  2016.01.04 00:05:01   order performed buy 0.01 at 1.0876 [#2 buy 0.01 EURUSD at 1.0876]
ES      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Open buy order #2: 10009 - Request is completed, Volume: 0.01, Price: 1.0876, Bid: 1.0873, Ask: 1.0876
JD      0       20:31:16.753    Core 1  2016.01.04 00:05:01   position modified [#2 buy 0.01 EURUSD 1.0876 sl: 1.0676 tp: 1.1076]
RP      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Modify position: 10009 - Request is completed, SL: 1.0676, TP: 1.1076, Bid: 1.0873, Ask: 1.0876, Stop Level: 3
OE      0       20:31:16.753    Core 1  2016.01.04 00:05:01   Buy Placed
EQ      0       20:31:16.753    Core 1  2016.01.04 00:10:01   OnTimer Initiated
FD      2       20:31:16.753    Core 1  2016.01.04 00:10:01   failed instant sell 0.01 EURUSD at 1.0872 sl: 1.0676 tp: 1.1076 [Invalid stops]
CF      0       20:31:16.753    Core 1  2016.01.04 00:10:01   Alert: Close position: Error 10016 - Invalid stops in the request
QE      0       20:31:16.753    Core 1  2016.01.04 00:10:01   Close Buy position #0: 10016 - Invalid stops in the request, Volume: 0.0, Price: 0.0, Bid: 0.0, Ask: 0.0


 Invalid stops.... hmm....

When opening a SELL order at 1.0872:

  • you can't set the SL at a lower price (1.0676) 
  • you can't set the TP at a higher price (1.1076)

 
honest_knave:
It is from a commercial book. You can download the source from here by the looks of things.
Ah ok. Thanks.