Can't work my Trailing Stop. P.s. Beginner to MQL5

 
static double Static_Open;      
static double Static_SL;        
static double Static_R;    

//---------------------------------------------------------------

 if(PositionSelect(_Symbol) == true && UseTrailingStop == true)                
     {                                                                           
      for(int i =0; i < PositionsTotal(); i++)                                   
        {                                                                        
         ulong positionticket = PositionGetTicket(i);                            
         int MinProfit;                                                          
         int Step;                                                               
         int TrailingPoints;                                                    
         Static_Open = PositionGetDouble(POSITION_PRICE_OPEN);
         Static_SL = PositionGetDouble(POSITION_SL);
         Static_R = (Static_Open - Static_SL) / _Point;                    
         int Static_R_int = (int) MathRound(1.5 * Static_R);             
         MinProfit = Static_R_int;                                            
         int Static_Step = (int) MathRound(Static_R);                         
         Step = Static_Step;                                                   
         TrailingPoints = Static_R_int;                                     
                                                                           
         Trail.TrailingStop(positionticket, TrailingPoints, MinProfit, Step); 
        }                                                                       
     } 

P.S. I have used static variables to calculate any R by using the initial Stoploss and Open Price.

P.P.S Also have used a ZeroMemory to set the static variables to zero if no positions are open.


When I compile my code, I do not get an error. However, when I run a backtest, the SL doesn't move. 

What am I possibly doing wrong? Please advise. 

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 

Don't use 'PositionSelect(_Symbol)'!!! - read the documentation!

Correct cycle:

   for(int i=PositionsTotal()-1; i>=0; i--)
    if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
       if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
	 {
	  ***
	 }

or

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      ulong ticket=PositionGetTicket(index);
      if(ticket>0);
        {
         if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
           {
            ***
           }
        }
     }
 
Vladimir Karputov #:

Don't use 'PositionSelect(_Symbol)'!!! - read the documentation!

Correct cycle:

or

Thanks for your response. 

My code is designed so I would only have 1 open position at any given time. 

I also do not understand what is meant by 'InpMagic'.


Sorry in advance for being a noob.

 
AliGaygusuz #:

Thanks for your response. 

My code is designed so I would only have 1 open position at any given time. 

I also do not understand what is meant by 'InpMagic'.


Sorry in advance for being a noob.

InpMagic refers to an input (indicator/expert parameter) variable with the name "InpMagic"

input ulong InpMagic = 1234; // Magic Number this EA will assign to trades and check for when managing trades


It's meant to store the "magic number" of an EA that uniquely identifies the trades that the EA should handle.

Assuming you're on MT5: when your EA opens a trade, it should always set the magic number value for the magic parameter in the MqlTradeRequest struct.


struct MqlTradeRequest
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Trade operation type
   ulong                         magic;            // Expert Advisor ID (magic number)
   ulong                         order;            // Order ticket
   string                        symbol;           // Trade symbol
   double                        volume;           // Requested volume for a deal in lots
   double                        price;            // Price
   double                        stoplimit;        // StopLimit level of the order
   double                        sl;               // Stop Loss level of the order
   double                        tp;               // Take Profit level of the order
   ulong                         deviation;        // Maximal possible deviation from the requested price
   ENUM_ORDER_TYPE               type;             // Order type
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Order execution type
   ENUM_ORDER_TYPE_TIME          type_time;        // Order expiration type
   datetime                      expiration;       // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
   string                        comment;          // Order comment
   ulong                         position;         // Position ticket
   ulong                         position_by;      // The ticket of an opposite position
  };

If your EA doesn't open any trades but, instead, manages trades, you should still check the magic number of the trade to see if it's value is 0 (the default value) as to not interfere with trades that other EAs are handling.

 
Alexander Martinez #:

InpMagic refers to an input (indicator/expert parameter) variable with the name "InpMagic"


It's meant to store the "magic number" of an EA that uniquely identifies the trades that the EA should handle.

Assuming you're on MT5: when your EA opens a trade, it should always set the magic number value for the magic parameter in the MqlTradeRequest struct.


If your EA doesn't open any trades but, instead, manages trades, you should still check the magic number of the trade to see if it's value is 0 (the default value) as to not interfere with trades that other EAs are handling.

Thanks Alexander.

That makes so much more sense. 

 
AliGaygusuz #:

Thanks Alexander.

That makes so much more sense. 

You're welcome, take care!