Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 852

 
Can't access the website
 

Hi developers,

View any strategy tester with custom date or any date option

See image:

strategy tester

I want me to get the start and end date values in my program in OnInit () function.

How can I get it?

 
Artyom Trishkin:

Form the whole loop into a function, and return the bar number from it if found, or WRONG_VALUE if not found.


Good afternoon. I think I have finished working with yesterday's problematic iCustom. I have made it all in the form of a function and used"Comment" and "Print" for control.

The idea of this trial Expert Advisor is to catch signals in the form of up/down arrows of the iCrossAD indicator and convert them to BUY or SELL command to be used in a future program.

I have a little experience, so please don't judge, but well-reasoned criticism and advice would be appreciated.

In fact, for the sake of this and wrote a post. EA and indicator files attached, the code below.

//+------------------------------------------------------------------+
//|                                                  Test_iCusom.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                             https://mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property description ""
#property strict
//--- includes
#include <DoEasy\Engine.mqh>
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
//---
enum Indicator_Direction
   {
   Direction_BUY,
   Direction_SELL,
   Direction_FLAT
   };
//---
input string   Inp_param_indi_iCrossAD = "Input parameters indicator iCrossAD";//----- "Внешние параметры индикатора iCrossAD" -----
input uint     InpPeriodFind           = 400;                 // Bars for calculate
input uint     InpUnheckedBars         = 2;                   // Unchecked bars
input uint     InpPeriodIND            = 21;                  // CCI period

//--- global variables

CEngine        engine;
CTrade         trade;
CPositionInfo  apos;
CSymbolInfo    asymbol;

int            CrossAD;                           //Хэндл индикатора iCrossAD

double         Buf_Arrow_Sell[],                  //Массив буфера для приема значений последних стрелок ВНИЗ из индикатора iCrossAD
               Last_Arrow_Sell_volume,            //Переменная для записи значения цены последней стрелки ВНИЗ индикатора iCrossAD
               Last_Arrow_Sell_index;             //Переменная для записи значения индекса свечи последней стрелки ВНИЗ индикатора iCrossAD
datetime       Last_Arrow_Buy_time;               //Переменная для записи времени стрелки
               
double         Buf_Arrow_Buy[], Last_Arrow_Buy_volume, Last_Arrow_Buy_index;
datetime       Last_Arrow_Sell_time;
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArraySetAsSeries(Buf_Arrow_Buy, true);
   ArraySetAsSeries(Buf_Arrow_Sell, true);
//---
   CrossAD = iCustom(asymbol.Name(), _Period, "iCrossAD",InpPeriodFind,InpUnheckedBars,InpPeriodIND);
   if (CrossAD == INVALID_HANDLE)
   {
      Print("Не удалось создать описатель индикатора iCrossAD!");
      return(INIT_FAILED);
   }
      else Print("Хендл iCrossAD = ",CrossAD);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- delete objects
   ObjectsDeleteAll(0,"",-1);
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   string direction = "no information";
   switch(iCustom_iCrossAD(InpPeriodFind))
      {
      case Direction_BUY: direction = "BUY";
         break;
      case Direction_SELL: direction = "SELL";
         break;
      case Direction_FLAT: direction = "FLAT";
         break;
      case WRONG_VALUE: direction = "no information";
         break;   
      }
   Comment("-------------------------", 
            "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
            "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
            "\n Last_Arrow_Buy_time         = ",Last_Arrow_Buy_time,
            "\n ---------------------- ",
            "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
            "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index,
            "\n Last_Arrow_Sell_time         = ",Last_Arrow_Sell_time,
            "\n ---------------------- ",
            "\n Indicator_Direction             = ",direction
            ); 
  }
//+------------------------------------------------------------------+
int iCustom_iCrossAD(uint PeriodFind) 
  { 
   Indicator_Direction direct = Direction_FLAT;
   
   if (CopyBuffer(CrossAD, 1, 0, PeriodFind, Buf_Arrow_Buy) != PeriodFind)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return(WRONG_VALUE);
      }
         for(int n=0; n<(int)PeriodFind; n++)
            {
               if(n==0)
                  Print("Last_Arrow_Buy_index n==",n," Last_Arrow_Buy_time = ",iTime(_Symbol,0,n));
               if(Buf_Arrow_Buy[n]==EMPTY_VALUE)
                  Print("Last_Arrow_Buy_index n==",n," Last_Arrow_Buy_time = ",iTime(_Symbol,0,n));
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Buy_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index,", Last_Arrow_Buy_time = ",Last_Arrow_Buy_time);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, PeriodFind, Buf_Arrow_Sell) != PeriodFind)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return(WRONG_VALUE);
      }
         for(int n=0; n<(int)PeriodFind; n++)
            {
               if(n==0)
                  Print("Last_Arrow_Sell_index n==",n," Last_Arrow_Sell_time = ",iTime(_Symbol,0,n));
               if(Buf_Arrow_Sell[n]==EMPTY_VALUE)
                  Print("Last_Arrow_Sell_index n==",n," Last_Arrow_Sell_time = ",iTime(_Symbol,0,n));
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Sell_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index,", Last_Arrow_Sell_time = ",Last_Arrow_Sell_time);
                  break;
               }
            }
   if(Last_Arrow_Buy_index < Last_Arrow_Sell_index)direct = Direction_BUY;
      else if(Last_Arrow_Buy_index > Last_Arrow_Sell_index)direct = Direction_SELL;
         else direct = Direction_FLAT;         
   return(direct); 
      //return(WRONG_VALUE); 
  }
