Last Update: June 28, 2024
BY eric
Keywords
There are many reasons to stay on MetaTrader trading platforms when coming to alorithmic trading even cTrader provides the great flexibility in this regard. There are two main reasons:
- there are more tradable symbols on MetaTrader platforms
- some stocks (i.e. ASX stocks) can only be traded on MetaTrader platfomrs exclusively
However, developing an algorithmic trading bot that isn't strictly tied to the MetaTrader (MT4/MT5) platform presents several challenges, particularly due to MetaTrader's reliance on its proprietary MQL programming language and the need for the terminal to be actively running. However, various approaches can mitigate these limitations and increase the flexibility and portability of trading systems.
Approaches to Reduce Dependency on MetaTrader are discussed below:
Message Queue Service
Using a Message Queue Service (e.g. ZeroMQ, TYO-MQ..., etc) for Integration:
ZeroMQ
This messaging library allows the creation of a communication bridge between MetaTrader and external applications. The idea is to write a lightweight MQL script in MetaTrader that communicates with an external application written in a different language (e.g., Python, C#, Java).
Workflow:
MetaTrader script handles sending market data and trade signals via ZeroMQ to an external application. The external application performs the core logic, technical analysis, and decision-making, and sends trading instructions back to the MetaTrader script via ZeroMQ.
Advantages:
- Leverage powerful technical analysis libraries in other languages.
- Maintain flexibility to run the core logic on different operating systems.
Disadvantages:
- Still requires MetaTrader to be running.
- Added complexity in setting up and maintaining the messaging infrastructure.
Direct Protocol Interaction
Direct Protocol Hacking:
This involves reverse-engineering the communication protocol between the MetaTrader client (terminal) and its broker server. Libraries in various languages (e.g., C#, C++) have been developed to directly communicate with the MetaTrader servers, bypassing the need for the MetaTrader terminal.
Advantages:
- Eliminates the need for the MetaTrader terminal to be running.
- Full flexibility in choosing the programming language and operating system.
Disadvantages:
- Not officially supported and may violate MetaTrader’s terms of service.
- Requires constant updates and maintenance due to changes in the MetaTrader client or server protocol.
- High risk of breaking down with updates from MetaTrader or the broker.
- Steps to Implement a Flexible Trading Bot
Choose Your Approach:
Decide whether to use ZeroMQ for a middle-ground solution or directly interact with the MetaTrader server for maximum flexibility.
Set Up Development Environment:
For ZeroMQ, set up the necessary libraries and create a simple MQL script to handle messaging.
For direct protocol interaction, acquire or develop a library that can mimic the MetaTrader client’s communication. Develop Core Logic in Preferred Language:
Use a language like Python, C#, or C++ to develop your trading algorithms. Utilize available technical analysis libraries such as TA-Lib for Python, or any other libraries suited to your needs.
Implement Communication:
For ZeroMQ: Ensure robust message handling and error checking between the MQL script and the external application.
For protocol hacking: Implement the necessary functions to handle login, data retrieval, order placement, and error handling directly with the broker server.
Testing and Validation:
Test extensively in a simulated environment to ensure the bot works correctly under various market conditions. Validate the stability of the communication and the trading logic.
Deployment:
Deploy on a server or cloud environment that supports the operating system and language of your choice. Monitor and maintain the system to ensure it stays updated with any changes from MetaTrader or your broker.
Conclusion
While MetaTrader has its limitations in terms of flexibility and dependency on MQL, solutions like ZeroMQ integration or direct protocol interaction offer viable paths to develop more flexible and robust trading bots. Each approach has its trade-offs between complexity, risk, and maintenance, so it’s crucial to choose the one that best fits your technical expertise and trading requirements.
Contact
For further information, I can be reach via:
- twitter: Eric Tang
- email: [email protected]