//+------------------------------------------------------------------+
//|                                                        EJ_4H.mq4 |
//|                                                        Chinplant |
//|                                                      version 1.0 |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+

#property copyright "Chinplant"

//---- input parameters
extern double    Lots=1;
extern double    TakeProfit=1000;
extern double    Stoploss=0;
extern int       Slippage=0;
extern bool      accum=false; //allow accumulation of same order types

int CurrentBarOpentime;
int majic=1234567890;

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 

   //LotCalc(1);
   int cnt, ticket, total;
      
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
 
   double BuySignal=0;
   double SellSignal=0;
   double CurrBuySignal=0;
   double CurrSellSignal=0;
   
   bool Buy_Order_Exist=false;
   bool Sell_Order_Exist=false;
   
   BuySignal=iCustom(NULL,0,"EJ_Signal",3,300,1,1);
   SellSignal=iCustom(NULL,0,"EJ_Signal",3,300,0,1);
   CurrBuySignal=iCustom(NULL,0,"EJ_Signal",3,300,1,0);
   CurrSellSignal=iCustom(NULL,0,"EJ_Signal",3,300,0,0);
   
   total  = OrdersTotal(); 
     
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS);
      // OrderPrint();
      if(OrderMagicNumber()==majic && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
           if (!accum) Buy_Order_Exist=true; // some orders already opened by this EA
            
            // should it be closed?
           if(CurrSellSignal>0)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            }
         else // go to short position
           {
            // should it be closed?
            
           if (!accum) Sell_Order_Exist=true; // some orders already opened by this EA
           
           if(CurrBuySignal>0)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
           }
        }
      }
   
     if (CurrentBarOpentime != Time[0] && (SellSignal>0 || BuySignal>0))
     {
       if (BuySignal>0 && !Buy_Order_Exist) 
      {
         if (Stoploss!=0)
           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-Stoploss*Point,Ask+TakeProfit*Point,"EJ_4H",majic,0,Green);
         else
           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit*Point,"EJ_4H",majic,0,Green);

         if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
         else Print("Error opening BUY order : ",GetLastError()); 
       }
         
     if (SellSignal>0 && !Sell_Order_Exist)
      {
        if (Stoploss!=0)
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Ask+Stoploss*Point,Bid-TakeProfit*Point,"EJ_4H",majic,0,Red);
        else
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit*Point,"EJ_4H",majic,0,Red);
       
        if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print("Error opening SELL order : ",GetLastError()); 
       } 
    }
   return(0);
  }
//+------------------------------------------------------------------+