How Can I solve this error in using ATR as trailingstop condition?

 

Please let me ask for some helps because I have tried many times .It is not in good condition .Still error.So I decide to get some help.

Here is trade condition 

 

  if ( OrdersTotal() == 0 )
    
       {
   
     OrderSend(Symbol(), OP_BUYSTOP,1, Ask+INCREMENT*pt, 3,Bid-5*pt ,0,"Buy", MAGIC,TimeCurrent()+10800, Blue);
     OrderSend(Symbol(), OP_SELLSTOP,1, Bid-INCREMENT*pt, 3,Ask+5*pt,0, "Sell", MAGIC, TimeCurrent()+10800, Red);
       }
   

I use stop pending orders  and set some stop loss .This is very close.

here picture of trade condition.

here open sell stop condition as sell position.


This sell orders has stop loss as shown in red line.

I tried to put order modify function

Here is my condition for modify SL.

   if(OrderType() == OP_SELL)

                //  0.32172 < 0.32477  - 5   =  0.3242                                                  

     if (Bid < NormalizeDouble(OrderOpenPrice()-TrailingStart*pt,Digits))

  in sell condition , if Bid (0.32172)  less than open price - trailingstartpoint (5) and answer is 0.3242

  This mean if current bid less than open price+traingstart ,need to modify exsiting Stop Loss .

  if(OrderType() == OP_SELL)
                //  0.32172 < 0.32477  - 5    0.3242                                                  
     if (Bid < NormalizeDouble(OrderOpenPrice()-TrailingStart*pt,Digits))
         {
 tStopLoss = NormalizeDouble(Ask+atr*pt,Digits);//0.00265+0.32172 =3244
            {
                 ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Red);
            }
                   
                    if ( ticket < 0 )
              {
                Alert("OrderModify error ",GetLastError());
              } 
         
          }

here is custom function whole body

void TrailStops(double Trailingstart,double atr)
{
  
   static int ticket;
  static double tStopLoss;
    for (int i = 0; i < OrdersTotal(); i++)
    {
        
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            continue;
        }
  
        if (OrderSymbol() != Symbol()) {
            continue;
        }
  
        if(OrderType() == OP_BUY) 

           if(Ask> NormalizeDouble(OrderOpenPrice()+TrailingStart* pt,Digits))
           {
        tStopLoss = NormalizeDouble(Bid-atr*pt,Digits);
          
             {
               ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Blue);
             }
                   if ( ticket < 0 )
              {
                Alert("OrderModify error ",GetLastError());
              } 
                
            
        }
        
         if(OrderType() == OP_SELL)
                //  0.32172 < 0.32477  - 5    0.3242                                                  
     if (Bid < NormalizeDouble(OrderOpenPrice()-TrailingStart*pt,Digits))
         {
 tStopLoss = NormalizeDouble(Ask+atr*pt,Digits);//0.00265+0.32172 =3244
            {
                 ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Red);
            }
                   
                    if ( ticket < 0 )
              {
                Alert("OrderModify error ",GetLastError());
              } 
                
               
            
         }
    
}
  
}  

I use atr value in argument and I will call ATR 


extern int       MAGIC=1803;
extern int ATR_Period =14;
extern double TrailingStart = 5; 



void OnTick()
  {
  
  
        
 double ATR = iATR(NULL, 0, ATR_Period, 0);
 

if(TrailingStart>0&&ATR>0)
 TrailStops (TrailingStart, ATR);

 }

Show error 130 .this mean invalid sl.


So Please help me What need to edit in my codes.

I need help from expert seniors.

Thanks All.

 
  1.  double ATR = iATR(NULL, 0, ATR_Period, 0);
    If the ATR on the EURUSD is 100 PIPs you read 0.0100º

  2. tStopLoss = NormalizeDouble(Bid-atr*pt,Digits);
    and that value multiplied by 0.0001º is nothing.

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?