If conditions not respected

 

Hello Everybody

I experienced several problems with my MQL5robot wich don't respect if conditions to open or close positions.
Here is my code :

 //--- Logical to enter new position
   if(PosSellOpened==false && PosBuyOpened==false && PosSellOpened==false && CurRsiValue<rsi_high_treshold && PrevRsiValue>rsi_high_treshold){OpenSellPosition();PositionOpened=true;}
   if(PosSellOpened==false && PosBuyOpened==false && PosSellOpened==false && CurRsiValue>rsi_low_treshold && PrevRsiValue<rsi_low_treshold){OpenBuyPosition();PositionOpened=true;}
   
   //--- Logical to manage positions
   if(CurRsiValue<rsi_high_treshold && PrevRsiValue>rsi_high_treshold && Pricepositiv==true && PositionOpened==false && PosSellOpened==false && PosBuyOpened==true){OpenSellPosition();PositionOpened=true;}
   if(CurRsiValue>rsi_low_treshold && PrevRsiValue<rsi_low_treshold && Pricepositiv==true && PositionOpened==false && PosBuyOpened==false && PosSellOpened==true){OpenBuyPosition();PositionOpened=true;}
   if(CurRsiValue>rsi_medium_treshold && PrevRsiValue<rsi_medium_treshold && Pricepositiv==true && PositionOpened==false && PosBuyOpened==false && PosSellOpened==true){OpenBuyPosition();PositionOpened=true;}

I added some comments to display my variables on screen and check if they were correctly registred by the program and they are correctly updated by the ticks.
But for any unknown reason, MT5 plateform do not respect some conditions and open and close positions sometimes completly out of "if" parameters.

I tried to rewrite the conditions in some differnt ways but the problem remains.

I don't understand this problem and really need help.
Thanks by advance

 
Have you tried rewriting it without the &&'s ?
 

chpeller:

I added some comments to display my variables on screen and check if they were correctly registred by the program and they are correctly updated by the ticks.

But for any unknown reason, MT5 plateform do not respect some conditions and open and close positions sometimes completly out of "if" parameters.

It's not. Print the values to the log at the time a trade is taken and check them.

 

Marco vd Heijden

rewriting it without the &&'s ?

How can i do that ? i am newby with mql5 langage.

If you can give me an example of code to avoid && it would be really great.

 

Alain Verleyen


Ok i will do that and tell you the result.

 
chpeller:

Marco vd Heijden

rewriting it without the &&'s ?

How can i do that ? i am newby with mql5 langage.

If you can give me an example of code to avoid && it would be really great.

You can try like this

//+------------------------------------------------------------------+
if(PosSellOpened==false)
  {
   if(PosBuyOpened==false)
     {
      if(PosSellOpened==false)
        {
         if(CurRsiValue<rsi_high_treshold)
           {
            if(PrevRsiValue>rsi_high_treshold)
              {
               OpenSellPosition();
               PositionOpened=true;
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

I'm not saying that this is the problem but this way you can check them individually.

 
  1. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  2. Use the debugger or print out your variables, and find out why.

  3. Why are you testing PosSellOpened twice?

  4. If you open a sell, shouldn't you set PosSellOpened to true? Isn't PositionOpened always equal to PosBuyOpened || PosSellOpened? Drop redundant variables.

  5. chpeller: rewriting it without the &&'s ? you can give me an example
    bool isPositionOpen = PosBuyOpened || PosSellOpened;
    bool wasRSIhigh     = PrevRsiValue>rsi_high_treshold;
    bool  isRSIhigh     =  CurRsiValue>rsi_high_treshold;
    bool isSellSignal   =  wasRSIhigh && !isRSIhigh;
    if(isSellSignal && !isPositionOpen)
                  {
                   OpenSellPosition();
                   PosSellOpened=true;
                  }
 

William Roeder


Thank you for your remarks. As i said i'am a newby and have a lot to learn. Coding is not my job.

Variable "PositionOpened" is used to avoid to open simultaneously multiple positions when conditions are met to open a position in the "Ontick()" function.

Variables "PosBuyOpened" and "PosSellOpened" are only used to identify the position type and usefull for my opening and closing rules.

There are probably better ways to do this but in my first steps in coding my ambition is just to try to make a functionnal program. I will optimize it in a second time, when i will be satisfied by it's perfomance.

Thank you for your code example, very interesting.

As you seen, I still have a lot to learn just for correctly code and much more to take the maximum benefits of mql5 possibilities. As OOP coding for example.

In your 3rd topic. you identified a real mistake, i discovered when modifying my code in Marco vd Heijden way. Effectively i wrote twice ==PosSellOpenend. I wanted to write "==PositionOpened". I corrected this and obviously have better results.

I commented all my variables to enable me to have a check in real time. But according to advices in this page i now also print some when opening positions to have a second cold check.
I will post results.

Coding is for me like a mountain, and i thank you all to help me to climb it.

 

Hi,

Finaly i had two major mistakes in my code.

1 - A logical mistake in boolean, i called twice the same condition, instead of the condition i needed.

2 - A miscoding of my closeall() function wich close all the opened positions instead of all the opened positions for the current symbol only.

Thank you for your gratefull help.
Reason: