Download MetaTrader 5

Help With Trailing Stop

To add comments, please log in or register
juzaman 2015.12.29 16:23 

Hi there, i need a little help.

When i open a position, i need to set the stop to move to breakeven when the price reaches 50% of the take profit distance. Only once, i don't need that the stop keeps following the price. Is this possible?

Thank you!

Carl Schreiber
Carl Schreiber 2015.12.29 19:32  
ThreadHandle 2015.12.30 14:55  
double tp = NormalizeDouble(Point*500, Digits);
double sl = NormalizeDouble(Point*500, Digits);
double min = MarketInfo(Symbol(),MODE_STOPLEVEL);
int setTrail = 0;
int ticket = -1;

void OnInit(){



void openTrade(){

  ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Bid-sl,Ask+tp,"My Bed Time",666,0,clrGreen);


void OnTick(){

  if(setTrail == 0){ if((tp/2) > (OrderTakeProfit() - Bid)){ if(OrderModify(ticket,OrderOpenPrice(),Bid-(_Point*min),OrderTakeProfit(),0,Blue)){ setTrail = 1; }}}


//Something like this, Your stoploss will around 50% from your takeprofit, as in 50% of 500 points, so you'll be 250 points from your takeprofit.
//Which puts you in profit not breaking even. 
//If you want to break even and not be in profit at 50% then you'll have to figure out the ratio between your stoploss and takeprofit if they are different values. 
//If your using Bid-sl instead of min then you might not break even if your stoploss is bigger than the takeprofit so again you'll have to calculate what the difference is.
//If the diff is bigger than the spread it wont break even. I.E spread = 50 points, sl 600 and tp = 500.....using min is easiest if they arent proportional 
//OrderTakeProfit - bid accounts for spread, instead of using Ask. You could do the full calculation with the spread, sl, tp and 50% but this is simpler.
//ANDDDD i need to get some sleep :D If you want nicer code or a better explanation i'll be awake in around 48 hours :D 

Oh just reverse the process for a sell, you might have to invert the tp into a negative value, 1 - tp - 1 is a simple way of inverting below 1.


Final note, if your doing multiple trades and close parameters are not the same, I.E they dont close in the same order they open then you'll have to track which have setTrails, just have an array with index values for the trade then have a sort that pushes the index vals down and resize to get rid of the floating closed trade index (the index is the pos of the close). Like....

int setTrail[];
int cur_trades = 0;

void openTrade(){

  int arrSize = ArraySize(setTrail);
  if(arrSize <= cur_trades){ArrayResize(setTrail, arrSize + 1); setTrail[arrSize] = 0;}
  if(OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Bid-sl,Ask+tp,"My Bed Time",666,0,clrGreen) != -1){ cur_trades++;}


void closer(){

  int i;
  for( i = OrdersTotal(); i >= 0 ; i--){ 
    if(SelectOrder(i, BY_POS) == true){
      //if(Whatever logic you use that doesnt close the trades down in order){
            if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE)){
                ArrayResize(setTrail, cur_trades);

void removeIndex(int pos){

  for(int i = pos; i <= cur_trades; i++){
      if(i+1 != cur_trades){
         setTrail[i] = setTrail[i+1]; //This should push the closed trade to the end of the array, because cur_trades hasnt decremented yet
                                      // i+1 goes to i and i gets removed from the array after we exit this function, this basically keeps order of
                                      //Who has and who hasnt had their setTrail while removing the closed orders from the array

void OnTick(){
  for(int i = cur_trades; i >= 0; i--){
    if(OrderSelect(i, BY_POS) == true){
      if(setTrail[i] == 0){ 
         if((tp/2) > (OrderTakeProfit() - Bid)){ 
               setTrail[i] = 1; 

//This is probably the really long way around keeping track of your trailing losses while having multiple trades that only get the stoploss moved once
//But it should function, i think ........ Theres probably loads of errors in there, im on a linux machine so dont have access to my code
//The built in functions and variables are from memory so i doubt this will work out the box but it should get you started.

//I might try this out myself now i've coded half of it. If i implement it i'll give you the code i use as it was your idea.

To add comments, please log in or register