
extern double risk_percent=0.05;
extern int slowma=96;
extern int fastma=34;
extern int currency_momentum=58;
extern int trend_momentum=38;
extern int maxpipfromEMA=35;

string trend,buy,sell;
double  maxaveragepips,trendbegin,maxtrend;

int start()
  {
  double lot,fastt,fasttW,sloww,slowwW,currency_momentumA,currency_momentumB,currency_momentumC,currency_momentumD,trend_momentumFastA,
         trend_momentumFastB,trend_momentumFastC,trend_momentumFastD,average,width,averagepips,opentime,stopip,trend_momentumSlowA,
         trend_momentumSlowB,stopaverage,widthW,trend_momentumSlowAR,AcctLeverage,CurrencyLotSize;
  int total,ticket,ope,i,cnt,open,block,big,can,buyjump,selljump,ranging,spike,trendover,trade,spikeW;
  
//*********************1-Hour definitions**********************************************************************************************
  average=iMA(NULL,PERIOD_H1,3,0,MODE_EMA,PRICE_CLOSE,1);
  fastt=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,1);
  sloww=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,1);
  fasttW=iMA(NULL,PERIOD_H1,fastma*2.38,0,MODE_EMA,PRICE_CLOSE,1);
  slowwW=iMA(NULL,PERIOD_H1,slowma*1.38,0,MODE_EMA,PRICE_CLOSE,1);
  currency_momentumA=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,currency_momentum,1,currency_momentum,0,1);
  currency_momentumB=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,currency_momentum,1,currency_momentum,1,1);
  currency_momentumC=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,currency_momentum,1,currency_momentum,0,3);
  trend_momentumFastA=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,trend_momentum,1,trend_momentum,0,1);
  trend_momentumFastB=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,trend_momentum,1,trend_momentum,0,2);
  trend_momentumFastC=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,trend_momentum,1,trend_momentum,0,3);
  trend_momentumFastD=iCustom(NULL,PERIOD_H1,"OSMAMA",4,6,trend_momentum,1,trend_momentum,1,1);
  trend_momentumSlowA=iCustom(NULL,PERIOD_H1,"OSMAMA",6,8,slowma*3,currency_momentum*3,currency_momentum*3,0,1);
  trend_momentumSlowB=iCustom(NULL,PERIOD_H1,"OSMAMA",6,8,slowma*3,currency_momentum*3,currency_momentum*3,0,5);
 //***********************************************************************************************************************
  
//***********Misc. Definitions*************************************************************************************
  AcctLeverage=AccountLeverage();
  CurrencyLotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
  total=OrdersTotal();
  width=MathAbs(fastt-sloww);
  widthW=MathAbs(fasttW-slowwW);
  lot=AccountEquity()*risk_percent/(CurrencyLotSize/AcctLeverage);
//**************************************************************************************************************

//************************Trade Size***********************************************
 
 //*****************************************************************************

//*************Checks for any Open Trades***********
  for(i=0;i<total;i++)
         {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
            {
            if(OrderSymbol()==Symbol())
               {
              open++;
               }
            }
         }
 //************************************************
 
 //***********************Spike Detector**************************************
   
      if((MathAbs(Close[1]-Open[1])>((width*2.62))) ||
         (MathAbs(Close[2]-Open[2])>((width*2.62))) ||
         (MathAbs(Close[3]-Open[3])>((width*2.62))) ||
         (MathAbs(Close[4]-Open[4])>((width*2.62))) ||
         (MathAbs(Close[5]-Open[5])>((width*2.62))))
         {
         spike++;
         }
      if((MathAbs(Close[1]-Open[1])>((widthW*2.62))) ||
         (MathAbs(Close[2]-Open[2])>((widthW*2.62))) ||
         (MathAbs(Close[3]-Open[3])>((widthW*2.62))) ||
         (MathAbs(Close[4]-Open[4])>((widthW*2.62))) ||
         (MathAbs(Close[5]-Open[5])>((widthW*2.62))))
         {
         spikeW++;
         }
        
//***************************************************************************

 //*************Checks for Trending or Ranging************************************************
  if((MathAbs(fastt-sloww)<=6*Point) || ((MathAbs(trend_momentumSlowA))<=(6*Point)))
      {
      ranging++;
      }
 
 //***********************Ends Trend after Appropiate Time*************************************
  if((currency_momentumA>0) && (average>sloww) && (trend=="sell"))
      {
      trend="endtrend";
      }
   if((currency_momentumA<0) && (average<sloww) && (trend=="buy"))
      {
      trend="endtrend";
      }
 //****************************************************************************************** 

 //*********************Jump Detector**************************************       
  if(((Open[2]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,2)) &&
     (Close[1]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,1))) ||
    ((Open[3]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,3)) &&
     (Close[2]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,2))) ||
    ((Open[4]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,4)) &&
     (Close[3]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,3))) ||
    ((Open[5]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,5)) &&
     (Close[4]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,4))) ||
    ((Open[6]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,6)) &&
     (Close[5]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,5))))
     {
     buyjump++;
     }
  if(((Open[1]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,1)) &&
     (Close[1]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,1))) ||
    ((Open[2]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,2)) &&
     (Close[2]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,2))) ||
    ((Open[3]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,3)) &&
     (Close[3]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,3))) ||
    ((Open[4]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,4)) &&
     (Close[4]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,4))) ||
    ((Open[5]<=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,5)) &&
     (Close[5]>=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,5))))
     {
     buyjump++;
     }
  if(((Open[2]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,2)) &&
     (Close[1]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,1))) ||
    ((Open[3]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,3)) &&
     (Close[2]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,2))) ||
    ((Open[4]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,4)) &&
     (Close[3]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,3))) ||
    ((Open[5]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,5)) &&
     (Close[4]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,4))) ||
    ((Open[6]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,6)) &&
     (Close[5]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,5))))
     {
     selljump++;
     }
  if(((Open[1]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,1)) &&
     (Close[1]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,1))) ||
    ((Open[2]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,2)) &&
     (Close[2]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,2))) ||
    ((Open[3]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,3)) &&
     (Close[3]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,3))) ||
    ((Open[4]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,4)) &&
     (Close[4]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,4))) ||
    ((Open[5]>=iMA(NULL,PERIOD_H1,fastma,0,MODE_EMA,PRICE_CLOSE,5)) &&
     (Close[5]<=iMA(NULL,PERIOD_H1,slowma,0,MODE_EMA,PRICE_CLOSE,5))))
     {
     selljump++;
     }

//************************************************************************
//*********************To Open Trades**************************************
     
        
     if((open<1) && (fastma<slowma) && (currency_momentum>trend_momentum))
         {
         if((selljump>0) && (fastt>sloww) && (spike<1) && (ranging<1) && (currency_momentumA<0) && ((currency_momentumC>0) ||
            ((trend_momentumSlowA<trend_momentumSlowB) && (trend_momentumFastA<0))) && ((MathAbs(sloww-average)<width*1.62) || 
            ((MathAbs(sloww-average))<maxpipfromEMA*Point)) && ((MathAbs(Close[1]-Open[1])<(width*2.62))) 
            && (currency_momentumA<currency_momentumB) && (trend_momentumFastA<trend_momentumFastD))
            {
            ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,10,0,0,0,1,0,Green);
            maxaveragepips=0;
            trend="sell";
            trendbegin=Bid;
            maxtrend=0;
            PlaySound("alert.wav");
            return(0);
            }
         if((buyjump>0) && (fastt<sloww) && (spike<1) && (ranging<1) && (currency_momentumA>0) && ((currency_momentumC<0) || 
           ((trend_momentumSlowA>trend_momentumSlowB) && (trend_momentumFastA>0))) && ((MathAbs(average-sloww)<width*1.62) || 
           ((MathAbs(average-sloww))<maxpipfromEMA*Point)) && ((MathAbs(Close[1]-Open[1])<(width*2.62)))
           && (currency_momentumA>currency_momentumB) && (trend_momentumFastA>trend_momentumFastD))
            {
            ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,10,0,0,0,1,0,Green);
            trend="buy";
            maxaveragepips=0;
            trendbegin=Ask;
            maxtrend=0;
            PlaySound("alert.wav");
            return(0);
            }
          if((trend=="sell") && (trend_momentumFastA<0) && (trend_momentumFastC>0) && (average<fastt) && (fastt<sloww)
            && ((MathAbs(fastt-average))<maxpipfromEMA*Point) && ((trend_momentumSlowA<trend_momentumSlowB) || 
            ((currency_momentumA<currency_momentumB) && (currency_momentumA<0) && (trend_momentumFastA<trend_momentumFastD) 
            && (average<maxtrend))) && ((MathAbs(fastt-sloww))>=(6*Point)))
            {
            ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,10,0,0,0,1,0,Green);
            maxaveragepips=0;
            PlaySound("alert.wav");
            return(0);
            }
         if((trend=="buy") && (trend_momentumFastA>0) && (trend_momentumFastC<0) && (average>fastt) && (fastt>sloww)
            && ((MathAbs(average-fastt))<maxpipfromEMA*Point) && ((trend_momentumSlowA>trend_momentumSlowB) || 
            ((currency_momentumA>currency_momentumB) && (currency_momentumA>0) && (trend_momentumFastA>trend_momentumFastD) 
            && (average>maxtrend))) && ((MathAbs(fastt-sloww))>=(6*Point)))
            {
            ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,10,0,0,0,1,0,Green);
            maxaveragepips=0;
            PlaySound("alert.wav");
            return(0);
            }
         if((width<=2*Point) && (((MathAbs(fasttW-slowwW))>=(6*Point)) && ((MathAbs(trend_momentumSlowAR))<=(6*Point)))
           && (currency_momentumA<0) && (trend_momentumFastA<0) && (trend_momentumSlowA<trend_momentumSlowB) 
           && ((MathAbs(average-sloww))<maxpipfromEMA*Point) && (spikeW<1)) 
            {
            ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,10,0,0,0,1,0,Green);
            trend="sell";
            maxaveragepips=0;
            trendbegin=Bid;
            maxtrend=0;
            PlaySound("alert.wav");
            return(0);
            }
        if((width<=2*Point) && (((MathAbs(fasttW-slowwW))>=(6*Point)) && ((MathAbs(trend_momentumSlowAR))<=(6*Point)))
          && (currency_momentumA>0) && (trend_momentumFastA>0) && (trend_momentumSlowA>trend_momentumSlowB) 
          && ((MathAbs(average-sloww))<maxpipfromEMA*Point) && (spikeW<1))
            {
            ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,10,0,0,0,1,0,Green);
            trend="buy";
            maxaveragepips=0;
            trendbegin=Ask;
            maxtrend=0;
            PlaySound("alert.wav");
            return(0);
            }
          }
        
       
//***************************************************************************

//******************To Close Trades******************************************               
   for(cnt=0;cnt<total;cnt++)
         {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
            {
            if(OrderType()==OP_BUY) 
               {
                if((currency_momentumA<0) && (currency_momentumA<currency_momentumB) && (Close[1]<fastt) && 
                  (trend_momentumFastA<trend_momentumFastD) && (spike<1)) 
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,10,Blue);
                     PlaySound("alert.wav");
                     return(0);
                     }
                 if((can>0) && (spike<1)) 
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,10,Blue);
                     PlaySound("alert.wav");
                     return(0);
                     }
               }
              if(OrderType()==OP_SELL) 
                 {
                 if((currency_momentumA>0) && (currency_momentumA>currency_momentumB) && (Close[1]>fastt) && 
                    (trend_momentumFastA>trend_momentumFastD) && (spike<1))
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,10,Blue);
                     PlaySound("alert.wav");
                     return(0);
                     }
                 if((can>0) && (spike<1))
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,10,Blue);
                     PlaySound("alert.wav");
                     return(0);
                     }
                  }
               }
            }
//*****************************************************************************
}
      