if statement not working

 

Hi everyone!

I am trying to program my first EA and these if conditions doesn't seem to work together. Does someone know why ?

Thank you very much


double BidClosePrice = Close[1];

double LastBidClosePrice = Close[2];

double LowerBbValue = iBands(NULL,PERIOD_M1,20,2.1,1,PRICE_CLOSE,MODE_LOWER,0);

double LastLowerBbValue = iBands(NULL,PERIOD_M1,20,2.1,2,PRICE_CLOSE,MODE_LOWER,0);


//------------------------------------------------------------------------------------------------------------------------------------------+

void OnTick()

  {

//---       

 //for buy

         if (LastBidClosePrice < LastLowerBbValue && BidClosePrice > LowerBbValue)

         {

         int OrderBuyID = OrderSend(NULL,0,Lot_Size,Ask,0.10,StopLossPriceBuy,0,NULL,145);

         if (OrderBuyID < 0)

         { 

         Alert("");

         Alert("Order rejected. Error is: " + GetLastError());

         }

          

         }

      

  }

 
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

  3. double BidClosePrice = Close[1];
    double LastBidClosePrice = Close[2];
    double LowerBbValue = iBands(NULL,PERIOD_M1,20,2.1,1,PRICE_CLOSE,MODE_LOWER,0);
    double LastLowerBbValue = iBands(NULL,PERIOD_M1,20,2.1,2,PRICE_CLOSE,MODE_LOWER,0);
    

    That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

 
William Roeder:
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

  3. That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.



Thank you for your answer, it helped me a lot!

I will check for mql4 forum section.

Reason: