
MQL4共通関数はAlert・Print・Sleep・型変換などMT4 EA開発の基盤となる関数群だ。ログ出力からエラー処理・通知まで、コード付きで全関数を網羅する。

MQL4とMQL5の共通関数の違い
| 機能 | MQL4 | MQL5 |
|---|---|---|
| アラート | Alert() | Alert() |
| チャートコメント | Comment() | Comment() |
| ログ出力 | Print() | Print() |
| フォーマット出力 | PrintFormat()(ビルド600+) | PrintFormat() |
| メール送信 | SendMail() | SendMail() |
| プッシュ通知 | SendNotification() | SendNotification() |
| サウンド | PlaySound() | PlaySound() |
| 型変換(double) | DoubleToStr() | DoubleToString() |
| 型変換(str→num) | StrToDouble(), StrToInteger() | StringToDouble(), StringToInteger() |
出力関数
Print / PrintFormat
void OutputExamples()
{
// Print — エキスパートタブに出力
Print("メッセージ:", Symbol()," Price=", Ask);
// 複数引数を連結して出力
Print("Balance=", AccountBalance()," Equity=", AccountEquity()," Margin=", AccountMargin());
// PrintFormat — printf形式(ビルド600+)
PrintFormat("Ticket: %d, Price: %.5f, Lots: %.2f",
12345, Ask, 0.1);
PrintFormat("[%s] %s %s %.2f @ %.5f",
TimeToStr(TimeCurrent(), TIME_MINUTES),
Symbol(),"BUY", 0.1, Ask);
}Comment
void UpdateComment()
{
string text ="";
text +="=== EA Status ===n";
text +="Time:" + TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) +"n";
text +="Balance:" + DoubleToStr(AccountBalance(), 2) +"n";
text +="Equity:" + DoubleToStr(AccountEquity(), 2) +"n";
text +="Spread:" + IntegerToString((int)MarketInfo(Symbol(), MODE_SPREAD)) +" ptsn";
text +="Positions:" + IntegerToString(OrdersTotal()) +"n";
Comment(text); // チャートの左上に表示
// クリアする場合
// Comment("");
}Alert
void CheckAlertCondition()
{
static datetime lastAlert = 0;
// 1時間に1回だけアラート
if(TimeCurrent() - lastAlert < 3600)
return;
double dd = (AccountBalance() - AccountEquity()) / AccountBalance() * 100;
if(dd > 10) // ドローダウン10%超
{
Alert("警告: ドローダウン", DoubleToStr(dd, 1),"% -",
Symbol()," Balance=", AccountBalance());
lastAlert = TimeCurrent();
}
}- Print(): エキスパートタブに出力。デバッグに最適。パフォーマンスへの影響は小さい
- Comment(): チャートの左上に表示。リアルタイムモニタリングに最適。最大1行あたり約250文字
- Alert(): ダイアログ表示+サウンド。主要な通知のみに使用。OnTick内で多用するとパフォーマンスが劣化
- PrintFormat(): printf形式。複雑なログ出力に便利(ビルド600+)
- Print(): エキスパートタブに出力。デバッグに最適。パフォーマンスへの影響は小さい
- Comment(): チャートの左上に表示。リアルタイムモニタリングに最適
- Alert(): ダイアログ表示+サウンド。主要な通知のみに使用
- PrintFormat(): printf形式。複雑なログ出力に便利(ビルド600+)
通知関数
// メール送信(ツール→オプション→Eメールで設定が必要)
void SendTradeEmail(string action, int ticket, double lots, double price)
{
string subject = StringFormat("[%s] %s %s", action, Symbol(), TimeToStr(TimeCurrent()));
string body = StringFormat("Action: %snSymbol: %snTicket: %dnLots: %.2fnPrice: %.5fnBalance: %.2f",
action, Symbol(), ticket, lots, price, AccountBalance()
);
if(!SendMail(subject, body))
Print("メール送信失敗:", GetLastError());
}
// プッシュ通知(ツール→オプション→通知で設定が必要)
void SendPushNotification(string message)
{
if(!SendNotification(message))
Print("プッシュ通知失敗:", GetLastError());
}
// サウンド再生
void PlayTradeSound(bool isEntry)
{
if(isEntry)
PlaySound("alert.wav");
else
PlaySound("ok.wav");
}Sleep / GetTickCount
// Sleep — ミリ秒単位の待機
// ※ OnTick()内での使用は推奨されない(ティック処理がブロックされる)
void WaitAndRetry()
{
Sleep(1000); // 1秒待機
RefreshRates();
}
// GetTickCount — ミリ秒精度のタイマー
void MeasureExecutionTime()
{
uint startTime = GetTickCount();
// 計測したい処理
for(int i = 0; i < 1000; i++)
double ma = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, i);
uint elapsed = GetTickCount() - startTime;
Print("処理時間:", elapsed,"ms");
}EA状態チェック関数
| 関数 | 戻り値 | 説明 |
|---|---|---|
| IsStopped() | bool | EA停止が要求されているか |
| IsTradeAllowed() | bool | 自動売買が許可されているか |
| IsConnected() | bool | サーバーに接続されているか |
| IsDemo() | bool | デモ口座か |
| IsTesting() | bool | バックテスト中か |
| IsOptimization() | bool | 最適化中か |
| IsVisualMode() | bool | ビジュアルモードか |
| IsExpertEnabled() | bool | EA実行が有効か |
| IsTradeContextBusy() | bool | 取引コンテキストがビジーか |
| IsLibrary() | bool | ライブラリとして実行中か |
| UninitializeReason() | int | 終了理由コード |
| TerminalInfoInteger() | long | ターミナル情報(ビルド600+) |
bool CheckEAReady()
{
if(IsStopped())
{
Print("EA停止が要求されました");
return false;
}
if(!IsTradeAllowed())
{
Print("自動売買が許可されていません");
return false;
}
if(!IsConnected())
{
Print("サーバー未接続");
return false;
}
if(IsTradeContextBusy())
{
Print("取引コンテキストビジー");
return false;
}
return true;
}
// テスト環境の判定
void DetectEnvironment()
{
if(IsTesting())
{
if(IsOptimization())
Print("最適化モード");
else if(IsVisualMode())
Print("ビジュアルバックテスト");
else
Print("通常バックテスト");
}
else
{
if(IsDemo())
Print("デモ口座でライブ稼働");
else
Print("本番口座でライブ稼働");
}
}- IsTesting()中はSendMail/SendNotification/Alert/PlaySoundが実行されても実際には送信されない(サイレント)
- Sleep()はバックテスト中は無視される(0ミリ秒で即座に完了)
- IsVisualMode()がtrueの場合のみ、Comment()やオブジェクト描画が表示される
- 最適化中(IsOptimization())はPrint()すらパフォーマンスに影響するため、最小限に抑える
- IsTesting()中はSendMail/SendNotification/Alert/PlaySoundが実行されても実際には送信されない(サイレント)
- Sleep()はバックテスト中は無視される(0ミリ秒で即座に完了)
- IsVisualMode()がtrueの場合のみ、Comment()やオブジェクト描画が表示される
- 最適化中(IsOptimization())はPrint()すらパフォーマンスに影響するため、最小限に抑える
型変換関数
| 関数 | 変換 | 例 |
|---|---|---|
| NormalizeDouble(v, d) | double → 正規化double | NormalizeDouble(1.23456, 3) = 1.235 |
| DoubleToStr(v, d) | double → string | DoubleToStr(1.5, 2) =”1.50″ |
| IntegerToString(v) | int → string | IntegerToString(42) =”42″ |
| StrToDouble(s) | string → double | StrToDouble(“1.5”) = 1.5 |
| StrToInteger(s) | string → int | StrToInteger(“42”) = 42 |
| TimeToStr(t, mode) | datetime → string | TimeToStr(now) =”2024.06.15 14:30″ |
| StrToTime(s) | string → datetime | StrToTime(“2024.06.15”) = datetime |
| ColorToString(c) | color → string | ColorToString(clrRed) =”255,0,0″ |
| StringToColor(s) | string → color | StringToColor(“255,0,0”) = clrRed |
| CharToStr(c) | int → string | CharToStr(65) =”A” |
| StringGetChar(s, p) | string → int | StringGetChar(“A”, 0) = 65 |
RefreshRates()はMQL4特有の重要関数です。MQL5では不要ですが、MQL4ではSleep()後やリトライ前に呼ばないと古い価格でOrderSendしてしまいERR_REQUOTE(138)が発生します。OrderSendのリトライループには必ずRefreshRates()を入れましょう。
RefreshRates()はMQL4特有の重要関数です。MQL5では不要ですが、MQL4ではSleep()後やリトライ前に呼ばないと古い価格でOrderSendしてしまいERR_REQUOTE(138)が発生します。OrderSendのリトライループには必ずRefreshRates()を入れましょう。
その他の共通関数
| 関数 | 説明 |
|---|---|
| ExpertRemove() | EAを自動的にチャートから削除 |
| TerminalClose(reason) | ターミナルを終了 |
| MessageBox(text, caption, flags) | メッセージボックス表示(ライブのみ) |
| RefreshRates() | 最新レートを再取得 |
| MarketInfo() | マーケット情報取得 |
| GetTickCount() | ミリ秒タイマー |
| MathRand() | 乱数生成 |
実践: 総合的な通知システム
enum NOTIFY_LEVEL
{
NOTIFY_LOG, // Printのみ
NOTIFY_ALERT, // Alert + Print
NOTIFY_PUSH, // Push通知 + Print
NOTIFY_ALL // 全通知
};
input NOTIFY_LEVEL NotifyLevel = NOTIFY_ALERT;
void Notify(string message, NOTIFY_LEVEL level = NOTIFY_LOG)
{
// 常にPrint
Print("[", Symbol(),"]", message);
if(IsTesting()) return; // テスト中は通知しない
if(level >= NOTIFY_ALERT && NotifyLevel >= NOTIFY_ALERT)
Alert(message);
if(level >= NOTIFY_PUSH && NotifyLevel >= NOTIFY_PUSH)
SendNotification(Symbol() +":" + message);
if(NotifyLevel >= NOTIFY_ALL)
{
string subject ="[EA]" + Symbol();
SendMail(subject, message);
}
}
// 使用例
void OnTradeEvent(string action, int ticket)
{
string msg = StringFormat("%s ticket=%d lots=%.2f profit=%.2f",
action, ticket, OrderLots(), OrderProfit());
Notify(msg, NOTIFY_ALERT);
}共通関数一覧(MQL5対応表)
| MQL4関数 | MQL5対応 | 説明 |
|---|---|---|
| Alert() | Alert() | アラートダイアログ |
| Comment() | Comment() | チャートコメント |
| Print() | Print() | エキスパートログ出力 |
| PrintFormat() | PrintFormat() | フォーマット出力 |
| SendMail() | SendMail() | メール送信 |
| SendNotification() | SendNotification() | プッシュ通知 |
| PlaySound() | PlaySound() | サウンド再生 |
| Sleep() | Sleep() | ミリ秒待機 |
| GetTickCount() | GetTickCount() | ミリ秒タイマー |
| IsTesting() | MQLInfoInteger(MQL_TESTER) | テスト中判定 |
| IsOptimization() | MQLInfoInteger(MQL_OPTIMIZATION) | 最適化中判定 |
| IsTradeAllowed() | MQLInfoInteger(MQL_TRADE_ALLOWED) | 取引許可判定 |
| IsConnected() | TerminalInfoInteger(TERMINAL_CONNECTED) | 接続状態判定 |
| IsDemo() | AccountInfoInteger(ACCOUNT_TRADE_MODE)==DEMO | デモ口座判定 |
| RefreshRates() | 不要(自動更新) | レート再取得 |
| ExpertRemove() | ExpertRemove() | EA自動削除 |
| DoubleToStr() | DoubleToString() | double→文字列 |
| StrToDouble() | StringToDouble() | 文字列→double |
| StrToInteger() | StringToInteger() | 文字列→int |
まとめ
MQL4の共通関数はEA開発の基盤となる機能を提供します。確認しておきたいポイントは以下の通りです。
- Print()でデバッグ、Comment()でリアルタイム表示 — 開発中はPrint()、本番ではComment()を使い分ける
- IsTesting()でテスト/本番を分岐 — 通知やSleep()はテスト中にスキップする
- RefreshRates()はリトライ前に必ず呼ぶ — MQL5では不要だがMQL4では必須
- DoubleToStr()→DoubleToString()の名称変更に注意(MQL5移植時)
全13カテゴリの学習は、MQLリファレンス総合ガイドを起点に進めましょう。MQL4の基本を押さえたら、実際のEA開発に取り組んでみてください。
MQL4共通関数 よくある質問
MQL4の共通関数にはどんなものがありますか?
Print()でログ出力、Alert()でダイアログ通知、Comment()でチャート表示、Sleep()で待機が基本です。GetLastError()/ResetLastError()でエラー処理を行い、TimeCurrent()で現在時刻を取得します。
MQL4でエラーコードを取得する方法は?
ResetLastError()でエラーをリセット→処理実行→GetLastError()でコード取得が正しいパターンです。ErrorDescription()でエラーコードを文字列に変換できます。取引関数の失敗後は必ずGetLastError()で確認しましょう。
MQL4の型変換関数の使い方は?
数値→文字列はDoubleToStr()/IntegerToString()、文字列→数値はStrToDouble()/StrToInteger()を使います。NormalizeDouble()で小数点以下の桁数を揃える処理は必須です。MQL5ではDoubleToString()と関数名が変わりました。
MQL4でデバッグに役立つ関数は?
Print()で実行ログを記録し、Comment()でチャートにリアルタイム情報を表示するのが基本です。Alert()はポップアップで即時通知でき、特定イベントの検知に使えます。__FUNCTION__や__LINE__マクロと組み合わせてエラー箇所を特定できます。
MQL4とMQL5の共通関数の違いは?
基本的な関数名はほぼ同じです。MQL5ではPrintFormat()の追加、DoubleToStr()→DoubleToString()への統一、GetMicrosecondCount()による高精度タイマーが主な変更点です。MQL4からの移行は関数名の末尾の変更に注意してください。
MQL4でSendNotification()を使う設定方法は?
MetaTraderスマートフォンアプリをインストールし、MT4のツール→オプション→通知でMetaQuotes IDを設定します。SendNotification(“メッセージ”)でスマートフォンにプッシュ通知を送れます。送信失敗時はGetLastError()でエラーコードを確認します。
MQL4リファレンス 関連記事
- MQL4 取引関数 — OrderSend・OrderClose・ポジション管理
- MQL4 インジケーター関数 — iMA・iRSI・iCustom
- MQL4 口座・マーケット情報 — ロット計算
- MQL4 イベントハンドラ — OnInit・OnTick
- MQL4 エラーコード — リトライパターン
- MQL4 時間・日付関数 — 取引時間フィルター
- EA始め方ガイド — MT4へのEA設定手順
⚠ リスクに関する注意事項
本記事は情報提供を目的としており、特定の金融商品の売買を推奨するものではありません。FX取引はレバレッジ取引の特性上、預託した証拠金以上の損失が生じる可能性があります。取引の際は、ご自身の判断と責任において行ってください。詳しくは特定商取引法に基づく表記をご確認ください。






