Trailing stop problem based on parabolic Sar

 
Good evening dear programmers. I have a problem with the programming of my code. Indeed I started to be interested in programming since already 6 months.

I started writing my code and I have some small problems in the programming of it. These are the conditions of sale and purchase grouped into two sections:

1st section: sale and purchase following the primary trend based on the parabolic SAR in H4 and

2nd section: Sale and purchase based on secondary trends based on the H1 SAR.

Well here it all looks beautiful but here are the concerns that I have despite the compilation of the code gives no error:

1- The trailing stop based on the values ​​of the parabolic SAR works I believe on the graph but in the newspaper it signals me errors (photos attached)

2-I wrote the code in two sections, but here is no order of the section 2 is taken, since I am still novice I would like to know how to integrate it so that they can function (photos in attachments ).

Merci d'avance chers leaders






//+------------------------------------------------------------------+
//| PSAR.mq4                                                  |
//| Copyright © 2005, MetaQuotes Software Corp.                      |
//| https://www.metaquotes.net/                                      |
//+------------------------------------------------------------------+
//declaration des variables globales
extern double Lots            = 0.02 ;
//Parabolic SAR parameters
double pas                    = 0.02;
double maximum                = 0.2 ;
//declaration des variables mais dans le code
double PSar_M30, PSar_H1, PSar_H4,PSar_H4_Previous ,PSar_H1_Previous;
double open_30, open_H1, open_H4,open_H4_Previous,close_H4,close_H4_Previous,close_H1,close_H1_Previous,open_H1_Previous;
int cnt, ticket, total;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start() 
{
/ * Definition of internal variables for quick access to data
in the source code, it is very often necessary to access the values of the indicator or to manage the calculated values.
To simplify coding and speed up access, the data is placed in internal variables. * /
PSar_M30 =iSAR (NULL,PERIOD_M30,pas,maximum,0);
PSar_H1  =iSAR (NULL,PERIOD_H1,pas,maximum,0) ;
PSar_H1_Previous=iSAR (NULL,PERIOD_H1,pas,maximum,1) ;
PSar_H4  =iSAR (NULL,PERIOD_H4,pas,maximum,0) ;
PSar_H4_Previous =iSAR (NULL,PERIOD_H4,pas,maximum,1) ;
open_30  =iOpen(NULL,PERIOD_M30,0);
open_H1  =iOpen(NULL,PERIOD_H1,0);
open_H4  =iOpen(NULL,PERIOD_H4,0);
close_H1 =iClose(NULL,PERIOD_H1,0);
close_H4 =iClose(NULL,PERIOD_H4,0);
open_H1_Previous =iOpen(NULL,PERIOD_H1,1);
open_H4_Previous  =iOpen(NULL,PERIOD_H4,1);
close_H1_Previous =iClose(NULL,PERIOD_H1,0);
   {
  / * initial verification of data * /
   if(Bars<100)
     {
     Print("Nbres de barres inferieures a 100");
     return(0); 
     }
     
   if(Lots> 0.05)
     {
     Print("Nbres de lots superieurs a 0.05");
     return(0);
     }   
/ * Verification of the commercial terminal - is it empty? If so, then: in our Expert Advisor,
we only use open positions with market orders and do not process pending orders.
However, to be sure, let's introduce a trading terminal control for previously passed orders: * /
   total=OrdersTotal();
   if(total<1) 
     {
/ * checks: availability of funds on the account, etc.
Before analyzing the market situation, it is advisable to check the status of your account
to make sure it has free funds for opening a position * /
        if(AccountFreeMargin()<(1000*Lots))
          {
              Print("We have no money. Free Margin = ", AccountFreeMargin());
              return(0); 
          }
/ * Is it possible to take a long position (BUY)?
Entry requirement for the primary trend based on the Parabolic Sar in TF H4: (If the value of the current SAR parabolic is lower
the value of the opening of the current candle and that these values were contrary for the preceding candle) or (the value of closing
of the preceding candle is lower than the value of the previous parabolic and that the value of the opening of the current candle is greater than
the value of the parabolic SAR) * /
      
        if((open_H4_Previous < PSar_H4_Previous && PSar_H4 < open_H4) || (close_H4_Previous<PSar_H4_Previous && open_H4>PSar_H4) ) 
          {
           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,PSar_H4,OrderTakeProfit(),"Ichimoku psar",1111206,0,Green);
            
           if(ticket>0)
             {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
                Print("Buy order opened : ",OrderOpenPrice());
             }
           else Print("Error buy order : ",GetLastError()); 
                return(0); 
         }
/ * Is it possible to take a short position (SELL)?
Entry requirement for the primary trend based on the Parabolic Sar in TF H4: (If the value of the current parabolic SAR is higher
the value of the opening of the current candle and that these values were contrary for the preceding candle) or (the value of closing
of the preceding candle is greater than the value of the preceding parabolic and that the value of the opening of the current candle is less than
the value of the current parabolic SAR) * /
        if((open_H4_Previous > PSar_H4_Previous && PSar_H4 > open_H4) || (close_H4_Previous>PSar_H4_Previous && open_H4<PSar_H4))
          {
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,PSar_H4,OrderTakeProfit(),"Ichimoku psar",01111206,0,Red);

           if(ticket>0)
             {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                Print("ouverture de vente : ",OrderOpenPrice());
             }
           else Print("Erreur d'ouverture de vente : ",GetLastError()); 
           return(0); 
         }
      return(0);
    }
/ * Check positions previously taken * /
   for(cnt=0;cnt<total;cnt++)
      {
        if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){
        Print("Erreur de selection");
        } 
         if(OrderType()<=OP_SELL &&  // check for opened position  
            OrderSymbol()==Symbol())  // check for symbol
           {
              if(OrderType()==OP_BUY)  // position longue est ouverte
                {
/ * should it be closed?
Condition to get out of a long position: (a reversal of sudden tendency) or (downward crossing between the PSAR and the candles) * /
                  if ((Bid <= PSar_H4) || (close_H4_Previous>PSar_H4_Previous && open_H4<PSar_H4)) 
                    {
                       if (!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) // close position
                          { Print("Erreur de fermeture dordre d'achat");
                           return(0); // exit
                          }
                    }
/ * do I have to reset the trailing stop?
trailing stop is based on the upward movement of the PSAR * /
                else if(PSar_H4_Previous < open_H4_Previous && PSar_H4 < open_H4) 
                    {
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),PSar_H4,OrderTakeProfit(),0,Green))
                        { Print("Error trailing BUY order");
                          return(0);
                        }
                    }
                 }
               else if(OrderType()==OP_SELL)  // position longue est ouverte
                {                            //si c'est une position courte
/ * should it be closed?
Condition to get out of a short position: (a reversal of sudden tendency) or (upward crossing between the PSAR and the candles * /
                  
                  if((Ask >= PSar_H4) || (close_H4_Previous<PSar_H4_Previous && open_H4>PSar_H4))
                    {
                     if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                       { Print("Error close BUY order"); //  position de fermeture 
                     return(0); // exit
                       }
                    }
/ * do I have to reset the trailing stop?
trailing stop is based on the bearish movement of PSAR * /
               else if(PSar_H4_Previous > open_H4_Previous && PSar_H4 > open_H4) 
                    { 
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),PSar_H4,OrderTakeProfit(),0,Red))
                        { Print("Error Trailing SELL order");
                        return(0);
                        }
                     }
                 }
              }
           }
return(0);
   }
//--------------------------------------------------------------------------------------
// Section 2, take orders for eventual retracements against trends
   {
/ * initial verification of data * /  
   if(Bars<100)
     {
     Print("Nbres de barres inferieures a 100");
     return(0); 
     }
     
   if(Lots> 0.05)
     {
     Print("Nbres de lots superieurs a 0.05");
     return(0);
     }   
/ * Verification of the commercial terminal - is it empty? If so, then: in our Expert Advisor,
we only use open positions with market orders and do not process pending orders.
However, to be sure, let's introduce a trading terminal control for previously passed orders: * /
   total=OrdersTotal();
   if(total<1) 
     {
/ * checks: availability of funds on the account, etc.
Before analyzing the market situation, it is advisable to check the status of your account
to make sure it has free funds for opening a position * /
        if(AccountFreeMargin()<(1000*Lots))
          {
              Print("We have no money. Free Margin = ", AccountFreeMargin());
              return(0); 
          }
/ * Is it possible to take a long position (BUY)?
Entry condition for the secondary trend based on Parabolic Sar in TF H1 and H4: (If primary trend in H4 is bearish and that in H1 is bullish) * /
       if((PSar_H1 < open_H1 && PSar_H4 > open_H4 ) || (PSar_H4 > open_H4 && ( close_H1 < PSar_H1 && open_H1> PSar_H1)))
         {
           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,PSar_H1,OrderTakeProfit(),"Ichimoku psar",1111206,0,Green);
           if(ticket>0)
             {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
                Print("BUY order in H1 : ",OrderOpenPrice());
             }
           else Print("Error BUY order in H1 : ",GetLastError()); 
                return(0); 
         }
/ * Is it possible to take a short position (SELL)?
Entry condition for the secondary trend based on Parabolic Sar in TF H1 and H4: If primary trend in H4 is bullish and that in H1 is bearish * /
       if((PSar_H1 > open_H1 && PSar_H4 < open_H4 ) || (PSar_H4 < open_H4 && (close_H1 > PSar_H1 && open_H1 < PSar_H1)))
         {
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,PSar_H1,OrderTakeProfit(),"Ichimoku psar",01111206,0,Red);
           if(ticket>0)
             {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                Print("SELL order in H1 : ",OrderOpenPrice());
             }
           else Print("Error SELL order in H1 : ",GetLastError()); 
           return(0); 
         }
         return(0);
      }
/ * Check positions previously taken in H1 * /
   for(cnt=0;cnt<total;cnt++)
      {
        if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){
        Print("Erreur de selection 2");
        }
         if(OrderType()<=OP_SELL &&   //
            OrderSymbol()==Symbol())  //
           {
              if(OrderType()==OP_BUY)  // position longue est ouverte
                {
/ * should it be closed?
Condition to get out of a long position: (Resumption of the previous primary trend) or (change of trend in H4 into an uptrend and
a still bullish trend in H1) * /
                  if((PSar_H4 < open_H4 ) || (PSar_H4 > open_H4 && PSar_H1 < open_H1) || (PSar_H4 > open_H4 && ( close_H1_Previous > PSar_H1 && open_H1< PSar_H1))) 
                    {
                       if (!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) // close position
                          { Print("Erreur de fermeture dordre d'achat");
                           return(0); // exit
                          }
                    }
/ * do I have to reset the trailing stop?
the trailing stop is based on the upward movement of PSAR in H1 * /
                else if(PSar_H1_Previous < open_H1_Previous && PSar_H1 < open_H1)
                    {
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),PSar_H1,OrderTakeProfit(),0,Green))
                        { Print("Error trailing BUY order");
                          return(0);
                        }
                    }
                 }
               else if(OrderType()==OP_SELL)  // position longue est ouverte
                {                            //si c'est une position courte
/ * should it be closed?
Condition to get out of a long position: (Resumption of the previous primary trend) or (change of trend in H4 into an uptrend and
a still bullish trend in H1) * /
                  if((PSar_H4 > open_H4 ) || (PSar_H4 < open_H4 && PSar_H1 > open_H1) || (PSar_H4 < open_H4 && ( close_H1_Previous < PSar_H1 && open_H1> PSar_H1))) 
                    {
                     if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                       { Print("Error close SELL order"); //  position de fermeture 
                     return(0); // exit
                       }
                    }
/ * do I have to reset the trailing stop?
We define the limit switch only in the case where the position already has a profit exceeding the end-of-leakage level
in points and if the new level of the end is better than the previous one. * /
               else if(PSar_H1_Previous > open_H1_Previous && PSar_H1 > open_H1) 
                    { 
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),PSar_H4,OrderTakeProfit(),0,Red))
                        { Print("Error Trailing SELL order");
                        return(0);
                        }
                     }
                 }
              }
           }
return(0);
}
}
Reason: