//+------------------------------------------------------------------+
//|                                           EMATrailingStop_v1.mq4 |
//|                                   Copyright  2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                    
//+------------------------------------------------------------------+
#property copyright "Copyright 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

//---- input parameters
extern int        EMATimeFrame    =  0;
extern int        Price           =  0;
extern int        EMAPeriod       = 89;
extern int        EMAShift        = 20;
extern int        InitialStop     = 20;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//----
   return(0);
  }

// ---- Stepped Stops
void TrailStop()
{
   double minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
   double ask = MarketInfo(Symbol(),MODE_ASK);
   double bid = MarketInfo(Symbol(),MODE_BID);
   double ema = iMA(Symbol(),EMATimeFrame,EMAPeriod,0,MODE_EMA,Price,EMAShift);

   for (int cnt=0;cnt<OrdersTotal();cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS);
   int mode=OrderType();   
      if ( OrderSymbol()==Symbol())
      {
         if (mode==OP_BUY)
         {
			double BuyStop = ema;
			if(OrderStopLoss() == 0 && InitialStop>0 ) BuyStop = Bid-InitialStop*Point; 
			if((bid - BuyStop) < minstop*Point) BuyStop = bid - minstop*Point;

			Print("MA=",ema," BuyStop=",BuyStop);
			   if ((OrderOpenPrice() <= BuyStop && BuyStop > OrderStopLoss()) || OrderStopLoss()==0)
			   {
			   bool result = OrderModify(OrderTicket(),OrderOpenPrice(),
			                             NormalizeDouble(BuyStop, Digits),
			                             OrderTakeProfit(),0,LightGreen);
			      if( !result )
               {
               Print("BUY: OrderModify failed with error");
               GetLastError();
               }
			      return(0);
                         
            }
         }  
// - SELL Orders         
         if (mode==OP_SELL)
         {
         double SellStop = ema;  
         if (OrderStopLoss()==0 && InitialStop > 0) SellStop = ask+InitialStop*Point;
         if((SellStop-ask) < minstop*Point)  SellStop = ask + minstop*Point; 
         Print("MA=",ema," SellStop=",SellStop);  
            if( (OrderOpenPrice() >= SellStop && OrderStopLoss() > SellStop) || OrderStopLoss()==0)
            {
            OrderModify(OrderTicket(), OrderOpenPrice(),
                        NormalizeDouble(SellStop, Digits),
			               OrderTakeProfit(),0,DarkOrange);
               if( !result )
               {
               Print("SELL: OrderModify failed with error");
               GetLastError();
               }
               return(0);
              
   			}	   
         }
      }
   }    
}


// ---- Scan Trades
int ScanTrades()
{  
   int total = OrdersTotal();
   int numords = 0;
     
   for(int cnt=0; cnt<total; cnt++)
   {       
   OrderSelect(cnt, SELECT_BY_POS);           
   if(OrderSymbol() == Symbol() && OrderType()<=OP_SELL)
   numords++;
   }
   return(numords);
}

         	                   
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
  
   if (ScanTrades()<1) return(0);
   else
   if (EMAPeriod > 0) TrailStop();
  
 return(0);
}//int start
//+------------------------------------------------------------------+





