How do I set default Stop Loss / Take Profit - page 2

 

Not running

 

...

Works. Tested right now.

You have to have "allow libe trading" in the tool->options->experts advisors

Save it in scripts and if your broker is 5 digit broker change the default stops and take profit (multiply by 10) If your broker is an ECN type broker it will not work (those are old scripts and ECN type brokers speared after that - in that case they would have to be "modernized")

masterofforex:
Not running
 

Buy With Stop Loss and Take Profit

I created a MT4 buy script a while back that should work with ECN brokers and attempts to set a stop loss and take profit a the desired level. I'll post the code below.

Code for Buy script with Stop Loss and Take Profit attached after (suitable for ECN).

//+------------------------------------------------------------------+

//| Buy_SL_TP.mq4 |

//| Copyright © 2011, Patrick M. White |

//| https://sites.google.com/site/marketformula/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2011, Patrick M. White"

#property link "https://sites.google.com/site/marketformula/"

#property show_confirm // comment out this line to eliminate the confirmation box

//+------------------------------------------------------------------+

//| script program start function |

//+------------------------------------------------------------------+

// 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;

int start()

{

//----

//////////////////////////////////////////////////////////////////

// Order Entry section

/////////////////////////////////////////////////////////////////

int ticket =-1;

datetime Exp = 0;

int retries = 0;

while(true) {

while(IsTradeContextBusy()) Sleep(100);

RefreshRates();

if(IsConnected() == true)

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0.0,0.0,"Buy_SL_TP",MagicNumber,0,Lime);

if (ticket >=0) break; // valid order placement

if (ticket == -1) {

int err = GetLastError();

Print("Entry Error #: " + err );

if (err == 2 || err == 3 || err == 5 || err == 64 || err == 65 || err == 131

|| err == 132 || err == 133 || err == 134 || err == 139 || err == 148

|| err == 149 || err == 150) return(0);

Sleep(3000); // take a short break then retry

retries += 1; // increment retries

if (retries >5) return(0); // end after 15 seconds of retries

}

}

//////////////////////////////////////////////////////////////////

// Stop Loss and Profit Target / Take Profit Modification

/////////////////////////////////////////////////////////////////

Sleep(3000);

retries = 0;

// now attempt to alter the stop loss and take profit

bool selected = OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);

while(selected) {

bool modified = OrderModify(ticket, OrderOpenPrice(), OrderOpenPrice() - stoploss * Point, OrderOpenPrice() + takeprofit * Point, 0, CLR_NONE);

if (modified == true) return(0);

err = GetLastError();

Print("Order Modification Error #: " + err );

if (err == 2 || err == 3 || err == 5 || err == 64 || err == 65 || err == 131

|| err == 132 || err == 133 || err == 134 || err == 139 || err == 148

|| err == 149 || err == 150) return(0);

Sleep(3000); // take a short break then retry

retries += 1; // increment retries

if (retries >5) return(0); // end after 15 seconds of retries

}

//----

Print("Unable to select order for SL/TP modification");

return(0);

}

The script attempts to place a buy order for 15 seconds, after which time it fails if order is not placed for some reason. Also, after order is placed, the script attempts to attach stop loss and take profit to the trade for 15 seconds.

A full description of the inputs can be found for the buy script with download:

Buy Stop Loss Take Profit

And there is also a Sell script that attaches stop loss and take profit to the order that may be downloaded.

Sell Stop Loss Take Profit

Please discuss in this thread.

 

Thank you Pipscooper.

The scripts are working fine for ECN as well.

However, is someone able to get rid of the MM from the code? I just want to use fixed lots.

Thank you

Reason: