//+------------------------------------------------------------------+
//|                                                  SimpleClose.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

datetime LastAlertTime;
extern int     TimeFrame = 60;
extern double  PriceTest = 0.0;
extern bool    AlertAbove  = false;
extern bool    AlertBelow  = false;
int cnt, ticket, total;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
   //------------------------------------------
   
   if(AlertAbove && AlertBelow) {
      Print("Both flags cannot be ture");
      return(0);
   }
   
   
    if (LastAlertTime < Time[0])
   {
      
      //int    counted_bars=IndicatorCounted();
       int i=1;
       //TimeFrame = PERIOD_H1;  // For Testing use One min. PERIOD_M1;
      double   testclose = iClose(NULL,TimeFrame,1);
      
      if (testclose > PriceTest && AlertAbove) 
      {
         //Alert("Above ... New Candle Forming on ",Symbol()," TimeFrame: ",Period());
         Print("Above ... New Candle Forming on ",Symbol()," TimeFrame: ",Period());
         
         string Symb=Symbol();                        // Symbol
         double Dist=1000000.0;                       // Presetting
         int Real_Order=-1;                           // No market orders yet
         //double Win_Price=WindowPriceOnDropped();     // The script is dropped here
         //-------------------------------------------------------------------------------- 2 --
            //--- should close the sell order
            //----
               //Print("Order Found ----------- start");
               cnt=0;
               ticket=0;
               total=0;
               
               total=OrdersTotal();
               if(total>0) 
                {
                  for(cnt=0;cnt<total;cnt++)
                  {
                     //Print("Order Found ");
                     if (OrderSelect(cnt,SELECT_BY_POS)==true) // If the next is available
                     {                                       // Order analysis:
                        //----------------------------------------------------------------------- 3 --
                        if (OrderSymbol()!= Symb) continue;    // Symbol is not ours
                        int Tip=OrderType();                   // Order type
                        if (Tip>1) continue;                   // Pending order  
                        if(OrderType() == OP_BUY) {
                           Alert("Buy order and expert is for Sell.... Kia kar raha hey bhai");
                        } else if(OrderType() == OP_SELL) {
                           OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                           OrderClose(OrderTicket(),OrderLots(),Ask,3,Black); // close position
                        }
                        //----------------------------------------------------------------------- 4 --
                        
                     }
                  }
               }
         //----     
      }   
       //Print(">>>>>????>>>Price Has Closed Above ", PriceTest, " On ", TimeFrame, " Minute Bar");
      if (testclose < PriceTest && AlertBelow) 
      {
         //Alert("Below .... New Candle Forming on ",Symbol()," TimeFrame: ",Period());
         Print("Below .... New Candle Forming on ",Symbol()," TimeFrame: ",Period());
         Symb=Symbol();                        // Symbol
         Dist=1000000.0;                       // Presetting
         Real_Order=-1;                           // No market orders yet
         //double Win_Price=WindowPriceOnDropped();     // The script is dropped here
         
         // Should close all buy orders.
         //----
               //Print("Order Found ----------- start");
               cnt=0;
               ticket=0;
               total=0;

               total=OrdersTotal();
               if(total > 0) 
                {
                  for(cnt=0;cnt<total;cnt++)
                  {
                     Print("Order Found ");
                     if (OrderSelect(cnt,SELECT_BY_POS)==true) // If the next is available
                     {                                       // Order analysis:
                        //----------------------------------------------------------------------- 3 --
                        if (OrderSymbol()!= Symb) continue;    // Symbol is not ours
                           Tip=OrderType();                   // Order type
                        if (Tip>1) continue;                   // Pending order  
                        if(OrderType() == OP_SELL) {
                           Alert("Sell order and expert is for Buy.... Kia kar raha hey bhai");
                        } else if(OrderType() == OP_BUY) {
                           OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                           OrderClose(OrderTicket(),OrderLots(),Bid,5,Red); // close position
                        }
                        //----------------------------------------------------------------------- 4 --
                        
                     }
                     
                  }
            }
         //----
      }
      
      //   Print(">>>>????>>>>>> Price Has Close Below ", PriceTest, " On ", TimeFrame, " Minute Bar");   
         
         
      //Alert("New Candle Forming on ",Symbol()," TimeFrame: ",Period());
      LastAlertTime = Time[0];
   }
   
   
   
   
   
   
   
   
   
   
   
   
   //--------------------------------------
   return(0);
  }
//+------------------------------------------------------------------+