need help for my first autotrailingstop funciton, i checked codes again and again, it just work wrong way. thanks

 

please figure the problem out. thanks a lot.


//+------------------------------------------------------------------+
//|                                                       移动止盈止损.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                  Indicator part                                  |
//+------------------------------------------------------------------+
extern int Kperiod=4;
extern int Dperiod=2;
extern int Slowing=2;
//+------------------------------------------------------------------+
//|                  Trailingstop part                               |
//+------------------------------------------------------------------+
extern string 自动止损参数="默认打开";// this is just a instruction of auto stoploss
extern bool AutoStoploss=True;
extern double stoploss= 15;
extern double baoben=10;
extern string 自动止盈参数="默认打开";// this is just a instruction of auto takeprofit
extern bool AutoTakeProfit=True;
extern double takeprofit=50;
extern string 盈利后移动止损="默认打开";// this is just a instruction of auto trailingstop
extern bool AutoTrailingStop=true;
extern double TrailingStop = 15;
extern string 分次离场参数="按比例分步撤退";// this is just a instruction of gradually closeorder
extern bool Gradually = False;                
extern int GraduallyNum = 3; 
static int yi = 1;            
double stp,tpt,OpenPrice;
double OriginalLot;
double Pip,Slip,Slipage;
bool   fenpiCB,fenpiCS;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  if(Digits==2 || Digits==3 || Digits==5) { Pip = 10*Point; Slip=10*Slipage;}
  if(Digits==4) { Pip = 1*Point; Slip=1*Slipage;}
   //---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason)); 
 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {  
    if(OrdersTotal()==0)
      {
       return;
      }
      else
       {   
           closeorder();
           trailingstop();
           if (Gradually=false){return;} 
           else fenpi();
       }    
    
  }  
 
bool trailingstop()
  {
    {
      for(int cnt=0;cnt<OrdersTotal();cnt++)  
    {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
       {
 
         if(OrderSymbol()==Symbol())
           {
             stp=OrderStopLoss();   
             tpt=OrderTakeProfit();  
             OpenPrice=OrderOpenPrice(); 
 
             if (OriginalLot == 0) 
               {
                 OriginalLot=OrderLots();
               } 
            }
         
        }
       
       if(OrderType()==OP_BUY)          
           {
              if (AutoStoploss && AutoTakeProfit && stp==0 && tpt==0&&OrderMagicNumber()<=0)   
              bool OM=OrderModify(OrderTicket(),OrderOpenPrice(),Ask-Pip*stoploss,Ask+Pip*takeprofit,0,Green)>0;
              else
                {
                  if (AutoStoploss && stp==0)                  
                    {
                      OrderModify(OrderTicket(),OrderOpenPrice(),Ask-Pip*stoploss,OrderTakeProfit(),0,Green);
                         }
      
                      if (AutoTakeProfit && tpt==0)               
                    {
                      OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+Pip*takeprofit,0,Green);
                    }
                }   
     
              if (AutoStoploss && AutoTakeProfit && stp>0 && baoben<=TrailingStop)
                    {
                      if (AutoTrailingStop  && ((Bid - OpenPrice) == Pip*baoben)&&OrderMagicNumber()<=0)
                        {
                          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
                                                     
                        }
                                                
                    
                    }
                 
               if (AutoStoploss && AutoTakeProfit && stp>0 && baoben>TrailingStop)
                  {
                    if (AutoTrailingStop  && ((Bid - OpenPrice) > Pip*TrailingStop)&&OrderMagicNumber()<=0)       
                       {
                         OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Pip*TrailingStop,OrderTakeProfit(),0,Green);
                          
                       }
                  }
                 
                }    
           
      
      if(OrderType()==OP_SELL)                   
           {
             if (AutoStoploss && AutoTakeProfit && stp==0 && tpt==0)  
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid+Pip*stoploss,Bid-Pip*takeprofit,0,Green);
               }
             else
               {
                 if (AutoStoploss && stp==0)          
                   {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid+Pip*stoploss,OrderTakeProfit(),0,Green);
                   }
                 if (AutoTakeProfit && tpt==0)        
                   {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid-Pip*takeprofit,0,Green);
                   }  
                } 
              if (AutoStoploss && AutoTakeProfit && stp>0 && baoben<=TrailingStop)
                    {
                      if (AutoTrailingStop  && ((OpenPrice-Ask) >= Pip*baoben)&&OrderMagicNumber()<=0)
                        {
                          
                            {
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
                              
                            }  
                              
                           
                        
                        } 
             
                    }
               if (AutoStoploss && AutoTakeProfit && stp>0 && baoben>TrailingStop)                   
                    {
                      if(AutoTrailingStop  && ((OpenPrice-Ask) > Pip*TrailingStop ))       
                        {
                                    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Pip*TrailingStop,OrderTakeProfit(),0,Green);
                        }
                    }
            }
            
            else
              {
                OriginalLot=0;
              }
   
     }
     }
     return(true);
 }
 

 bool fenpi()
 {
     double lot=OrderLots();
    {
      for(int cnt=0;cnt<OrdersTotal();cnt++)  
    {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
       {
 
         if(OrderSymbol()==Symbol())
           {
             stp=OrderStopLoss();   
             tpt=OrderTakeProfit();  
             OpenPrice=OrderOpenPrice(); 
             
           }
         if(OrderType()==OP_BUY)          
           {
             
             { 
                if (NormalizeDouble((Bid - OpenPrice)/Pip,0) == TrailingStop*yi)
                       {
                         fenpiCB=true;
                         if  (fenpiCB==true && lot >= NormalizeDouble(OriginalLot*(1/GraduallyNum),2))
                      {
                        OrderClose(OrderTicket(),NormalizeDouble(OriginalLot/GraduallyNum,2),Bid,3,CLR_NONE);
                      }
                           if (yi<GraduallyNum){yi=yi+1;}
                           else return(0);
                       }
                         else fenpiCB=false;
                   }
           }
         if(OrderType()==OP_SELL) 
            {
              
               { 
                      if (NormalizeDouble((OpenPrice - Ask)/Pip,0) == TrailingStop*yi)
                        {           
                         fenpiCS=true;
                     if  (fenpiCS==true && lot >= NormalizeDouble(OriginalLot*(1/GraduallyNum),2))
                       {
                         OrderClose(OrderTicket(),NormalizeDouble(OriginalLot/GraduallyNum,2),Ask,3,CLR_NONE);
                       }
                            if (yi<GraduallyNum){yi=yi+1;}
                              else return(0);
                        }
                       else fenpiCS=false;
                    }
        
        
           }
           
           
       }
    }
  }
  return(true);
}
 
