String Array example in MQL4 book

 
https://book.mql4.com/variables/arrays
I am working through this example in the book. The website code is reproduced for your convenience however it differs from the download code stringarray.mq4
I believe both examples are erroneous.
Is this book even a good way to learn MQL?

//--------------------------------------------------------------------
// stringarray.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
extern double Level=1.3200;                     // Preset level 
string Text[101];                               // Array declaration
//--------------------------------------------------------------------
int init()                                      // Special funct. init()
  {                                             // Assigning values
   Text[1]="one ";             Text[15]="fifteen ";
   Text[2]="two ";             Text[16]="sixteen ";
   Text[3]="three ";           Text[17]="seventeen ";
   Text[4]="four ";            Text[18]="eighteen ";
   Text[5]="five ";            Text[19]="nineteen ";
   Text[6]="six ";             Text[20]="twenty ";
   Text[7]="seven ";           Text[30]="thirty ";
   Text[8]="eight ";           Text[40]="forty ";
   Text[9]="nine ";            Text[50]="fifty ";
   Text[10]="ten ";            Text[60]="sixty";
   Text[11]="eleven ";         Text[70]="seventy ";
   Text[12]="twelve ";         Text[80]="eighty ";
   Text[13]="thirteen ";       Text[90]="ninety";
   Text[14]="fourteen ";       Text[100]= "hundred";
   // Calculating values
   for(int i=20; i<=90; i=i+10)                // Cycle for tens
     {
      for(int j=1; j<=9; j++)                  // Cycle for units
         Text[i+j]=Text[i] + Text[j];          // Calculating value   
     }
   return;                                     // Exit init()
  }
//--------------------------------------------------------------------
int start()                                     // Special funct. start()
  {
   int Delta=NormalizeDouble((Bid-Level)/Point,0);// Excess 
//--------------------------------------------------------------------
   if (Delta>=0)                                // Price is not higher than level
     {
      Alert("Price below level");               // Alert
      return;                                   // Exit start()
     }
//--------------------------------------------------------------------
   if (Delta<100)                               // Price higher than 100
     {
      Alert("More than hundred points");        // Alert
      return;                                   // Exit start()
     }
//--------------------------------------------------------------------
   Alert("Plus ",Text[Delta],"pt.");            // Displaying
   return;                                      // Exit start()
  }
