Help with Bool function comparisons with bool operation && - page 2

 
Amir Yacoby #:

What do you intend to achieve with that line?

Do you know how a for loop works?

Thanks for the response. 
Sorry for this long post it's sort of lengthy to describe. 

YES I created "histoupcount" to hold the bar number MACD signal line that crossed up. "histodowncount" for the bar that crosses down below MACD signal line. 

So in this case A_low or A_high will use histoupcount=i or histodowncount=i bar index number.  I will use this to find the fractals on either side of those bars. "histoupcount / histodowncount".

A_low is a fractal before histo_() crosses up. And A_high is the fractal before histo_() crosses down. Since histo_() is a bool I used global histo_1 instead for this part. 

Anyhow here is the unfinished code but mostly functional.

//+------------------------------------------------------------------+
//|                                                depthcharger1.mq4 |
//|                        Some Stuff I wrote....................... |
//| NOTE: Indictators needed to review     (MACD True,Fractals)      |
//| For Alert/Notification ONLY to check in on the charts            |
//|                    NOT for trading robot                         |
//+------------------------------------------------------------------+
#property copyright "NONE"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//Some inputs maybe future backtesting
extern double    TakeProfit=20.0; //optional use later
extern double    Lots=0.1;
extern double    StopLoss=10.0; //optional use later
extern int       MagicNumber=123486;

//+++++ user can change retrace and/or extension:
extern double fibo_retrace=0.500; // C retracement level desired
extern double fibo_extension=0.618;  // D extention level desired

double val1,val2,histo,histo_1,A,B,C,D,fibhigh,fiblow;
int histoupcount,histodowncount;
bool traded = false;

int ticket,total,result;

//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
//int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    tiles();
    //histo_up_count();
    //histo_down_count();
    //Print ("histoupcount ", histoupcount);
    //Print ("histodowncount ", histodowncount);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll(0,OBJ_RECTANGLE_LABEL);
   ObjectsDeleteAll(0,OBJ_LABEL);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
    val1=iFractals(NULL, 0, MODE_UPPER,3);
    val2=iFractals(NULL, 0, MODE_LOWER,3);
    histo  = iCustom(NULL,0,"MACD True",2,1);
    total = OrdersTotal();
    signal();
    histo_up_count();
    histo_down_count();
    Print("A_high B_low ",A_high()," ",B_low());
    Print("A_low B_high ",A_low()," ",B_high());
    //Print("down_fib up_fib ",down_fib()," ",up_fib());
    //Print("UP ",fibhigh=A+(B-A)*fibo_retrace);
    //Print("DOWN ",fiblow=B+(A-B)*fibo_retrace);
    //Print ("A+B-A= ",A,+B-A);
    //Print ("histoupcount ", histoupcount);
    //Print ("histodowncount ", histodowncount); 
    
  }
//+ started this but ditched it maybe use for future back testing ------------------------------------------------------------------+

void BUY()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Green);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  
   
void SELL()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask-StopLoss*pips2dbl,Ask+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Red);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  

void exit()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask-StopLoss*pips2dbl,Ask+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Red);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  

//--------------------------  MAIN CODE  ---------------
void signal()
   {
   if(histo()==true) ObjectSet("B4",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B4",OBJPROP_BGCOLOR,clrBlack);
   if(histo()==false) ObjectSet("B5",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B5",OBJPROP_BGCOLOR,clrBlack);
   
   if(A_high()==true) ObjectSet("B6",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B6",OBJPROP_BGCOLOR,clrBlack);
   if(A_low()==true) ObjectSet("B3",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B3",OBJPROP_BGCOLOR,clrBlack);
   
   if(B_high()==true) ObjectSet("B2",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B2",OBJPROP_BGCOLOR,clrBlack);
   if(B_low()==true) ObjectSet("B7",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B7",OBJPROP_BGCOLOR,clrBlack);
  
   if(up_fib()==true) ObjectSet("B1",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet ("B1",OBJPROP_BGCOLOR,clrBlack);
   if(down_fib()==true) ObjectSet("B8",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B8",OBJPROP_BGCOLOR,clrBlack);
  
      
   }

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0)
         {
         histoupcount = i;
         }
     
      else return;
              
      }   
      return;
    }

void histo_down_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 < 0)
         {
         histodowncount = i;
         }    
      else return;        
      
      }   
      return;
    }
   

        
bool histo()
   {
   if(histo > 0) return(true);
   else return(false);
   }

bool A_high()
   {
      { 
      if(histo()==false)
         {
         for (int i=histodowncount; val1==0; i++)
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            A = val1;
            if(A!=0)
               {
               Print(A, " A high Located at bar ",i);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }
   
bool A_low()
   {  
      {
      if(histo()==true)
         {
         for (int i=histoupcount; val2==0; i++)
            {
            val2=iFractals(NULL, 0, MODE_LOWER, i);
            A = val2;
            if(A!=0)
               {
               Print(A, " A Low Located at bar ",i);
               return(true);
               }
            }
         }
       }        
    return(false);                 
   }


bool B_high()
   {
      { 
      if(histo()==true)
         {
         for (int i=histoupcount; i!=0; i--)
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            B = val1; //Print(B, " B = val1");
            if(B!=0)
               {
               Print(B, " B high Located at bar ", i);               
               return(true);
               }
             }
            }
        }
    return(false);                 
   }


bool B_low()
   {
      { 
      if(histo()==false) 
         {
         for (int i=histodowncount; i!=0; i--)
            {
            val2=iFractals(NULL, 0, MODE_LOWER, i);
            B = val2; //Print(" B = val2");
            if(B!=0)
               {
               Print(B, " B low Located at bar ", i);
               //Print(PRICE_CLOSE);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }
   
  
bool up_fib() //Needs work
   {
      {
      //Print(A_low()," ",B_high()," up_fib"); 
      if(A_low()==true && B_high()==true)
         {
         //fibhigh=A+(B-A)*fibo_retrace;
         //Print("UP A,B,C ",fibhigh);
                  
         return(true);
         }                   
     }
    return(false);                 
   }
   

bool down_fib()
   {
      {
      //Print(A_high()," ",B_low()," down_fib"); 
      if(A_high()==true && B_low()==true)
         {
         //fiblow=B+(A-B)*fibo_retrace;
         //Print("DOWN A,B,C ",fiblow);
                  
         return(true);
         }
          
          
        }
    return(false);                 
   }   
  

//-------------------------------------notes: need something for new B fractals when histo true and new B found, turn off signal lights wait for new histo direction

int tiles()
   {
    
   int chart_ID=0;


//----------------------B9

   string name9="B9";
   if(!ObjectCreate(0,name9,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name9,OBJPROP_XDISTANCE,0);
   ObjectSet(name9,OBJPROP_YDISTANCE,300);
   ObjectSet(name9,OBJPROP_YSIZE,250);
   ObjectSet(name9,OBJPROP_XSIZE,140);  
   ObjectSet(name9,OBJPROP_BGCOLOR,clrDimGray);
   ObjectSet(name9,OBJPROP_BACK,false);
//--- successful execution 


//----------------------B10
   string name10="B10";
   if(!ObjectCreate(0,name10,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name10,OBJPROP_XDISTANCE,15);
   ObjectSet(name10,OBJPROP_YDISTANCE,370);  
   //ObjectSet(name10,OBJPROP_BGCOLOR,clrLime);
   ObjectSetText(name10,"BUY",12,"Arial",clrLime);
   
   

//----------------------B11
   string name11="B11";
   if(!ObjectCreate(0,name11,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name11,OBJPROP_XDISTANCE,10);
   ObjectSet(name11,OBJPROP_YDISTANCE,450);  
   //ObjectSet(name10,OBJPROP_BGCOLOR,clrLime);
   ObjectSetText(name11,"SELL",12,"Arial",clrRed);
   //ObjectSet(chart_ID,name10,OBJPROP_FONT,Arial);


//-----------------------B12
   string name12="B12";
   if(!ObjectCreate(0,name12,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
     
   ObjectSet(name12,OBJPROP_XDISTANCE,15);
   ObjectSet(name12,OBJPROP_YDISTANCE,320);  
   ObjectSetText(name12,CharToStr(217),25,"Wingdings",clrLime);
   
//-----------------------B13
   string name13="B13";
   if(!ObjectCreate(0,name13,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
     
   ObjectSet(name13,OBJPROP_XDISTANCE,15);
   ObjectSet(name13,OBJPROP_YDISTANCE,470);  
   ObjectSetText(name13,CharToStr(218),25,"Wingdings",clrRed);
     

//----------------------B1
   string name1="B1";
   if(!ObjectCreate(0,name1,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }

//--- set text and coordinates   
   ObjectSet(name1,OBJPROP_XDISTANCE,90);
   ObjectSet(name1,OBJPROP_YDISTANCE,330);
   ObjectSet(name1,OBJPROP_YSIZE,15);
   ObjectSet(name1,OBJPROP_XSIZE,40);  
   ObjectSet(name1,OBJPROP_BGCOLOR,clrBlack);
   
//--- successful execution 

//-----------------------B2
   string name2="B2";
   if(!ObjectCreate(0,name2,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set text and coordinates 
   ObjectSet(name2,OBJPROP_XDISTANCE,90);
   ObjectSet(name2,OBJPROP_YDISTANCE,350);
   ObjectSet(name2,OBJPROP_YSIZE,15);
   ObjectSet(name2,OBJPROP_XSIZE,40);  
   ObjectSet(name2,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B3
   string name3="B3";
   if(!ObjectCreate(0,name3,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name3,OBJPROP_XDISTANCE,90);
   ObjectSet(name3,OBJPROP_YDISTANCE,370);
   ObjectSet(name3,OBJPROP_YSIZE,15);
   ObjectSet(name3,OBJPROP_XSIZE,40);  
   ObjectSet(name3,OBJPROP_BGCOLOR,clrBlack);
   //return(true);

//-------------------------B4  
   string name4="B4";
   if(!ObjectCreate(0,name4,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name4,OBJPROP_XDISTANCE,90);
   ObjectSet(name4,OBJPROP_YDISTANCE,390);
   ObjectSet(name4,OBJPROP_YSIZE,15);
   ObjectSet(name4,OBJPROP_XSIZE,40);  
   ObjectSet(name4,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B5
   string name5="B5";
   if(!ObjectCreate(0,name5,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name5,OBJPROP_XDISTANCE,90);
   ObjectSet(name5,OBJPROP_YDISTANCE,450);
   ObjectSet(name5,OBJPROP_YSIZE,15);
   ObjectSet(name5,OBJPROP_XSIZE,40);  
   ObjectSet(name5,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B6 
   string name6="B6";
   if(!ObjectCreate(0,name6,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name6,OBJPROP_XDISTANCE,90);
   ObjectSet(name6,OBJPROP_YDISTANCE,470);
   ObjectSet(name6,OBJPROP_YSIZE,15);
   ObjectSet(name6,OBJPROP_XSIZE,40);  
   ObjectSet(name6,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B7  
   string name7="B7";
   if(!ObjectCreate(0,name7,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name7,OBJPROP_XDISTANCE,90);
   ObjectSet(name7,OBJPROP_YDISTANCE,490);
   ObjectSet(name7,OBJPROP_YSIZE,15);
   ObjectSet(name7,OBJPROP_XSIZE,40);  
   ObjectSet(name7,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B8 
   string name8="B8";
   if(!ObjectCreate(0,name8,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name8,OBJPROP_XDISTANCE,90);
   ObjectSet(name8,OBJPROP_YDISTANCE,510);
   ObjectSet(name8,OBJPROP_YSIZE,15);
   ObjectSet(name8,OBJPROP_XSIZE,40);  
   ObjectSet(name8,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

   return(true);



  }
    


 As you will see. I use 5min on EURUSD chart but any time is good.  Crossing up or down you will see the print in the expert tab printing the values for A and B also the bar locations. 

I get Print, I get Object colors changing as needed. 
However, I had assumed return was actually TRUE since the colors were all correct. 

However, it's actually false all the time, but giving me Object color as true. 

I assumed my syntax was wrong but that does not seem to be the case. Now I have ran out of what to look for. 
I keep thinking the for loop is starting as false and returning false for some reason. Yet I don't see why it would be false while Object shows true. 

My inexperience is likely not letting me see the problem but I will keep reading it. 

Question: What if for loop starts as false ? Will this make the function false by default ? I assume so ? But then why Object color change as true ? 

Thanks everyone this is an interesting problem. 

 


 
Agent86 #:


Question: What if for loop starts as false ? Will this make the function false by default ? I assume so ? But then why Object color change as true ? 

Thanks everyone this is an interesting problem. 

 


I don't use MT4 and can't run this code, but still it can be so much improved even without trying to understand what it's supposed to do.

As for the question. yes, the condition in the second expression (FI val1==0 in A_high) is evaluated before each time the for is going to be executed, and the first time is already false - so no entry. 
You can see it easily if you use the builtin debugger (the green triangle) when you stop at the for loop (double click on that line before running).

One solution is to set the val1 to 0 before the for loop. But even that is not necessary, because you return from the function when it's 0.

An example of a better written A_high

bool A_high()
  {
   if(!histo())
     {
      val1=0;
      for(int i=histodowncount; val1==0; i++)
        {
         val1=iFractals(NULL, 0, MODE_UPPER, i);
         if(val1!=0)
           {
            Print(val1, " A high Located at bar ",i);
            return(true);
           }
        }
     }
   return(false);
  }

No need to ask if (cond == true), instead if(cond). Same for if(cond == false), instead if(!cond).

You initialize the object back ground color to the same color you set in case it's false.

You have non relevant additional brackets in A_high,A_low etc.. see example.
You have non relevant additional variables, not need to complicate (A,B,C,D).

Start with that. 
 

 
Amir Yacoby #:

I don't use MT4 and can't run this code, but still it can be so much improved even without trying to understand what it's supposed to do.

As for the question. yes, the condition in the second expression (FI val1==0 in A_high) is evaluated before each time the for is going to be executed, and the first time is already false - so no entry. 
You can see it easily if you use the builtin debugger (the green triangle) when you stop at the for loop (double click on that line before running).

One solution is to set the val1 to 0 before the for loop. But even that is not necessary, because you return from the function when it's 0.

An example of a better written A_high

No need to ask if (cond == true), instead if(cond). Same for if(cond == false), instead if(!cond).

You initialize the object back ground color to the same color you set in case it's false.

You have non relevant additional brackets in A_high,A_low etc.. see example.
You have non relevant additional variables, not need to complicate (A,B,C,D).

Start with that. 
 

Thanks, I see it now. Helps a lot.  
I knew I was getting close to something but just could not see it.
Learning to debug better would likely lead me in the right direction earlier. 

Thanks again. Back to the drawing board. 

P.S
It is suppose to look like this and then eventually sent me phone notification to check in on the charts. 



ABC alert