Trade State - the EA trading mode switcher depending on the time
Info
The Trade State - the EA trading mode switcher depending on the time is a Library for MetaTrader 5 that have you ever restricted the operation of your ea depending on time. For example, how does one make the EA not perform any trading activities during the night session.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use Trade State - the EA trading mode switcher depending on the time
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
Have you ever restricted the operation of your EA depending on time.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: AS Q Trade Frequency Controller - another powerful library for MetaTrader 5 traders.
Have you ever restricted the operation of your EA depending on time? For example, how does one make the EA not perform any trading activities during the night session? Or, if you trade on the derivatives section of Moscow Exchange, have you ever closed every positions before the market closure of before the weekend? How about more complex structures? For example, how does one make the EA able to open and close positions up to 7pm and to not perform any new trades after 7pm? Actually it is possible to get such flexible configuration. The CTradeState special module will help with that.
Also recommended: Asynchronous Institutional Trade Engine (Library) - similar library with strong performance on MetaTrader 5.
How does the CTradeState work?The module returns one of the six states, defined using the ENUM_TRADE_STATE structure:
In your Expert Advisor you must perform a set of actions, depending on which specific modifier from the given structure the CTradeState returns (the value is returned using the GetTradeState method).
Before using the trade modes, they must be set, for which the SetTradeState method must be called. The prototype of the SetTradeState method is as follows:
For example, for the trade state module to return the TRADE_WAIT modifier (control over the opened positions is stopped, new positions are not opened) at the time from 18:45 to 18:49 inclusive, it is necessary to call SetTradeState with the following parameters:
It is possible to specify not only the trade mode start and end times, but also the day of the week, for which this mode is active. If the trading mode needs to be specified for every day of the week, the ALL_DAYS_OF_WEEK modifier needs to be used. If the trading mode needs to be specified only for a certain day of the week, specify one of the values of the system enumeration in the day_of_week variable.
In order to get the current trading mode, use the GetTradeState method.
As an example, consider a combination of modes, which can be used for a useful trading on the derivatives section of Moscow Exchange:
Usage example.
This combination of modes is set using the following sequence of SetTradeState calls (example as a script):
As a check, this script implements a printing of the mode that corresponds to the specific trading time.
It is important to realize that the proposed module is just an auxiliary tool. The decision to stop trading (and to close all positions) must be made by the expert itself. However, it is quite easy to create different time zones and trade modes for any expert using this module.
You may also like: To Close All Open Trades of Different Symbols - excellent alternative for library users on MetaTrader 5.
⚠ 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.