Syntax problem with if statement

 

I'm working on an EA in MQL5. I have tried just about every variation of parentheses I can on these lines, and I keep getting some variation of "open parentheses expected" errors.

Here's the minimalist version with only the parentheses required by if, plus one required by the logic grouping:

if(bid - openPrice > TrailingStop * Point && (stopLoss < bid - TrailingStop * Point || stopLoss == 0))

Which, as expected, gives these errors:

'&&' - open parenthesis expected        SRSI+Volume+MA.mq5      131     58
'||' - open parenthesis expected        SRSI+Volume+MA.mq5      131     100

Here's the version with parentheses only around the logical expressions:

if((bid - openPrice > TrailingStop * Point) && ((stopLoss < bid - TrailingStop * Point) || (stopLoss == 0)))

Which produces:

')' - open parenthesis expected SRSI+Volume+MA.mq5      131     58
')' - open parenthesis expected SRSI+Volume+MA.mq5      131     102

Here's the version if I add parentheses around mathematical expressions as well, to clarify order of operations:

if(((bid - openPrice) > (TrailingStop * Point)) && ((stopLoss < (bid - (TrailingStop * Point))) || (stopLoss == 0)))

And that produces:

')' - open parenthesis expected SRSI+Volume+MA.mq5      131     61
')' - open parenthesis expected SRSI+Volume+MA.mq5      131     108

I'm completely stumped. I've even resorted to ChatGPT and Bard to try to figure it out, and they were no help — just went round and round in circles with the same variations I had tried.

 
   if(bid - openPrice > TrailingStop * Point() && (stopLoss < bid - TrailingStop * Point() || stopLoss == 0));

 

As a follow-up to the previous post, either use _Point which is a predefined variable or use Point() which is a function.

 
Scott Allen: I've even resorted to ChatGPT and Bard to try to figure it out, and they were no help

And please stop using code generators, especially the AI based ones. They are absolutely horrible.

Reference the documentation instead, or read some books on MQL coding or even watch some videos on it. Just don't use generators.

 
Fernando Carreiro #:

And please stop using code generators, especially the AI based ones. They are absolutely horrible.

Reference the documentation instead, or read some books on MQL coding or even watch some videos on it. Just don't use generators.

Yashar Seyyedin #:
   if(bid - openPrice > TrailingStop * Point() && (stopLoss < bid - TrailingStop * Point() || stopLoss == 0));

Thank you!

 
Fernando Carreiro #:

And please stop using code generators, especially the AI based ones. They are absolutely horrible.

Reference the documentation instead, or read some books on MQL coding or even watch some videos on it. Just don't use generators.

That would mean putting off what I'm doing for at least 6-12 months. And I'm really an ex-developer - I did my time writing code. I don't really want to do it any more than I have to.

ChatGPT actually does an amazing job at generating PineScript. It's frustrating that it's not better at MQL, but it also points out why there's a much steeper learning curve for MQL than PineScript.

I've started the tutorials and created a simple EA. I've also used the wizard and then managed to make minor tweaks to that code. And I've even managed to generate a couple of functional, profitable EAs using ChatGPT.

People who've been at this a while, especially people who have a background in languages like C++ or even Python, I think have forgotten how really daunting this language is. And I'm not a total novice, but the last time I developed full-fledged applications was in Visual Foxpro. So...

Please don't knock it. It's part of the process. And within a year, they'll be capable of not doing stupid things like invalid syntax, or using v4 syntax in a v5 script.

Thanks for the help.

 
Scott Allen #:

That would mean putting off what I'm doing for at least 6-12 months. And I'm really an ex-developer - I did my time writing code. I don't really want to do it any more than I have to.

ChatGPT actually does an amazing job at generating PineScript. It's frustrating that it's not better at MQL, but it also points out why there's a much steeper learning curve for MQL than PineScript.

I've started the tutorials and created a simple EA. I've also used the wizard and then managed to make minor tweaks to that code. And I've even managed to generate a couple of functional, profitable EAs using ChatGPT.

People who've been at this a while, especially people who have a background in languages like C++ or even Python, I think have forgotten how really daunting this language is. And I'm not a total novice, but the last time I developed full-fledged applications was in Visual Foxpro. So...

Please don't knock it. It's part of the process. And within a year, they'll be capable of not doing stupid things like invalid syntax, or using v4 syntax in a v5 script.

Thanks for the help.

That is an interesting idea.  AI is a possible solution to coding.  But there are some issues with MQL5 coding.   

Some are the following:

1. I have found that the Strategy Tester is not perfect, Institutional spikes is not modeled well on the Strategy tester. 

2. Also, the performance of an EA on different brokers can be very different.   

3. Then there is the common the C and C++ issues with the compiler.  I noticed it is better to use an INT instead of a string, double or bool to return conditions....This is not a MQL5 things but an anomaly in C. 

4. Just today I found out that SymbolInfoInteger will return a long when requesting the Symbol_Point, but to use it you need to convert it to an INT when you use it...say...to get the ASK price. 

There are a lot of funny bugs no one really cares to fix. We just learn to live with it. 

Relying on AI to create the code, in 2023, is going to give you grief.


Cheers,

Chris Pinter