Find Trailing Stop Code in an existing EA

To add comments, please log in or register
Walter Demilly
153
Walter Demilly  

I have an EA coded which contains a Trailing Stop I like.   I know that there are other standalone TS EAs or Scripts out there but their instructions aren't clear to me.   This TS works on the floating P/L of the account and closes all positions when the stop is triggered.  This is the code:   Can anyone help me turn it into a standalone EA or Script?   Thanks!

//+------------------------------------------------------------------+
//| Expert Trailing Stop                                             |
//+------------------------------------------------------------------+

void SetTrailingStop() {

   if(TrailStop<=0||TrailStart<=0) return;
   double newsl =0, trail_start=0;
   trail_start = AccountBalance()*TrailStart;
   
   double profit=0;
   for (int i = 0; i < OrdersTotal(); i++) {
      int select = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(!select || OrderMagicNumber() != Magic_number )continue;
      profit += OrderProfit()+OrderCommission()+OrderSwap();
   }
      
   if(profit> 0 && profit > max_profit) {
      max_profit = profit;
      GlobalVariableSet("Stratum_"+(string)Magic_number+"profit",max_profit);
      if(max_profit>trail_start) {
         trail_stop = max_profit*(1-TrailStop);
         GlobalVariableSet("Stratum_"+(string)Magic_number+"trail_stop",trail_stop);
      }
   }
   
   if(trail_stop>0 && profit <= trail_stop) {
      close_all_trade = true; 
      max_profit =0;
      trail_stop =0;
      GlobalVariableDel("Stratum_"+(string)Magic_number+"profit");
      GlobalVariableDel("Stratum_"+(string)Magic_number+"trail_stop");
   }
   
   if(close_all_trade) close_all_trade = !CloseAllOrders();
   
   return;
}
To add comments, please log in or register