fbtrader / estudando

0 stars 0 forks source link

Cap 2 #6

Open fbtrader opened 6 years ago

fbtrader commented 6 years ago

Book: Expert Advisor Programming For Metatrader 5 Andrew R. Young

Chapter 2 - Variables & Data Types 11 Variables 11 Data Types 12 Integer Types 12 Real Types 13 String Type 13 Boolean Type 14 Color Type 15 Datetime Type 16 Constants 17 Arrays 18 Multi-Dimensional Arrays 19 Iterating Through Arrays 20 Enumerations 21 Structures 23 Typecasting 25 Input Variables 26 Local Variables 27 Global Variables 30 Static Variables 31 Predefined Variables 32

fbtrader commented 6 years ago

Variables int myNumber = 1;

int myNumber; int yourNumber = 2; myNumber = yourNumber;

For numerical types, the initial value will be 0, and for string types it will be a null or empty string (NULL or "").

fbtrader commented 6 years ago

Data Types

Integer types, real, srtings

fbtrader commented 6 years ago

Integer char -128 to 127 short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

fbtrader commented 6 years ago

You will frequently see the int or long type used in MQL5 functions

fbtrader commented 6 years ago

There are also unsigned integer types, which do not allow negative numbers. The unsigned types use the same amount of memory as their signed counterparts, but the maximum value is double that of the signed type.

uchar,ushort,uint,ulong

fbtrader commented 6 years ago

In practice, you will rarely use unsigned integer types, but they are available for you to use.

fbtrader commented 6 years ago

Real Types (prices) float 9 (7 digits) double (15 digits)

fbtrader commented 6 years ago

String

string myString = "This is a string";

string myQuote = "We are \"escaping\" double quotes"; Print(myQuote); // Output: We are "escaping" double quotes

string mySlash = "This is a backslash: \"; Print(mySlash); // Output: This is a backslash: \

string myNewline = "This string has \n a new line character"; Print(myNewline); // Output: This string has // a newline character

fbtrader commented 6 years ago

string insert = "concatenated"; string myConcat = "This is an example of a " + insert + " string."; Print(myConcat); // Output: This is an example of a concatenated string.

fbtrader commented 6 years ago

string newString; string insert = "concatenated"; StringConcatenate(newString,"This is another example of a ", insert, " string"); Print(newString); // Output: This is another example of a concatenated string.

t is more memory-efficient than using the concatenation operator.

fbtrader commented 6 years ago

string myMultiline = "This is a multi-line string. " "These lines will be joined together."; Print(myMultiline); // Output: This is a multi-line string. These lines will be joined together.

fbtrader commented 6 years ago

Boolean Type 0 (false) or 1 (true)

bool myBool = true; Print(myBool); // Output: true

default value will be 0, or false

Any non-zero value in a boolean variable will evaluate to true

myBool = 5; if(myBool == true) Print("myBool is true"); // Output: myBool is true

fbtrader commented 6 years ago

Color Type

Colors can be represented by predefined color constants, RGB values, or hexadecimal values.

color lineColor = clrRed; color lineColor = C'255,0,0'; color lineColor = 0xFF0000;

Reference (web colors)

image

fbtrader commented 6 years ago

Datetime Type

which is the number of seconds elapsed since January 1, 1970. For example, January 1, 2012 at midnight in Unix time is 1,325,397,600.

format yyyy.mm.dd hh:mm:ss. Here's an example: datetime myDate = D'2012.01.01 00:00:00';

You can omit parts of the datetime constant if they are not used. The following examples will demonstrate:

datetime myDate = D'2012.01.01 00:00'; // Hour and minute datetime myDate = D'2012.01.01 00'; // Hour only datetime myDate = D'2012.01.01'; // Date only

So what happens if you leave out the date? The compiler will substitute today's date – the date of compilation. Assuming that today is January 1, 2012, here's what happens when we use a datetime constant without a date: datetime myDate = D''; // 2012.01.01 00:00 datetime myDate = D'02:00'; // 2012.01.01 02:00

fbtrader commented 6 years ago

The DATE constant returns the date of compilation. It is the same as using a blank datetime constant. The DATETIME constant returns the current time and date on compilation

fbtrader commented 6 years ago

Constants You cannot assign a new value to a constant like you can for a variable. Global constants are defined in your program using the #define preprocessor directive.

define COMPANY_NAME "Easy Expert Forex"

By placing a const specifier before a variable declaration, you are indicating that the value of the variable cannot be changed:

const int cVar = 1; cVar = 2; // Compile error

fbtrader commented 6 years ago

Arrays An array is a variable of any type that can hold multiple values.

int myArray[3]; //3 positions myArray[0] = 1; myArray[1] = 2; myArray[2] = 3;

static array. A static array has a fixed size.

int myArray[3] = {1,2,3};

Array indexing starts at zero.

int myArray[3]; myArray[3] = 4; // This will cause a compile error

fbtrader commented 6 years ago

dynamic array is an array that is declared without a fixed size. Dynamic arrays must be sized before they can be used, and a dynamic array can be resized at any time. The ArrayResize() function is used to set the size of a dynamic array.

double myDynamic[]; ArrayResize(myDynamic,3); myDynamic[0] = 1.50;

fbtrader commented 6 years ago

Dynamic arrays are used in MQL5 for storing indicator values and price data. You will frequently declare dynamic arrays for use in MQL5 functions. The functions themselves will take care of properly sizing the array and filling it with data. When using dynamic arrays in your own code, be sure to size them using ArrayResize() first.

fbtrader commented 6 years ago

Multi-Dimensional Arrays

double myDimension[3][3]; myDimension[0][1] = 1.35;

array within an array

Two-dimensional arrays are very useful if you have a set of data that can be organized in a table format.

Only the first dimension of a multi-dimensional array can be dynamic. All other dimensions must have a static size declared. This is useful if you're not sure how many "rows" your table should have. Let's look at an example: double myDimension[][3]; int rows = 5; ArrayResize(myDimension,rows);

An array can have up to four dimensions, but it is rare that you will ever need more than two or three. In most cases, a structure would be an easier method of implementing a complex data structure

fbtrader commented 6 years ago

Iterating Through Arrays

string myArray[3] = {"cheese","bread","ale"}; for(int index = 0; index < 3; index++) { Print(myArray[index]); } // Output: cheese bread ale

fbtrader commented 6 years ago

the ArraySize() function can be used to determine the size of an array:

int myDynamic[]; ArrayResize(myDynamic,10); int size = ArraySize(myDynamic); for(int i = 0; i < size; i++) { myDynamic[i] = i; Print(i); // Output: 0, 1, 2... 9 }

fbtrader commented 6 years ago

Enumerations

An enumeration is an special integer type that defines a list of constants representing integer values

enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, };

So Sunday = 0, Monday = 1, Saturday = 6 and so on

MQL5 uses in the MqlDateTime structure for the day of the week ENUM_DAY_OF_WEEK enumeration also uses these values.

DayOfWeek Day; Day = Monday; Print(Day); // Output: 1

When we create an enumeration, the name of the enumeration becomes a type,

You can assign a value to each constant in an enumeration by using the assignment operator (=).

enum yearIntervals { month = 1, twoMonths, // 2 quarter, // 3 halfYear = 6, year = 12, };

All of the standard enumerations in MQL5 begin with ENUM_ and are all uppercase with underscore characters. You can view the standard enumerations in the MQL5 Reference under Standard Constants... > Enumerations and Structures.

fbtrader commented 6 years ago

Structures - pág 23