//+------------------------------------------------------------------+ //| SafeGuard.mq4 | //| Copyright 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ extern double EAStopLoss=500.0; extern double InitialStop=100.0; extern double Margin=50.0; extern bool UseAlert = true; extern bool UseEmail = true; int MagicNumber = 101090; int init() { //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int pos3pre = 1; int pos4cur = 0; int cnt = 0; int openpozprice = 0; int mode = 0; int deinit() { //---- return(0); } bool dEQ(double d1, double d2) { if (NormalizeDouble((d1 - d2), 4) == 0) return(true); return(false); } bool dNEQ(double d1, double d2) { if (NormalizeDouble((d1 - d2), 4) == 0) return(false); return(true); } void DoAlert(string str) { if (UseAlert) Alert(str); if (UseEmail) SendMail("SafeGuard", str); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int ticket; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType() == OP_BUY) { if (( OrderOpenPrice()-Bid >= InitialStop * MarketInfo(OrderSymbol(), MODE_POINT)) && (dEQ((OrderOpenPrice()-OrderStopLoss()),EAStopLoss* Point ))) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss()+1* MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Green); ticket=OrderSend(Symbol(),OP_SELL,OrderLots(),Bid,3,Bid+Margin*Point,OrderStopLoss(),"SafeGuard",MagicNumber,0,Green); DoAlert("SafeGuard: Sell " + Bid ); } if (Bid - OrderOpenPrice() > Margin * MarketInfo(OrderSymbol(), MODE_POINT)) { if (OrderStopLoss() != OrderOpenPrice()) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), Red); } } if ( OrderOpenPrice()-Bid >= (InitialStop-Margin-5) * MarketInfo(OrderSymbol(), MODE_POINT)) { if (( OrderOpenPrice()-Bid <= (InitialStop-Margin)* Point ) && (dNEQ((OrderOpenPrice()-OrderStopLoss()),EAStopLoss* Point ))) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-EAStopLoss* Point, OrderTakeProfit(), Red); } } } else if (OrderType() == OP_SELL) { (InitialStop-5)*Point, " and ", dNEQ((OrderOpenPrice()-OrderStopLoss()),EAStopLoss* Point ) ); if ((Ask - OrderOpenPrice() >= InitialStop * MarketInfo(OrderSymbol(), MODE_POINT)) && (dEQ((OrderStopLoss()-OrderOpenPrice()),EAStopLoss* Point ))) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss()-1 * Point, OrderTakeProfit(), Red); ticket=OrderSend(Symbol(),OP_BUY,OrderLots(),Ask,3,Ask-Margin*Point,OrderStopLoss(),"SafeGuard",MagicNumber,0,Red); DoAlert("SafeGuard: Buy " + Bid ); } if (OrderOpenPrice() - Ask > Margin * MarketInfo(OrderSymbol(), MODE_POINT)) { if (OrderStopLoss() != OrderOpenPrice()) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), Red); } } if (Ask - OrderOpenPrice() >= (InitialStop-Margin-5) * MarketInfo(OrderSymbol(), MODE_POINT)) { if ((Ask - OrderOpenPrice() <= (InitialStop-Margin) * MarketInfo(OrderSymbol(), MODE_POINT)) && (dNEQ((OrderOpenPrice()-OrderStopLoss()),EAStopLoss* Point ))) { OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+EAStopLoss* Point, OrderTakeProfit(), Red); } } } } //---- return(0); } //+------------------------------------------------------------------+