//+------------------------------------------------------------------+
//|                                                   coendroute.mq4 |
//|                                                         |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Endroute"
#property link      ""

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2

int Order = SIGNAL_NONE;

extern string Remark1 = "== Main Settings ==";
extern int MagicNumber = 0;

extern string Remark2 = "== COG Settings ==";
extern int bars_back = 240;
extern int m = 4;
extern int i = 0;
extern double kstd = 1.0;
extern int sName = 500;
extern color Line = Green;

extern double Lots = 0.1;
int Slippage = 5;

extern bool UseStopLoss = False;
extern int StopLoss = 100;

extern bool UseTakeProfit = False;
extern int TakeProfit = 12;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
int Low1, High1;

int n;

int buy_op = 0;
int sell_op = 0;

int ticket;
int lots;

double StopLossLevel;
double TakeProfitLevel;

if (Line == Yellow) Low1 = 4;
if (Line == Yellow) High1 = 3;

if (Line == Green) Low1 = 2;
if (Line == Green) High1 = 1;

if (Line == Blue) Low1 = 0;
if (Line == Blue) High1 = 0;


double COGHigh = iCustom(NULL, 0, "Center of Gravity", bars_back, m, i, kstd, sName, High1, 0);
double COGLow = iCustom(NULL, 0, "Center of Gravity", bars_back, m, i, kstd, sName, Low1, 0);

double COGHigh1 = iCustom(NULL, 0, "Center of Gravity", bars_back, m, i, kstd, sName, High1, 1);
double COGLow1 = iCustom(NULL, 0, "Center of Gravity", bars_back, m, i, kstd, sName, Low1, 1);

double ema5 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema13 = iMA(NULL, 0, 13, 0, MODE_EMA, PRICE_CLOSE, 0);

double ema51 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 1);
double ema131 = iMA(NULL, 0, 13, 0, MODE_EMA, PRICE_CLOSE, 1);


for(n = 0; n < OrdersTotal(); n++) {
   OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
   if(OrderType() == OP_BUY && OrderComment() == ("COG " + Symbol())) {
      buy_op = 1;
      ticket = OrderTicket();
      lots = OrderLots();
   }
   if(OrderType() == OP_SELL && OrderComment() == ("COG " + Symbol())) {
      sell_op = 1;
      ticket = OrderTicket();
      lots = OrderLots();
   }
}

Comment("Order: ", Order, " BUY: ", buy_op, " SELL: ", sell_op, " EMA5>EMA13 ", (ema5>=ema13), " EMA5<EMA13 ", (ema5<=ema13), " EMA51>EMA131 ", (ema51>ema131), " EMA51<EMA131 ", (ema51<ema131));

if(Order == SIGNAL_NONE) {
   if (Close[0] < COGLow && Close[1] >= COGLow1) Order = SIGNAL_BUY;
   if (Close[0] > COGHigh && Close[1] <= COGHigh1) Order = SIGNAL_SELL;
}

if(Order != SIGNAL_NONE) {
   if(Order == SIGNAL_BUY) {
      if(ema5 > ema13 && ema51 <= ema131) {
         if(buy_op == 0) {
            if (UseStopLoss == true) { StopLossLevel = Ask - StopLoss * Point; } else { StopLossLevel = 0.0; }
            if (UseTakeProfit == true) { TakeProfitLevel = Ask + TakeProfit * Point; } else { TakeProfitLevel = 0.0; }
            
            ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "COG " + Symbol(), MagicNumber, 0, DodgerBlue);
            if(ticket != -1) {
               buy_op = 1;
               return(0);
            }
         }
      }
      if(ema5 < ema13 && ema51 >= ema131) {
         if(buy_op == 1) {
            OrderClose(ticket, Lots, Bid, 0, 0);
            buy_op = 0;
            Order = SIGNAL_NONE;
            return(0);
         }
      }
   }
   
   if(Order == SIGNAL_SELL) {
      if(ema5 < ema13 && ema51 >= ema131) {
         if(sell_op == 0) {
         
            if (UseStopLoss == true) { StopLossLevel = Bid + StopLoss * Point; } else { StopLossLevel = 0.0; }
            if (UseTakeProfit == true) { TakeProfitLevel = Bid - TakeProfit * Point; } else { TakeProfit = 0.0; }
            
            ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "COG " + Symbol(), MagicNumber, 0, DeepPink);
            if(ticket != -1) {
               sell_op = 1;
               return(0);
            }
         }
      }
      if(ema5 > ema13 && ema51 <= ema131) {
         if(sell_op == 1) {
            OrderClose(ticket, Lots, Ask, 0, 0);
            sell_op = 0;
            Order = SIGNAL_NONE;
            return(0);
         }
      }
   }
}
         
            
      

return(0);
}


//----

//+------------------------------------------------------------------+