EMATrailingStop_v1a





//+------------------------------------------------------------------+
//|                                           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 BuyStop, SellStop;
   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)
         {
			if(OrderStopLoss() == 0)
			{
			   if( InitialStop>0 )  BuyStop = Bid-InitialStop*Point; else BuyStop = ema;
			}
			else
			   BuyStop = ema;
			if((bid - BuyStop) < minstop*Point) BuyStop = bid - minstop*Point;

			Print("MA=",ema," BuyStop=",BuyStop);
			   if (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)
         {  
         if (OrderStopLoss()==0)
         {
            if (InitialStop > 0) SellStop = ask+InitialStop*Point; else SellStop = ema;
         }
         else
            SellStop = ema;
         if((SellStop-ask) < minstop*Point)  SellStop = ask + minstop*Point; 
         Print("MA=",ema," SellStop=",SellStop);  
            if( 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
//+------------------------------------------------------------------+









Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It can change open orders parameters, due to possible stepping strategy

Other Features: