//+------------------------------------------------------------------+
//|                                             Profit_maker_two.mq4 |
//|                    Coded By GGG Mācon France                 GGG |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "GGG"
#property link      ""
#property version   "1.00"
// trade_off_last_candle.ea by rockit; copyleft
#property strict

	enum OC_Or_HA
	 {
	 CO =0,
	 HA =1
	 };
 input  OC_Or_HA  choose_OC_Or_HA =0;
	
	enum closing_trade
   	{
	     none = 0,
	     opposite_candle =1,
	     close_on_high_low_SL =2
	     };
 input closing_trade   close_mode = 2;    


input bool Open_Buy = false;
input bool Open_Sell = true;
input ENUM_TIMEFRAMES TF = 1440; // time frame
input double Lots = 0.1; // alotment
input double TP = 30500; // tp 
input int back = 27;
input double Add  = 500;
input int MagicNumber = 125;
input double close_if_profit_over = 100.0;
input bool and_opposite_candle = true;

datetime dt;
double profit_buy = 0, profit_sell = 0;
//=============================================
int OnInit()
{
	dt = iTime(_Symbol, TF, 0);
	return INIT_SUCCEEDED;
}
//==============================================================================
void OnTick()
  {
  //----------------------------------------------------------------------------
	if(iTime(_Symbol, TF, 0) == dt) return;
 //-----------------------------------------------------------------------------	
   double osl_buy_2 =  Low[iLowest(NULL,TF,MODE_LOW,back,0)] - Add*Point; 
  double osl_sell_2 = High[iHighest(_Symbol, TF,MODE_HIGH,back,0)] + Add*Point;
  bool om = false;  double direction = 0;
  //--- close order if profit is enough -----------------------------------------
                           calcul_profit();

 
 
 
	switch( choose_OC_Or_HA )
	  {
	  case CO :  direction = iClose(_Symbol, TF, 1) - iOpen(_Symbol, TF, 1); break;
	  case HA :  direction = iCustom(NULL,TF,"Heiken_Ashi_NK",0,1)-
	                        iCustom(NULL,TF,"Heiken_Ashi_NK",1,1); break;
	  default :break;
	  }                     
	
 if( Open_Buy &&  direction > 0 )
	  {
		int ticket = OrderSend(_Symbol, OP_BUY, Lots, Ask, 3, 0,0, "", MagicNumber );
		if( ticket > 0 )
	   	{
    		if( OrderSelect(ticket, SELECT_BY_TICKET) == true )
		     {
   	     om = false;
	        om = 	OrderModify(ticket, OrderOpenPrice(), osl_buy_2,Ask+ TP*Point, 0);
	        if( om == false ) Print( " ERROR MODIFYING ORDER");
	        }
	     else Print(" ORDER SELECT NOT FOUND ");
	     }
	   else Print(" ORDERSEND ERROR ");
	 }
	   
	if( Open_Sell && direction < 0 )
	   {
		int ticket = OrderSend(_Symbol, OP_SELL, Lots, Bid, 3, 0,0, "", MagicNumber );
		if( ticket > 0 )
		  { 
	   	if( OrderSelect(ticket, SELECT_BY_TICKET) )
		     {	
	         om = false;
		      om = OrderModify(ticket, OrderOpenPrice(), osl_sell_2, Bid-TP*Point, 0);
	        if( om == false ) Print( " ERROR MODIFYING ORDER");
	        }
	     else Print(" ORDER SELECT NOT FOUND ");
	     }
	   else Print(" ORDERSEND ERROR ");
	 }
	//----------------------------------------------------------------------------	     
	switch ( close_mode )
	   {
	    case none : 
	    break;
	    case opposite_candle : 
	         if( direction > 0 ) Close_Order(OP_SELL);  
	         if( direction < 0 ) Close_Order(OP_BUY);
	    break;
	   case  close_on_high_low_SL :
	
             for( int g = 0; g <= OrdersTotal(); g++)
                {
                if( OrderSelect(g,SELECT_BY_POS,MODE_TRADES))
                  {
                 if( OrderType() == 0)
                   {
                    if( osl_buy_2  > OrderStopLoss() + 5*Point )  
                      {       
                      om = false;
                      om = OrderModify(OrderTicket(),OrderOpenPrice(), osl_buy_2, OrderTakeProfit(),0);
                      if( om == false ) Print("ERROR MODIFYING BUY ORDER ");
                      }
                    }
                  if( OrderType() == 1)
                     { 
                     if( osl_sell_2 <  OrderStopLoss() - 5*Point )  
                       {
                        om = false;
                        om =  OrderModify(OrderTicket(),OrderOpenPrice(),osl_sell_2,OrderTakeProfit(),0);
                        if( om == false ) Print("ERROR MODIFYING SELL ORDER ");
                      }
                    }
           	}}
	  default : break;
	   }         
	   
	   
  //-----------------------------------------------------------------------------
   if( close_if_profit_over > 0 && ! and_opposite_candle)
     {
     if( profit_buy > close_if_profit_over ) Close_Order(OP_BUY);
     if( profit_sell > close_if_profit_over ) Close_Order(OP_SELL);
    }
  //------------------------------------------------------------------------------
  //-----------------------------------------------------------------------------
   if( close_if_profit_over > 0 && and_opposite_candle )
     {
     if( profit_buy  > close_if_profit_over && direction < 0 ) Close_Order(OP_BUY);
     if( profit_sell > close_if_profit_over && direction > 0 ) Close_Order(OP_SELL);
    }
  //------------------------------------------------------------------------------
	      
  dt = iTime(_Symbol, TF, 0);
  }
//----------------------- CLOSE ORDER FUNCTION
//===================================================================================
void Close_Order(int kind  )
{
   int      cnt,       total = 0;
  bool om = false;
   total = OrdersTotal();
   for(cnt=total-1;cnt>=0;cnt--)
   {
    if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) )
      {
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if( OrderType() == kind )
           {
            om = false;
            om =  OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), 3,Violet);
            if( om == false ) Print(" ERROR CLOSING ORDER");   
            }      
            if( OrderType() > 1 ) om = OrderDelete(OrderTicket());
           }
        }
      }      
}


//===================================================================================
void  calcul_profit()
  {
   profit_buy = 0; profit_sell = 0;  
   int  total = OrdersTotal();
   for(int cnt=0; cnt<total; cnt++)
     {
     if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) )
       {
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
         if( OrderType() == OP_BUY ) profit_buy += OrderProfit();
         if( OrderType() == OP_SELL ) profit_sell += OrderProfit();
         }
        }
      }      
}


