//+-----------------------------------------------------+
//| EXPERT ADVISOR                     UpdateStops.mq4  |
//| copy to [MT4\experts] folder and recompile          |
//| status messages printed in Experts tab of terminal  |
//+-----------------------------------------------------+
//| This is free experimental software.                 |
//| No guarantees are expressed or implied.             |
//| Feedback welcome via Forex Factory private message. |
//+-----------------------------------------------------+
#property copyright "Copyright © 2009 Robert Dee"
#property link      "www.forexfactory.com/member.php?u=12983"
#property show_inputs

#define EA_MAGIC      20090120
#define EA_NAME       "UpdateStops"

extern int HardStop          = 20; // if an open order is missing a stop loss then a stop loss will be added using HardStop value
extern int MoveStopTarget    = 20; // when an open order reaches MoveStopTarget the stop will be immediately moved to BE+MoveStopProfit
extern int MoveStopProfit    = 0;  // when the stop is moved, MoveStopProfit is the pips of profit that will be locked in

//+------------------------------------------------------------------+
//| Find all orders and set stops or move stops to BE + MoveStopProfit
//+------------------------------------------------------------------+
bool MoveStops()
{
int  i;
bool order_modified = False;

// use HardStop value to set SL when SL is missing
// use MoveStopTarget value for one time move of SL to BE+MoveStopProfit
i=0;
while(OrderSelect(i,SELECT_BY_POS))
   {
   switch(OrderType())
      {
      case OP_SELL:
         if(OrderStopLoss() == 0) // if stop loss not set
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+HardStop*Point),OrderTakeProfit(),0,CLR_NONE);
            order_modified = True;
            }
         if(OrderStopLoss() > OrderOpenPrice() && OrderOpenPrice()-Low[0] >= MoveStopTarget*Point) // favourable price move
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()-MoveStopProfit*Point),OrderTakeProfit(),0,CLR_NONE);
            order_modified = True;
            }
         break;
      case OP_BUY:
         if(OrderStopLoss() == 0) // if stop loss not set
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()-HardStop*Point),OrderTakeProfit(),0,CLR_NONE);
            order_modified = True;
            }
         if(OrderStopLoss() < OrderOpenPrice() && High[0]-OrderOpenPrice() >= MoveStopTarget*Point) // favourable price move
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+MoveStopProfit*Point),OrderTakeProfit(),0,CLR_NONE);
            order_modified = True;
            }
         break;
      }
   i++;
   }

return(order_modified);
}

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
{
if(IsExpertEnabled() == False){Print("IsExpertEnabled() says that expert advisors are NOT enabled");return;}
if(IsTradeAllowed() == False){Print("IsTradeAllowed() says that trading is NOT allowed at this time");return;}
MoveStops();
}

void init()
{
Print("Copyright © 2009 Robert Dee All Rights Reserved, Version "+EA_MAGIC);
Print("Any missing SL values will be set to "+HardStop+" pips loss");
Print("When profit for an order reaches "+MoveStopTarget+" pips, the SL for that order will be moved to BE+"+MoveStopProfit); 
Print(OrdersTotal()+" orders currently being monitored");
}

