MQL4: Issue with bool after OrderSelect()

 

Hi to all,

hope someone in the english section can help me.

I try to create a GRID-EA. For the following problem I will use the assumption just trading long.



1. Step: using an input variable to define the gridsize --> Grid_Abstand = e.g.: 200
2. Step : find out the current price-level on the chart --> PREISLEVEL()    [it's the OrderOpenPrice() ofthe newest market-order]
3. Step : define the upper-level and the lower-level of the grid-area 


input int Grid_Abstand = 200;
input int MagicNumber  = 1234;


// GRID_START_TICKET() = OrderOpenPrice() of the newest Market-Order
   double PREISLEVEL()
     {
      double PreisLevel = 0;

      if(OrderSelect( GRID_START_TICKET() , SELECT_BY_TICKET , MODE_TRADES ) == false )
        {
         PreisLevel = NormalizeDouble( ( Ask + Bid ) / 2 , Digits );
        };
      if(OrderSelect( GRID_START_TICKET() , SELECT_BY_TICKET , MODE_TRADES ) == true )
        {
         PreisLevel = NormalizeDouble( OrderOpenPrice() , Digits );
        };
      return(PreisLevel);
     };


// find out the GRID-Area(Upper-/Lower Range)
   double GRID_UPPER_1_HIGH()
     {
      double Grid_Upper_1_High = PREISLEVEL() + ( 1 * Grid_Abstand * Point ) + ( Grid_Abstand / 2 ) * Point;
      return( Grid_Upper_1_High );
     };

   double GRID_UPPER_1_LOW()
     {
      double Grid_Upper_1_Low  = PREISLEVEL() + ( 1 * Grid_Abstand * Point )-( Grid_Abstand / 2 ) * Point;
      return( Grid_Upper_1_Low );
     };


   double GRID_CURRENT_HIGH()
     {
      double Grid_Current_High = PREISLEVEL() + ( Grid_Abstand / 2 ) * Point;
      return( Grid_Current_High );
     };
   double GRID_CURRENT_LOW()
     {
      double Grid_Current_Low  = PREISLEVEL() - ( Grid_Abstand / 2 ) * Point;
      return( Grid_Current_Low );
     };


   double GRID_LOWER_1_HIGH()
     {
      double Grid_Lower_1_High = PREISLEVEL() - ( 1 * Grid_Abstand * Point ) + ( Grid_Abstand / 2 ) * Point;
      return( Grid_Lower_1_High );
     };
   double GRID_LOWER_1_LOW()
     {
      double Grid_Lower_1_Low  = PREISLEVEL() - ( 1 * Grid_Abstand * Point ) - ( Grid_Abstand / 2 ) * Point;
      return( Grid_Lower_1_Low );
     };


      // Example for the check, if e.g. a BUYLIMIT Order is placed within the LOWER grid frames
      bool GRID_LOWER_1_BUYLIMIT()
        {
         bool Grid_Lower_1_Buylimit = false;

         for(int i = OrdersTotal()-1; i >= 0; i--)
           {
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
              {
               if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
                 {
                  if(OrderType() == OP_BUYLIMIT)
                    {
                     if(OrderOpenPrice() < GRID_LOWER_1_HIGH() && OrderOpenPrice() > GRID_LOWER_1_LOW())
                       {
                        Grid_Lower_1_Buylimit = true;
                       }
                    }
                 }
              }
           }

         return (Grid_Lower_1_Buylimit);
        };



unfortunately the check 

if(OrderOpenPrice() < GRID_LOWER_1_HIGH() && OrderOpenPrice() > GRID_LOWER_1_LOW())

will not work and I have no idea why.

I tried several options, also by adding Print() after every single selection/task and I just could find out, that the a.m. "range-check" does not work out.

BUT the GRID_LOWER_1_HIGH() and GRID_LOWER_1_LOW() are defined and NOT unknown... so that is what confuses me.

Hope someone can help me and point out where my mystake is. Thx in advance :-)

 

OrderOpenPrice() returns value for the last selected order.

Function PREISLEVEL() calls OrderSelect() and is changing selected order.

You should call GRID_LOWER_1_HIGH() and GRID_LOWER_1_LOW() before loop in GRID_LOWER_1_BUYLIMIT().

Store results in local variable and compare variables. Something like this:


      bool GRID_LOWER_1_BUYLIMIT()
        {
         bool Grid_Lower_1_Buylimit = false;
         double grid_lower_high =  GRID_LOWER_1_HIGH();
         double grid_lower_low = GRID_LOWER_1_LOW();

         for(int i = OrdersTotal()-1; i >= 0; i--)
           {
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
              {
               if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
                 {
                  if(OrderType() == OP_BUYLIMIT)
                    {
                     if(OrderOpenPrice() < grid_lower_high  && OrderOpenPrice() > grid_lower_low)
)
                       {
                        Grid_Lower_1_Buylimit = true;
                       }
                    }
                 }
              }
           }

         return (Grid_Lower_1_Buylimit);
        };
 
Drazen Penic:

OrderOpenPrice() returns value for the last selected order.

Function PREISLEVEL() calls OrderSelect() and is changing selected order.

You should call GRID_LOWER_1_HIGH() and GRID_LOWER_1_LOW() before loop in GRID_LOWER_1_BUYLIMIT().

Store results in local variable and compare variables. Something like this:



Hi, THANKS for the answer.

Makes absolutely sense and now it works :-DDDDD