MQL5 Hotkeys: Assign Keyboard Shortcuts to Experts and Indicators via Inputs

MQL5 Hotkeys: Assign Keyboard Shortcuts to Experts and Indicators via Inputs
Download ALL MT5 experts (1835)
YouTube Video Thumbnail



Similar MetaTrader Tools

MQL5 Hotkeys: Assign Keyboard Shortcuts to Experts and Indicators via Inputs

Info

The MQL5 Hotkeys: Assign Keyboard Shortcuts to Experts and Indicators via Inputs is a Expert Advisor for MetaTrader 5 that hotkeys allows you to assign keyboard shortcuts to your mql5 experts or indicators. This utility is provided as a single include file with no external dependencies.

Usage

This tool is typically used for automated trading on major forex pairs and gold.

Platform

This Expert Advisor works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Experts folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use MQL5 Hotkeys: Assign Keyboard Shortcuts to Experts and Indicators via Inputs

1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.

2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.

3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.

4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.

Frequently Asked Questions

Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.

Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.

Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.

What this tool does

Hotkeys allows you to assign keyboard shortcuts to your MQL5 experts or indicators.

Typical Use Case

This Expert Advisor excels in automated trading and technical analysis on MetaTrader 5.

Compatible Platform & Setup

This Expert Advisor works on MetaTrader 5. Place the file in the MQL5/Experts folder and restart the terminal.

Description & Settings

Related: Canvas Tabs: A Lightweight Tab Bar for MQL5 using C Canvas - another powerful expert for MetaTrader 5 traders.

Hotkeys allows you to assign keyboard shortcuts to your MQL5 experts or indicators. This utility is provided as a single include file with no external dependencies.

Also recommended: Xlsx Writer: Generate Excel .xlsx Files from MQL5 Without External Libraries - similar expert with strong performance on MetaTrader 5.


Users define each shortcut directly within the input parameters, using formats like Shift+B or Ctrl+Shift+1. The OnChartEvent function then receives the pressed action, ready for processing.

Invalid shortcuts are detected and rejected when the program starts, with clear explanations provided. Reasons for rejection include a key already being in use by another action, a Ctrl shortcut reserved by the terminal, or the use of the Alt key, which the terminal does not report.

#include
<Hotkeys\Hotkeys.mqh>


Features

Shortcuts as Inputs: Users can input shortcut definitions like "Shift+B", "ctrl + shift + 1", or simply "B", accommodating various casing and spacing. An empty input or "none" will disable the shortcut.

Exact Modifier Matching: A shortcut like Shift+B will only trigger on that specific combination, not on a plain B or Ctrl+Shift+B. This prevents accidental actions, such as opening a trade while typing on the chart.

Single Action Per Press: Key presses identified as auto-repeats from holding a key down are ignored. This ensures an action is executed only once per press, not repeatedly.

Unique Action Binding: Assigning a shortcut that is already in use by another action is prohibited. The existing binding remains unchanged.

Clear Rejection Reasons: When a shortcut cannot be bound, the system provides a clear explanation, including the name of the action and the specific issue, making it easy to inform the user.

Assignable Keys: Letters A to Z and digits 0 to 9 can be bound individually, with Shift, or with Ctrl+Shift. Function keys (F1-F12), Esc, arrow keys, and plain Ctrl shortcuts are reserved by the terminal and cannot be used.

Up to 64 Actions: You can bind up to 64 distinct actions. An action can be any integer value, allowing integration with your own enums.

Disable All Shortcuts: The SetEnabled(false) function can disable all shortcuts globally. This is useful, for instance, when a text box in your panel requires keyboard input.

Error Handling: Every function call that might fail returns either HOTKEYS_OK or a HOTKEYS_ERROR_* code indicating the reason. The library does not generate any output on its own.

Example Usage

#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 demonstrates binding four shortcuts via its inputs: Shift+H draws a horizontal line at the bid price, Shift+D deletes these lines, Shift+G toggles the grid visibility, and Shift+Q displays the quote window. It does not execute any trades. Upon initialization, it prints its active bindings and provides reasons for five example shortcuts that could not be bound:

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 accompanying image shows a live execution on a demo account, with each defined shortcut being pressed on the chart.


API Reference

Function
Description

Bind(action, name, chord, error)
Bind an action to a text-defined shortcut, or update an existing one. An empty string or "none" will unbind the action.

BindKey(action, name, key, modifiers, error)
Similar to Bind, but uses a key code and HOTKEYS_SHIFT/HOTKEYS_CTRL flags for modifiers.

ChartEvent(id, lparam, dparam, sparam)
To be called from OnChartEvent. Returns the bound action or HOTKEYS_NO_ACTION if no action was triggered.

Match(key, repeats, flags, modifiers)
Performs the same checks as ChartEvent but with provided modifiers, useful for custom testing.

Unbind(action), Clear()
Remove a specific action's binding or clear all bindings.

SetEnabled(enabled), IsEnabled()
Enable or disable all hotkeys. Returns true if enabled, false otherwise.

Count(), Action(i), Name(i), Key(i), Modifiers(i), Label(i)
Retrieve information about the current bindings, for example, to list them in a panel.

LabelOf(action), NameOf(action)
Get the text representation of a shortcut (e.g., "Ctrl+Shift+X") and the name of an action.

HotkeyParse(text, key, modifiers, error), HotkeyLabel(key, modifiers), HotkeyCheck(key, modifiers, error)
Functions to parse, generate labels for, or validate individual shortcuts.

The CHotkeys class is designed for easy copying and assignment as it does not manage any internal pointers.

You may also like: Risk Guard Lite: A MQL5 EA for Controlled Risk Trading - excellent alternative for expert users on MetaTrader 5.

Source Code

#property copyright "RobotFX"
#property link "https://robotfx.org"
#property version "1.0"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#property strict
#include <Hotkeys\Hotkeys.mqh>
#define EXAMPLE_PREFIX "HotkeysExample_"
#define EXAMPLE_LINE_COLOR clrDodgerBlue
enum ENUM_EXAMPLE_ACTION
  {

.......

Leave your opinion, ask a question, share some knowledge

⚠ Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.