//+-------------------------------------------------------------------------------------------+
//|                                                                               Stealth.mq4 |
//|                                                           Copyright © 2008, Scott Merritt |
//|                                                                 http://www.metaquotes.net |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2008, Scott Merritt"
#property link      "http://www.metaquotes.net"
#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh> 
//+-------------------------------------------------------------------------------------------+

extern  bool    use_hidden_stop_loss = true;
extern int      hidden_sl    = 200;
extern  bool    use_hidden_take_profit = true;
extern  int     hidden_tp    = 100;
extern double     BreakEven       = 50;    // Profit level to lock in pips  
extern double     BreakEvenShift  = 25;    // Close out level if price retraces

int      digit=0;
double   point;
//+-------------------------------------------------------------------------------------------+

void hidden_take_profit()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    if ( OrderSymbol()==Symbol()  )
     {
  if (OrderType() == OP_BUY &&  OrderOpenPrice()+hidden_tp*point<=Bid )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
  if (OrderType() == OP_SELL &&  OrderOpenPrice()-hidden_tp*point>=Ask  )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
  
      }
  }
  return;
}
//+-------------------------------------------------------------------------------------------+
void hidden_stop_loss()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    if ( OrderSymbol()==Symbol()  )
     {
  if (OrderType() == OP_BUY &&  OrderOpenPrice()-hidden_sl*point>=Bid )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
  if (OrderType() == OP_SELL &&  OrderOpenPrice()+hidden_sl*point<=Ask  )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
  
      }
  }
  return;
}
//+-------------------------------------------------------------------------------------------+
//| expert initialization function                                                            |
//+-------------------------------------------------------------------------------------------+

int init()
  {
//----

   if (Digits == 3 || Digits == 5) {
      if (Digits == 3)
         point = 0.01;
      else
         point = 0.0001;
   }
   else point = Point;

//----
   return(0);
  }
  
//+-------------------------------------------------------------------------------------------+
//| expert deinitialization function                                                          |
//+-------------------------------------------------------------------------------------------+

int deinit()
  {
//---- 

//----
   return(0);
  }

//+-------------------------------------------------------------------------------------------+
//| expert start function                                                                     |
//+-------------------------------------------------------------------------------------------+

int start()
{
//----
 digit  = MarketInfo(Symbol(),MODE_DIGITS);

if (use_hidden_stop_loss)           hidden_stop_loss();
if (use_hidden_take_profit)       hidden_take_profit();
   
   if (ScanTrades()<1) return(0);
   else
   if (BreakEven>0) TrailStops(); 
//----
   return(0);
}


//+-------------------------------------------------------------------------------------------+



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);
}

void TrailStops()
{        
    int total=OrdersTotal();
    double BuyStop,SellStop;
    for (int cnt=0;cnt<total;cnt++)
    { 
     OrderSelect(cnt, SELECT_BY_POS);   
     int mode=OrderType();    
        if ( OrderSymbol()==Symbol() ) 
        {
            if ( mode==OP_BUY )
            {
               if ( Bid-OrderOpenPrice()>point*BreakEven ) 
               {
               if(OrderStopLoss()< OrderOpenPrice())
                 {
                  BuyStop = OrderOpenPrice();
                  OrderModify(OrderTicket(),OrderOpenPrice(),
                              NormalizeDouble(BuyStop+(BreakEvenShift*point), digit),
                              OrderTakeProfit(),0,LightGreen);
                 }
               else if(Bid-OrderStopLoss() > point*BreakEven)
                 {
                  BuyStop = OrderStopLoss()+(BreakEvenShift*point);
                  if(BuyStop > OrderStopLoss()+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),
                              NormalizeDouble(BuyStop, digit),
                              OrderTakeProfit(),0,LightGreen);                 
                 } 
			      return(0);
			      }
			   }
            if ( mode==OP_SELL )
            {
               if ( OrderOpenPrice()-Ask>point*BreakEven) 
               {
               if(OrderStopLoss()> OrderOpenPrice() || OrderStopLoss() == 0)
                 {
                  SellStop = OrderOpenPrice();
                  OrderModify(OrderTicket(),OrderOpenPrice(),
   		                     NormalizeDouble(SellStop-(BreakEvenShift*point), digit),
   		                     OrderTakeProfit(),0,Yellow);	                     
                 }
               else if(OrderStopLoss()-Ask > point*BreakEven)
                 {
                  SellStop = OrderStopLoss()-(BreakEvenShift*point);
                  if(SellStop < OrderStopLoss()-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),
   	   	                  NormalizeDouble(SellStop, digit),
   		                     OrderTakeProfit(),0,Yellow);	    
   		        }
               return(0);
               }    
            }
         }   
      } 
}

