MQL4 Script for Calculating Profit per Pair and Total Profit

Answered using ChatGPT 4o by ChatGPT4o

Question:

buatkan skript mql4 untuk fungsi profit per pair dan total profit

Answer:

Tentu, berikut adalah contoh skrip MQL4 yang menghitung profit per pair dan total profit dari semua posisi terbuka di MetaTrader 4. Skrip ini akan menampilkan hasilnya di jendela "Experts" di terminal MetaTrader 4.

//+------------------------------------------------------------------+
//|                                                      ProfitInfo.mq4|
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property strict

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double totalProfit = 0.0;
   double pairProfit = 0.0;
   string currentPair = "";
   
   // Loop through all open orders
   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // Check if the order is a buy or sell position
         if(OrderType() == OP_BUY || OrderType() == OP_SELL)
           {
            // If the pair changes, print the profit for the previous pair
            if(currentPair != OrderSymbol())
              {
               if(currentPair != "")
                 {
                  Print("Profit for ", currentPair, ": ", pairProfit);
                 }
               currentPair = OrderSymbol();
               pairProfit = 0.0;
              }
            
            // Add the profit of the current order to the pair profit
            pairProfit += OrderProfit();
            // Add the profit of the current order to the total profit
            totalProfit += OrderProfit();
           }
        }
     }
   
   // Print the profit for the last pair
   if(currentPair != "")
     {
      Print("Profit for ", currentPair, ": ", pairProfit);
     }
   
   // Print the total profit
   Print("Total Profit: ", totalProfit);
  }
//+------------------------------------------------------------------+

Penjelasan:

  1. Skrip ini dimulai dengan mendeklarasikan variabel totalProfit untuk menyimpan total profit dari semua posisi terbuka dan pairProfit untuk menyimpan profit per pair.
  2. Skrip kemudian melakukan loop melalui semua order yang terbuka menggunakan OrdersTotal() dan OrderSelect().
  3. Untuk setiap order yang merupakan posisi buy atau sell (OP_BUY atau OP_SELL), skrip memeriksa apakah pair saat ini berbeda dari pair sebelumnya. Jika berbeda, skrip mencetak profit untuk pair sebelumnya dan mengatur pairProfit ke 0.
  4. Profit dari order saat ini ditambahkan ke pairProfit dan totalProfit.
  5. Setelah loop selesai, skrip mencetak profit untuk pair terakhir dan total profit.

Untuk menggunakan skrip ini, Anda bisa menyalinnya ke dalam MetaEditor, menyimpannya sebagai file .mq4, dan kemudian menjalank