optimizer not working when i use stops(buy and sell) orders

 
Here is the code snippet am using
if (isSell()) // if it is a sell signal
            {
               if (trigger_strat)// if the trigger strategy has been selected 
                                 //(for every signal to next there can only be a maximum of 7 trades)
                  {
   
                     // close all opposite trades and limits
                        // check if trigger had already been pulled
                        closeAllBuyStops(); // close all buy stops 
                        closeAllBuyPositions();// close all buy positions
                        
                        if (sell_trigger == false)// checking whether trigger had been pulled in this particular signal
                        {
                           openGrids("sell",max_trades);// open the sell grid
                           //tradeNormal("sell");
                           sell_trigger = true;
                           buy_trigger  = false;
                           
                           // consider adding function that decides the spacing of buy and sell grids
                           // (depending on previous volatility you can place them closer or further apart)
                           
                        }
                    }
                  
                  // ------------------------------------------------------- no trigger strategy for sell goes here -----------------------------------------
                  else
                  {
                  
                     // input strategy 
                  }
            }
 
Here is the code for the openGrids function.
void openGrids(string ordType, int maxt)

{

   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   if(ordType == "buy")

      { 
        double tp = Ask + 100;

        //int atrval = grid.atrValue();

        if (maxt == 1)

        {
           trade.Buy(0.2,NULL,Ask,Ask-30,tp,NULL);

        }

        else if (maxt > 1 )

        {

         for(int i= 1; i< maxt+1; i++)

           {
            if (i==1)
            {
               trade.BuyStop(0.2,Ask+(3*i),_Symbol,Ask-30,Ask+100,ORDER_TIME_GTC,0,NULL);
            }
            else
            {
               trade.BuyStop(0.2,Ask+(10*i),_Symbol,Ask+(10*i)-30,Ask+100,ORDER_TIME_GTC,0,NULL);
            }

            

            //trade.SellStop(0.2,Ask+(atrval*i),_Symbol,0,0,ORDER_TIME_GTC,0,NULL);

           }

         

        

        }else 

        {

        

        }

        

   

      } 

    else if (ordType == "sell")

       {

       

       //int atrval = grid.atrValue();

       

       double tp = Bid - 100;

        if (maxt == 1)

        {

            

            trade.Sell(0.2,NULL,Bid,Bid+40,Bid-100,NULL );

        }

        else if (maxt>1)

        

        {

            for(int i= 1; i< maxt+1; i++)
           {
            if (i == 1)
            {
               trade.SellStop(0.2,Bid-3,_Symbol,Bid+30,Bid-100,ORDER_TIME_GTC,0,NULL);
            }
               trade.SellStop(0.2,Bid-(10*i),_Symbol,Bid-(10*i)+30,Bid-100,ORDER_TIME_GTC,0,NULL);

           } 

        }

        

       }

}