//+------------------------------------------------------------------+
//|                                         GANN_TrailingStop_v1.mq4 |
//|                                         Copyright © 2009         |
//|                         Written by GodfreyH ,godfreyh@gmail.com  |   
//+------------------------------------------------------------------+

//---- input parameters
extern int StopLoss = 80;
extern int gannstop_lb = 10;
extern int gannstop_timeframe = 0;
extern int TrailAfter = 0; //pips before gann trailing stop activates.
extern int extraPIPS =0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//----
   return(0);
  }

// ---- Stepped Stops
void TrailStop()
{
            double HILO,pBid, pAsk, pp, BuyStop,spr, SellStop, op, os, tp, digits; 
            HILO = iCustom(NULL,gannstop_timeframe,"###Gann_HiLo_Activator_v2###",gannstop_lb,0,1);
            digits = MarketInfo(Symbol(), MODE_DIGITS);
            pBid = MarketInfo(Symbol(), MODE_BID);
		      pAsk = MarketInfo(Symbol(), MODE_ASK);
		      pp = MarketInfo(Symbol(),MODE_POINT);
		      op = OrderOpenPrice();
		      os = OrderStopLoss();
		      tp = OrderTakeProfit();
   for (int cnt=0;cnt<OrdersTotal();cnt++)
   { 
   OrderSelect(cnt, SELECT_BY_POS);   
   int mode=OrderType();    
      if ( OrderSymbol()==Symbol()) 
      {
         if (mode==OP_BUY) 
         {
			BuyStop = HILO -(extraPIPS*pp);
			if (digits > 0)
			BuyStop = NormalizeDouble(BuyStop,digits);
			Print("GANN=",HILO," BuyStop=",BuyStop);
	 	if  ((pBid - op) > (pp * TrailAfter))
			   {
			   bool result = OrderModify(OrderTicket(),OrderOpenPrice(), BuyStop,OrderTakeProfit(),0,LightGreen);
			      if( !result )
               {
               Print("BUY: OrderModify failed with error #",GetLastError());
               }
			      return(0);
                          
            }
         }   
// - SELL Orders          
         if (mode==OP_SELL)
         {
            HILO = iCustom(NULL,0,"###Gann_HiLo_Activator_v2###",gannstop_lb,0,1);
		   	SellStop =HILO +(extraPIPS*pp);
		   	op = OrderOpenPrice();
		      os = OrderStopLoss();
		      tp = OrderTakeProfit();
		      pAsk = MarketInfo(Symbol(),MODE_ASK);
		      double InitialStop = StopLoss *pp;
		      if (os==0 && InitialStop > 0) SellStop = pAsk+InitialStop*pp;
            if (digits > 0)
		      SellStop = NormalizeDouble( SellStop,digits);
		      Print("GANN=",HILO," SellStop=",SellStop);   
            if( (op <= SellStop && os > SellStop)   ||os==0 )
            {
            OrderModify(OrderTicket(), OrderOpenPrice(),SellStop,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 (gannstop_lb >0) TrailStop(); 
   
 return(0);
}//int start
//+------------------------------------------------------------------+





