Download MetaTrader 5
To add comments, please log in or register
You sell neither signals nor apps? No problem, everyone can make profit!
Adam Davies
352
Adam Davies 2015.03.25 23:41 

Hey guys, I'm learning to make ea's so that I can make one for my trade strategy. I need a trailing stop that starts immediately as soon as the trade is opened. The line of code I am using at the moment allows a trailing stop to kick in from when the trade reaches the value I enter, but the issue is that it jumps the SL straight to break even and then moves up from there. What I need is a trailing stop that starts immediately and moves up with every price movement. Could someone please tell me how to do this? Here are the 1st few lines of code I am using at the moment (ignore the values as these will be adjusted once I have the correct trailing stop in place). I'm using MQL4 if that makes a difference.

 

Thanks in advance!  


extern int MagicNumber=10001;

extern double Lots =1;

extern double StopLoss=10;

extern double TakeProfit=10;

extern int TrailingStop=5;

extern int Slippage=3; 

Jiayu Fu
1216
Jiayu Fu 2015.03.26 08:49  

I misunderstood your problem, but if you want a trailing stop that starts immediately and moves up with every price movement. 

You can use:

//1º select the order
//2º check the magicNumber
//3º check order type if it's buy then this code

 if(TrailingStop>0)//if you set a trailing stop then unwrap
{
if(OrderStopLoss()<Bid-Point*TrailingStop) //move the stoploss only if the price rise
            {
             if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
               {Print("OrderClose error ",GetLastError());}
            }
}

//*you can't set the trailing stop less than the stoplevel that your broker put.


 

Adam Davies
352
Adam Davies 2015.03.26 12:56  
Thank you! Yes that is what I am after, I'm going to test it now :)
Adam Davies
352
Adam Davies 2015.03.26 17:00  
Aeither:

I misunderstood your problem, but if you want a trailing stop that starts immediately and moves up with every price movement. 

You can use:


 

I replaced some of my original code with this and it works perfectly when buying now so thank you! But it doesn't work for sell orders. Does the code need altering for sell orders? Here is what I have so far with the code you gave me replacing some of the old code. I really appreciate your help!

  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)//if you set a trailing stop then unwrap
{
if(OrderStopLoss()<Bid-Point*TrailingStop) //move the stoploss only if the price rise
            {
             if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
               {Print("OrderClose error ",GetLastError());}
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
Jiayu Fu
1216
Jiayu Fu 2015.03.26 23:52  
Bluntside:

I replaced some of my original code with this and it works perfectly when buying now so thank you! But it doesn't work for sell orders. Does the code need altering for sell orders? Here is what I have so far with the code you gave me replacing some of the old code. I really appreciate your help!

in the case of Sell you need to replace and use this:
if(OrderType()==OP_SELL)
         {
if(TrailingStop>0)
         {
if(OrderStopLoss()>Ask+Point*TrailingStop) 
         {
  if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
    {Print("OrderClose error ",GetLastError());}
         }
         }
         }
Adam Davies
352
Adam Davies 2015.03.27 01:38  
Aeither:
in the case of Sell you need to replace and use this:
Thank you so much. I will try this tomorrow! You're a legend :)
Jiayu Fu
1216
Jiayu Fu 2015.03.27 08:15  
Bluntside:
Thank you so much. I will try this tomorrow! You're a legend :)

You're welcome,  

at   2015.03.27 01:38 

..yes it's a little late, better go to bed :P 

Adam Davies
352
Adam Davies 2015.03.27 23:40  
Aeither:

You're welcome,  

at   2015.03.27 01:38 

..yes it's a little late, better go to bed :P 

Too many late nights recently! Still haven't had a chance to add that last bit of code but I will be doing it over the weekend
/
To add comments, please log in or register