Download MetaTrader 5

Reading Closed Order Profit On Account History

To add comments, please log in or register
Syarif Nur Arief
1510
Syarif Nur Arief  

Hello,

i build EA that will make history reading for closed order profit for 5 Last closed order (Last 1 closed profit, Last 2 closed profit, Last 3 closed profit, Last 4 closed profit and Last 5 closed profit)

below code is good reading if i only trade on only 1 pair, but the problem is coming when i trade on multipair, reading closed order profit is wrong for 5 last closed order

is there someting wrong on my code here, can someone help me please :

///history closed order 1
double Closed_Order_1()//scan closed order 1
{  
    int time = 0;
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_1 = 0;
    int Profit_1 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-1, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {
                Profit_1 = OrderTicket();
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     last_s_a = OrdersHistoryTotal();
                     ticket_20q = ticket_8q;
                     Closed_Order_1 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_1);
}
///history closed order 1
///history closed order 2
double Closed_Order_2()//scan closed order 2
{  
    int time = 0;
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_2 = 0;
    int Profit_2 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-2, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {
                Profit_2 = OrderTicket();
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     last_s_a = OrdersHistoryTotal();
                     ticket_20q = ticket_8q;
                     Closed_Order_2 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_2);
}
///history closed order 2
///history closed order 3
double Closed_Order_3()//scan closed order 3
{  
    int time = 0;
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_3 = 0;
    int Profit_3 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-3, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {
                Profit_3 = OrderTicket();
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     last_s_a = OrdersHistoryTotal();
                     ticket_20q = ticket_8q;
                     Closed_Order_3 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_3);
}
///history closed order 3
///history closed order 4
double Closed_Order_4()//scan closed order 4
{  
    int time = 0;
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_4 = 0;
    int Profit_4 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-4, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {
                Profit_4 = OrderTicket();
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   { 
                     last_s_a = OrdersHistoryTotal();
                     ticket_20q = ticket_8q;
                     Closed_Order_4 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_4);
}
///history closed order 4
///history closed order 5
double Closed_Order_5()//scan closed order 5
{  
    int time = 0;
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_5 = 0;
    int Profit_5 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-5, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {
                Profit_5 = OrderTicket();
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     last_s_a = OrdersHistoryTotal();
                     ticket_20q = ticket_8q;
                     Closed_Order_5 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_5);
}
///history closed order 5
Keith Watford
Moderator
10065
Keith Watford  
    int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;

Why use variable names that give no indication of what they represent? It can make even simple code difficult to follow.

I can't work out what you are trying to do 

                Profit_1 = OrderTicket();

 What is Profit_1 and why are you storing a ticket number in it? The name suggests that you would be storing the profit.

Syarif Nur Arief
1510
Syarif Nur Arief  
GumRai:

Why use variable names that give no indication of what they represent? It can make even simple code difficult to follow.

I can't work out what you are trying to do 

 What is Profit_1 and why are you storing a ticket number in it? The name suggests that you would be storing the profit.

yes, that is wrong storing name,

i will delete unused and wrong storing variable 

Syarif Nur Arief
1510
Syarif Nur Arief  

Here it is clear code, this code is to collect history closed order Last 1, last 2, last 3, last 4 and last 5 closed order in profit value. this code is work if only trade on 1 pair in 1 account, but failed when trade in multipair

///history closed order 1
double Closed_Order_1()//scan closed order 1
{     
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_1 = 0;
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-1, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {                
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     
                     ticket_20q = ticket_8q;
                     Closed_Order_1 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_1);
}
///history closed order 1
///history closed order 2
double Closed_Order_2()//scan closed order 2
{        
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_2 = 0;    
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-2, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {                
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {
                     
                     ticket_20q = ticket_8q;
                     Closed_Order_2 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_2);
}
///history closed order 2
///history closed order 3
double Closed_Order_3()//scan closed order 3
{      
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_3 = 0;    
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-3, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {                
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {                     
                     ticket_20q = ticket_8q;
                     Closed_Order_3 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_3);
}
///history closed order 3
///history closed order 4
double Closed_Order_4()//scan closed order 4
{        
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_4 = 0;    
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-4, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {                
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {                      
                     ticket_20q = ticket_8q;
                     Closed_Order_4 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_4);
}
///history closed order 4
///history closed order 5
double Closed_Order_5()//scan closed order 5
{       
    int ticket_8q;
    int ticket_20q = 0;
    double Closed_Order_5 = 0;    
        for ( int i = OrdersHistoryTotal();i >= 1;i--)
        {
            if(OrderSelect(i-5, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if(OrderType() <= 1 && OrderSymbol() == Symbol()) 
            {                
                ticket_8q = OrderTicket();
                if (ticket_8q > ticket_20q) 
                   {                     
                     ticket_20q = ticket_8q;
                     Closed_Order_5 = OrderProfit() + OrderSwap() + OrderCommission();
                   }
            }
        }
    return(Closed_Order_5);
}
///history closed order 5
Keith Watford
Moderator
10065
Keith Watford  

I repeat what I said before

GumRai:

Why use variable names that give no indication of what they represent? It can make even simple code difficult to follow.

  int last_s_a;
    int ticket_8q;
    int ticket_20q = 0;

 

 I won't read your code with variable names like this

whroeder1
15073
whroeder1  
Your code assumes that history is ordered; it is not. Could EA Really Live By Order_History Alone? - MQL4 forum
Syarif Nur Arief
1510
Syarif Nur Arief  
GumRai:

I repeat what I said before

 I won't read your code with variable names like this

 

 

Okay, sorry. 

Syarif Nur Arief
1510
Syarif Nur Arief  
WHRoeder:
Your code assumes that history is ordered; it is not. Could EA Really Live By Order_History Alone? - MQL4 forum



Thanks for your answer
To add comments, please log in or register