Coding a trailing stop

 

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);
           
           
           
           
           
           }}}
           
           }}
//+------------------------------------------------------------------+

}
 

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

          General rules and best pratices of the Forum. - General - MQL5 programming forum
 
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?

 
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.

 
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. 

 
micobez:

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

Just saying "does not work" is not very helpful. You'd better specify what does not work, what error messages you see, what you have tried, and so on.

The first line seems odd to me. This will go on forever, without stopping?

for(int i=0;i>=0;i++)

On top of that, index i is not used to select a specific open position inside the if-loop?

 
WindmillMQL: This will go on forever, without stopping?
I answered that in your other thread.