bool closeorder()



string getUninitReasonText(int reasonCode) 
  { 
   string text=""; 
//--- 
   switch(reasonCode) 
     { 
      case REASON_ACCOUNT: 
         text="Account was changed";break; 
      case REASON_CHARTCHANGE: 
         text="Symbol or timeframe was changed";break; 
      case REASON_CHARTCLOSE: 
         text="Chart was closed";break; 
      case REASON_PARAMETERS: 
         text="Input-parameter was changed";break; 
      case REASON_RECOMPILE: 
         text="Program "+__FILE__+" was recompiled";break; 
      case REASON_REMOVE: 
         text="Program "+__FILE__+" was removed from chart";break; 
      case REASON_TEMPLATE: 
         text="New template was applied to chart";break; 
      default:text="Another reason"; 
     } 
//--- 
   return text; 
 }
 
 
 

Please allow me to offer you some constructive criticism... 


It looks like you might not fully understand the concept of scope. You are using curly braces in a way that is designed to limit scope when this is not the desired outcome. For example: 

 bool fenpi()
 {
     double lot=OrderLots();
    { //<-- this should not be here!
      for(int cnt=0;cnt<OrdersTotal();cnt++)  
    {

Another thing, you are trying to do way too much in your functions. Break the problem down into more manageable functions with less nesting (flatten your code). Also, stop using global variables unless you absolutely have to, and when you do it's good practice to prefix the variable name with "g_". Here is an example of how you might refactor your code to make it easier to read and debug. 

#property strict

input bool  inpAutoStoploss = true;
input int   inpStopLossPips = 1;

int g_point_modifier = 1;
//+------------------------------------------------------------------+
int OnInit()
{

   if(_Digits == 3 || _Digits == 5)
      g_point_modifier = 10;
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
void OnTick()
{
   if(OrdersTotal() == 0)
      return;
   if(inpAutoStoploss)
      my_trailing_stops();  
}
//+------------------------------------------------------------------+

#include <stdlib.mqh>
#include <Double.mqh> /* https://www.mql5.com/en/code/19727 */
bool trailing_stop_by_ticket(int ticket, int trailing_points)
{
   if(!OrderSelect(ticket, SELECT_BY_TICKET) || OrderType() > 1)
      return false;
   string symbol = OrderSymbol();
   int stops_level = (int)SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL);
   if(trailing_points < stops_level)
   {
      Print("TrailingStopError: Invalid Stops");
      return false;
   }
   MqlTick tick;
   if(!SymbolInfoTick(symbol, tick))
      return false;
   double delta_points = trailing_points * SymbolInfoDouble(symbol, SYMBOL_POINT);
   double stop_curr = OrderStopLoss();
   int    type = OrderType();
   double stop_new;
   if(type == OP_BUY)
      stop_new = CDouble::RoundToTick(tick.bid - delta_points, symbol);
   else
      stop_new = CDouble::RoundToTick(tick.ask + delta_points, symbol);
   if(stop_curr == 0.0
      || (type == OP_BUY && stop_new > stop_curr)
      || (type == OP_SELL && stop_new < stop_curr)
   ){
      return OrderModify(ticket, OrderOpenPrice(), stop_new, OrderTakeProfit(), 0, clrRed);
   }
   return true;
}
//+------------------------------------------------------------------+
void my_trailing_stops()
{
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS)
         && OrderSymbol() == _Symbol
         && OrderMagicNumber() <= 0
      ){
         bool is_trailed = trailing_stop_by_ticket(
            OrderTicket(), 
            inpStopLossPips * g_point_modifier
         );
         if(!is_trailed)
            Print("TrailingError: ", ErrorDescription(_LastError));
      }
   }
}
 
nicholi shen:

Please allow me to offer you some constructive criticism... 


It looks like you might not fully understand the concept of scope. You are using curly braces in a way that is designed to limit scope when this is not the desired outcome. For example: 

Another thing, you are trying to do way too much in your functions. Break the problem down into more manageable functions with less nesting (flatten your code). Also, stop using global variables unless you absolutely have to, and when you do it's good practice to prefix the variable name with "g_". Here is an example of how you might refactor your code to make it easier to read and debug. 

I'm learning mql4 2 weeks, this is my first coding work, thank you very much for your advice, I will continue to work hard to fix it
Reason: