//+------------------------------------------------------------------+
//|                                                        Trend.mq4 |
//|                                                   Trade Learning |
//|                                    http://www.trade-learning.com |
//+------------------------------------------------------------------+
#property copyright "Trade Learning"
#property link      "http://www.trade-learning.com"

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#define NONE 0
#define OPENED 1
#define CLOSED 2

extern int MagicNumber = 1422031210;

extern int StopLoss = 100;
extern int TakeProfit = 100;
extern int Breakeven = 0;
extern int Slippage = 30;

int Ticket;
double Pips;
int CountBar;
int Bar;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   int Position, signal = SIGNAL_NONE;
   RefreshRates();
   Bar = Bars;
   
   Position = HandleOpenPositions();
   
   if(Position == OPENED) {
      Tralling();
      CountedBars();
      return(0);
   }
   
   signal = GetSignal();
   Pips = 0;
   
   if(signal == SIGNAL_BUY) {
      OpenTrade(OP_BUY, Lot());
      CountedBars();
      return(0);
   }

   if(signal == SIGNAL_SELL) {
      OpenTrade(OP_SELL, Lot());
      CountedBars();
      return(0);
   }
   
   CountedBars();
   return(0);
}

//+------------------------------------------------------------------+
//| expert CountedBars function                                      |
//+------------------------------------------------------------------+
int CountedBars() {
   if(Bar != CountBar) {
      CountBar = Bar;
   }
}

//+------------------------------------------------------------------+
//| expert TestBars function                                         |
//+------------------------------------------------------------------+
/*bool TestBars() {
   if(Bar != CountBar) {
      return(True);
   }
   
   return(False);
}*/

//+------------------------------------------------------------------+
//| expert HandleOpenPositions function                              |
//+------------------------------------------------------------------+
int HandleOpenPositions() {
   int i;
   
   for(i = 0; i < OrdersTotal(); i++) {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
         break;
      }

      if(OrderType() == OP_BUY || OrderType() == OP_SELL) {
         return(OPENED);
      }
   }
   
   return(CLOSED);
}

//+------------------------------------------------------------------+
//| expert GetSignal function                                        |
//+------------------------------------------------------------------+
int GetSignal() {
   int signal = SIGNAL_NONE;
   double SIGNAL_UP, SIGNAL_DOWN;
   
   SIGNAL_UP = iCustom(Symbol(), 0, "PriceactionInd", 0, 0);
   SIGNAL_DOWN = iCustom(Symbol(), 0, "PriceactionInd", 1, 0);
   
   if(SIGNAL_UP > 0) {
      signal = SIGNAL_SELL;
   }

   if(SIGNAL_DOWN > 0) {
      signal = SIGNAL_BUY;
   }
   
   return(signal);
}


//+------------------------------------------------------------------+
//| expert Tralling function                                        |
//+------------------------------------------------------------------+
int Tralling() {
   if(OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES) == True) {
      switch(OrderType()) {
         case OP_BUY:
            if(Ask > OrderOpenPrice()) {
               if(Ask - OrderOpenPrice() > Pips) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() - (Pips - (Ask - OrderOpenPrice())), OrderTakeProfit(), OrderExpiration(), Orange);
                  Pips = Ask - OrderOpenPrice();
               }
               
               Comment("Ask : " + (Ask - OrderOpenPrice()));
            }
            break;
         case OP_SELL:
            if(Bid < OrderOpenPrice()) {
               if(OrderOpenPrice() - Bid > Pips) {
                 OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() + (Pips - (OrderOpenPrice() - Bid)), OrderTakeProfit(), OrderExpiration(), Orange);
                 Pips = OrderOpenPrice() - Bid;
               }
               Comment("Bid : " + (OrderOpenPrice() - Bid));
            }
            break;
      }
   }
}

//+------------------------------------------------------------------+
//| expert GetSignal function                                        |
//+------------------------------------------------------------------+
int OpenTrade(int Signal, double Lot) {
   switch(Signal) {
      case OP_BUY:
         Ticket = OrderSend(Symbol(), OP_BUY, Lot(), Ask, 0, StopLoss(OP_BUY), TakeProfit(OP_BUY), NULL, MagicNumber, 0, Green);
         break;
      case OP_SELL:
         Ticket = OrderSend(Symbol(), OP_SELL, Lot(), Bid, 0, StopLoss(OP_SELL), TakeProfit(OP_SELL), NULL, MagicNumber, 0, Red);
         break;
   }
}

//+------------------------------------------------------------------+
//| expert Lot function                                              |
//+------------------------------------------------------------------+
double Lot() {
   return(0.1);
}

//+------------------------------------------------------------------+
//| expert StopLoss function                                         |
//+------------------------------------------------------------------+
double StopLoss(int Type) {
   double Sl;
   
   switch(Type) {
      case OP_BUY:
         if(StopLoss == 0) {
            Sl = 0;
         } else {
            Sl = Ask - StopLoss * Point;
         }
         break;
      
      case OP_SELL:
         if(StopLoss == 0) {
            Sl = 0;
         } else {
            Sl = Bid + StopLoss * Point;
         }
         break;
   }
   
   return(Sl);
}

//+------------------------------------------------------------------+
//| expert StopLoss function                                         |
//+------------------------------------------------------------------+
double TakeProfit(int Type) {
   double Tp;
   
   switch(Type) {
      case OP_BUY:
         if(TakeProfit == 0) {
            Tp = 0;
         } else {
            Tp = Ask + TakeProfit * Point;
         }
         break;
      
      case OP_SELL:
         if(TakeProfit == 0) {
            Tp = 0;
         } else {
            Tp = Bid - TakeProfit * Point;
         }
         break;
   }
   
   return(Tp);
}