buatkan skript mql4 untuk fungsi profit per pair dan total profit
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:
totalProfit
untuk menyimpan total profit dari semua posisi terbuka dan pairProfit
untuk menyimpan profit per pair.OrdersTotal()
dan OrderSelect()
.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.pairProfit
dan totalProfit
.Untuk menggunakan skrip ini, Anda bisa menyalinnya ke dalam MetaEditor, menyimpannya sebagai file .mq4
, dan kemudian menjalank