if doesn't works well MQL5 [solved]

 

Hello everyone,

I'm beginner in EA creation (And in this forum so if I made a mistake in the section or other tell my so i'll can change thanks).

I want to do something when the price is under the ema 50.

But I've a problem with the action after if. The last if has to comment "ok" if test is true, it does but randomly.

First I thought test maybe false so I commented its value but it isn't that. The value is true when the price is under and false when above so it works.

But even if test is true it show "ok" randomly. Someone have an idea ? Here is my full code. Thanks for reading.

int handdlema = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);
double lastma;
bool test;

void OnTick()
  {
   double tablema[];
   CopyBuffer(handdlema,0,0,5,tablema);
   lastma = NormalizeDouble(tablema[4],5);
   MqlRates price[];
   CopyRates(_Symbol,_Period,0,5,price);
   if(price[4].close<lastma) test=true;
   else test=false;
   if(test) Comment("ok");
   Comment("\n",price[4].close,lastma,test);
  }
 
ouhooo33:

Hello everyone,

I'm beginner in EA creation (And in this forum so if I made a mistake in the section or other tell my so i'll can change thanks).

I want to do something when the price is under the ema 50.

But I've a problem with the action after if. The last if has to comment "ok" if test is true, it does but randomly.

First I thought test maybe false so I commented its value but it isn't that. The value is true when the price is under and false when above so it works.

But even if test is true it show "ok" randomly. Someone have an idea ? Here is my full code. Thanks for reading.

Hello + welcome 

The comment on the bottom replaces the one on top . (if i understood your post correctly , i'm not thoroughly caffeinated yet)

You can do this though :

int handdlema = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);
double lastma;
bool test;
string boolToString(bool _variable,string _true_text,string _false_text){
if(_variable){return(_true_text);}
return(_false_text);
}
void OnTick()
  {
   double tablema[];
   CopyBuffer(handdlema,0,0,5,tablema);
   lastma = NormalizeDouble(tablema[4],5);
   MqlRates price[];
   CopyRates(_Symbol,_Period,0,5,price);
   if(price[4].close<lastma) test=true;
   else test=false;
   Comment(boolToString(test,"Okay",""),"\n",price[4].close,lastma,test);
  }

So this says , display okay if test true or nothing if test false , then change line and print the rest

 
Your code
   if(price[4].close<lastma) test=true;
   else test=false;
Simplified
   test = price[4].close<lastma;
 
Lorentzos Roussos #:

Hello + welcome 

The comment on the bottom replaces the one on top . (if i understood your post correctly , i'm not thoroughly caffeinated yet)

You can do this though :

So this says , display okay if test true or nothing if test false , then change line and print the rest

Thanks you very much for you fast response and explaination ! it works well have a good day 
 
William Roeder #:
Your code
Simplified
noted ; ) thanks 
 

HELLO EVEYONE 

I AM HAVING A DIFFICULT TIME CODING , PLEASE HELP 

HOW CAN I CODE ROOM TO THE LEFT ? BY THAT I MEAN THE AREA WHERE THE CHART DOESNT TRADE IN  AT LEAST 10 OR 17 CANDLES ? EXAMPLE BELOW .

IN my mind 

double lowest [10] > low 1 

where lowest[0] = iLow(_Symbol,PERIOD_CURRENT,2);

and low 1 is the low of the setup bar . does it make sense

Files:
 
João Buta #: I AM HAVING A DIFFICULT TIME CODING , PLEASE HELP 

HOW CAN I CODE ROOM TO THE LEFT ? BY THAT I MEAN THE AREA WHERE THE CHART DOESNT TRADE IN  AT LEAST 10 OR 17 CANDLES ? EXAMPLE BELOW .

  1. Don't SHOUT at us, that is very RUDE.

  2. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  3. Just look if a candle is in range.
    Not tested, not compiled, just typed.
    bool is_empty_left(int iBar, int length){
        double H=iHigh(_Symbol, _Period, iBar), L=iLow(_Symbol, _Period, iBar);
        while(length-- > 0){ ++iBar;
            if(iHigh(_Symbol, _Period, iBar) < L
            || iLow( _Symbol, _Period, iBar) > H) continue; // Above or below signal candle.
            return false;
        }
        return true;
    }
    Not tested, not compiled, just typed.
 
William Roeder #:
  1. Don't SHOUT at us, that is very RUDE.

  2. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  3. Just look if a candle is in range.
    Not tested, not compiled, just typed.
    Not tested, not compiled, just typed.
Thank you very much sir . I am sorry for the disrespect .
 

 Good day everyone , i have just started programming . i programmed the strategy below and would like to know your comment on what the EA is missing and how i can make it more robust ( survive a computer and market crash as well as rectify  errors ) . The strategy is engulfing candle with room to the left it is as follow:

<Deleted>

 
  1. João Buta #: Good day everyone

    Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor


  2.    High1 = NormalizeDouble(iHigh(_Symbol,PERIOD_CURRENT,1),_Digits);

    You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  3. int bars = iBars(_Symbol,PERIOD_CURRENT);
    if(barsTotal < bars){
         barsTotal = bars;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  4. int OnInit(){
       int barsTotal = iBars(_Symbol,PERIOD_CURRENT);

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  5.  High1 >= s/r =< Low1 ){

    That doesn't do what you think it does

    True = non-zero and false = zero so you get:

    if( 3 < 2 < 1 )
    if( false < 1 )
    if(     0 < 1 )
    if(     true  )
    if( 3 > 2 > 1 )
    iftrue > 1 )
    if(     1 > 1 )
    if(     false )
    

  6. Where do you check if you already have an open position or a pending order?

 
João Buta #:
Thank you very much sir . I am sorry for the disrespect .

Do as William said and edit your post to post the code properly.

At the same time it would be a good idea to remove all the unnecessary empty lines. I will not bother to try to read code with so much blank space.

Reason: