How can I make this EA only close the orders with any magic number?

 

Hi, I have this EA, and I whant to make it only close trades that have any magic number. How can I do this?


//+------------------------------------------------------------------+
//|                                                        Timer.mq5 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property link      "https://www.mql5.com/ru/market/product/43516"
#property version   "1.000"

//--- display the window of input parameters when launching the script
#property script_show_inputs

//--- parameters for data reading
input string InpFileName="archivo_python.txt"; // file name
input string InpDirectoryName="Files"; // directory name
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
//#include <File.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
//--- input parameters
input uchar    InpAfterHour   = 0; // After: Hour ... (max 255)
input uchar    InpAfterMinutes= 42; // After: Minutes ... (max 255)
input uchar    InpAfterSeconds= 59; // After: Seconds ... (max 255)
//---
int file_handles;
uchar valor_uchar;
long     m_after  = 0;
bool         started=false;     // flag of counter relevance
string str1,str2,str3,str4;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
  str1="C:/Users/jsgas/AppData/Roaming/MetaQuotes/Terminal/D0E8209F77C8CF37AD8BF550E51FF075/MQL5/Files/archivo_";
  str3="_python.txt";
  str2=Symbol();
  str4=str1+str2+str3;

   // Abrir el archivo para lectura
   string file_path = str4;
   file_handles = FileOpen(file_path, FILE_READ|FILE_TXT);
   
   if(file_handles != INVALID_HANDLE)
     {
      PrintFormat("Archivo %s abierto correctamente", file_path);
      
      // Leer el valor uchar desde el archivo
      if (FileReadInteger(file_handles, INT_VALUE))
        {
         PrintFormat("Valor uchar leído desde el archivo: %u", valor_uchar);
         m_after  = 0;
         m_after=valor_uchar;
        }
      else
        {
         Print("Error al leer el valor uchar desde el archivo");
        }

      // Cerrar el archivo después de la lectura
      FileClose(file_handles);
     }
   else
     {
      m_after  = 0;
      m_after=InpAfterHour*60*60+InpAfterMinutes*60+InpAfterSeconds;
      PrintFormat("Error al abrir el archivo %s, Código de error = %d", file_path, GetLastError());
     }
     
     

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
        {
         if(TimeCurrent()-m_position.Time()>=m_after)
            m_trade.PositionClose(m_position.Ticket()); // close a position
        }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| called when a Trade event arrives                                |
//+------------------------------------------------------------------+
void OnTrade()
  {
   if(started) SimpleTradeProcessor();
   
  }
  
 void SimpleTradeProcessor()
  {
  str1="C:/Users/jsgas/AppData/Roaming/MetaQuotes/Terminal/D0E8209F77C8CF37AD8BF550E51FF075/MQL5/Files/archivo_";
  str3="_python.txt";
  str2=Symbol();
  str4=str1+str2+str3;
     // Abrir el archivo para lectura
   string file_path = str4;
   file_handles = FileOpen(file_path, FILE_READ|FILE_TXT);
   
   if(file_handles != INVALID_HANDLE)
     {
      PrintFormat("Archivo %s abierto correctamente", file_path);
      
      // Leer el valor uchar desde el archivo
      if (FileReadInteger(file_handles, INT_VALUE))
        {
         PrintFormat("Valor uchar leído desde el archivo: %u", valor_uchar);
         m_after  = 0;
         m_after=valor_uchar;
        }
      else
        {
         Print("Error al leer el valor uchar desde el archivo");
        }

      // Cerrar el archivo después de la lectura
      FileClose(file_handles);
     }
   else
     {
      m_after  = 0;
      m_after=InpAfterHour*60*60+InpAfterMinutes*60+InpAfterSeconds;
      PrintFormat("Error al abrir el archivo %s, Código de error = %d", file_path, GetLastError());
     }
     
  }
 

The magic number is set to zero in the constructor of CTrade through the method ClearStructures. So there is no NO magic number since the magic is a part of the request structure.

What you want to ask is "is the magic number not zero". Except if you use your own request/result stuff.

 
Javier Santiago Gaston De Iriarte CabreraHi, I have this EA, and I whant to make it only close trades that have any magic number. How can I do this?

You are not posing a problem, you are not even showing your attempts, i.e. you don't know how to program and you expect someone else to do the work for you.

Well, there's no problem with that. Be patient...maybe someone with a lot of free time will help you. Personally I am offended by this kind of postings.

"I don't want to learn to program. You learn and work for me." That has a name...selfishness.

 
Miguel Angel Vico Alba #:

"I don't want to learn to program. You learn and work for me." That has a name...selfishness.

nah... its called laziness.

Javier Santiago Gaston De Iriarte Cabrera:

Hi, I have this EA, and I whant to make it only close trades that have any magic number. How can I do this?


a clue...

if(m_position.Magic()!=MagicNumber)
continue;