explanation of some MT4 code so I can understand it

To add comments, please log in or register
Walter Demilly
151
Walter Demilly  
This is part of a longer MT4 EA.  

It is supposed to be a trailing stop based on % of equity and % of floating profit.  
It probably works but I'd like an explanation in plain language what each step of the code does.
I also need to know if the inputs for this are whole numbers (1,2,3) or %.
I can send you other parts of the EA if you need them.
Thanks!


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("instratt_"+(string)Magic_number+"profit",max_profit);
      if(max_profit>trail_start) {
         trail_stop = max_profit*(1-TrailStop);
         GlobalVariableSet("instratt_"+(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("instratt_"+(string)Magic_number+"profit");
      GlobalVariableDel("instratt_"+(string)Magic_number+"trail_stop");
   }
   
   if(close_all_trade) close_all_trade = !CloseAllOrders();
   
   return;
}
GrumpyDuckMan
882
GrumpyDuckMan  
RTaylor:

Hello Friend,

If you don't understand this part of the code. May I ask how do you understand the rest of the code??

Alain Verleyen
38661
Alain Verleyen  
RTaylor:
You should ask the author.
To add comments, please log in or register