//+------------------------------------------------------------------+
Files:
iCrossAD.mq5  49 kb
 
Hello, my Expert Advisor has a function to delete pending orders when another order triggers. How can I prescribe in the external parameters to be able to disable this function? Many thanks in advance.
Files:
ths42o20.txt  1 kb
 

Hello!

I converted the indicator from MT4 to MT5. I want to use the indicator as an additional filter.

I am just studying MT5. But I cannot find the error. My МТ4 and МТ5 reflections are different.

I have a request to the experts - please help me to find an error in *.mql5 file.

I am attaching the source code.

I am very, very grateful for your help.

Files:
ReVoIn.mq4  4 kb
ReVoIn.mq5  11 kb
 
Priffekt:
Hi, I have a function in my EA to delete pending orders when another order triggers. How can I prescribe in the external parameters to be able to disable this function? Many thanks in advance.
DeleteOppositeOrders();
void DeleteOppositeOrders() {
  bool fd, fep1, fep2;

  fep1=ExistPosition(1);
  fep2=ExistPosition(2);

  for (int i=OrdersTotal()-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol()) {
        fd=False;
        if (OrderType()==OP_BUYSTOP && OrderMagicNumber()== Magik) {
          if (fep2) fd=OrderDelete(OrderTicket());
        }
        if (OrderType()==OP_SELLSTOP && OrderMagicNumber()== Magik) {
          if (fep1) fd=OrderDelete(OrderTicket());
        }
        if (fd && UseSound) PlaySound(NameFileSound);
      }
    }
  }
}
bool ExistPosition(int mn) {
  bool Exist=False;
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol() && OrderMagicNumber()== Magik) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          Exist=True; break;
        }
      }
    }
  }
  return(Exist);
}

This is your code, it's better to attach this way.

 

Of course, I am not very suitable for the role of advisor, but the task seems to be not difficult.

Please note that I'm not going into your code itself, there is a lot of controversy, even for me (dummies), starting with the fact that your function is of type void. This type is used either to indicate that the function does not return a value, or as a function parameter indicates the absence of parameters. And you have return(Exist) at the end of your code;

Declare an input variable, write it as a parameter for your function and exit the function if you set 'this variable to False.

 
input bool On_Off = true;
DeleteOppositeOrders(On_Off);
void DeleteOppositeOrders(bool on_off) {

  if(on_off==false)return;

  bool fd, fep1, fep2;

  fep1=ExistPosition(1);
  fep2=ExistPosition(2);

  for (int i=OrdersTotal()-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol()) {
        fd=False;
        if (OrderType()==OP_BUYSTOP && OrderMagicNumber()== Magik) {
          if (fep2) fd=OrderDelete(OrderTicket());
        }
        if (OrderType()==OP_SELLSTOP && OrderMagicNumber()== Magik) {
          if (fep1) fd=OrderDelete(OrderTicket());
        }
        if (fd && UseSound) PlaySound(NameFileSound);
      }
    }
  }
}
bool ExistPosition(int mn) {
  bool Exist=False;
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol() && OrderMagicNumber()== Magik) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          Exist=True; break;
        }
      }
    }
  }
  return(Exist);
}
 
Priffekt:
Hi, I have in my EA the function to delete pending orders when another order triggers. How can I prescribe in the external parameters that this function can be disabled. Many thanks in advance.

Find all False and True in the text of your code. Replace them with false and true. This language is case sensitive.

 
Sergey Voytsekhovsky:

Find all False and True in the text of your code. Replace them with false and true. This language is case sensitive.

Good afternoon, I've changed all the values, but I'm interested in the possibility of disabling the function itself in the Expert Advisor settings.
DeleteOppositeOrders(); void DeleteOppositeOrders() { bool fd, fep1, fep2; fep1=ExistPosition(1); fep2=ExistPosition(2); for (int i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol()) { fd=false; if (OrderType()==OP_BUYSTOP && OrderMagicNumber()== Magik) { if (fep2) fd=OrderDelete(OrderTicket()); } if (OrderType()==OP_SELLSTOP && OrderMagicNumber()== Magik) { if (fep1) fd=OrderDelete(OrderTicket()); } if (fd && UseSound) PlaySound(NameFileSound); } } } bool ExistPosition(int mn) { bool Exist=false; for (int i=0; i<OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderMagicNumber()== Magik) { if (OrderType()==OP_BUY || OrderType()==OP_SELL) { Exist=true; break; } } } return(Exist); }
Reason: