trade.PositionClose() is not working

 

Hello Friends

 I have a interesting problem.  This is my source code. This codes are work when i use it in mql5 script file. I can close all positions. but this same codes are not working in expert advisor. I got error like in screenshot. it is really nonsensical problem. I open and close position manually. also script works. but i cant close from expert advisor. I need your ideas. Thanks

http://prntscr.com/czaibb 

 

 

void CloseAll()
{
   CTrade trade;
   for (int i=PositionsTotal()-1;i>=0; i--)
   {
      {                
         if(!trade.PositionClose(PositionGetSymbol(i),5))
         {
            Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         } else {
            Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
         }
      }
   }

}
Ekran Görüntüsü
Ekran Görüntüsü
  • prnt.sc
Lightshot ile Yakalanmış
 

Possibly the server just didn't work: pay attention to pauses between trade orders.

Pauses are equal to several seconds - it is much. 

 
Vladimir Karputov:

Possibly the server just didn't work: pay attention to pauses between trade orders.

Pauses are equal to several seconds - it is much. 

Server not working ? this is a backtest.

Pause several seconds ? What's that ?

 
Hakan Sari:

Hello Friends

 I have a interesting problem.  This is my source code. This codes are work when i use it in mql5 script file. I can close all positions. but this same codes are not working in expert advisor. I got error like in screenshot. it is really nonsensical problem. I open and close position manually. also script works. but i cant close from expert advisor. I need your ideas. Thanks

...
This error comes from CTrade class, you are trying to close a position which can not be found.
 
Alain Verleyen:

1. Server not working ? this is a backtest.

2. Pause several seconds ? What's that ?

1. Possibly the server can emulate different real situations (for example reject).

2. In the screenshot: 

picture 

 
And another question: what type of account - hedge or netting?
 
Vladimir Karputov:

1. Possibly the server can emulate different real situations (for example reject).

There is no server in a backtest.

2. In the screenshot: 

 

Ok I see what you mean, the EA is very slow.

The problem comes from

bool CTrade::PositionClose(const string symbol,const ulong deviation)
  {
...
   uint retcode      =TRADE_RETCODE_REJECT;
...
      else
        {
         //--- position not found
         m_result.retcode=retcode;
         return(false);
        }
...
The OP need to search why some positions were not found.
 

The code to test the theory:

//+------------------------------------------------------------------+
//|                                                       TestEA.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#include <Trade\Trade.mqh>
#property tester_file "fractals.csv"
//---
CTrade my_trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   static long counter=0;
   counter++;
   if(counter%300==0 && counter<3000)
      my_trade.Buy(0.01,NULL);
   if(counter>3000)
      CloseAll();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAll()
  {
   CTrade trade;
   for(int i=PositionsTotal()-1;i>=0; i--)
     {
        {
         if(!trade.PositionClose(PositionGetSymbol(i),5))
           {
            Print(PositionGetSymbol(i),"PositionClose() method failed. Return code=",trade.ResultRetcode(),
                  ". Code description: ",trade.ResultRetcodeDescription());
              } else {
            Print(PositionGetSymbol(i),"PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
                  " (",trade.ResultRetcodeDescription(),")");
           }
        }
     }
  }
//+------------------------------------------------------------------+

On hedge account open nine positions. Then they all closed - and there is no error. 

OE      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #2 EURUSD at 1.08716 [done at 1.08716]
KE      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
JE      0       12:45:29.539    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #3 (1.08716 / 1.08728 / 1.08716)
OE      0       12:45:29.539    Trades  2016.10.25 01:36:40   deal #12 sell 0.01 EURUSD at 1.08716 done (based on order #12)
EG      0       12:45:29.539    Trade   2016.10.25 01:36:40   deal performed [#12 sell 0.01 EURUSD at 1.08716]
KR      0       12:45:29.539    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#12 sell 0.01 EURUSD at 1.08716]
NQ      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #3 EURUSD at 1.08716 [done at 1.08716]
MQ      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
EI      0       12:45:29.539    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #4 (1.08716 / 1.08728 / 1.08716)
EH      0       12:45:29.539    Trades  2016.10.25 01:36:40   deal #13 sell 0.01 EURUSD at 1.08716 done (based on order #13)
CS      0       12:45:29.539    Trade   2016.10.25 01:36:40   deal performed [#13 sell 0.01 EURUSD at 1.08716]
NN      0       12:45:29.539    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#13 sell 0.01 EURUSD at 1.08716]
QM      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #4 EURUSD at 1.08716 [done at 1.08716]
OM      0       12:45:29.539    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
FM      0       12:45:29.540    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #5 (1.08716 / 1.08728 / 1.08716)
QL      0       12:45:29.540    Trades  2016.10.25 01:36:40   deal #14 sell 0.01 EURUSD at 1.08716 done (based on order #14)
KO      0       12:45:29.540    Trade   2016.10.25 01:36:40   deal performed [#14 sell 0.01 EURUSD at 1.08716]
GJ      0       12:45:29.540    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#14 sell 0.01 EURUSD at 1.08716]
RI      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #5 EURUSD at 1.08716 [done at 1.08716]
CI      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
EQ      0       12:45:29.540    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #6 (1.08716 / 1.08728 / 1.08716)
KP      0       12:45:29.540    Trades  2016.10.25 01:36:40   deal #15 sell 0.01 EURUSD at 1.08716 done (based on order #15)
MK      0       12:45:29.540    Trade   2016.10.25 01:36:40   deal performed [#15 sell 0.01 EURUSD at 1.08716]
JF      0       12:45:29.540    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#15 sell 0.01 EURUSD at 1.08716]
QE      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #6 EURUSD at 1.08716 [done at 1.08716]
QE      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
HE      0       12:45:29.540    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #7 (1.08716 / 1.08728 / 1.08716)
MD      0       12:45:29.540    Trades  2016.10.25 01:36:40   deal #16 sell 0.01 EURUSD at 1.08716 done (based on order #16)
GG      0       12:45:29.540    Trade   2016.10.25 01:36:40   deal performed [#16 sell 0.01 EURUSD at 1.08716]
MR      0       12:45:29.540    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#16 sell 0.01 EURUSD at 1.08716]
LQ      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #7 EURUSD at 1.08716 [done at 1.08716]
OQ      0       12:45:29.540    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
KI      0       12:45:29.540    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #8 (1.08716 / 1.08728 / 1.08716)
GH      0       12:45:29.540    Trades  2016.10.25 01:36:40   deal #17 sell 0.01 EURUSD at 1.08716 done (based on order #17)
IS      0       12:45:29.540    Trade   2016.10.25 01:36:40   deal performed [#17 sell 0.01 EURUSD at 1.08716]
LN      0       12:45:29.540    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#17 sell 0.01 EURUSD at 1.08716]
HM      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #8 EURUSD at 1.08716 [done at 1.08716]
NM      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
IL      0       12:45:29.541    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #9 (1.08716 / 1.08728 / 1.08716)
JL      0       12:45:29.541    Trades  2016.10.25 01:36:40   deal #18 sell 0.01 EURUSD at 1.08716 done (based on order #18)
DO      0       12:45:29.541    Trade   2016.10.25 01:36:40   deal performed [#18 sell 0.01 EURUSD at 1.08716]
DJ      0       12:45:29.541    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#18 sell 0.01 EURUSD at 1.08716]
MH      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #9 EURUSD at 1.08716 [done at 1.08716]
LI      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
OQ      0       12:45:29.541    Trade   2016.10.25 01:36:40   instant sell 0.01 EURUSD at 1.08716, close #10 (1.08716 / 1.08728 / 1.08716)
RQ      0       12:45:29.541    Trades  2016.10.25 01:36:40   deal #19 sell 0.01 EURUSD at 1.08716 done (based on order #19)
QK      0       12:45:29.541    Trade   2016.10.25 01:36:40   deal performed [#19 sell 0.01 EURUSD at 1.08716]
KG      0       12:45:29.541    Trade   2016.10.25 01:36:40   order performed sell 0.01 at 1.08716 [#19 sell 0.01 EURUSD at 1.08716]
EG      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   CTrade::OrderSend: instant sell 0.01 position #10 EURUSD at 1.08716 [done at 1.08716]
MF      0       12:45:29.541    TestEA (EURUSD,M1)      2016.10.25 01:36:40   PositionClose() method executed successfully. Return code=10009 (done at 1.08716)
OF      0       12:45:29.938    Tester  final balance 9996.65 USD
Files:
TestEA.mq5  5 kb
 
Vladimir Karputov:

The code to test the theory:

Seems that your original code works correctly in real time too

This is the code used to test (your code is used without any change) :

int OnInit(void)                { return(INIT_SUCCEEDED);}
void OnDeinit(const int reason) {                        }
void OnTick()                   { CloseAll();            }
#include <Trade\Trade.mqh>
void CloseAll()
{
   CTrade trade;
   for (int i=PositionsTotal()-1;i>=0; i--)
   {
      {                
         if(!trade.PositionClose(PositionGetSymbol(i),5))
         {
            Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         } else {
            Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
         }
      }
   }
}

And this is the result (tested it both as an expert and as a script - for the script test only change was that CloseAll() call was moved to OnStart())


 
Mladen Rakic:

... call was moved to OnStart())

It is your main mistake.

 
Vladimir Karputov:

It is your main mistake.

Why? I wanted to test it (the original code from the first post) as a script too, not just like an EA, and in the script the OnStart() is executed when you drop it on chart.

There is no mistake (in either version - code or execution wise). I did not get an execution error in either version and the positions were properly closed


PS: in order to avoid confusion - I was referring to the first post (the code of it) - this post

Hakan Sari:

Hello Friends

 I have a interesting problem.  This is my source code. This codes are work when i use it in mql5 script file. I can close all positions. but this same codes are not working in expert advisor. I got error like in screenshot. it is really nonsensical problem. I open and close position manually. also script works. but i cant close from expert advisor. I need your ideas. Thanks

http://prntscr.com/czaibb 

 

 

void CloseAll()
{
   CTrade trade;
   for (int i=PositionsTotal()-1;i>=0; i--)
   {
      {                
         if(!trade.PositionClose(PositionGetSymbol(i),5))
         {
            Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         } else {
            Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
         }
      }
   }

}

the quoted post was wrong in my post

Reason: