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

 

For a tester this

OrderSend

will do and even work on fast servers, but if not a tester it's better to do it according to the documentation :)

 
Guys, I need help, I haven't been able to make the necessary changes to the following code for a few days now:
void start()
{
   StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL); 
   
   // Определение направления пересечения мувингов
   if (Init) InitMetod(); 
   
   // Трейлинг стоп открытых позиций
   if (Trailing != 0 ) RealTrailOrder(TrailStop, TrailStep, StopLevel, MagicNumber);
   
   // Ожидание нового бара на графике
   if(timeprev == Time[0]) return;
   timeprev = Time[0];
   
   // Открытие ордера по методу Пуриа
   CheckForOpen();
}
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen() // Открытие ордера по методу Пуриа
{
   double malw,mas1,mas2,macd;
   int    res;  
   // Считывание параметров индикаторов
   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  && OrderSell)
     {
     OrderBuy=true;
     OrderSell=false;
     Order=true;
     }
   
   if(malw<mas1 && malw<mas2  && OrderBuy)
     {
     OrderBuy=false;
     OrderSell=true;
     Order=true;
     }

   // Открываем ордер Buy
   if(macd>0 && OrderBuy && Order)
     {
     res=OrderSend(Symbol(),OP_BUY,Lots(),Ask,Slip,Bid-StopLoss*Point,Ask+TakeProfit*Point,"",MagicNumber,0,Blue);
     Order=false;
     return;
     }     
   // Открываем ордер Sell   
   if(macd<0 && OrderSell && Order)
     {
     res=OrderSend(Symbol(),OP_SELL,Lots(),Bid,Slip,Ask+StopLoss*Point,Bid-TakeProfit*Point,"",MagicNumber,0,Red);
     Order=false;
     return;
     }
 
}  
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double Lots()  // Расчет объема лота
{
   double Lots;
   if (Lot > 0) return(Lot);
   Lots=AccountFreeMargin()/5000;
   Lots=MathMin(15,MathMax(0.01,Lots));
   if(Lots<0.1) Lots=NormalizeDouble(Lots,2);
   else 
     {
     if(Lots<1) Lots=NormalizeDouble(Lots,1);
     else       Lots=NormalizeDouble(Lots,0);
     }
   return(Lots);
}
//+------------------------------------------------------------------+
//| Init metod Puria function                                        |
//+------------------------------------------------------------------+
void InitMetod()  // Опредеоение начального пересечения мувингов
{
   double malw,mas1,mas2;
   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);

   if((malw<=mas1 && malw>=mas2) || (malw>=mas1 && malw<=mas2))
     {
     Init=false;
     OrderBuy=true;
     OrderSell=true;
     }   
   return;
}

Actually, what I tried but could not add here: make the EA wait for 4 candle open prices in a row after all the indicators (candle open prices ) coincide:


  1. 1. The opening price of the 1st candle - (5) over (75) and (85) + MACD bar opened above 0 - 1 out of 4 confirmations is;
    2. The opening price of the 2nd candle - (5) over (75) and (85) + MACD bar opened above 0- 2 out of 4 confirmations is;
    3.The opening price of the 3rd candle - (5) over (75) and (85) + MACD bar opened above 0- 3 out of 4 confirmations yes;
    4. The opening price of the 4th candle - (5) over (75) and (85) + MACD bar opened above 0- 4 out of 4 confirmations yes- Open trade to buy on the same candle (4th).

The same situation with orders to sell, only in the other direction indicators should open. And there is an important point - if at, say, any stage of the 1 st candle to the 4th, the indicators change their position(for example, on the 3rd opening candle, the MACD bar opens either below 0, or = 0) - then everything resets, because the signals were not checked on the opening of 4-th candle in a row.

Please advise, experienced programmers, what needs to be changed in the code to make these conditions work? I've tried a lot of things, and I don't know...

 
   if(malw>mas1 && malw>mas2  && OrderSell)

As they say, don't take our word for it.

OrderSell

dunno what might be in it, share the decision-making logic in the code.

 

artem artem #:

The opening price of the 1st candle - (5) over (75) and (85) + MACD bar opened above 0 - 1 out of 4 confirmations yes;

2 The opening price of the 2nd candle - (5) over (75) and (85) + MACD bar opened above 0- 2 out of 4 confirmations yes;

3 The opening price of the 3rd candle - (5) over (75) and (85) + MACD bar opened above 0- 3 out of 4 confirmations is;

4 The opening price of the 4th candle - (5) over (75) and (85) + MACD bar opened above 0- 4 of 4 confirmations yes- Open trade to buy on the same candle (4th).

    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
       if(CheckForOpen()==0)
         {
          if(OrderSend(Symbol(),OP_BUY,Lots(),Ask,Slip,Bid-StopLoss*Point,Ask+TakeProfit*Point,"",MagicNumber,0,Blue)) Print("BUY OK");
         }
       if(CheckForOpen()==1)
         {
          if(OrderSend(Symbol(),OP_SELL,Lots(),Bid,Slip,Ask+StopLoss*Point,Bid-TakeProfit*Point,"",MagicNumber,0,Red)) Print("SELL OK");
         }
      }
    //+------------------------------------------------------------------+
    //| Check for open order conditions                                  |
    //+------------------------------------------------------------------+
    int CheckForOpen() // Открытие ордера по методу Пуриа
      {
       double malw,mas1,mas2,macd;
       int    res=-1, buy=0, sell=0;
       // Считывание параметров индикаторов 3 свечи
       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 свечи
       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 свечи
       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 свечи
       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) res=0;
       if(sell==4) res=1;
       return(res);
      }
     
    malw

    Scoring one value with another, nothing ? Split first block = solution, second block = solution, compare blocks.

     
    Volodymyr Zubov #:

    Scoring one value with another, nothing ? Split first block = solution, second block = solution, compare blocks.

    Is this written to whom? or to what?
     
    It turns out you have
    malw
    in the code and a logical error comes out there.
     
    Volodymyr Zubov #:
    It turns out you have, in the code, a logical error.
    Check before you write
     

    OK, but the variables will crash if you use them that way and then you won't know what the "miracles" are

     
    Volodymyr Zubov #:

    OK, but the variables will crash if you use them that way and then you won't know what the "miracles" are.

    If you know another option, write it down...

    You can help people.)

    Reason: