#property copyright "DrewNya"
#property link      "joinme@yandex.ru"

extern double    iLot = 1;
extern int       periodAMA = 9;
extern int       nfast = 2;
extern int       nslow = 30;
extern double    G = 2.0;
extern double    dK = 2.0; 
extern string    iType = "YO";
extern string    curSymbol = "GBPUSD";
extern string    T1, T2, T3;
extern double    p1, p2, p3;

extern int       EMA1=5;
extern int       Shift1=0;
extern int       EMA2=8;
extern int       Shift2=0;
extern int       EMA3=13;
extern int       Shift3=0;

extern int Ticks = 0;

extern int TFrame = PERIOD_M15;

int              cbars = 0, prevbars = 0, prevtime = 0;
double           slowSC, fastSC;

int init()
  {
      return(0);
  }

int deinit()
  {
      return(0);
  }

int start()
  {
      int i, pos = 0;
      double noise = 0.000000001, AMA, AMA0, signal, ER;
      double dSC, ERSC, SSC, ddK;
      bool isBUY,isSELL;
      int cnt,total;

      bool isOpen = false;
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         //if(curSymbol == OrderSymbol()) isOpen = true;
         //Print(OrderSymbol());
      } 
      
      if(OrdersTotal() == 0)
      {
         //Ticks = Ticks + 1;      
      }
      
      slowSC = (2.0 / (nslow + 1));
      fastSC = (2.0 / (nfast + 1));
      cbars = 50;

      
      if(cbars > 0) cbars--;
      pos = 40 - periodAMA - 2;
      AMA0 = iClose(NULL,TFrame,pos + 1);
      while(pos >= 0)
      {
         if(pos == Bars - periodAMA - 2) AMA0 = Close[pos + 1];
         signal = MathAbs(iClose(NULL,TFrame,pos) - iClose(NULL,TFrame,pos + periodAMA));
         noise = 0.000000001;
         for(i = 0; i < periodAMA; i++)
         {
            noise = noise + MathAbs(iClose(NULL,TFrame,pos + i) - iClose(NULL,TFrame,pos +  i + 1));
         }
         ER = signal / noise;
         dSC = (fastSC - slowSC);
         ERSC = ER * dSC;
         SSC = ERSC + slowSC;
         AMA = AMA0 + (MathPow(SSC, G) * (iClose(NULL,TFrame,pos) - AMA0));

         if(!isOpen)
         {
            isBUY = false;
            isSELL = false;
         }

         ddK = (AMA - AMA0);
         if((MathAbs(ddK)) > (dK * Point) && (ddK > 0))
         {
            isBUY = true;
            isSELL = false; 
         }
         if((MathAbs(ddK)) > (dK * Point) && (ddK < 0))
         {
            isBUY = false;
            isSELL = true; 
         }

         AMA0 = AMA;
         pos--;
      }
      
      if(!isOpen) iType = "YO";

      if(OrdersTotal() == 0)
      {
      if(isBUY)
      {
         if((!isOpen) && (iType != "BUY"))// && (Ticks >= 14)
         {
            if(flat()) OrderSend(Symbol(),OP_BUY,iLot,Ask,3,0,Ask+0.0010,"",2,0,Blue);      
            if(!flat()) OrderSend(Symbol(),OP_SELL,iLot,Bid,3,0,Bid-0.0010,"",1,0,Red);      
            if(isOpen) iType = "BUY";
         }
      }
      if(isSELL)
      {
         if((!isOpen) && (iType != "SELL"))// && (Ticks >= 14)
         {
            if(!flat()) OrderSend(Symbol(),OP_BUY,iLot,Ask,3,0,Ask+0.0010,"",2,0,Blue);      
            if(flat()) OrderSend(Symbol(),OP_SELL,iLot,Bid,3,0,Bid-0.0010,"",1,0,Red);      
            if(isOpen) iType = "SELL";
         }
      }
      }
   
      double prof;
      if(OrdersTotal() >= 1)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
            
            //if(curSymbol == OrderSymbol())
            //{
               prof = OrderProfit() / 10;
               if(OrderType()==OP_BUY)
               {
                  if(prof >= 4)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
                     Ticks = 0;
                  }
               }
               if(OrderType()==OP_SELL)
               {
                  if(prof >= 4)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                     Ticks = 0;
                  }
               }
            //}
         }
      }
      
      prevbars = Bars;

      return(0);
  }
bool flat()
{
   double e1=iMA(Symbol(),TFrame,EMA1,Shift1,MODE_EMA,PRICE_MEDIAN,1);
   double e2=iMA(Symbol(),TFrame,EMA2,Shift3,MODE_EMA,PRICE_MEDIAN,1);
   double e3=iMA(Symbol(),TFrame,EMA3,Shift3,MODE_EMA,PRICE_MEDIAN,1);
   if(e1>e2 && e2>e3) return(false);
   if(e1<e2 && e2<e3) return(false);
   return(true);
}

