//+------------------------------------------------------------------+
//|                                        Fight Back by Lia 786.mq4 |
//|                                                          lia 786 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//+------------------------------------------------------------------+
#define LONG      0  
#define SHORT     1
#define NOTRADE   9

extern double RiskPercent = 1;
extern int StopLoss = 500;
extern int TakeProfit = 750;
extern string txComment = "Order EA1";
extern int MagicNumber = 12345;
extern double Lots = .01;
extern int Slippage = 3;
extern int TrailingStop = 500;
extern int TrailingStep = 0;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
   {

   return(0);
   }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
   {

   return(0);
   }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   {
   
   if(TrailingStop > 0) MoveTrailingStop();
   
   if ( BuySignal() && ( LastClosedTradeType() == SHORT || LastClosedTradeType() == NOTRADE) )
      {
      Print("Switch to buy");
            
      //1=sell
      closePos(OP_SELL);
      if (bolehTrade())
         {
         if(OrderSend(Symbol(), OP_BUY, itungLot(), Ask, Slippage, Ask-StopLoss*Point, Ask+TakeProfit*Point, txComment, MagicNumber, Blue) == -1)
            {
            Print( "OrderSend, type = OP_BUY, lots = ", itungLot(), " entry(Ask) = ", DoubleToStr(Ask, Digits), " Bid: ", DoubleToStr(Bid, Digits), 
            " SL = ", DoubleToStr(Ask-StopLoss*Point, Digits), " TP = ", DoubleToStr(Ask+TakeProfit*Point, Digits), " ", txComment, "  ... failed: error ", GetLastError() );
            }
         }
      }
   else if ( SellSignal() )
      {
      Print("Switch to sell");
            
      //0=buy
            
      closePos(OP_BUY);
      if (bolehTrade() && ( LastClosedTradeType() == LONG || LastClosedTradeType() == NOTRADE) )
         {
         if(OrderSend(Symbol(), OP_SELL, itungLot(), Bid, Slippage, Bid+StopLoss*Point, Bid-TakeProfit*Point, txComment, MagicNumber, Red) == -1)
            {
            Print( "OrderSend, type = OP_SELL, lots = ", itungLot(), " entry(Bid) = ", DoubleToStr(Bid, Digits), " Ask: ", DoubleToStr(Ask, Digits), 
            " SL = ", DoubleToStr(Bid+StopLoss*Point, Digits), " TP = ", DoubleToStr(Bid-TakeProfit*Point, Digits), " ", txComment, "  ... failed: error ", GetLastError() );
            }
         }
      }

   return(0);
   }
//+------------------------------------------------------------------+

void MoveTrailingStop()
   {
   int cnt;
   
   if(TrailingStop == 0) return;  

   for(cnt = 0; cnt < OrdersTotal(); cnt++)
      {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) && 
         OrderType() == OP_BUY && OrderSymbol() == Symbol() )
         {
         if( NormalizeDouble(OrderStopLoss(),Digits) < NormalizeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits) || OrderStopLoss() < 0.01 )
            {
            if(! OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid-Point*TrailingStop,Digits), OrderTakeProfit(), 0, Green) )
               {
               Print("MoveTrailingStop: OrderModify failed - Buy # ", OrderTicket(), " error # ", GetLastError() );
               Print("Current SL: ", DoubleToStr(OrderStopLoss(), Digits), " new SL: ", DoubleToStr(NormalizeDouble(Ask+Point*TrailingStop, Digits), Digits) );
               }
            }
         }

      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) && 
         OrderType() == OP_SELL && OrderSymbol() == Symbol() )
         {
         if( NormalizeDouble(OrderStopLoss(),Digits) > NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits) || OrderStopLoss() < 0.01 )
            {
            if(! OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask+Point*TrailingStop, Digits), OrderTakeProfit(), 0, Red) )
               {
               Print("MoveTrailingStop: OrderModify failed - Sell # ", OrderTicket(), " error # ", GetLastError() );
               Print("Current SL: ", DoubleToStr(OrderStopLoss(), Digits), " new SL: ", DoubleToStr(NormalizeDouble(Ask+Point*TrailingStop, Digits), Digits) );
               }
            }
         }
      }
   }

bool BuySignal()
 {
    if((iMomentum(NULL,0,14,PRICE_CLOSE,1)>100)&&(iMA(NULL,0,13,1,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,0,34,1,MODE_EMA,PRICE_CLOSE,1))&&(iRSI(NULL,0,10,PRICE_CLOSE,1)>80)) // Here is your open buy rule
     {
   
      return(true);
      }
      
   else return(false);
   }

bool SellSignal()
   { 
   if((iRSI(NULL,0,10,PRICE_CLOSE,1)<20)&&(iMA(NULL,0,13,1,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,0,34,1,MODE_EMA,PRICE_CLOSE,1))&&(iMomentum(NULL,0,14,PRICE_CLOSE,1)<100)) // Here is your open Sell rule
     
      {
      return(true);
      }
      
  
   }

bool bolehTrade()
   {
   if (DayOfWeek() == 5 || OrdersTotal() > 0) 
      { 
      return(false); 
      } 
      
   else return(true); 
   }

double itungLot()
   {
   double xLots=Lots;
   return (xLots);
   }

void closePos(int TradeType)
   {
   for (int a = OrdersTotal()-1; a >= 0; a--)
      {
      if (OrderSelect(a, SELECT_BY_POS, MODE_TRADES) && 
         OrderType() == TradeType &&
         OrderMagicNumber() == MagicNumber )
         {
         Print("Order found");
         if (OrderType()==OP_BUY) Print("Closing buy");
         if (OrderType()==OP_SELL) Print("Closing sell");
         
         if(! OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage) )
            {
            Print("closePos: OrderClose failed - ", OrderType(), " # ", OrderTicket(), " error # ", GetLastError() );
            Print("Trying to close at: ", OrderClosePrice(), " Bid: ", DoubleToStr(Bid, Digits), " Ask: ", DoubleToStr(Ask, Digits) );
            }
         }
      }
   }


//+-----------------------------------------------------------------------------------+
//| LastClosedTradeType function - returns an int based on the Type of the last trade |
//| that was closed for this Symbol and Magic Num - BUY  0, SELL 1, NON - 9           |
//+-----------------------------------------------------------------------------------+
int LastClosedTradeType()
   {
   datetime MostRecentOrderCloseTime = 0;
   int RecentOrderType = NOTRADE;      // default value to represent no orders in History
   
   if(OrdersHistoryTotal() == 0) return(NOTRADE);
   
   for (int OrderPosition = 0; OrderPosition < OrdersHistoryTotal(); OrderPosition++)
      {
      if (OrderSelect(OrderPosition, SELECT_BY_POS, MODE_HISTORY) &&    // select from Order History
         OrderMagicNumber() == MagicNumber &&                           // select by magic Number 
         OrderSymbol() == Symbol() )                                    // and Symbol
         {
         if(OrderCloseTime() > MostRecentOrderCloseTime) 
            {
            MostRecentOrderCloseTime = OrderCloseTime();
            if(OrderType() == OP_BUY || OrderType() == OP_SELL )
               RecentOrderType = OrderType();                           // finds the most recently close valid order
            }
         }
      }
      
   return(RecentOrderType);
//   return(NOTRADE);

   }

