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

 
artem artem the MACD is. And it has to be confirmed 4 times and the order should open on the 4th candle. I attached a file to make it visually clear.


SanAlex, can you zoom in to have clearly visible MACD bars and how fast one crosses slow ones? I can hardly see it.

Try

#define  MagicNumber  122122
extern string s1             = "Trading options";
extern double Lot            = 0.01;    // размер лота 0 - авт.расчет
extern double StopLoss       = 40;     // стоплосс
extern double TakeProfit     = 10;     // тейкпрофит
extern double TrailStop      = 21;     // уровень без убытка
extern int    Trailing       = 0;      // трейлинг стоп 1 вкл. 0 выкл.
extern int    Breakeven      = 0;      // перенос стоп лосса в без убыток
extern string s2             = "Day & Hour";
extern int    HrStart        = 0;      // время начала торговли
extern int    HrEnd          = 23;     // время окончания торговли
extern int    Monday         = 1;      // Понедельник 1 вкд. 0 выкл.
extern int    Tuesday        = 1;      // Вторник
extern int    Wednesday      = 1;      // Среда
extern int    Thursday       = 1;      // Четверг
extern int    Friday         = 1;      // Пятница
//+------------------------------------------------------------------+
// параметры индикаторов
double MovingPeriodLw        = 5;      
double MovingPeriodS1        = 75;
double MovingPeriodS2        = 85;
double StopLevel;
double TrailStep             = 3;      // шаг трейлинг стопа
bool OrderBuy = true, OrderSell = true, Order = false, Init = true;
int timeprev = 0, Slip = 3.0, start, cnt;

//+------------------------------------------------------------------+
//| Init function                                                    |
//+------------------------------------------------------------------+
void OnInit()
{
   if (Digits == 3 || Digits == 5) { // Пересчет для 5-ти знаков                                                    
      TakeProfit *= 10;
      TrailStop *= 10;
      TrailStep *= 10;
      StopLoss *=10;
      Slip *=10;
   } 
   return; 
}
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
{
   StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL); 
   if(CheckForOpen()!=start)
     {start=CheckForOpen(); cnt=0;}
   else
     {cnt+=1;}
   // Определение направления пересечения мувингов
//   if (Init) InitMetod(); 
   
   // Трейлинг стоп открытых позиций
   if (Trailing != 0 ) RealTrailOrder(TrailStop, TrailStep, StopLevel, MagicNumber);
   
   // Ожидание нового бара на графике
   if(timeprev == Time[0]) return;
   timeprev = Time[0];

   if(cnt==3)
     {
      // Открытие ордера по методу Пуриа
      if(CheckForOpen()==0 && OrderBuy==true) // Если сигнал для покупок 
        {
         if(OrderSend(Symbol(),OP_BUY,Lots(),Ask,Slip,Bid-StopLoss*Point,Ask+TakeProfit*Point,"",MagicNumber,0,Blue))
           {OrderBuy=false; Print("BUY OK");}
        } 
      if(CheckForOpen()==1 && OrderSell==true) // Если сигнал для продаж 
        {
         if(OrderSend(Symbol(),OP_SELL,Lots(),Bid,Slip,Ask+StopLoss*Point,Bid-TakeProfit*Point,"",MagicNumber,0,Red))
           {OrderSell=false; Print("SELL OK");}
        }
     }   
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
int CheckForOpen() // Открытие ордера по методу Пуриа
  {
   double malw,mas1,mas2,macd;
   int    res=-1, buy=0, sell=0;
   // Считывание параметров индикаторов 3 свечи (4ой)
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,3);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,3);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,3);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,3);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 2 свечи (3ей)
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,2);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,2);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,2);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,2);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 1 свечи (2ой)
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,1);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,1);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,1);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,1);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 0 свечи (1ой)
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,0);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,0);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,0);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,0);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   
   if(buy <4)  OrderBuy=true; 
   if(sell<4)  OrderSell=true;
   if(buy ==4) { res=0; OrderSell=true;} 
   if(sell==4) { res=1; OrderBuy =true;}
   return(res);
  }
 
MakaeFX, now for some reason it does not open from the 4th confirmation candle, but actually from the 7th. Screenshot from the tester attached
 
artem artem #:

SanAlex, can you zoom in so that the MACD bars are clearly visible and the fast one crosses the slow one? I can hardly see it.

but what I added there - the purpose of the Expert Advisor is different. The aim is to get the total profit of all open pairs and to close all the Expert Advisors.

Screenshot 2021-10-09 192219

Screenshot 2021-10-09 192331

Files:
 
artem artem #:
MakaeFX, now for some reason it does not open from the 4th confirmation candle, but actually from the 7th. I attached a screenshot from the tester
Upload your EA file, I will look in the tester
 
SanAlex, so the point is that what I want to implement will be even better + I've been doing it for a few days now, and I'm already fundamentally interested in what the right code should look like. And it will be very useful for the future, and the Expert Advisor will be really good. But your version is also good, I can't say anything against it.
 
MakarFX, attached an EA
 
artem artem #:
MakarFX, attached EA
What build of MetaEditor?
 
Exactly MetaEditor - version 5.00 build 2382
 
artem artem #:
exactly MetaEditor - version 5.00 build 2382

It's not clear how you compiled the file you posted...

Here, try and add what else you need

Files:
artem.mq4  13 kb
 

MakarFX, is it OK if I test on all ticks - from 01.08.21 to 03.09.21 - then 46 orders

and if I test only by opening prices within the same period - 29 orders ?

+ several orders explicitly missed their opening price if they were tested by ticks. I attached a screenshot, it shows

Files: