無料でロボットをダウンロードする方法を見る
Telegram上で私たちを見つけてください。
私たちのファンページに参加してください
興味深いスクリプト?
それではリンクにそれを投稿してください。-
他の人にそれを評価してもらいます
スクリプトを気に入りましたか?MetaTrader 5ターミナルの中でそれを試してみてください。
ライブラリ

Hotkeys: keyboard shortcuts for MQL4 experts and indicators, set in the inputs - MetaTrader 4のためのライブラリ

George Ciprian Mandache
George Ciprian Mandache
Mostly writing code that does stuff...

User guides are on my blog. For questions or bugs, write in the product's Comments tab or send me a message.
ビュー:
57
パブリッシュ済み:
\MQL4\Include\Hotkeys\
Hotkeys.mqh (19.86 KB) ビュー
MQL5フリーランス このコードに基づいたロボットまたはインジケーターが必要なら、フリーランスでご注文ください フリーランスに移動

Hotkeys gives your MQL4 expert or indicator keyboard shortcuts. It is one include file with no other dependencies. The user sets each shortcut as text in the inputs, such as Shift+B or Ctrl+Shift+1, and your OnChartEvent gets back the action that was pressed, ready for a switch.

A shortcut that cannot work is refused when the program starts, with the reason: a key another action already uses, a Ctrl shortcut the terminal keeps for itself, or Alt, which the terminal never reports. The user finds out at start instead of pressing a key that does nothing.

#include <Hotkeys\Hotkeys.mqh>

Features

  • Shortcuts are inputs. The user types "Shift+B", "ctrl + shift + 1" or just "B", in any case and with any spaces. An empty input or "none" switches that shortcut off.
  • Modifiers match exactly. Shift+B fires only on Shift+B, never on a plain B or on Ctrl+Shift+B, so a trader who types on the chart does not open a trade by accident.
  • One press, one action. A key press that the terminal marks as an auto-repeat of a held key is ignored, so holding a key down does not fire its action again and again.
  • No key fires two actions. Binding a shortcut that another action already has is refused, and the old binding stays as it was.
  • Clear reasons. Every refusal says what is wrong and what to use instead, starting with the action's name, so you can show it to the user as it is.
  • Letters A to Z and digits 0 to 9 can be bound alone, with Shift or with Ctrl+Shift. The F keys, Esc, the arrows and plain Ctrl shortcuts belong to the terminal and are refused.
  • Up to 64 actions. An action is any int you pick, so your own enum works.
  • SetEnabled(false) turns every shortcut off, for example while your panel's text box has the keyboard.
  • Every call that can fail returns HOTKEYS_OK or a HOTKEYS_ERROR_* code with the reason, and the library never prints anything by itself.

Example

#include <Hotkeys\Hotkeys.mqh>

enum ENUM_ACTION { ACTION_BUY, ACTION_SELL, ACTION_CLOSE_ALL };

input string InpBuyKey = "Shift+B";        // Buy
input string InpSellKey = "Shift+S";       // Sell
input string InpCloseKey = "Ctrl+Shift+X"; // Close all

CHotkeys keys;

int OnInit() {
    string error;
    if (keys.Bind(ACTION_BUY, "Buy", InpBuyKey, error) != HOTKEYS_OK ||
        keys.Bind(ACTION_SELL, "Sell", InpSellKey, error) != HOTKEYS_OK ||
        keys.Bind(ACTION_CLOSE_ALL, "Close all", InpCloseKey, error) != HOTKEYS_OK) {
        Alert(error);                       // e.g. Sell: Shift+B is already bound to Buy
        return INIT_PARAMETERS_INCORRECT;
    }
    return INIT_SUCCEEDED;
}

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
    switch (keys.ChartEvent(id, lparam, dparam, sparam)) {
        case ACTION_BUY:
            // open a buy
            break;
        case ACTION_SELL:
            // open a sell
            break;
        case ACTION_CLOSE_ALL:
            // close the open orders
            break;
    }
}

The attached HotkeysExample expert binds four shortcuts from its inputs: Shift+H draws a horizontal line at the bid, Shift+D deletes those lines, Shift+G shows or hides the grid and Shift+Q prints the quote. It places no orders. At start it prints its bindings and the reasons Hotkeys gives for five shortcuts that cannot work:

bound action=Line key=Shift+H
bound action=Clear key=Shift+D
bound action=Grid key=Shift+G
bound action=Quote key=Shift+Q
refused text=Shift+B reason="Sell: Shift+B is already bound to Buy"
refused text=Ctrl+B reason="Sell: Ctrl+B is a terminal shortcut, use Ctrl+Shift+B or Shift+B"
refused text=Alt+B reason="Sell: Alt is not available, the terminal reports only Shift and Ctrl"
refused text=F5 reason="Sell: F5 is a terminal key, use A to Z or 0 to 9"
refused text=Shift+Shift+S reason="Sell: "Shift+Shift+S" names a modifier twice"

The picture is a real run on a MetaTrader 4 demo account, with the shortcuts pressed on the chart.

The Experts log of the example on MetaTrader 4 at start and after five key presses

API

Call What it does
Bind(action, name, chord, error) Bind an action to a shortcut written as text, or replace its shortcut; "" or "none" leaves it with no key
BindKey(action, name, key, modifiers, error) The same with a key code and the HOTKEYS_SHIFT and HOTKEYS_CTRL bits
ChartEvent(id, lparam, dparam, sparam) Call from OnChartEvent; returns the action pressed or HOTKEYS_NO_ACTION
Match(key, repeats, flags, modifiers) The same check with the modifiers given, for your own tests
Unbind(action), Clear() Remove one action or all of them
SetEnabled(enabled), IsEnabled() Turn every shortcut off or on
Count(), Action(i), Name(i), Key(i), Modifiers(i), Label(i) Read the bindings, for example to list them on a panel
LabelOf(action), NameOf(action) The shortcut ("Ctrl+Shift+X") and name of one action
HotkeyParse(text, key, modifiers, error), HotkeyLabel(key, modifiers), HotkeyCheck(key, modifiers, error) Read, write or check a single shortcut

CHotkeys owns no pointers, so it can be copied by assignment.

Backtest Data Preflight: check broker bar history before a backtest Backtest Data Preflight: check broker bar history before a backtest

Check the first and last available bars and the gaps between bars for a date range before a backtest in the MetaTrader 4 Strategy Tester.

CustomStopOut: your own margin call and stop-out levels, worst order closed first CustomStopOut: your own margin call and stop-out levels, worst order closed first

A script that sets your own margin call and stop-out levels above the broker's. It alerts when the margin level falls below your margin call level and, below your stop-out level, closes open orders one at a time, the worst net result first, until the level is back up, the way a broker stop-out works.

Three Colors Three Colors

Example: Moving Average indicator filling by different colors

MACD Sample MACD Sample

Classical MACD Sample.