Download MetaTrader 5
To add comments, please log in or register
Do you want to order an application? Find out how to obtain the desired result
Arav007
412
Arav007 2014.03.23 15:32 

I have been trying to make a 'Trailing Stop' function with Break Even.

The Break Even part is working but the Trailing Stop part isn't working.

Not understanding where I am making the 'Mistakes'?

extern double TrailingStop  =7;
extern double TrailingStep  =2;
extern double BreakEvenStart=5;
extern double BreakEven     =1;

Trailing_Stop(Symbol(), MagicNumber); 

////////////////////////////////////////////////
void Trailing_Stop(string symbol, int magic)
           {
           double vPoint,vSlippage; 
           if (Digits == 3 || Digits == 5)
            {vPoint = Point * 10; vSlippage = Slippage * 10;    }
            else
             {vPoint = Point; vSlippage = Slippage;}
            RefreshRates();          
                                   
            for ( int i=OrdersTotal()-1; i>=0; i--)    
            {
             if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
              {
                if(OrderSymbol()==symbol)
                 {
                   if(OrderMagicNumber()==magic)
                   {
                    if(OrderType()==OP_BUY)
                     {
                      if(OrderStopLoss()<OrderOpenPrice())
                      {
                       if(Bid>=OrderOpenPrice()+vPoint*BreakEvenStart)                         
                            {                             
                             OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+vPoint*BreakEven,OrderTakeProfit(),0,Green);
                             return;
                            }
                         }                                            
                      else
                       {
                        if(Bid-TrailingStop>=OrderStopLoss())
                         {
                         OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+vPoint*TrailingStep,OrderTakeProfit(),0,Green);
                             return;
                         }
                        }// End else
                       }//End if(OrderType()==OP_BUY)
                       else
                       //else the OrderType()=OP_SELL
                       {
                        if(OrderStopLoss()>OrderOpenPrice())
                         {
                           if(Ask<=OrderOpenPrice()-BreakEvenStart)
                            {
                             OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-vPoint*BreakEven,OrderTakeProfit(),0,Green);
                             return;
                            }
                          }
                          else
                          { 
                           if(Ask+TrailingStop<=OrderStopLoss())
                            {
                             OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-vPoint*TrailingStep,OrderTakeProfit(),0,Green);
                             return;                            
                            }
                           } //End else
                          }//End else 
                         }
                      }//End if(OrderSymbol()==symbol)
                     }//End if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
                    }//End for ( int i=OrdersTotal()-1; i>=0; i--)

Regards

Alain Verleyen
Moderator
28522
Alain Verleyen 2014.03.23 16:35  

I suggest you to begin to fix your indentation, what a mess, how can you follow what this code is doing ?

void Trailing_Stop(string symbol,int magic)
  {
   double vPoint,vSlippage;
   if(Digits==3 || Digits==5)
     {vPoint=Point*10; vSlippage=Slippage*10;    }
   else
     {vPoint=Point; vSlippage=Slippage;}
   RefreshRates();

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES) || OrderSymbol()!=symbol || OrderMagicNumber()!=magic) continue;

      if(OrderType()==OP_BUY)
        {
         if(OrderStopLoss()<OrderOpenPrice())
           {
            //-- Breakeven
            if(Bid>=OrderOpenPrice()+vPoint*BreakEvenStart)
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+vPoint*BreakEven,OrderTakeProfit(),0,Green);
               return;
              }
           }
         else
           {
            //-- Trailing stop
            if(Bid-TrailingStop>=OrderStopLoss())
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+vPoint*TrailingStep,OrderTakeProfit(),0,Green);
               return;
              }
           }
        }
      if(OrderType()=OP_SELL)
        {
         if(OrderStopLoss()>OrderOpenPrice())
           {
            //-- Breakeven
            if(Ask<=OrderOpenPrice()-BreakEvenStart)
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-vPoint*BreakEven,OrderTakeProfit(),0,Green);
               return;
              }
           }
         else
           {
            //-- Trailing stop
            if(Ask+TrailingStop<=OrderStopLoss())
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-vPoint*TrailingStep,OrderTakeProfit(),0,Green);
               return;
              }
           } 
        } 
     }
  }
What are the values of TrailingStop/TrailingStep ? Why don't you check the returned value of OrderModify() ? Is your ordermodify for trailing stop executed with an error or not executed at all ?
Keith Watford
Moderator
7887
Keith Watford 2014.03.23 18:25  
extern double TrailingStop  =7;



if(Bid-TrailingStop>=OrderStopLoss())

Bid - 7 is almost certainly a negative number

Arav007
412
Arav007 2014.03.23 18:26  
angevoyageur:

I suggest you to begin to fix your indentation, what a mess, how can you follow what this code is doing ?

What are the values of TrailingStop/TrailingStep ? Why don't you check the returned value of OrderModify() ? Is your ordermodify for trailing stop executed with an error or not executed at all ?

I could follow the Code as I denoted comments beside lines.

Anyway, you probably missed the Values of TrailingStop/TrailingStep at the Top of my posted code.

So far not getting any 'Error' in the Journal Tab while testing in Strategy Tester. So the Trailing part isn't Executing. :(

EDIT:

*There is a mistake in defining New_SL.

It should be: New_SL=OrderStopLoss()+vPoint*TrailingStep;

Files:
hotash-6.mq4 17 kb
Keith Watford
Moderator
7887
Keith Watford 2014.03.23 18:32  
Arav007:

I could follow the Code as I denoted comments beside lines.

Anyway, you probably missed the Values of TrailingStop/TrailingStep at the Top of my posted code.

So far not getting any 'Error' in the Journal Tab while testing in Strategy Tester. So the Trailing part isn't Executing. :(


Did you miss my post?
Arav007
412
Arav007 2014.03.23 18:45  
GumRai:

Bid - 7 is almost certainly a negative number


But at certain point it has to be Positive.

Like if the price moves 20 pips, then it'd easily be a positive number.

if(OrderStopLoss()<OrderOpenPrice())
 {
  if(Bid>=OrderOpenPrice()+vPoint*BreakEvenStart)
   {
    Set BE;
   }
  }
else //if(OrderStopLoss()>OrderOpenPrice()) [The Stop Loss is at BE now]
 {
  if(Bid-TrailingStop>=OrderStopLoss()) // Bid=BE(OrderStopLoss)+TrailingStop
   { 
    Set Trailing Stop;
   }
  }

For example a Buy trade was opened at 1.3550.

Now the Bid is 1.3556, so

OrderOpenPrice()+BreakEvenStart=1.3550+0.0005=1.3555

So, 1.3556>1.3555

BE price=OrderOpenPrice()+BreakEven=1.3551

New Bid is 1.3557, so

Bid-TrailingStop=1.3557-0.0007=1.3550 which is not > OrderStopLoss() [1.3551]

Then now the Bid is 1.3565

Bid-TrailingStop=1.3565-0.0007=1.3558 which is > OrderStopLoss() [1.3551]

So the New Stop Loss has to be:

OrderStopLoss()+TrailingStep= 1.3551+0.0002=1.3553

But this is not Happening.

Probably I have made a fatal mistake somewhere.

Alain Verleyen
Moderator
28522
Alain Verleyen 2014.03.23 19:01  
Arav007:

I could follow the Code as I denoted comments beside lines.

Anyway, you probably missed the Values of TrailingStop/TrailingStep at the Top of my posted code.

So far not getting any 'Error' in the Journal Tab while testing in Strategy Tester. So the Trailing part isn't Executing. :(

EDIT:

*There is a mistake in defining New_SL.

It should be: New_SL=OrderStopLoss()+vPoint*TrailingStep;

It's not about you following the code, it's about the others.

I don't miss the values, I invite you to check them. Your trailing code is never executed, read the replies again.

Arav007
412
Arav007 2014.03.23 19:17  
angevoyageur:

It's not about you following the code, it's about the others.

I don't miss the values, I invite you to check them. Your trailing code is never executed, read the replies again.


So is my calculation wrong?

Have I set the Logic wrong?

Please indicate where the fault is? I'm unable to understand.

Keith Watford
Moderator
7887
Keith Watford 2014.03.23 20:00  
Arav007:


But at certain point it has to be Positive.

Like if the price moves 20 pips, then it'd easily be a positive number.

For example a Buy trade was opened at 1.3550.

Now the Bid is 1.3556, so

OrderOpenPrice()+BreakEvenStart=1.3550+0.0005=1.3555

So, 1.3556>1.3555

BE price=OrderOpenPrice()+BreakEven=1.3551

New Bid is 1.3557, so

Bid-TrailingStop=1.3557-0.0007=1.3550 which is not > OrderStopLoss() [1.3551]

Then now the Bid is 1.3565

Bid-TrailingStop=1.3565-0.0007=1.3558 which is > OrderStopLoss() [1.3551]

So the New Stop Loss has to be:

OrderStopLoss()+TrailingStep= 1.3551+0.0002=1.3553

But this is not Happening.

Probably I have made a fatal mistake somewhere.

It is not

Bid-TrailingStop=1.3557-0.0007=1.3550 which is not > OrderStopLoss() [1.3551]

It's

Bid-TrailingStop=1.3557-7

which will be a negative number

Tjipke de Vries
6276
Tjipke de Vries 2014.03.23 22:14  
Arav007:


So is my calculation wrong?

Have I set the Logic wrong?

Please indicate where the fault is? I'm unable to understand.


Use more Print(" ......") inside your code to check what your code is doing
Arav007:

I have been trying to make a 'Trailing Stop' function with Break Even.

The Break Even part is working but the Trailing Stop part isn't working.

Not understanding where I am making the 'Mistakes'?

Regards

where does it fails ???

So where do you look for errors ????

Then you had found with using Print() what GumRai is pointing out

By the way if you have problems with your code and you want free help then use the Forum

that is the section you have to use asking your questions about issues you have on your code

if i or others help you on forum with a issue then don't start private messages

I have not the time to help you personal for free checking all your codes your write me at personal messages

if you want personal private lessons from me or maybe someone else then it's for me going with using section Jobs ofcours then getting paid

and in a way it's easier to communicate at a time we agree

Arav007
412
Arav007 2014.03.24 05:32  
GumRai:

It is not

Bid-TrailingStop=1.3557-0.0007=1.3550 which is not > OrderStopLoss() [1.3551]

It's

Bid-TrailingStop=1.3557-7

which will be a negative number


This is it.

Thanks. I got it now. Actually I was translating the '7' as the value of 'TrailingStop'. The Fault was I hadn't converted the TrailingStop into decimal places.

It's working now. :)

/ /12
To add comments, please log in or register