Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Hotkeys: keyboard shortcuts for MQL5 experts and indicators, set in the inputs - library for MetaTrader 5

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.
Views:
13
Rating:
(1)
Published:
hotkeys-output.png (110.61 KB)
\MQL5\Include\Hotkeys\
Hotkeys.mqh (19.86 KB) view
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Hotkeys gives your MQL5 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 positions
            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 demo account, with each shortcut pressed on the chart.

The Experts log of the example at start and after six 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.

Daily loss and max loss guard - prop firm rules in one include Daily loss and max loss guard - prop firm rules in one include

Watches the equity like a prop firm does: daily loss limit and max loss limit, static or trailing, floating profit included. Stops new trades at a warning level, closes everything on a breach and remembers it after a restart.

Return-to-Mean Clock: How Many Bars Does Price Take to Come Back to the Moving Average Return-to-Mean Clock: How Many Bars Does Price Take to Come Back to the Moving Average

Measures how long price takes to come back to its moving average. The histogram shows the distance of the close from an EMA in ATR. Every time that distance first reaches a stretch level (1, 2 and 3 ATR by default), the indicator follows the stretch until the close is back at the average, runs further, or the wait is over. A panel shows, for each level, how often price came back, how often it ran further first, and the median and 80th-percentile number of bars the return took. One line can be pasted into a comment to compare symbols. It is a statistics tool and gives no trade signals.

Inside Bar Engulfing Scanner Inside Bar Engulfing Scanner

Engulfing and inside bar scanner for Gold (XAUUSD) and any symbol on MT5. Marks bullish and bearish engulfing candles and inside bars on closed bars, draws the mother bar range as a box and can alert on each pattern. Arrow buffers for iCustom. Open source code.

Multi Timeframe Trend Dashboard Multi Timeframe Trend Dashboard

Multi timeframe trend dashboard for Gold (XAUUSD) and any symbol on MT5. Shows up, down or flat for up to 6 timeframes from a fast and slow EMA on the last closed bar, with the distance in points and an alert when all line up. Open source code.