Set Fixed SL and TP Script for MT4/MT5
One of the first rules in money management and risk management in Forex is to always have a stop-loss set for all your trades. Remember that a broker can execute a margin call on your account if a trade moves against you and you don't have sufficient funds to cover the loss. Long story short, missing a stop-loss may result in your account wiped or badly damaged. The MetaTrader script SetFixedSLandTP (short for Set Fixed Stop-Loss and Take-Profit) allows you to set a stop-loss and an optional take-profit price for all orders based on a set of filters.
There may be cases where you need to open an order quickly or you simply forget to set a stop-loss and take-profit for your order. Sometimes, you may also have many orders open based on the same strategy and you may want to set the stop-loss and take-profit for all of them at once. You can use the SetFixedSLandTP script to do it faster than you would be setting SL and TP manually.
You can download the script for educational purposes and test it on a demo account before applying it to any live trades.
You can also use this script's code as an example of an MQL4 program that works with orders and their parameters in MT4. Conversely, the MT5 version of the script can be used to learn how to work with orders and positions in MetaTrader 5.
When you run the script on a chart, you can set some parameters to limit which orders will be affected. You will also have to specify the distance in points from the selected price for the stop-loss and take-profit price.
The input parameters are:
- Stop-Loss in points — the distance in points between the selected price and the stop-loss. If set to zero, stop-loss won't be applied to trades.
- Take-Profit in points — the distance in points between the selected price and the take-profit. If set to zero, take-profit won't be applied to trades.
- Current symbol only? — can be false or true; it specifies whether you want to limit the changes only to trades on the current chart's symbol or you want it to apply to all trades in the account.
- Type of trades to apply to — select a trade direction to work with if you want to apply SL and TP to Buy and Sell trades separately.
- Modify only trades matching the magic number — can be false or true; it specifies whether you want to limit the changes only to trades with a specific magic number.
- Matching magic number — in case the previous parameter is set to true, you need to specify which magic number to use for the match.
- Modify only trades with the following comment — can be false or true, depending on whether you want to modify only trades with a specific comment.
- Matching comment — input the comment to match in case the previous parameter is set to true.
- Delay to wait between modifying trades (in milliseconds) — this is in case you want to delay the execution of the changes; could be necessary with some brokers or systems.
- PriceType — you can either apply SL and TP distance to the trade's open price (the normal way) or apply them based on the trade's current price (Bid for Buy orders and Ask for Sell orders).
- Apply to pending orders too? — can be false or true; it specifies whether you want to apply your SL and TP to pending orders as well.
The script is free to download and use.
Download (ver. 1.01, 2023-09-12)➥ Download Set Fixed SL and TP for MT4 ➥ Download Set Fixed SL and TP for MT5
To install the script, please follow the instructions below:
- Download the script archive file.
- Open the MetaTrader 4/5 data folder (via File→Open Data Folder).
- Open the MQL4 or MQL5 folder.
- Copy all the folders from the archive directly to the MQL4 or MQL5 folder.
- Restart MetaTrader 4/5 or refresh the list of scripts by right-clicking the Navigator subwindow of the platform and choosing Refresh.
You can also read a more detailed instruction on how to perform the installation.
Do you have any suggestions or questions regarding this script? You can always discuss Set Fixed SL and TP with other FX traders and MQL programmers on the scripts forums.
1.01 - 2023-09-12
- Added an MT5 version of the script.
- Added an option to apply stop-loss and take-profit based on the current price rather than open price.
- Added an option to work with pending orders as well.
- Added an option to set SL and TP not only for the current trading symbol.
- Added an option to skip either SL or TP by setting the respective input parameter to zero.
- Added a filter based on trade's direction.
- Changed the script to work based on points rather than pips.
- Removed the Slippage input parameter (wasn't used).
- Refactored the MQL4 code.