Download MetaTrader 5

Need help to spot some slight error .

To add comments, please log in or register
mrwayne
252
mrwayne  

I was doing a tra-il ing EA , when I tested on demo I notice that only the buy orders got it correctly ( I think ) , whereas the sell orders only moved stop loss ONCE . I still can't spot where's the problem though . 

 

** Edited the code **

Ian Venner
2584
Ian Venner  

Your code format makes it hard to read,  but for one thing how can you use this in a trailing stop. Order has to be still open so there is no OrderClosePrice()

 ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue )
mrwayne
252
mrwayne  

But buy orders works fine

Ian Venner
2584
Ian Venner  
Well without knowing what Figure is ..... did you write that code knowing OrderClosePrice == 0 on open orders therfore OrderClosePrice - OrderOpenPrice has a negative value if the order is still open ?
mrwayne
252
mrwayne  

 

 

 

 

Nope I didn't , all my other script using just OrderClosePrice - OrderOpenPrice or vice versa works fine .

mrwayne
252
mrwayne  
Okay I tried on another buy order it now fails . It also just moved stop loss ONCE .
Ian Venner
2584
Ian Venner  

OK yes you are right, I didn't realize OrderClosePrice follows the current price when an order is still open, so OrderClosePrice should be the current Bid price on buy orders. I think that must be an undocumented feature.

mrwayne
252
mrwayne  

 


 

The buy orders looks okay , but the sell order only moved stop loss Once ... It should at least move twice .

mrwayne
252
mrwayne  

Can someone give me a hand , I'm very close actually . I personally don't think there's wrong in my coding but I just don't know why it won't work properly all the time .

 

 

It's a long EA since I combined everything into 1 , pardon me a bit my eyes are quite blur now . 

 Figure returns the value of points for each particular pairs .

This is the trailing part . 

 

 

//+------------------------------------------------------------------+
//| Program's global variables                                       |
//+------------------------------------------------------------------+
   extern double StartTrailingStop = 15.0 ;
   extern double TrailingStop = 5.0 ;
   extern double CummulativeValue = 5.0 ;
   extern int    NumberOfTrail = 10 ;
   double Figure ;
//+------------------------------------------------------------------+

 

 

 

   
//+------------------------------------------------------------------+
//| Program's sub-function                                           |
//+------------------------------------------------------------------+
   double PointsDetection()    {              Figure = 0.0 ;                                             if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.01 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.001 )          {             Figure = 0.01 ;          }          else          {             if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.0001 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.00001 )             {                Figure = 0.0001 ;             }          }             return ( Figure ) ;    }

 


 

 

 

 

//+------------------------------------------------------------------+
//| Program's sub-function                                           |
//+------------------------------------------------------------------+
   void ProcessTrailingStop()
   {
      
      bool Result ;
      
      
      
      
      
         for ( int x = ( OrdersTotal() - 1 ) ; x >= 0 ; x-- )
         {
            if ( OrderSelect ( x , SELECT_BY_POS , MODE_TRADES ) == True )
            {
               PointsDetection() ;
               
               
               

                  if ( OrderType() == OP_BUY )
                  {
                     for ( int xai = 0 ; xai < NumberOfTrail ; xai++ )
                     {
                        if ( xai == 0 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() < ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                        
                        
                        
                        if ( xai == 1 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                        
                        
                        
                        if ( xai >= 2 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * ( xai - 1 ) ) ) * Figure ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * xai ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                     }
                  }
                  else
                  {
                     if ( OrderType() == OP_SELL )
                     {
                        for ( int xaii = 0 ; xaii < NumberOfTrail ; xaii++ )
                        {
                           if ( xaii == 0 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() > ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                           
                           
                           
                           if ( xaii == 1 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                           
                           
                           
                           if ( xaii >= 2 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xaii ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * ( xaii - 1 ) ) ) * Figure ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * xaii ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                        }
                     }
                  }

            }
         }
ffoorr
1089
ffoorr  

Maybe there :

 ( OrderStopLoss() == ( OrderOpenPrice() - ( TrailingStop * Figure ) )

https://forum.mql4.com/45053

Maybe it should be coded this way to avoid server error :

 figure = PointsDetection();

if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() >= OrderOpenPrice() ) || ( OrderStopLoss() > ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                                 if( result == true ) break;                               
 }


for the buy order :

( OrderStopLoss() < ( OrderOpenPrice() + ( TrailingStop * Figure ) )
mrwayne
252
mrwayne  

I tried but still didn't worked out .... Why use break in a if statement ???

For sell order ... it should be minus and for buy order it should be plus .... 

12345
To add comments, please log in or register