Stop Loss and Take Profit Scripts
Info
The Stop Loss and Take Profit Scripts is a Script for MetaTrader 4 that a collection of predefined scripts a collection of predefined scripts saves time by having available for immediate use the required setting without having to key it in. Script List: SL (SL-20,.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Script works exclusively on MetaTrader 4 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL4/Scripts folder via File ? Open Data Folder in MetaTrader 4.
How to Install and Use Stop Loss and Take Profit Scripts
1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.
2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.
3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.
4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."
Frequently Asked Questions
Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.
Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.
Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.
What this tool does
A collection of predefined Scripts A collection of predefined Scripts saves time by having available for immediate use the required setting without having to key it in.
Typical Use Case
This Script excels in automated trading and technical analysis on MetaTrader 4.
Compatible Platform & Setup
This Script works on MetaTrader 4. Place the file in the MQL4/Scripts folder and restart the terminal.
Description & Settings
Related: XI T Buy Sell Auto Stoploss. mq4 - Stoploss set from Fractal Levels, Money management Risk/Reward - another powerful script for MetaTrader 4 traders.
Also recommended: Modify stop loss or take profit - similar script with strong performance on MetaTrader 4.
A collection of predefined ScriptsA collection of predefined Scripts saves time by having available for immediate use the required setting without having to key it in.
Script List:
SL (SL-20, ..SL-100, ..SL-500, ..SL-3000 etc)
SL Modify
SL Delete
SL Break Even
TP (TP-20, ..TP-100, ..TP- 500, ..TP-3000 etc)
TP Modify
TP Delete
Market Buy
Market Sell
Buy Limit
Buy Stop
Sell Limit
Sell Stop
Close all open and pending orders
Close all open orders
Close all pending orders
Close first this symbol
Close open this symbol
Unzip and place in experts/scripts folder. (Hampster FREE Zip Archiver - - as of Jan-2014 no crapware in download).
Note
Test: Drag a SL Script onto the chart.
Test: Dragging another SL Script onto the chart will cause the SL to adjust to the new value. Same applies for TP.
Point and pip calculators can be added
Make your own Scripts from downloaded Scripts - This looks long-winded but once done its actually a quick 1,2,3. You will find it surprisingly quick and easy.
Why keep re-setting a Scrip, Indicator or EA default parameter from, say, 34 to 50 ? Permanently change the default setting to 50 or make a copy with default 50 !
Follow these steps
The downloaded Scripts should be in the experts/scripts folder.
Choose a SL Script and test that it works with your MT4. Prove that it works - you do not want to waste your time with something that does not work.
Press Ctrl+N which will open the Navigator Pane.
If any other pane is also open, to avoid clutter, it can be closed. Click
x
at top-right of the relevant pane.
Locate the tested SL Script under the Script folder.
Right click on SL Script and select Modify - Enter. Image 1 shows 0-A-SL-0500 selected in the Scripts folder.
Image 1
Left click or press the Enter Key to open the source code in the MetaEditor.
The source code will open in the MetaEditor – see Image 2. Note the SL Script name on the code window tab and right of the yellow diamond icon at top.
If more than one code window is loaded in the MetaEditor close each window (right click in the area of the name tab of the code window → click close) and leave only the tested SL Script code open. For beginners less mistakes are made this way.
The only code you will need to change are the numerical values highlighted in red and blue.
Stop !
Before changing any code the next action is to save the Script to another name. This ensures that the original script code is not replaced with unintended edits.
Image 2
Continue....
If more than one code window is loaded in the MetaEditor ensure that the correct code widow is selected.
Click on File → Save As.
Change the name from O-A SL 500.mq4 to 0-A SL 0800.mq4 or whatever SL value you have chosen. (0020, 0050, 0100, 0200, 1000 etc., naming is applied because it ensures consecutive numbering; if not 100 will appear before 1 to 99. As the maximum SL number is in thousands 4 digits are used to ensure consecutive numerical naming increment in the Navigator Pane. A and B are used to separate SL and TP groups respectively. 0 preceding A and B ensures that the entire collection is kept separate from other scripts.)
Click the Save button.
MedaEditor – [0-A SL-A SL 0800.mq4] will now be indicated right of the yellow icon and 0-A SL 0800.mq4 on the code window tab. Although 0-A-SL-0500.mq4 is no longer visible in the MetaEditor it remains - unchanged - in the experts/scripts folder.
The only code to edit are the two 500 numbers.
Relevant Information
The code behind the Inputs Tab of Scripts, Indicators and Expert Advisors can always be located by finding the code word 'extern' (without quotations) which will always be near the top of the program.
The code word 'int' stands for integer (..-2, -1, 0, 1, 2, ..). Basically negative or positive numbers that do not include decimals places. 'Int' defines 'Stoploss' as an integer value.
The code to change - as far basic edits go - will be the code after the = sign. Eg1: extern color Line_Color = Red; - Change Red to Blue or Green etc. Eg2: extern int Line_Width = 1; - Change 1 to 2, 3, 4 or 5. Eg3: extern string Font_Type = "Arial"; - Change Arial to "Arial Bold" or "Verdana". Note quotation marks (" bla") required for parameters of string variables (extern string ...... = "bla";).
Text after // is used as descriptive comment and excluded from code function. Sometimes code is commented out (// ..bla bla.. or /*..bla bla...*/ ) for reasons like fault finding by process of elimination.
Editing
Change the two 500 numbers to your chosen numerical value and remember to include the semicolon (;) where applicable. The numerical value should form part of the script name (i.e. 0-A-SL-0800.mq4) for fast identification..
Click the Compile button.
Always remember to compile. Edited code will not become effective until it is compiled.
Check the left hand bottom of the MetaEditor for
0 errors and 0 warnings
in the ToolBox (Ctrl+T).
Image 3
If an error is flagged the code will not compile and the code must compile for the program to work.
If an error is flagged press Ctrl+Z in steps and follow the code in the MetaEditors as it is undone and returned to original copy. Try to determine where a mistake was made for future reference and learning.
Click the Compile button to test that the code is free of errors and start again.
If, after the edit of 500 to 800 no errors have been flagged in the Toolbox the New 0-A-SL-0800 Script will be found in the Scripts folder ready for use..
Testing
Open a trade in the MT4 demo.
Attach Script 0-A-SL-0500 to the the chart. A red SL Line at 500 points from entry will be drawn across the chart.
Check SL difference against Entry price to ascertain accuracy.
Attach Script 0-A-SL-0800 to the chart. The red SL Line will jump from 500 to 800 points from entry.
Check SL difference against Entry price to ascertain accuracy.
To delete the Stoploss attach Script 0-A-SL-Delete to the chart.
Conclusion
Apply the same procedure to other Scripts.
double: def - real number - includes fraction part.
int: def - (integer) whole number - no fraction part.
Before: 0-2 Buy Limit.mq4
After: 0-2 Buy Limit 0200-0600.mq4
After: 0-2 Buy Limit 0300-2000.mq4
You may also like: Placing Stop Orders Grid - excellent alternative for script users on MetaTrader 4.
⚠ 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.