Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1432

 
jeremy10p100 # :

Hello everyone,

I'm new so I hope I'm writing in the right place: I'm looking to code an EA that could help me with my money management, like :

"close all trades when past losses+current drawdown are below x%" + "close all trades immediately until end of session/day".

If anyone knows how to do this or can help me it would be great to get in touch.

Thanks, trade well ;)

Hello,

Try searching the market for ready-made solutions - https://www.mql5.com/fr/search#!keyword=money%20management&module=mql5_module_market

You can also order a customised solution from Freelance - https://www.mql5.com/fr/job

 
Hello everyone!!! Please tell me how can I copy values from an array into the database? Here is the code from the example of working with the database and when I try to insert data from the array into the database in the SALARY field, I get an error when running the script. And another question, and if for example I have an array with the data of orders and there are quite a lot of them, how can I move all this array into a table in the database?
 VALUES (1,'Paul',a,'California',29000.00)  -->  VALUES (1,'Paul',a,'California',Mass[0])

void OnStart()
  {
  double Mass[4]={1,2,5,6};
  int a =222;
  Alert(Mass[3]);
   string filename="company.sqlite";
//--- создадим или откроем базу данных в общей папке терминалов
   int db=DatabaseOpen(filename, DATABASE_OPEN_READWRITE | DATABASE_OPEN_CREATE |DATABASE_OPEN_COMMON);
   if(db==INVALID_HANDLE)
     {
      Print("DB: ", filename, " open failed with code ", GetLastError());
      return;
     }
//--- если таблица COMPANY существует, то удалим её
   if(DatabaseTableExists(db, "COMPANY_2"))
     {
      //--- удаляем таблицу
      if(!DatabaseExecute(db, "DROP TABLE COMPANY_2"))
        {
         Print("Failed to drop table COMPANY with code ", GetLastError());
         DatabaseClose(db);
         return;
        }
     }
//--- создаем таблицу COMPANY
   if(!DatabaseExecute(db, "CREATE TABLE COMPANY_2("
                       "ID INT PRIMARY KEY     NOT NULL,"
                       "NAME           TEXT    NOT NULL,"
                       "AGE            INT     NOT NULL,"
                       "ADDRESS        CHAR(50),"
                       "SALARY         REAL );"))
     {
      Print("DB: ", filename, " create table failed with code ", GetLastError());
      DatabaseClose(db);
      return;
     }

//--- вставляем данные в таблицу
   if(!DatabaseExecute(db, "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1,'Paul',a,'California',Mass[0]); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2,'Allen',25,'Texas',Mass[1]); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3,'Teddy',a,'Norway',Mass[2]);"
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4,'Mark',25,'Rich-Mond',65000.00);"))
     {
      Print("DB: ", filename, " insert failed with code ", GetLastError());
      DatabaseClose(db);
      return;
     }

//--- создадим запрос и получим хендл на него
   int request=DatabasePrepare(db, "SELECT * FROM COMPANY_2");
   if(request==INVALID_HANDLE)
     {
      Print("DB: ", filename, " request failed with code ", GetLastError());
      DatabaseClose(db);
      return;
     }

   DatabasePrint(
      db,          // хендл базы данных, полученный в DatabaseOpen
      "COMPANY_2",      // таблица или SQL-запрос
      0              // комбинация флагов
   );
  }
//+------------------------------------------------------------------+


 

Try doing it this way, for example.

Instead of

if(!DatabaseExecute(db, "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1,'Paul',a,'California',Mass[0]); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2,'Allen',25,'Texas',Mass[1]); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3,'Teddy',a,'Norway',Mass[2]);"
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4,'Mark',25,'Rich-Mond',65000.00);"))

write

if(!DatabaseExecute(db, "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1,'Paul',a,'California'," + DoubleToString(Mass[0]) + "); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2,'Allen',25,'Texas'," + DoubleToString(Mass[1]) + "); "
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3,'Teddy',a,'Norway'," + DoubleToString(Mass[2]) + ");"
                       "INSERT INTO COMPANY_2 (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4,'Mark',25,'Rich-Mond',65000.00);"))

This is not the only way.

 
Yuriy Bykov #:

Try doing it like this, for example.

Instead of

write

It's not the only way.

Thank you! It works. Where can I read about it? And other ways?

 
lego9955 creates a custom character.

I need to change it so that it creates not just one character, but several characters that are written in the script.

For example, three AB AC AD, in the future any number of this principle.

Cycle with substitution of the next symbol name from the list each time.

Something like:

string sNames = "AB,AC,AD";
string aNames[];

void OnStart()

  {
   int i;
   bool ch=true;

   if(StringSplit(sNames,StringGetCharacter(",",0),aNames) <= 0)return;

   ResetLastError();
   for(i=0; i<ArraySize(aNames); i++)
      {
       ch=ch && CustomSymbolCreate(aNames[i]);
       if(!ch)
         {
          PrintFormat("Symbol '%s' : error %d",aNames[i],GetLastError());
          break; // При первой ошибке выходим
         }
      }
   if(!ch)return;
   ...
  } 
 

I did this before, thought I had done it and deleted my post.

void OnStart()
{
string arr[]= { "AB", "AC", "AD" };

  for(int i=0; i<ArraySize(arr); i++)

  {
   bool ch =CustomSymbolCreate(arr[i]);
   SymbolSelect(arr[i],true); 
  }  
}

When I refreshed the page I saw that you had replied. Yes, the error handling is more correct. Thanks!

 
lego9955 error handling is more correct. Thanks!
ArraySize(arr)

It would be more correct to define in ushort outside the loop

 
lynxntech #:

it would be more correct to define in uint outside the loop

I often do this, but I still want to believe that the compiler can optimise such standard things.

 

The robot has two filters: Stochastic and RSI, each with a set of parameters. Only one filter should be used. I.e. if Stochastic is on, RSI should be off and vice versa.

Now I have done this:

  • I made input variables for Stochastic and RSI, enabling and disabling their work.
  • In the OnInit block I check, if Stochastic and RSI are on at the same time, the error INIT_PARAMETERS_INCORRECT.
  • In the OnInit block I check if Stochastic is switched off, then all its parameters must be zero, otherwise the error INIT_PARAMETERS_INCORRECT/ Similarly with RSI.

Objective:

  • Not to waste time when optimising for incompatible choices. For example, so that if RSI is selected, Stochastic parameters (size of overbought/oversold zones, K, D periods, deceleration, smoothing method, price) are not searched.
  • It turns out that my option doesn't work. During optimisation these variants seem to be over-optimised anyway, which affects the final result of the optimisation.

Please advise how to make the optimiser not to search Stochastic parameters when RSI is enabled and vice versa.

 
lego9955 error handling is more correct. Thanks!

Just in case, it is easier to write them in one line, as I have shown, than an array of separate lines.

Reason: