Data Type (資料型別)

Type Size in Bytes Range
bool 1 0 to 1
char 1 -2^7 to 2^7 -1
uchar 1 2^8 -1
short 2 -2^15 to 2^15 -1
ushort 2 0 to 2^16 - 1
int 4 -2^31 to 2^31 -1
uint 4 0 to 2^32 - 1
long 8 -2^63 to 2^63 - 1
ulong 8 0 to 2^64 - 1
flost 4 1.175494351e-38 to 3.402823466e+38 and -1.175494351e-38 to -3.402823466e+38
double 8 2.2250738585072014e-308 to 1.7976931348623158e+308 and -2.2250738585072014e-308 to -1.7976931348623158e+308
color 4 -1 to 0xFFFFFF
datatime 8 0 (1970.01.01 0:00:00) to 32535244799 (3000.12.31 23:59:59)
enum 4 預設值由 0 往下遞減
string dependent on length Ignore
struct or class dependent on member size Ignore

數值皆可用 0x 作為 prefix 當作 16 進位表示

Example

  • string

    string svar="This is a character string";
    string svar2=StringSubstr(svar,0,4);
    Print("Copyright symbol\t\x00A9");
    FileWrite(handle,"This string contains a new line symbols \n");
    string MT4path="C:\\Program Files\\MetaTrader 4";
    
  • color

    //--- Literals
    C'128,128,128'    // Gray
    C'0x00,0x00,0xFF' // Blue
    //color names
    clrRed               // Red
    clrYellow            // Yellow
    clrBlack             // Black
    //--- Integral representations
    0xFFFFFF          // White
    16777215          // White
    0x008000          // Green
    32768             // Green
    
  • Datetime

    datetime NY=D'2015.01.01 00:00';     // Time of beginning of year 2015
    datetime d1=D'1980.07.19 12:30:27';  // Year Month Day Hours Minutes Seconds
    datetime d2=D'19.07.1980 12:30:27';  // Equal to D'1980.07.19 12:30:27';
    datetime d3=D'19.07.1980 12';        // Equal to D'1980.07.19 12:00:00'
    datetime d4=D'01.01.2004';           // Equal to D'01.01.2004 00:00:00'
    datetime compilation_date=__DATE__;             // Compilation date
    datetime compilation_date_time=__DATETIME__;    // Compilation date and time
    datetime compilation_time=__DATETIME__-__DATE__;// Compilation time
    //--- Examples of declarations after which compiler warnings will be returned
    datetime warning1=D'12:30:27';       // Equal to D'[date of compilation] 12:30:27'
    datetime warning2=D'';               // Equal to __DATETIME__
    

String (字串)

因為指標的操作在 MQL4/5 封印, 所以針對字串也有自己的 type, 以及 內建函式

Array (陣列)

static array

MQL4 將宣告固定 size 的 array 稱作 static array

dynamic array

第一維度尚未定義的 array ex. double array[][5]、 int array[] ...etc , 從 C 語言的觀點就僅是宣告個指標的概念, 而這個實為指標的變數可透過 MQL4 內建的 Array 函式 來操作 (因為 MQL4/5 的語法將一般的指標操作行為都封印住了)。

Type Casting (型別轉換)

一般而言是不太建議在程式過程中做 非必要性 的型別轉換, 尤其是 Narrowing Conversion (Size 大的 type 轉成小的 type) 會導致 data loss。

  • Implicit Conversion (隱式轉型)

隱式轉型會自動發生在以下幾種時機

  1. 不同型態變數之間的指派
    float a = 5.7;
    int b = a; //result b=5
    
  2. 多種型態的運算式
    float a=5.3;
    int b=3;
    a = a + b; //result a=8.3
    
  • Explicit Conversion (顯式轉型)
    var_1 = (type)var_2;
    

results matching ""

    No results matching ""