Autotrading problem

 
It seems that the autotrade status before an expert or script is attached to chart, is decisive for the runtime of the expert or script and not the status that is set by the user during runtime.

The script below should demonstrate this. The prerequisite is that there are one or more positions and that autotrading is disabled before the script is started.

Then start the script. It first sets a Windows message to enable autotrading in expert settings (found this here).

This is also confirmed in the Expert Journal.

Then waits 3 seconds and performs a CTrade::PositionClose on all positions.


However, this fails with the message "auto trading disabled by client", 
where previously exactly the opposite was reported.

Do I have to put up with this or is there a workaround?

The point is to ensure that all positions can be closed via Expert or script,

so autotrading enabled can be enforced from script/expert.


Thank you for comments

Code:

//+------------------------------------------------------------------+
//| Toggle auto-trading button                                       |
//+------------------------------------------------------------------+
void AlgoTradingStatus(bool Enable)
  {
   bool Status = (bool) TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);

   if(Enable != Status)
     {
      HANDLE hChart = (HANDLE) ChartGetInteger(ChartID(), CHART_WINDOW_HANDLE);
      PostMessageW(GetAncestor(hChart, GA_ROOT), WM_COMMAND, MT_WMCMD_EXPERTS, 0);
     }
  }  
void closeAllPositions(void)
{
   if(PositionsTotal() == 0)
      return;
   int nbrPos=PositionsTotal();
   string symbols[];
   ArrayResize(symbols,nbrPos);

   // Collect symbols   
        for(int i=0;i < nbrPos ;++i)
        {
                symbols[i]=PositionGetSymbol(i);
   }
   
   // Close symbols
   
   for(int i=0;i < nbrPos ;++i)
   {
      ResetLastError();
      CTrade td;
      if(td.PositionClose( symbols[i],INT_MAX) == false)
      {
         PrintFormat("#error: close pos for %s.  code=%d",symbols[i],GetLastError());                   
      }
   }

}  
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   
   bool Status = (bool) TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
   PrintFormat("AutoTrading %s",(Status == true ? "on" : "off"));   

   AlgoTradingStatus(true);   
   Print("AutoTrading on");   
   Sleep(3000);
   closeAllPositions();
}
 
Do you know that autotrading (algo trading) is not just a global on/off but also each program has its own on/off?

If autotrading is globally off when program is started, by default program will have autotrading disabled.

By opposite, if autotrading is globally on when the program is started, program will have autotrading enabled by default.

So, yes, autotrading global status when you start a program is important, but for a specific reason...

Your script seems that enable autotrading global (like pushing the button) but you should care about the "algo trading" tick on the program your run also!
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Fernando Carreiro #:
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

Makes sense, thank you.

 
Fabio Cavalloni #:
Do you know that autotrading (algo trading) is not just a global on/off but also each program has its own on/off?

If autotrading is globally off when program is started, by default program will have autotrading disabled.

By opposite, if autotrading is globally on when the program is started, program will have autotrading enabled by default.

So, yes, autotrading global status when you start a program is important, but for a specific reason...

Your script seems that enable autotrading global (like pushing the button) but you should care about the "algo trading" tick on the program your run also!

Thank you for reply. Have 2 questions:

1. How to enable/disable autotrading on expert level?

2. Does this mean you can't programmatically disable autotrading for all experts?

 

Cornelius Eichhorst #:Thank you for reply. Have 2 questions:

1. How to enable/disable autotrading on expert level?

2. Does this mean you can't programmatically disable autotrading for all experts?

1. When you attach an EA to the chart, it's in the "Common" section ...


2. In normal MQL programming, you have no access to control (enable/disable) that, because it would be a security breach of the user's preferences. It is up to the user to decide what they want to do, not up to the EA to enforce it.

 
Fernando Carreiro #:

1. When you attach an EA to the chart, it's in the "Common" section ...


2. In normal MQL programming, you have no access to control (enable/disable) that, because it would be a security breach of the user's preferences. It is up to the user to decide what they want to do, not up to the EA to enforce it.

Thanks for reply.