Coding a trailing stop

To add comments, please log in or register
micobez
68
micobez  

My code for a trailing stop does not work, can someone please advise me on what is wrong with the code

if(PositionsTotal()>0){
  
  for(int i=0;i>=0;i++)
  {
  /*
string position =PositionGetSymbol( i);
int TYPE=PositionGetInteger(POSITION_TYPE);


// buy
if(TYPE==POSITION_TYPE_BUY);
{
double profitpoint= .70*(Rotprofitbuy-Rotslbuy)+Rotslbuy;
if(Ask>=profitpoint){
double newsl= 0.5*(Rotprofitbuy-Rotslbuy)+Rotslbuy;
bool res= trade.OrderModify(i,NULL,newsl,Rotprofitbuy,NULL,NULL,NULL);
}}
// sell
if(TYPE==POSITION_TYPE_SELL);
{
double profitpoint= 0.7*(Rotslsell-Rotprofitsell)+Rotprofitsell;
if(Bid<=profitpoint){
double newsl= 0.5*(Rotslsell-Rotprofitsell)+Rotprofitsell;
bool res= trade.OrderModify(i,NULL,newsl,Rotprofitbuy,NULL,NULL,NULL);
}
}


}}
}
*/


   if(m_position.Select(Symbol()))
        {
        if(m_position.PositionType()==POSITION_TYPE_BUY)  
           {
          double profitpoint= .70*(Rotprofitbuy-Rotslbuy)+Rotslbuy;
if(Ask>=profitpoint){
double newsl= 0.5*(Rotprofitbuy-Rotslbuy)+Rotslbuy;
bool res= trade.OrderModify(i,NULL,newsl,Rotprofitbuy,NULL,NULL,NULL);
           
           
           }}}
           if(m_position.Select(Symbol()))
        {
        if(m_position.PositionType()==POSITION_TYPE_SELL)  
           {
double profitpoint= 0.7*(Rotslsell-Rotprofitsell)+Rotprofitsell;
if(Bid<=profitpoint){
double newsl= 0.5*(Rotslsell-Rotprofitsell)+Rotprofitsell;
bool res= trade.OrderModify(i,NULL,newsl,Rotprofitbuy,NULL,NULL,NULL);
           
           
           
           
           
           }}}
           
           }}
//+------------------------------------------------------------------+

}
William Roeder
20808
William Roeder  

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum
micobez
68
micobez  
William Roeder:

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum

No one replied on the other thread, what am I meant to do?

Keith Watford
Moderator
21965
Keith Watford  
micobez:

No one replied on the other thread, what am I meant to do?

Well on the other thread, William posted for you to edit your post and use the code button (Alt + S).

You didn't.

A lot of people won't bother to read code if you can't be bothered to post it correctly.

Strange that you have managed to post it correctly here!

Please don't just post that something doesn't work. Supply as much information as possible.

I have deleted your other topic.

jonathan75
46
jonathan75  
micobez:

My code for a trailing stop does not work, can someone please advise me on what is wrong with the code

Hi Micobez, 


I'm not an expert for the coding, but with 2 or 3 person helping, I coded this Trailing stop : 

extern double TrailingStart=2500 ; // Trailing starting 
extern double TrailingStop=2000 ; // Break Even
extern double TrailingStep=100;

// Trailing stop 
 
 double tStopLoss = NormalizeDouble(OrderStopLoss(),Digits);
 double sl = OrderStopLoss(); 
 bool ticket = false ; 
 
RefreshRates();
      if (OrdersTotal()>0)
         {
         for (i=OrdersTotal(); i >= 0 ; i--) 
               {
               if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)== true)
                  if (OrderType()==OP_BUY)
                        {
                  if (Ask>NormalizeDouble(OrderOpenPrice()+TrailingStart*Point,Digits) && tStopLoss < NormalizeDouble (Bid - (TrailingStop+TrailingStep)*Point,Digits))
                              {
                        tStopLoss = NormalizeDouble (Bid-TrailingStop*Point,Digits); 
                      ticket = OrderModify (OrderTicket(),OrderOpenPrice(),tStopLoss, OrderTakeProfit(),0,White);
                       
                             }
                          } 
                   if (OrderType()==OP_SELL) 
              {
                  if (Bid < NormalizeDouble (OrderOpenPrice()-TrailingStart*Point,Digits) && (sl > (NormalizeDouble (Ask+(TrailingStop+TrailingStep)*Point,Digits)))
)
   {
tStopLoss = NormalizeDouble(Ask+TrailingStop*Point,Digits);
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Red);

                 
         }
    }
   }
   }

I hope that will help you. 

To add comments, please log in or register