
#property copyright "Copyright © 2009, primajaya"
#property link      ""

//---- input parameters
extern int       TP1           = 500;
extern int       SL1           = 10;
extern int       TP2           = 500;
extern int       SL2           = 10;
extern int       TP3           = 500;
extern int       SL3           = 10;
extern int       TP4           = 500;
extern int       SL4           = 10;
extern int       RangePoint   = 0;
extern double    Lots      = 1;
extern double    RiskPercent  = 1;
extern bool      UseMM        = false;
extern int       MagicNumber  = 123456789;
extern bool      DelOpposite = false;
extern bool MoveToBEP = True;
extern int LockProfit = 1;
extern int TrailingStop = 10;
double LotSize;

#define slip 2
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*point();
   double tik = iVolume(NULL,0,0);
   int exp = TimeCurrent() + (Period()*59);
   double buyPrice, sellPrice,tp1,sl1,tp2,sl2,tp3,sl3,tp4,sl4;
   RefreshRates();
   
   if (UseMM)LotSize = (AccountBalance()*RiskPercent)/10000; 
   else LotSize = Lots;
   
   if(High[1]-Ask < stoplevel) buyPrice = Ask + RangePoint*point();
   else buyPrice = High[1];
   
   if(Bid-Low[1] < stoplevel) sellPrice = Bid - RangePoint*point();
   else sellPrice = Low[1];
   
   if(OrdersTotal() == 0)
   {
      if(tik <= 5)
      {
         ////////////////////// TP1 SL1 ///////////////////////////////
         openOrder(Symbol(), OP_BUYSTOP,LotSize,buyPrice,buyPrice-SL1*point(), buyPrice+TP1*point(),"_Buy_1",MagicNumber,exp,Navy);
         openOrder(Symbol(), OP_SELLSTOP,LotSize,sellPrice,sellPrice+SL1*point(), sellPrice-TP1*point(),"_Sell_1",MagicNumber,exp,Crimson);   
         
         ////////////////////// TP2 SL2 ///////////////////////////////
         openOrder(Symbol(), OP_BUYSTOP,LotSize,buyPrice,buyPrice-SL2*point(), buyPrice+TP2*point(),"_Buy_2",MagicNumber,exp,Navy);
         openOrder(Symbol(), OP_SELLSTOP,LotSize,sellPrice,sellPrice+SL2*point(), sellPrice-TP2*point(),"_Sell_2",MagicNumber,exp,Crimson);  
         
         ////////////////////// TP3 SL3 ///////////////////////////////
         openOrder(Symbol(), OP_BUYSTOP,LotSize,buyPrice,buyPrice-SL3*point(), buyPrice+TP3*point(),"_Buy_3",MagicNumber,exp,Navy);
         openOrder(Symbol(), OP_SELLSTOP,LotSize,sellPrice,sellPrice+SL3*point(), sellPrice-TP3*point(),"_Sell_3",MagicNumber,exp,Crimson);  
         
         ////////////////////// TP4 SL4 ///////////////////////////////
         openOrder(Symbol(), OP_BUYSTOP,LotSize,buyPrice,buyPrice-SL4*point(), buyPrice+TP4*point(),"_Buy_4",MagicNumber,exp,Navy);
         openOrder(Symbol(), OP_SELLSTOP,LotSize,sellPrice,sellPrice+SL4*point(), sellPrice-TP4*point(),"_Sell_4",MagicNumber,exp,Crimson);  
      }   
   }
   
   if(OrdersTotal () == 8)
   {
      for (int i=0;i<OrdersTotal();i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol())
         {
            if(OrderMagicNumber()==MagicNumber)
            {
               if(DelOpposite)
               {
                  if(OrderType()==OP_BUY) delPending(OP_SELLSTOP,MagicNumber);
                  if(OrderType()==OP_SELL) delPending(OP_BUYSTOP,MagicNumber);
               }
            }
         
         }
      }
   
   }
   
   if(OrdersTotal() > 0)
   {
      if(MoveToBEP) BEP();
      if(TrailingStop>0) TS();
   }   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void openOrder(string simbol, int trade, double lotsize, double price, double sl, double tp,string pesan, int magic, int exp, color warna) 
{                     
  int tiket=OrderSend(simbol,trade,lotsize,price,slip,sl,tp,pesan,magic,exp,warna);                              
      if(tiket>0)
      {  
         if(OrderSelect(tiket,SELECT_BY_TICKET,MODE_TRADES)) OrderPrint(); 
      }
         else Print("Cannot send order because of : ",GetLastError());        
}

void delPending(int trade, int MagicNumber)
{  
    for(int i=0;i<OrdersTotal();i++)
    {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderType()==trade && OrderMagicNumber()==MagicNumber)
       OrderDelete(OrderTicket(),CLR_NONE);
    }   
}

double point()
{
   double p;
   if(Digits==5 || Digits==3) p = Point*10;
   else p = Point;
   
   return(p);
}

void BEP()
{
      for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderOpenPrice() > point()*TP1)
               {
                  if(OrderStopLoss()<OrderOpenPrice())
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+point()*LockProfit,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }
            }
         
            if(OrderType()==OP_SELL)
            {
               if(OrderOpenPrice()-Ask > point()*TP1)
               {
                  if(OrderStopLoss()>OrderOpenPrice())
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-point()*LockProfit,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }      
            }
         } 
      }   
}

void TS()
{
 for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderOpenPrice()>point()*TrailingStop)
               {
                  if(OrderStopLoss()<Bid-point()*TrailingStop || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-point()*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }
            }
         
            if(OrderType()==OP_SELL)
            {
               if((OrderOpenPrice()-Ask)>(point()*TrailingStop))
               {
                  if((OrderStopLoss()>(Ask+point()*TrailingStop)) || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+point()*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }      
            }
         } 
      
      }   
}