help with code.

 
I have this piece of code and i want it to stop sending an order incase the order is already sent...it keep sending the order many times incase the conditions 
set are met.: Basically it check orders history and incase an order is closed but it does not exist in the open orders as a buystop/sellstop or buy/sell then it 
send the order as a pending. Magic number is used to filter the orders. 





void calculate_orders ()



{



   //Print ("Launching order check");

   int jl = beginning_hour *60 + startMinute;

   int jk = TimeHour(TimeCurrent()) * 60 + TimeMinute(TimeCurrent());

   //Print ("Jl= ", jl);

   //Print ("Jk = ", jk);





    for (int j=jl; j<= jk; j++)

         {

             //  Print ("Checking History");

             int total_order = checkTotalOrders(j);



             int magicnumber = j;

             //Print ("Checking Magic Number", magicnumber);

             for(int p=0; p<OrdersHistoryTotal(); p++)



        {

         bool orderSelect; // Returned boolean value from 'OrderSelect' will be stored on this variable

         string orderSymbol; // Currency pair's name from selected order will be stored on 'orderSymbol'

         int orderType; // Order Type of selected order will be store on this variable

                        // Start to catch values for above variables..

         datetime    orderOpenDay;

         datetime    orderOpenTime;

         datetime    orderOpenHour;

         

         orderSelect =OrderSelect(p, SELECT_BY_POS, MODE_HISTORY);

         orderSymbol =OrderSymbol();

         orderType = OrderType();

         orderOpenTime = OrderOpenTime();

         orderOpenDay =  TimeDay(orderOpenTime);

         orderOpenHour = TimeHour(orderOpenTime);

         

 

         if(orderSymbol==_Symbol && OrderMagicNumber() == magicnumber && orderSelect == 1 && orderOpenDay>= TimeDay(TimeCurrent()))

           {

           

          // Print ("Order Found In History");

           

           double orderOpenPrice = OrderOpenPrice();

           double orderStopLoss  = OrderStopLoss();

           orderType      = OrderType();

           int orderTypee = 1;

           if (orderType == OP_BUY || orderType == OP_BUYSTOP)

            {

               //Print ("Order type is a Buy Trade");

               orderTypee = OP_BUYSTOP;

            } else if (orderType == OP_SELL || orderType == OP_SELLSTOP) {orderTypee = OP_SELLSTOP;}

           

                 for (int q=0; q<=OrdersTotal();q++)

                 

                 {

                           //  Print ("Checking Current orders");

                             bool orderSelect1; // Returned boolean value from 'OrderSelect' will be stored on this variable

                             string orderSymbol1 = OrderSymbol(); // Currency pair's name from selected order will be stored on 'orderSymbol'

                             int orderType1; // Order Type of selected order will be store on this variable

                             int orderMagic;

                             orderSelect1=OrderSelect(q, SELECT_BY_POS, MODE_TRADES);

                             orderSymbol=OrderSymbol();

                             orderType=OrderType();

                             orderMagic = OrderMagicNumber();

                             int toLaunch=0;

                            // Print("Magic Number", magicnumber);

                             if( orderSelect == 1) 

                             

                             { 

                               

                                if(OrderMagicNumber() == magicnumber && orderSymbol1==_Symbol )

                                {

                                      { 

                               //Print("Order Selected");

                                orderType1       = OrderType();

                                double orderOpenPrice1 = OrderOpenPrice();

                                orderStopLoss =   OrderStopLoss();

                               // int toLaunch = 0;

                                

                                if (orderOpenPrice == orderOpenPrice1 && orderType == orderTypee)

                                {

                                    Print("Available already");  

                                

                                } else  

                                

                                   {       

                                          if (orderOpenPrice != orderOpenPrice1 && (orderTypee == OP_BUYSTOP || orderTypee == OP_BUY) && total_order <ZBuyStops)

                                          {

                                           Print("Ready to execute Order ", orderType, " Price ", orderOpenPrice, "Stop Loss ", orderOpenPrice - spread);

                                           int ticket;

                                           ticket = OrderSend(_Symbol,OP_BUYSTOP,lot,orderOpenPrice,4,orderOpenPrice - spread,0.0,IntegerToString(magicnumber,10),magicnumber,0,clrRed);

                                           Print("Order Mg = ", magicnumber);

                                           Print("Order Ticket = ", ticket);

                                           

                                           if (ticket >=0)

                                           

                                              {

                                                break;

                                              }

                                          }else if(orderTypee == OP_SELLSTOP || orderTypee == OP_SELL)

                                          

                                             {

                                                orderTypee = OP_SELLSTOP;

                                                Print("Ready to execute Order ", orderType, " Price ", orderOpenPrice, "Stop Loss ", orderOpenPrice + spread);

                                                orderTypee = OP_SELLSTOP;

                                                int ticket;

                                                ticket = OrderSend(_Symbol,OP_SELLSTOP,lot,orderOpenPrice,4,orderOpenPrice + spread,0.0,IntegerToString(magicnumber,10),magicnumber,0,clrRed);

                                                

                                                if (ticket>=0)

                                                

                                                 {

                                                 

                                                 break;

                                                 }else{ 

                                                        Print("Order failed to close with error - ",GetLastError());

                                                 

                                                       }





                                             

                                             } 

          }

   

                                           

                                

                                   }

                                   

                                 }

                                 }

                             

                             }





                

                 }

           

           

           

           

           }   

        } 

                  

     

}
 

You should format your code to make it easier to read and remove all the unnecessary white space.

Many people will not even bother to try to read your code unless it is tidied up.