

#property copyright "Copyright © 2008, AKA TradeForexFX"
#property link      "http://www./"

extern string  Expert_Name    = "Three Level TP Beta";



extern int MagicNumber = 59768;
extern double Lots = 3.0;
bool EachTickMode = True;

extern bool MoveStopOnce = True;
extern int MoveStopWhenPrice = 4;
extern int MoveStopTo = 1;


extern bool Close_Lots_Mode = True;
extern double First_Close = 1.0 ;
extern int First_Close_TP = 7 ;

extern double Second_Close = 2.0 ;
extern int Second_Close_TP = 15 ;


extern double Third_Close = 0.0 ;
extern int Third_Close_TP = 3000 ;

extern int Slippage = 0;

double   myPoint;
int BarCount;

//******************************************

// Trading Hours variables end here
//************************************



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   
   BarCount = Bars;

  myPoint = SetPoint(Symbol());
   

   return(0);

}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {

   if (Close_Lots_Mode) ClosePartialLots();
   
   if (MoveStopOnce)  MoveStopOnceMode();
   
}

// Modified to use a function for easier use in an EA
void ClosePartialLots()
{
   int Total;
   double QtyOrders = 0.00;
   double QtyOrderSells = 0.00;

   Total = OrdersTotal();
   
   //Check position
   bool IsTrade = False;

// Better to do this from highest to lowest
//   for (int i = 0; i < Total; i ++) {
   for (int i = Total - 1; i >= 0; i --) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//*******************************************
// Important to have code check MagicNumber and Symbol

//      if (OrderSymbol() != Symbol()) && (OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
//*******************************************
      
      if(OrderType() <= OP_SELL)
      {
         IsTrade = True;
         
         if(OrderType() == OP_BUY) QtyOrders = OrderLots();
       
         if(OrderType() == OP_SELL) QtyOrderSells = OrderLots();
       
Comment (
          "Initial Lot(s) -  ", Lots , "\n",

          "Quantity in trade -  ", QtyOrders + QtyOrderSells , "\n"
          
          
         ); 
            
         if(OrderType() == OP_BUY) {
       
 //Close_Lots - Buy
 
            if(QtyOrders == Lots)
            {                      
               if(Bid - OrderOpenPrice() > myPoint * First_Close_TP)
               {
                     OrderClose(OrderTicket(), First_Close , Bid, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
   
   
            if( ( (QtyOrders - Third_Close) > 0.00 ) 
                && (QtyOrders - (Second_Close + Third_Close ) <= 0.00))
            {                 
               if(Bid - OrderOpenPrice() >  (myPoint * Second_Close_TP))
               {
                     OrderClose(OrderTicket(), Second_Close , Bid, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
   
            if((QtyOrders - Third_Close <= 0.00)
                && (QtyOrders > 0.00 ))
            {                 
               if(Bid - OrderOpenPrice() > myPoint * Third_Close_TP)
               {
                     OrderClose(OrderTicket(), QtyOrders , Bid, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
      
         }
       
         if(OrderType() == OP_SELL) {
         

  //Close_Lots - Sell
  // Sell trades must exit at Ask price 
  

 
            if(QtyOrderSells == Lots)
            {                      
               if(OrderOpenPrice() - Ask > myPoint * First_Close_TP)
               {
                     OrderClose(OrderTicket(), First_Close , Ask, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
   
   
            if((QtyOrderSells - Third_Close > 0.00 ) 
             &&(QtyOrderSells - (Second_Close + Third_Close ) <= 0.00) )
            {                 
               if(OrderOpenPrice() - Ask >  (myPoint * Second_Close_TP))
               {
                     OrderClose(OrderTicket(), Second_Close , Ask, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
   
            if((QtyOrderSells - Third_Close <= 0.00 )
                && (QtyOrderSells > 0.00 ))
            {                 
               if(OrderOpenPrice() - Ask > myPoint * Third_Close_TP)
               {
                     OrderClose(OrderTicket(), QtyOrderSells , Ask, Slippage, MediumSeaGreen);
                     continue;
               }
            }           
         } 
      } 
   } 
}


// Fix of Point for Brokers using 3 and 5 digit pricing
double SetPoint(string mySymbol)
{
   double mPoint, myDigits;
   
   myDigits = MarketInfo (mySymbol, MODE_DIGITS);
   if (myDigits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}



// Modified to use a function for easier use in an EA
void MoveStopOnceMode()
{
   int Total;
   double QtyOrders = 0.00;
   double QtyOrderSells = 0.00;

   Total = OrdersTotal();
   
   //Check position
   bool IsTrade = False;

// Better to do this from highest to lowest
//   for (int i = 0; i < Total; i ++) {
   for (int i = Total - 1; i >= 0; i --) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//*******************************************
// Important to have code check MagicNumber and Symbol

      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
//*******************************************
      
      if(OrderType() > OP_SELL) continue;
      IsTrade = True;
      if(OrderType() == OP_BUY)
        {
         QtyOrders = OrderLots();
         //MoveOnce 
         if(MoveStopOnce && MoveStopWhenPrice > 0)
           {
            if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice)
              {
               if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo)
                 {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                  if(!EachTickMode) BarCount = Bars;
                  continue; 
                 }
              }
           }  
        }
      else
        {
         QtyOrderSells = OrderLots();
         //MoveOnce 
         if(MoveStopOnce && MoveStopWhenPrice > 0)
           {
            if(OrderOpenPrice() - Ask >= Point * MoveStopWhenPrice)
              {
               if(OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo)
                 {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                  if(!EachTickMode) BarCount = Bars;
                  continue; 
                 }
              }
           }
        }
     }
  }