Ads 468x60px

Smaller time frame always follow the bigger time frame. It's better wait be patience to enter in position than risk with BIG SL. Having strong trading discipline and taking losses when necessary is a sign of serious trading approach

Saturday, July 7, 2012

Metatrader script stop loss & take profit

Have you ever wished you could double click an MT4 script to sell and have it automatically attach a stop loss and take profit order to the entry in Metatrader? With the Metatrader script described in this article you can do just that. By default within the MQL4 code, 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 Metatrader broker. If slippage is greater than the amount specified within the MQL4 code, 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 within Metatrader 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 sell 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 according to the MQL4 code, it exits.
After entry the MT script then attempts to modify the stop loss and profit target to the amounts in the defaults in the MQL4 code. 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.

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