How do you count down a a boolean conditions in mql5

 

Hello everyone am have been trying to count down or simply count a boolean condition where So in the code below

I have tried to go over 20 bars and using time differnce check if there is a double ma cross over but unfortunately this code doesnt seem to work it neither recognizes the first nor the second crossover, can anyone help me rectify this error here .Thank you  in advance

MqlRates Candles[];
 int copied = CopyRates(_Symbol,PERIOD_CURRENT,1,20,candles);
for(int i=1; i<copied; i++)
{
 
    bool ma_cross = ((rma[i]>yma[i] && rma[i-1]<yma[i-1])||(rma[i]<yma[i] && rma[i-1]>yma[i-1]));
    datetime timex = iTime(_Symbol, PERIOD_CURRENT, i);

    if(ma_cross && !ma_cross_last && timex != time_last)
    {
        count_D++;
        if(count_D == 2)
        {
            drawVerticalLine("2nd cross over", timex, clrYellow);
            count_D = 0;
        }
        time_last = timex;
    }
    ma_cross_last = ma_cross;
    Comment("Mac_sta ", count_D, "count", i);
}
Documentation on MQL5: Python Integration / order_calc_margin
Documentation on MQL5: Python Integration / order_calc_margin
  • www.mql5.com
order_calc_margin - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
ZhuwaoAbel:
count_D

Are you initialising count_D to 0 before the loop?

Also what is its type?
 
R4tna C #:

Are you initialising count_D to 0 before the loop?

Also what is its type?

Count_D is type int and yes i have initialized it before the loop

 
ZhuwaoAbel #:

Count_D is type int and yes i have initialized it before the loop

The I guess you need to debug these lines and see if they are behaving as expected

bool ma_cross = ((rma[i]>yma[i] && rma[i-1]<yma[i-1])||(rma[i]<yma[i] && rma[i-1]>yma[i-1]));
    datetime timex = iTime(_Symbol, PERIOD_CURRENT, i);

    if(ma_cross && !ma_cross_last && timex != time_last)
 

1. I see you have rma and yma as arrays. Could there be a "Array Index Out Of Range" error? Check journal tab for possible messages.

2. Also the draw line function is not there to judge.

One needs to see the whole code to make an effective help.

 
Yashar Seyyedin #:

1. I see you have rma and yma as arrays. Could there be a "Array Index Out Of Range" error? Check journal tab for possible messages.

2. Also the draw line function is not there to judge.

One needs to see the whole code to make an effective help.

True i have checked not array out of range ,here is the full code  the image spikefailure.png shows the outcome i got before  i tried a new method to detect  a double ma crossover  using function Get2ma() now i  the whole EA wont run at all as shown in the runtime .png
//+------------------------------------------------------------------+
//|                                                            Trial |
//|                                               N.Zhuwao_Smilypros |
//|                                                    smilypros.com |
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>
CTrade trade;
ENUM_APPLIED_PRICE App_price =PRICE_CLOSE;
int handlersi;
int handlerma;
int handleyma;
int count_D=0;

double rsi_buffer[];
double rma[];
double yma[];
bool mac_stat=true;
bool timeflag =false;
MqlRates candles[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   handlersi = iRSI(_Symbol,PERIOD_CURRENT,13,App_price);
   if(handlersi==INVALID_HANDLE) {
      Alert(__FUNCTION__,"> Handle is Invalid..Check the name!",GetLastError());
   }
   handleyma =iMA(_Symbol,PERIOD_CURRENT,50,0,MODE_LWMA,handlersi);
   if(handleyma==INVALID_HANDLE) {
      Alert(__FUNCTION__,"> Handle is Invalid..Check the name!",GetLastError());
   }
   handlerma=iMA(_Symbol,PERIOD_CURRENT,7,0,MODE_LWMA,handlersi);
   if(handlerma==INVALID_HANDLE) {
      Alert(__FUNCTION__,"> Handle is Invalid..Check the name!",GetLastError());
   }
   
   //+------------------------------------------------------------------+
   //|  Sort buffers from current to last data
   //+------------------------------------------------------------------+
   ArraySetAsSeries(rma,true);
   ArraySetAsSeries(yma,true);
   ArraySetAsSeries(rsi_buffer,true);
  
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   Comment("");
   // Get the total number of objects
   int total = ObjectsTotal(0);

   // Loop through all objects
   for(int i=total-1; i>=0; i--) {
      // Get the name of the object
      string name = ObjectName(0,i);

      // Delete the object
      ObjectDelete(0,name);
   }

   Print("All objects and text have been cleared from the chart.");
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if(handlerma!=INVALID_HANDLE || handlersi!=INVALID_HANDLE || handleyma!=INVALID_HANDLE) {

      int copied = CopyRates(_Symbol,PERIOD_CURRENT,1,20,candles);
      CopyBuffer(handlersi,MAIN_LINE,0,10,rsi_buffer);
      CopyBuffer(handlerma,MAIN_LINE,0,10,rma);
      CopyBuffer(handleyma,MAIN_LINE,0,10,yma);
     
      double entry = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      //double bandwidth = 100*(upper_buffer[0]-lower_buffer[0])/middle_buffer[0];
      double squeeze_thresh=350;
      count_D = 0;
      bool ma_cross_last = false;
      datetime time_last=0;

     
       //=(rma[1]<=yma[1]&&rma[0]>yma[0])||(rma[1]>=yma[1]&&rma[0]<yma[0]);
     // bool ma_cross=((rma[0]>yma[0] && rma[2]<yma[2]&&rma[1]<=yma[1])||(rma[0]<yma[0]&&rma[2]>=yma[2]&&rma[1]>=yma[1]));
     for(int i=1; i<copied; i++)
     {
    bool ma_cross = ((rma[i]>yma[i] && rma[i-1]<yma[i-1])||(rma[i]<yma[i] && rma[i-1]>yma[i-1]));

    if(ma_cross && !ma_cross_last)
    {
        count_D++;
        if(count_D == 2)
        {
            datetime timex = iTime(_Symbol, PERIOD_CURRENT, i);
            drawVerticalLine("2nd cross over", timex, clrYellow);
            count_D = 0;
        }
    }
    ma_cross_last = ma_cross;
    Comment("Mac_sta ", count_D, "count", i);
}
     



   }

}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void drawVerticalLine(string name, datetime dt, color cor = clrAliceBlue)
{
   ObjectDelete(0,name);
   ObjectCreate(0,name,OBJ_VLINE,0,dt,0);
   ObjectSetInteger(0,name,OBJPROP_COLOR,cor);
}
//+------------------------------------------------------------------+
int Get2ma(bool macross,int count=0){
int i=0;
do
     {
      if(macross){
      i++;
      Comment("\n Count ",i);
      if(i==count){
   Alert("Congratulations",i);
   }
      }
     }
   while(i<count);
   return i;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool getmacross(bool ma_cross,int count=0)
{
   int i=0;
   if(ma_cross) {
      i++;
      if(i==count) {
         Comment("2 ma_crossing",i);
      }

   }
   return true;
}
//+------------------------------------------------------------------+
void OnTimer()
{
   mac_stat=false;
   EventKillTimer();
}
//+------------------------------------------------------------------+
Files:
 

You have array out of range in backtester.

You have copied=20 while you have only 10 values for rma.

That is the problem. Read 10 candles or read 20 values for rma.

 

i guess you are right i didnt see it coming , i have modified the code by allowing the moving averages to read  20 candles , now it only counts the first ma_crossover and resets to 0 such that when there is a second occurance of the crossover it always counts only up to 1.

Files:
spf.PNG  13 kb
 
ZhuwaoAbel #:
if(handlerma!=INVALID_HANDLE || handlersi!=INVALID_HANDLE || handleyma!=INVALID_HANDLE)

This won't solve the problem you described, but I think the line I'm quoting doesn't work the way you intended.

void OnStart()
  {
   int handlerma = INVALID_HANDLE,
       handlersi = INVALID_HANDLE,
       handleyma = 123;
   Alert(handlerma!=INVALID_HANDLE || handlersi!=INVALID_HANDLE || handleyma!=INVALID_HANDLE);
  }


 
Vladislav Boyko #:

This won't solve the problem you described, but I think the line I'm quoting doesn't work the way you intended.



Thanks you are right . i reassigned a variable to check  handle value  in case its equal or below 0  in this case raise an alert 

 
Your code ignores the first cross and draws a vertical line for the second. again ignores the third and draws a vertical line for the forth cross and so on for 20 candles. 

What is wrong now? What do you expect?

drawVerticalLine("2nd cross over", timex, clrYellow);

Every time the vertical line is replaced with the same name:"2nd cross over".

Reason: