You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Check out the new article: Step-by-Step Implementation of a Local Stop Loss System in MQL5.
This article shows how to build a local stop-loss system in an MQL5 Expert Advisor that keeps stop levels on the terminal side. It walks through the execution logic, event handlers, inputs, and an OOP design using CTrade, CPositionInfo, CHashMap/CHashSet, and chart objects. You will implement multi-position tracking, draggable stops, visual spacers and labels, plus cleanup and disconnection behavior to create a practical risk-control utility.
Is there a way to address the drawbacks of the traditional stop order while still preserving the benefits of a stop loss?
Absolutely! The solution is a local stop-loss system. The key feature of a local stop-loss system is that the stop price is set and maintained locally on the trader's terminal and is not disclosed to any broker or exchange. The local stop loss looks and behaves like a traditional stop order without the disadvantages of disclosing the stop price. It can be set, moved, and dragged to reduce losses and protect gains. When the market price reaches this local stop price, it is executed by the terminal as a regular market order to close the position. This prevents stop hunting and quote widening, as the stop price is never disclosed to anyone, be it the broker-dealer, market-maker, or the exchange. A local stop loss can also be placed arbitrarily close to the opening price of a position, giving the trader complete freedom over his trading choices.
To implement a local stop-loss system, you must store each position's stop price locally and automate the stop-condition monitoring. The tools provided by the MQL5 language make this an ideal candidate for an Expert Advisor, hence called EA. This article provides a step-by-step tutorial to implement a local stop-loss system using an MQL5 EA along with an explanation of the design process. This tutorial will follow modern object-oriented programming techniques. We will use object functions to create stop-loss objects. Trade execution will be handled via CTrade, and open-position access via CPositionInfo. We will also see why the choice of data structures such as CHashMap can provide significant performance improvements in the tracking and monitoring of our positions. No prerequisites are needed; the necessary concepts will be explained along with links for further reading. The tutorial will begin with an understanding of stop-loss execution, followed by the definition and testing of the main algorithm. We will then improve upon our code to make it easy to handle multiple positions with visual labeling. All in all, this will serve as an EA development guide from input definition to production-ready practical trading utility. Let's begin!
Author: Prasad Fidelis Dsa