【MQLリファレンス】データ型一覧 プログラム例あり

【辞書】MQLリファレンス
朝日奈りさ
朝日奈りさ

記事が長いので Ctrl+f で検索することをオススメします。

データ型とは

新しく変数を作成するときに「int」「double」を書きます。

それがデータ型です。

MQLでは、多くのデータ型が用意されています。

細かく分類して、詳しく紹介します。

 

整数型(char, short, int, long, uchar, ushort, uint, ulong)

データ型使用方法
char最小値は -128 で最大値は 127 です。
short最小値は -32,768 で最大値は 32,767 です。
int最小値は -2,147,483,648 で最大値は 2,147,483,647 です。
long最小値は -9,223,372,036,854,775,808 で最大値は 9,223,372,036,854,775,807 です。
uchar最小値は 0 で最大値は 255 です。
ushort最小値は 0 で最大値は 65,535 です。
uint最小値は 0 で最大値は 4,294,967,295 です。
ulong最小値は 0 で最大値は 18,446,744,073,709,551,615 です。

使用例

char   ch=12;
short  sh=-5000;
int    in=2445777;
long   lo=-5111222333444;
uchar  uch=200;
ushort ush=62111;
uint   uin=3111222333;
ulong  ulo=10111222333444555666;

注意点とサンプルプログラム

マイナスを表現できない整数型(uchar, ushort, uint, ulong)に、マイナス値を代入すると、予期しない結果になります。

たとえば、下記のプログラムを実行すると、無限ループになります。

//--- 無限ループ
void OnTick()
 {
  uchar  u_ch;
 
  for(char ch=-128;ch<128;ch++)
    {
     u_ch=ch;
    Print("ch = ",ch," u_ch = ",u_ch);
    }
 }

 

論理型(bool)

データ型使用方法
bool「true」 または 「false」 の論理値を代入できます。
それぞれの数値として表現すると 1 と 0 です。

使用例

bool a = true;
bool b = false;
bool c = 1;

注意点とサンプルプログラム

boolは「true」や「false」といった値を代入できますが、システム上では数値として扱われます。

そのため、0 は「false」と判断され、それ以外の値は「ture」と判断されます。

  int i=5;
  double d=-2.5;

  //i=5だがtrueとして処理される
  if(i) Print("i = ",i," and is set to true");
  else Print("i = ",i," and is set to false");
 
  //d=-2.5だがtrueとして処理される
  if(d) Print("d = ",d," and has the true value");
  else Print("d = ",d," and has the false value");
 
  i=0;
  //i=0なので、falseとして処理される
  if(i) Print("i = ",i," and has the true value");
  else Print("i = ",i," and has the false value");
 
  d=0.0;
  //d=0.0なので、falseとして処理される
  if(d) Print("d = ",d," and has the true value");
  else Print("d = ",d," and has the false value");
 
//--- 実行結果
//   i= 5 and has the true value
//   d= -2.5 and has the true value
//   i= 0 and has the false value
//   d= 0 and has the false value

 

リテラル型(ushort)

データ型使用方法
ushortASCIIコード(文字定数)を代入できます。ASCIIを代入する場合はシングルクォーテーション(‘)で囲みます。

使用例

    string str_test = "";
    ushort temp_char = '0';
    
    //temp_charに 0 を代入して表示
    temp_char = '0';
    StringSetCharacter(str_test,0,temp_char);
    Print(str_test);                  // 0 を表示

    //temp_charにASCIIコードの 0 を代入して表示
    temp_char = '\x30';
    StringSetCharacter(str_test,0,temp_char);
    Print(str_test);                  // 0 を表示

    //temp_charにASCIIコードの 0 を代入して表示
    temp_char = 0x30;
    StringSetCharacter(str_test,0,temp_char);
    Print(str_test);                  // 0 を表示

サンプルプログラム

void OnTick()
 {

//--- 文字定数を宣言する
  int a='A';
  int b='$';
  int c='©';     // コード 0xA9
  int d='\xAE';   // シンボル ® のコード

//--- 定数を出力する
  Print(a,b,c,d);

//--- 文字列に文字を追加する
  string test="";
  StringSetCharacter(test,0,a);
  Print(test);

//--- 文字列内の文字を置換する
  StringSetCharacter(test,0,b);
  Print(test);

//--- 文字列内の文字を置換する
  StringSetCharacter(test,0,c);
  Print(test);

//--- 文字列内の文字を置換する
  StringSetCharacter(test,0,d);
  Print(test);

//--- 文字を数として表す
  int a1=65;
  int b1=36;
  int c1=169;
  int d1=174;

//--- 文字列に文字を追加する
  StringSetCharacter(test,1,a1);
  Print(test);

//--- 文字列に文字を追加する
  StringSetCharacter(test,1,b1);
  Print(test);

//--- 文字列に文字を追加する
  StringSetCharacter(test,1,c1);
  Print(test);

//--- 文字列に文字を追加する
  StringSetCharacter(test,1,d1);
  Print(test);
 }

 

文字列型(string)

データ型使用方法
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";

 

浮動小数点型(double, float)

データ型使用方法
double小数点を持つ数値を代入することができます。数値を表現する場合は、doubleを使うのが一番便利です。
float小数点を持つ数値を代入することができます。doubleの半分の数値しか代入できないため、メモリ節約などの理由がない限り、使用する場面は限られます。

使用例

  double a=12.111;
  double b=-956.1007;
  float c =0.0001;
  float d =16;

注意点とサンプルプログラム

小数点を含む数値を計算した後、イコール(=)で比較することはできません。

よく間違えるし、エラーも出ないので、注意しましょう。

void OnTick()
 {
//---
  double three=3.0;
  double x,y,z;

  x=1/three;
  y=4/three;
  z=5/three;

  if(x+y==z)
     Print("1/3 + 4/3 == 5/3");
  else
     Print("1/3 + 4/3 != 5/3");

// 結果:1/3 + 4/3 != 5/3
// 小数点以下の計算をした後、==は正しく機能しません。
 }

 

カラー型(color)

データ型使用方法
color色の情報を代入することができます。RGBで代入できますが、色の名称で簡単に指定できるため、そちらを使った方が良いでしょう。

使用例

//--- リテラル
color gray = C'128,128,128';  // 灰色
color blue = C'0x00,0x00,0xFF'; // 青色
// 色の名称
color red = clrRed;           // 赤色
color yellow = clrYellow;         // 黄色
color black = clrBlack;         // 黒色
//--- 整数表現
color white1 = 0xFFFFFF;         // 白色
color white2 = 16777215;         // 白色
color green1 = 0x008000;         // 緑色
color green2 = 32768;             // 緑色

 

日付時刻型(datetime)

データ型使用方法
datetime日付と時刻を代入できます。値の範囲は1970年1月1日から3000年12月31日までです。

使用例

datetime NY=D'2015.01.01 00:00';     // 2015 年の初めの時刻
datetime d1=D'1980.07.19 12:30:27'; // 年月日時分秒
datetime d2=D'19.07.1980 12:30:27'; // D'1980.07.19 12:30:27' と同じ
datetime d3=D'19.07.1980 12';       // D'1980.07.19 12:00:00' と同じ
datetime d4=D'01.01.2004';           // D'01.01.2004 00:00:00' と同じ
datetime compilation_date=__DATE__;             // コンパイル日付
datetime compilation_date_time=__DATETIME__;   // コンパイル日付と時刻
datetime compilation_time=__DATETIME__-__DATE__;// コンパイル時刻

//--- コンパイラが警告を返す宣言の例
datetime warning1=D'12:30:27';       // D'2024/04/26 12:30:27' と同じ
datetime warning2=D'';               // __DATETIME__ と同じ

 

列挙型(enum)

データ型使用方法
enum定数を列挙することができます。デフォルトで用意されている定数だけでなく、自分で宣言した定数も代入することができます。ただしプログラムの途中で変更することはできません。

使用例

enum months // 名前付き定数の列挙
  {
   January,
   February,
   March,
   April,
   May,
   June,
   July,
   August,
   September,
   October,
   November,
   December
  };

 

構造体(複合データ型 struct)

データ型使用方法
structvoid型以外のデータ型を代入できます。オブジェクト指向のプログラミングで良く使われます。

使用例

struct trade_settings
 {
  uchar  slippage;     // 使用可能の slippage サイズが 1 バイト
  char   reserved1;   // 1 バイトを抜かす
  short  reserved2;   // 2 バイトを抜かす
  int    reserved4;   // 4 バイトを抜かし、8 バイト境界の整列を保証する。
  double take;         // 利益固定の値
  double stop;         // プロテクティブストップの値
 };

 

クラス(複合データ型 class)

データ型使用方法
class基本的には構造体と同じですが、クラスと構造体の異なる点は次の通りです。
・クラスの宣言には class キーワードが使用されます。
・クラスメンバは、他の値が示されていない限り、デフォルトで private アクセス指定子を持っています。構造体のデータメンバは、他の値が示されていない限り、デフォルトで public アクセスを持っています。
・クラスオブジェクトは、クラス内で仮想関数が宣言されない場合でも常に仮想関数の表を持っています。構造体は仮想関数を持つことが出来ません。
・new 演算子はクラスオブジェクトに適用することが出来ますが、構造体に適用することは出来ません。
・クラスは他のクラスからのみ継承でき、構造体は他の構造体のみから継承出来ます。

使用例

//+------------------------------------------------------------------+
//| 日付を操作するクラス                                                   |
//+------------------------------------------------------------------+
class MyDateClass
 {
private:
  int               m_year;         // 年
  int               m_month;         // 月
  int               m_day;           // 日
  int               m_hour;         // 時間
  int               m_minute;       // 分
  int               m_second;       // 秒
public:
  //--- デフォルトコンストラクタ
                    MyDateClass(void);
  //--- パラメトリックコンストラクタ
                    MyDateClass(int h,int m,int s);
 };

 

コメント