LoginSignup
1
1

More than 3 years have passed since last update.

MT4 で実行可能なプログラムの種類

Posted at

はじめに

MT4 では3種類のプログラムがあります。

  • EA (Expert Advisor)
  • カスタムインジケーター
  • スクリプト

イベントハンドラ

各プログラムにはイベントハンドラと呼ばれる MT4 のシステムから呼び出される関数があります。
自分で作成したプログラムでは必要なイベントハンドラのみ定義すれば自動的に呼び出されます。
また、イベントハンドラはプログラムの種類によって呼び出されるものと呼び出されないものがありますので注意が必要です。コンパイル時に警告を出してくれるものもあります。

EA (Expert Advisor)

自動売買を実行するためのプログラムで、実行すると停止するまでチャート上に常駐します。
定期的に OnTick() が呼び出されますので、自動売買のロジックはこのハンドラに記載します。
一つのチャート上では一つだけ実行が可能です。

イベントハンドラ

int OnInit();
void OnDeinit(const int);
void OnTick();
void OnTimer();
double OnTaster();
void OnChartEvent(const int, const long &, const double &, const string &);

サンプルプログラム

#property version   "1.00"
#property strict

int OnInit()
{
    Print(__FUNCTION__);
    EventSetTimer(60);
    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
    Print(__FUNCTION__);
    EventKillTimer();
}

void OnTick()
{
    Print(__FUNCTION__);
}

void OnTimer()
{
    Print(__FUNCTION__);
}

double OnTester()
{
    Print(__FUNCTION__);
    return 0.0;
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    Print(__FUNCTION__);
}

カスタムインジケーター

チャートをカスタムするためのプログラムで、実行するとインジケーターから削除するまでチャート上に常駐します。
一つのチャート上で複数の実行が可能です。

イベントハンドラ

int OnInit();
void OnDeinit(const int);
int OnCalculate(const int, const int, const datetime &[], const double &[], const double &[], const double &[], const double &[], const long &[], const long &[], const int &[]);
void OnTick();
void OnTimer();
void OnChartEvent(const int, const long &, const double &, const string &);

サンプルプログラム

#property version   "1.00"
#property strict
#property indicator_chart_window

int OnInit()
{
    Print(__FUNCTION__);
    EventSetTimer(60);
    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
    Print(__FUNCTION__);
    EventKillTimer();
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    Print(__FUNCTION__);
    return rates_total;
}

void OnTick()
{
    Print(__FUNCTION__);
}

void OnTimer()
{
    Print(__FUNCTION__);
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    Print(__FUNCTION__);
}

スクリプト

実行すると OnStart() が呼び出されて、それが完了次第プログラムは終了します。

イベントハンドラ

int OnInit();
void OnDeinit(const int);
void OnStart();

サンプルプログラム

#property version   "1.00"
#property strict

int OnInit()
{
    Print(__FUNCTION__);
    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
    Print(__FUNCTION__);
}

void OnStart()
{
    Print(__FUNCTION__);
}

おわりに

今回紹介した3種類のプログラムはそれぞれ用途が違いますので、目的に応じたものを選択するようにしてください。
また、イベントハンドラについては後日紹介します。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1