Modify All TakeProfit and StopLoss Filter orders by Magic Number and Comment

OrderModify_All_TP_n_SL_by_Magic_n_Comment


This script scans for open orders and sets a Stop Loss and Take Profit to all of the filtered orders.

You can filter orders by:

  •     Magic Number
  •     Comment
You will have to specify the Stop Loss and Take Profit in pips.


Code :
//+------------------------------------------------------------------+
//|                   OrderModify_All_TP_n_SL_by_Magic_n_Comment.mq4 |
//|                                      Copyright 2018, MetaEditor. |
//|                              http://www.meta-editor.blogspot.com |
//+------------------------------------------------------------------+
#property copyright     "Copyright 2018, MetaEditor."
#property link          "http://www.meta-editor.blogspot.com"
#property description   "http://www.meta-editor.blogspot.com"
#property version       "1.00"
#property strict
#property show_inputs

//Configure the external variables
extern int TakeProfit            = 40;        //Take Profit in pips
extern int StopLoss              = 20;        //Stop Loss in pips
extern bool OnlyMagicNumber      = false;     //Modify only orders matching the magic number
extern int MagicNumber           = 0;         //Matching magic number
extern bool OnlyWithComment      = false;     //Modify only orders with the following comment
extern string MatchingComment    = "";        //Matching comment
extern double Slippage           = 2;         //Slippage
extern int Delay                 = 0;         //Delay to wait between modifying orders (in milliseconds)

//Function to normalize the digits
double CalculateNormalizedDigits()
{
   if(Digits<=3){
      return(0.01);
   }
   else if(Digits>=4){
      return(0.0001);
   }
   else return(0);
}

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   //Counter for orders modified
   int TotalModified=0;
   
   //Normalization of the digits
   if(Digits==3 || Digits==5){
      Slippage=Slippage*10;
   }
   double nDigits=CalculateNormalizedDigits();
   
   //Scan the open orders backwards
   for(int i=OrdersTotal()-1; i>=0; i--){
   
      //Select the order, if not selected print the error and continue with the next index
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         continue;
      } 
      
      //Check if the order can be modified matching the criteria, if criteria not matched skip to the next
      if(OrderSymbol()!=Symbol()) continue;
      if(OnlyMagicNumber && OrderMagicNumber()!=MagicNumber) continue;
      if(OnlyWithComment && StringCompare(OrderComment(),MatchingComment)!=0) continue;
      
      //Prepare the prices
      double TakeProfitPrice=0;
      double StopLossPrice=0;
      double OpenPrice=OrderOpenPrice();
      RefreshRates();
      if(OrderType()==OP_BUY){
         TakeProfitPrice=NormalizeDouble(OpenPrice+TakeProfit*nDigits,Digits);
         StopLossPrice=NormalizeDouble(OpenPrice-StopLoss*nDigits,Digits);
      } 
      if(OrderType()==OP_SELL){
         TakeProfitPrice=NormalizeDouble(OpenPrice-TakeProfit*nDigits,Digits);
         StopLossPrice=NormalizeDouble(OpenPrice+StopLoss*nDigits,Digits);      
      }
         
      //Try to modify the order
      if(OrderModify(OrderTicket(),OpenPrice,StopLossPrice,TakeProfitPrice,0,clrNONE)){
         TotalModified++;
      }
      else{
         Print("Order failed to update with error - ",GetLastError());
      }      
      
      //Wait a delay
      Sleep(Delay);
   
   }
   
   //Print the total of orders modified
   Print("Total orders modified = ",TotalModified);
   
  }
//+------------------------------------------------------------------+

Download : OrderModify_All_TP_n_SL_by_Magic_n_Comment.mq4
 
Comments
0 Comments

0 komentar:

Posting Komentar