trade.PositionClose() is not working - page 2

 

For each program a trade environment is prepared at different stages:

  • In scripts it is possible to work fully in OnStart (),
  • in indicators - in OnCalculate (),
  • in experts - in OnTick ()

 
Vladimir Karputov:

For each program a trade environment is prepared at different stages:

  • In scripts it is possible to work fully in OnStart (),
  • in indicators - in OnCalculate (),
  • in experts - in OnTick ()

The expert version of the test code :

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(),")");
         }
      }
   }
}

The script version of the test code :

void OnStart(void)                { 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(),")");
         }
      }
   }
}

These are the versions I have tested. Feel free to test them too.

 
 

Mistake somewhere in work of standard library (a method of the choice of a position). The similar mistake has emerged in the Russian part of a forum: https://www.mql5.com/ru/forum/99060.

Please give such information: reboot the terminal and copy the first three lines from a tab "Journal". Shall be approximately so:

2016.10.28 08:25:41.372 Terminal        MetaTrader 5 x64 build 1455 started (MetaQuotes Software Corp.)
2016.10.28 08:25:41.374 Terminal        Windows 10 Pro (x64 based PC), IE 11.00, UAC, Intel Core i3-3120M  @ 2.50GHz, RAM: 3698 / 8077 Mb, HDD: 38140 / 233310 Mb, GMT+02:00
2016.10.28 08:25:41.374 Terminal        C:\Users\KVN\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075

 Also specify the name of the trade server to which you are connected, and also account type: hedge or netting.

Терминал не видит позицию
Терминал не видит позицию
  • www.mql5.com
Есть три открытых позиции, PositionsTotal() возвращает 3. Соответственно три прохода цикла...
 

PositionClose(PositionGetSymbol(i),0.1))  add 0.1=deviation 

try it!