//--------------------------------------------------------------------
It seems that
 if (Delta>=0

should actually be

 if (Delta<=0

as we want to know if the delta between the price level and the bid level is nothing or negative. This is correct in the download code example.

Similarly

if (Delta<100)      

should be

if (Delta>100)      

as we want to know if delta exceeds the max array value

In the download code this has also been correted.

However the program still does not peform correctly. The Delta value calculated is wildly inaccurate, always negative and much larger than reality.

The equation seems correct. Is it a problem with NormalizeDouble?

Arrays - Variables - MQL4 Tutorial
Arrays - Variables - MQL4 Tutorial
  • book.mql4.com
Arrays - Variables - MQL4 Tutorial
 

Yeah, the downloaded code is correct.

I tested it out and it works.

Did you set an appropriate level in the indicator menu properties before it ran?

I tested it on GBPUSD with a value of 1.37 and the Delta was calculated correctly.



 

Yes, it's a bug in the sample code that is shown. If the delta is positive the price is obviously above the level. The mq4 file is correct.

 

I am testing on EURUSD with a level of 1.7351 and look at the Delta values. I will try with GBPUSD.
 

Ah of course the error is mine. The level should be 1.1735. 

Apologies.

 
clemmo:

I am testing on EURUSD with a level of 1.7351 and look at the Delta values. I will try with GBPUSD.

It's right, by my calculations.


Bid = 1.175

Level = 1.7351

Result = Bid - Level = -0.5601

Result / Point = -0.5601 / 0.00001 = -56010

The result is slightly off (-56010 vs -56013), but that can be explained by the fact that Bid (1.175) is probably not as precise due to the output being displayed.


clemmo:

Ah of course the error is mine. The level should be 1.1735. 

Apologies.

NP, cheers.

 
Alexander Martinez:

It's right, by my calculations.


Bid = 1.175

Level = 1.7351

Result = Bid - Level = -0.5601

Result / Point = -0.5601 / 0.00001 = -56010

The result is slightly off (-56010 vs -56013), but that can be explained by the fact that Bid (1.175) is probably not as precise due to the output being displayed.

Yes you are correct. I simply was using a level very far above the correct price. May I ask if the number of digits precision has any effect on all of this?

int Delta=NormalizeDouble((Bid-Level)/Point,5);

would work just the same, or not?

 
clemmo:
https://book.mql4.com/variables/arrays
I am working through this example in the book. The website code is reproduced for your convenience however it differs from the download code stringarray.mq4
I believe both examples are erroneous.
Is this book even a good way to learn MQL?

BTW, as to answer your question: I'm not a fan of books / reading when starting off. I prefer videos because they tend to be targeted more towards absolute beginners.

Once you have a solid foundation / understanding, then books / reading are good because they help fill in the gaps by providing the finer details that the videos left out.

With that said, MQL is built on top of C++, so, if you know C++, you should be able to transition smoothly.

Or, if you've already learned any programming language, transitioning won't be as tough, it's just learning the syntax and how C++/MQL does things.

But if this is your first language, MQL / C++ aren't exactly beginner friendly.

If you're looking for good resources, there's tons of C++ tutorials on YouTube that start you off with the basics to help you build a foundation of not just C++, but of programming altogether.

 
Alexander Martinez:

BTW, as to answer your question: I'm not a fan of books / reading when starting off. I prefer videos because they tend to be targeted more towards absolute beginners.

Once you have a solid foundation / understanding, then books / reading are good because they just help fill in the gaps by providing the finer details that the videos left out.

With that said, MQL is built on top of C++, so, if you know C++, you should be able to transition smoothly.

Or, if you've already learned any programming language, transitioning won't be as tough, it's just learning the syntax and how C++/MQL does things.

But if this is your first language, MQL / C++ aren't exactly beginner friendly.

If you're looking for good resources, there's tons of C++ tutorials on YouTube that start you off from the basics to help you build a foundation of not just C++, but of programming altogether.

Thanks. Do you have a video series that you recommend in particular for c++?

I would say I'm no longer a complete beginner but I have little practical experience. I think the MQL book is good as it doesn't go too quickly between concepts but it has some elementary errors which is very frustrating for beginners who can't be sure if it's their own fault.

Also it's not quite English, sort of a pseudo-translation.

Still I think learning directly what we want to do is often the best approach as a general C++ course will not deal with practical trading considerations. I only wish there was an MQL5 book along the same lines as the MQL4 version, preferably with updated corrected lessons.

 
clemmo:

May I ask if the number of digits precision has any effect on all of this?

would work just the same, or not?

In this particular case, no.

int Delta

An int is a whole number, so when you assign it a value from a double (a decimal number), it will be typecast (converted) into the datatype of int.

But, even if Delta were a double, there' s just zeros left after dividing by Point, so it'll be something like 85.0.

However, if you hadn't divided by Point, then setting the precision of NormalizeDouble would come into play.

Leaving the line in question for your convenience:

int Delta=NormalizeDouble((Bid-Level)/Point,0);
 
Alexander Martinez:

In this particular case, no.

An int is a whole number, so when you assign it a value from a double (a decimal number), it will be typecast (converted) into the datatype of int.

But, even if Delta were a double, there' s just zeros left after dividing by Point, so it'll be something like 85.0.

However, if you hadn't divided by Point, then setting the precision of NormalizeDouble would come into play.

Leaving the line in question for your convenience:

That makes sense. Thank you, Alexander.

Reason: