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>
#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.
| 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
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
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
Example: Moving Average indicator filling by different colors
MACD Sample
Classical MACD Sample.