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;
}