Profit Factor calculations

 

hello guys , hope you are doing well. i wrote a code that i'm currently using to calculate profit factor for a specific magic number:

double Grosswin(){
int win=OrderProfit();
double grosswin=0;
int i= 0;
if (cnt>0)
for( i ;i<cnt ; i++)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if (OrderMagicNumber()== magicnumber)
if(OrderProfit()>0){grosswin+=OrderProfit()+ OrderCommission()+OrderSwap();}
return(grosswin);
}

double Grossloss(){
double loss=OrderProfit();
double grossloss=0;
int i= 0;
if (cnt>0)
for( i ;i<cnt ; i++)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if (OrderMagicNumber()== magicnumber)
if(OrderProfit()<0){grossloss+=OrderProfit()+ OrderCommission()+OrderSwap();}
return(grossloss);
}

double ProfitFactor(){
double proffactor=0;
if(Grossloss() != 0 || Grosswin() != 0){
proffactor=(Grosswin()/-Grossloss());
return(proffactor);
}
}

I want this code to be calculated on all the magic numbers available in the terminal , i have a code that retrieve all the magic number in the terminal but i could not use it in my code

double magic2(){
int totalOrders = OrdersHistoryTotal();
    int magicNumbers[];
    ArrayResize(magicNumbers, totalOrders);
    int magicNumberCount = 0;
    
    for (int i = 0; i < totalOrders; i++)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
        int currentMagicNumber = OrderMagicNumber();
        bool isUnique = true;
        
        for (int j = 0; j < magicNumberCount; j++)
        {
            if (magicNumbers[j] == currentMagicNumber)
            {
                isUnique = false;
                break;
            }
        }
        
        if (isUnique)
        {
            magicNumbers[magicNumberCount] = currentMagicNumber;
            magicNumberCount++;
        }
    }
    
    for ( i = 0; i < magicNumberCount; i++)
    {
        Print("Magic number: ", magicNumbers[i]);
    }
The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
  • www.mql5.com
The problem of calculation of the total position volume of the specified symbol and magic number is considered in this article. The proposed method requests only the minimum necessary part of the history of deals, finds the closest time when the total position was equal to zero, and performs the calculations with the recent deals. Working with global variables of the client terminal is also considered.
 
double Grosswin(){
int win=OrderProfit();

MT4: You can not use any Trade Functions until you first select an order.

 
if (cnt>0)
for( i ;i<cnt ; i++)
What is cnt? When is it set? Is it reset between your two function calls?