Taking the last profit and storing it in a variable | MQL4

Yuri Odilon Paula Da Silva
164
Yuri Odilon Paula Da Silva  

Hey guys,

I've been trying to do a function for a while but I can't find the error ... I'm doing a function that returns the Profit / Loss from the previous order.

I already posted this help here, some colleagues responded with some ideas but without success.

I'll detail the function step by step, whoever can help me there.


1. Check the last order

2. Check if the last order made a profit

  2.1 If you made a profit:

  2.2 Divide the previous profit by the stop value (global variable)

  2.3 adds the value of the division to the initial value of the lot

  2.4 Store the value of the new lot in the newLots variable

  2.5 return newLots


double gerenciaSoros(int MAGICMA_n,    // The MAGICMA No.
                     double loteIni,   // lote standart
                     int nSoros){      // not used yet

double calcnewOrder  = 0;
double newLots       = 0;
double lastProfit    = -1;
int orders           = OrdersTotal();

   for(int i=0;i<orders;i++){
         if((OrderSelect(i, SELECT_BY_POS) == true)){
            if((OrderMagicNumber() == MAGICMA_n) && (OrderProfit() > 0)){
            lastProfit = OrderProfit();
            }
          }
         }
         if(lastProfit > 0){
            calcnewOrder = (double)lastProfit/stopLoss;   //stoploss is global
            newLots      = calcnewOrder+loteIni;
         }
     
  return(newLots);
}

I stored the function return the variable "EntradaSoros"

double entradaSoros = gerenciaSoros(MAGICMA, Lote, nivelSoros); // nivelSoros is global (not used yet)


Up there on onTick I put this check so that if the return of the function is> 0, that is, there is profit.

Then it changes the value of the default batch to the new batch (newLots)

 if(entradaSoros > 0){
   Lote = entradaSoros;  //Lote is global 
 }


I'm doing it this way, in my opinion the error is time to identify the last order, a colleague here on the forum said that when the order is closed, mt4 sends an order against the current operation and it appears in the Historic as zero ... so I don't think I know how to identify that last order to store in the variable "lastProfit"


Thank you all for the attention.

Mohamad Zulhairi Baba
27183
Mohamad Zulhairi Baba  
Yuri Odilon Paula Da Silva:

Hey guys,

I've been trying to do a function for a while but I can't find the error ... I'm doing a function that returns the Profit / Loss from the previous order.

I already posted this help here, some colleagues responded with some ideas but without success.

I'll detail the function step by step, whoever can help me there.


https://docs.mql4.com/trading/ordershistorytotal

OrdersHistoryTotal - Trade Functions - MQL4 Reference
OrdersHistoryTotal - Trade Functions - MQL4 Reference
  • docs.mql4.com
The number of closed orders in the account history loaded into the terminal. The history list size depends on the current settings of the "Account history" tab of the terminal.
Yuri Odilon Paula Da Silva
164
Yuri Odilon Paula Da Silva  

Hello, thanks for the help!

I've already changed to

int orders         = OrdersHistoryTotal();


Even so, the function is not returning the last profit.


Marco vd Heijden
Moderator
13427
Marco vd Heijden  

The last profit of what ?

The last profit of all closed orders ?

The last profit of all open orders ? 

The last profit of all open and closed orders ? 

The last account profit ? 

Be specific.

//+------------------------------------------------------------------+
//|                                                 LastOrderP&L.mq4 |
//|      Copyright 2020, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime last_time   = 0;
double   last_profit = 0;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int i=OrdersHistoryTotal(); i>0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderCloseTime()>last_time)
           {
            last_time = OrderCloseTime();
              {
               last_profit = OrderProfit();
              }
           }
        }
     }

   Print("Last order closed at: "+TimeToString(last_time)+" P&L: "+DoubleToString(last_profit,2));
//return(last_profit);
  }
//+------------------------------------------------------------------+
Yuri Odilon Paula Da Silva
164
Yuri Odilon Paula Da Silva  
Guys !

I managed to solve the problem! It was a logic error.
I was putting the function last and defining parameters that had not yet been defined in the sequence.

The solution was to create an include .mqh I took the variable that received the function's return and put it together on onTick, it worked fine.

Thank you all for the attention !