Scripts: Panel for Calculating History Profit

 

Panel for Calculating History Profit:

This CalculateHistoryProfit script version 1.0 is designed to calculate profit for a specified period using a chart panel.

Panel for Calculating History Profit

Author: Sergey Porphiryev

 
    //+------------------------------------------------------------------+
    //| Calculate total profit and deal count for the selected period |
    //+------------------------------------------------------------------+
    void CalculateProfit()
    {
       total_profit = 0;
       total_deals = 0;
      
       HistorySelect(0, TimeCurrent());
       int total_orders = HistoryDealsTotal();
  
       for (int i = total_orders - 1; i >= 0; i--)
       {
           ulong deal_ticket = HistoryDealGetTicket(i);
           datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME); // Why isn't this line lower than the next?
           if (HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) != 1) continue; // What about CloseBy deals and IN-commission?
  
           if (deal_time >= start_date && deal_time <= end_date) // Why not HistorySelect?
           {
               string deal_symbol = HistoryDealGetString(deal_ticket, DEAL_SYMBOL);
               int deal_magic = (int)HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
               int deal_type = (int)HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
              
               // Filtering by transaction type (buy or sell)
               bool type_filter = (deal_type == 0 || deal_type == 1);
  
               // Character filtering
               bool symbol_filter = (StringFind(current_symbols, "ALL") != -1) || (StringFind(current_symbols, deal_symbol) != -1); // It is expensive to check the first condition every time in the loop.
  
               // Filtering by Magiks
               bool magic_filter_all = (StringFind(current_magics, "ALL") != -1); // It is expensive to check every time in a loop.
              
               // Split the string with mages into an array
               string magic_values[];
               int count = StringSplit(current_magics, ',', magic_values); // This should have been taken out of the loop as well.
  
               // Check for "Empty" and magick 0
               bool magic_filter_empty = false;
               for (int j = 0; j < count; j++) // Dear Cycle.
               {
                   if (magic_values[j] == "Empty" && deal_magic == 0) // It is cheaper to check the second condition first.
                   {
                       magic_filter_empty = true;
                       break;
                   }
               }
  
               // Check for a specific magik
               bool magic_filter_exact = false;
               for (int j = 0; j < count; j++)
               {
                   if (IntegerToString(deal_magic) == magic_values[j]) // Why compare string variables with each other?
                   {
                       magic_filter_exact = true;
                       break;
                   }
               }

Some comments on the code.

 
fxsaber #:

Some comments on the code.

OOO!!! Thank you very much for your valuable notes!!!! It will be something to do this weekend ;))))))

 
Nice work. Thank you for sharing.