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.
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.
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.
- 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.
- Use the debugger or print out your variables, and find out why.
- Why are you testing PosSellOpened twice?
- If you open a sell, shouldn't you set PosSellOpened to true? Isn't PositionOpened always equal to PosBuyOpened || PosSellOpened? Drop redundant variables.
- 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; }
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Everybody
I experienced several problems with my MQL5robot wich don't respect if conditions to open or close positions.
Here is my code :
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