MT4 Scripts‎ > ‎

Buy With Stop Loss and Take Profit MQL4 for Metatrader 4

Have you ever wished you could double click a MT4 script to buy and have it automatically attach a stop loss and take profit order to the entry? With this Metatrader 4 script you can do just that. By default a box will pop up every time you double click the Metatrader script. To disable the popup box, please comment out the following line in the MQL4 code:

#property show_confirm  // comment out this line to eliminate the confirmation box
The internal MQL4 inputs deserve some explanation. they are:

// edit these values as desired below:
extern double Lots =             0.01;
extern int stoploss =            20;
extern int takeprofit =          30;
extern int Slippage =            7;
extern int MagicNumber =         0;
Lots represents the trade size. If you are dealing through a forex broker who offers only mini lots and not micro lots, you will need to modify the Lots amount to 0.1. 

stoploss represents the amount of the stop loss (in pips) for the trade entry. For no stop loss, set stoploss = 0;

takeprofit is the amount of the profit target you wish in pips from the trade entry. If you don't want a take profit, set takeprofit = 0;

Slippage is the slippage amount in pips that is allowed for trade entry at your forex broker. If slippage is greater than the amount specified, there will be no entry.

MagicNumber is a MQL4 variable to set the magic number. This is a way to allow for multiple strategies to be traded and managed on a single forex account. If you don't need a magic number, leave the value at the default zero.

Code Logic: The MT4 script attempts to place a buy order in your forex account. If an error is encountered in order placement, the Metatrader script will keep trying a maximum of 5 times with a break of 3 seconds in between each retry for a total of 15 seconds. If the Metatrader script is unable to place the trade, it exits.

After entry the MQL4 script then attempts to modify the stop loss and profit target to the amounts in the defaults. If the Metatrader script executes correctly and the stop loss and take profit are set, it exits. If an error occurs the Metatrader script will continue trying to place the order for a maximum of 5 retries of 3 seconds each. All of this can be seen by studying the MQL4 code contained in the MQ4 file download (see below).

Download the file below and copy it into your "..\experts\scripts" folder. After restarting Metatrader 4 so that it has a chance to compile the MQL4 code and find the Metatrader script and place it in the Navigator pane. You should see the script Buy_SL_TP listed under "Scripts" if you copied the MQ4 file into the right location, and restarted your Metatrader. 

You may post this MT4 script to other sites, but if you do, you must post a link to this page directly and you may not sell it or the source code. You are otherwise free to copy, use and distribute this MQL4 script for non-commercial use. 
For updates Follow patrickmwhite on Twitter
ċ
Buy_SL_TP.mq4
(3k)
Patrick White,
Apr 30, 2011, 10:26 PM
Comments