OnTradeTransaction not called while optimization is enabled

Vladimir Karputov  
ipsec :

Hi all,


I wrote a EA using the event OnTradeTransaction . While running my EA without optimization its works fine.

If I run my EA with optimization enabled the OnTradeTransaction is not called.

Someone know why?

I'm using build 2345


Best regards,

Fernando Ribeiro

Show your MQL5 code - you need to try to reproduce the result. I have no problems with OnTradeTransaction.

ipsec  

Hi Vladimir Karputov,


This code create a file "test.txt" inside the Common folder with content if OnTradeTransaction has been called (check attached file for example).

Attached too the return of optimization table and graph to confirm transactions has been made.


Thanks.


My code:

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

input int epochs = 1; // Epochs

CTrade  trade;
bool  one_position_only;
int   h;

int OnInit()
  {
   one_position_only=false;
   h=FileOpen("test.txt",FILE_WRITE|FILE_ANSI|FILE_TXT|FILE_COMMON);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   FileClose(h);
  }

void OnTick()
  {
   if(!one_position_only)
     {
      if(!trade.Buy(SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN)))
        {
         Print("Buy() method failed. Return code=",trade.ResultRetcode(),
               ". Descrição do código: ",trade.ResultRetcodeDescription());
        }
      else
        {
         Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
               " (",trade.ResultRetcodeDescription(),")");
        }
     }
  }

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   if(h==INVALID_HANDLE)
     {
      Alert("Error opening file");
      return;
     }

   switch(trans.type)
     {
      case TRADE_TRANSACTION_DEAL_ADD:
        {
         FileWrite(h,"Called TRADE_TRANSACTION_DEAL_ADD");
        }
      break;
      default:
        {
         FileWrite(h,"Called " + IntegerToString(trans.deal));
        }
      break;
     }

   Alert("File created");
   one_position_only = true;
  }
Alain Verleyen  
ipsec:

Hi Vladimir Karputov,


This code create a file "test.txt" inside the Common folder with content if OnTradeTransaction has been called (check attached file for example).

Attached too the return of optimization table and graph to confirm transactions has been made.


Thanks.


My code:

PICNIC issue.

There is no problem with OnTradeTransaction() and optimization. But there is a problem in your way to deal with file on a multi-threaded (optimization) environment. Fix your code.

ipsec  
Alain Verleyen:

PICNIC issue.

There is no problem with OnTradeTransaction() and optimization. But there is a problem in your way to deal with file on a multi-threaded (optimization) environment. Fix your code.

Hi Alain,


Its not a PICNIC issue. Can be a user error (I'm not an inerrable person).


This code is to reproduce the result. I have used text file to reduce the complexity of my original code.

My original code have a single line calling a function (from a DLL) to send data (rest post) to another server.


My original code is:

   double arr[];
   ArrayResize(arr, 15);
   ArrayInitialize(arr, 1.0);

   Populate(Symbol(), Period(), 15, arr, 2, 1.0, arr);


I put local variables (arr) to test the OnTradeTransaction.

While running with non optimization I got my data ([array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), 2, 1, array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])]) in my local server.

While running with optimization no requests (http requests) are done.


Maybe the DLL function cannot be called with optimization in the OnTradeTransaction (i don't found this restriction), but the same method works in another events, like onTick with optimization.


Have a nice day.

Alain Verleyen  
ipsec:

Hi Alain,


Its not a PICNIC issue. Can be a user error (I'm not an inerrable person).


This code is to reproduce the result. I have used text file to reduce the complexity of my original code.

My original code have a single line calling a function (from a DLL) to send data (rest post) to another server.


My original code is:


I put local variables (arr) to test the OnTradeTransaction.

While running with non optimization I got my data ([array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), 2, 1, array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])]) in my local server.

While running with optimization no requests (http requests) are done.


Maybe the DLL function cannot be called with optimization in the OnTradeTransaction (i don't found this restriction), but the same method works in another events, like onTick with optimization.


Have a nice day.

When something is not going as the user want, and he is thinking the problem comes from the "outside", it's what I called a PICNIC issue. In 99% of the case, the problem originated in the user misunderstanding or error. (And it happened to everyone, even the best). I didn't want to offend you.

Though I am sure (because I tested) that there is no problem with OnTradeTransaction and optimization.

About DLL call while optimizing :

A second limitation - the prohibition on the use of DLL when testing EAs. DLL calls are absolutely forbidden on remote agents for security reasons. On local agent, DLL calls in tested EAs are allowed only with the appropriate permission "Allow import DLL".

About WebRequest and Strategy Tester :

The Alert(), MessageBox(), PlaySound(), SendFTP(), SendMail(), SendNotification() and WebRequest() functions designed for interaction with the "outside world" are not executed in the Strategy Tester.


Documentation on MQL5: MQL5 programs / Testing Trading Strategies
Documentation on MQL5: MQL5 programs / Testing Trading Strategies
  • www.mql5.com
The idea of automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
ipsec  
Alain Verleyen:

When something is not going as the user want, and he is thinking the problem comes from the "outside", it's what I called a PICNIC issue. In 99% of the case, the problem originated in the user misunderstanding or error. (And it happened to everyone, even the best). I didn't want to offend you.

Though I am sure (because I tested) that there is no problem with OnTradeTransaction and optimization.

About DLL call while optimizing :

About WebRequest and Strategy Tester :



Hi Alain,

I put my DLL to send data to Event Viewer. Let me explain it.


My log return:

{"symbol":"E","period":1,"number_of_features":15,"state":[0.9,0.884,0.87,0.539,0.522,0.507,0.132,0.1,0.153,8,14,26,-1,-1,0],"next_state":[],"action":2,"reward":0}


Look the next_state array is empty, the action must be != 2 and reward is default 0. Just the default values.


A right log must be like this:

{"symbol":"E","period":1,"number_of_features":15,"state":[0.9,0.885,0.87,0.726,0.703,0.68,0.1,0.141,0.122,15,23,29,-1,0,0],"next_state":[0.9,0.884,0.869,0.745,0.72,0.697,0.24,0.1,0.134,19,18,20,-1,-1,-1],"action":0,"reward":-0.41}


I'm using a struct to store this data (global variable). So I think the threads are updating it and making a mess.

So you are right. My EA don't works with multi-thread environment.

Now I ask you.

Is possible to disable multi-thread? I don't need this.

I need to run the same code (EA) n times, without change parameters. Just iterate 20000 times.


Best Regards,

Fernando

Alain Verleyen  
ipsec:

Hi Alain,

I put my DLL to send data to Event Viewer. Let me explain it.


My log return:


Look the next_state array is empty, the action must be != 2 and reward is default 0. Just the default values.


A right log must be like this:



I'm using a struct to store this data (global variable). So I think the threads are updating it and making a mess.

So you are right. My EA don't works with multi-thread environment.

Now I ask you.

Is possible to disable multi-thread? I don't need this.

I need to run the same code (EA) n times, without change parameters. Just iterate 20000 times.


Best Regards,

Fernando

It will slow down the optimization but if it's what you want you can do it by enabling only 1 agent.
Reason: