last_order_profit(int magic)

 
double last_order_profit(int magic){
 
   for(int i=OrdersHistoryTotal();i >= 0;i--)
     {
     
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
         if (OrderMagicNumber() == magic){
             
           break;
         }
 
     }
 
   return OrderProfit();
}
 
As in your other topic, is there a point here?
 
  for(int i=OrdersHistoryTotal();i >= 0;i--){
     OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
     if (OrderMagicNumber() == magic) break;
You are assuming that history is orders, it isn't. Go through all orders and remember the last closed one and it's profit (=OrderProfit()+OrderSwap()+OrderCommission())
Reason: