Store previous profit in a variable | MQL4

 

Hey guys ! see if anyone can help me:

I'm trying to take the profit from the previous order and store it in a variable.

I'm doing this way:

double lastProfit = 0;

       for(int i=0; i<OrdersHistoryTotal(); i++) {
        if(OrderSelect(i, SELECT_BY_TICKET, MODE_HISTORY) == true)        
        lastProfit = OrderProfit();      

       }


Can anyone tell me where I'm going wrong?

 
Yuri Odilon Paula Da Silva:

Hey guys ! see if anyone can help me:

I'm trying to take the profit from the previous order and store it in a variable.

I'm doing this way:


Can anyone tell me where I'm going wrong?

Better catch it in OnTradeTransaction. The instant a trade closes, you get your value.

<Deleted by moderator as not relevant to MQL4>

I trust this piece of code will see you through so much hustle in the future. 

 
  1. Nelson Wanyama: Better catch it in OnTradeTransaction
    Doesn't exist in MT4.

  2. Yuri Odilon Paula Da Silva: Can anyone tell me where I'm going wrong?
    double lastProfit = 0;
    
           for(int i=0; i<OrdersHistoryTotal(); i++) {
            if(OrderSelect(i, SELECT_BY_TICKET, MODE_HISTORY) == true)        
            lastProfit = OrderProfit();      
           }

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

  3. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum

  4. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  5. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum
    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing
 
Nelson Wanyama:

Better catch it in OnTradeTransaction. The instant a trade closes, you get your value.

I trust this piece of code will see you through so much hustle in the future. 

Thanks for your idea!

Is this function from MQL5? Because my project is for MQL4.

 
William Roeder:
  1. Doesn't exist in MT4.

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

  3. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum

  4. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  5. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum
    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing

Thanks for your answer!


I did it this way, and even then it is not returning the previous value.

double lastProfit = 0;

for(int i=0;i<OrdersTotal();i++){
       if((OrderSelect(i, SELECT_BY_TICKET,MODE_HISTORY)==true) && (OrderMagicNumber() == MAGICMA_n))
       lastProfit = (OrderProfit()+OrderSwap()+OrderCommission());
     }


Reason: