# Knowing the profit of the last closed position

With two trials to get the profit of the last closed position (Hedging account), I am not arriving anywhere.

The first trial is personal, and it goes like this:

```//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
{
MqlTick last_tick;
SymbolInfoTick(aPair,last_tick);
} //--- *

//+------------------------------------------------------------------+
//| CheckProfit: Checks If Last Postion Was Profitable               |
//| I am in HEDGING account.                                         |
//+------------------------------------------------------------------+
bool CheckProfit_MethodA()
{
//-- first open a position, for the exercice:
//-- Immediately get the ticket number:
Print("the last ticket was: " + IntegerToString(lastTicket));
//-- sleep some time
Sleep(4000);
//-- then close this position, to provide some data for coming code:
//-- Now get the outcome of that position (loss off course):
//--- select history for access
HistorySelect(TimeCurrent()-24*60*60,TimeCurrent());//since yesterday
double lastProfit = HistoryDealGetDouble(lastTicket,DEAL_PROFIT);
Print("and its profit was: " + DoubleToString(lastProfit));
if (lastProfit>0) return true;
return false;
}
```

I am not even receiving a proper ticket:

The second trial was given in MQL5 forum, and while I get all the profits, how can I be sure that the orders of the loop correspond to the order of opening positions?

```void ScanClosedTrades() // Courtesy Diane Minshew
{
HistorySelect(0,TimeCurrent());
ulong  ticket = 0;
double profit;
uint   dealsTotal=HistoryDealsTotal();
//--- Loop for tracking wins losses and lot sizes for trading
for (uint i=0; i < dealsTotal; i++)
{
if((ticket=HistoryDealGetTicket(i))>0)
{
profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
}
if (profit < 0)
{
Print("Profit: %f",profit);//profit);
}
}
}```

It gives something like this:

Any help?

Save/Update that double each time a position closes :

```double LAST_TRADE_PROFIT=0;     // global variable

{
static int previous_open_positions = 0;
int current_open_positions = PositionsTotal();
if(current_open_positions < previous_open_positions)    // a position just got closed.
{
previous_open_positions = current_open_positions;
HistorySelect(TimeCurrent()-300, TimeCurrent()) // 5 minutes ago up to now :)
int All_Deals = HistoryDealsTotal();
if(All_Deals < 1) Print("Some nasty shit error has occurred :s");
ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1); // last deal (should be an DEAL_ENTRY_OUT type)
// here check some validity factors of the position-closing deal (symbol, position ID, even MagicNumber if you care...)
}
else if(current_open_positions > previous_open_positions) previous_open_positions = current_open_positions; // a position just got opened.
}```

if you want to get that LAST_TRADE_PROFIT variable refreshed anywhere outside OnTrade() and OnTradeTransaction() events, you need to load more history, and search through deals differently.

• 2018.07.03
• www.mql5.com
Hi, Im looking for a way to retrieve the last closed order/deal/position close time, possibly in datetime...

Code2219 or probably 2319 Thanks but it is now working. I made an EA based on your code:

```//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
for(int i=0; i<5; i++)
{
//-- first open a position, for the exercice:
Sleep(4000);
Sleep(4000);
}
Sleep(12000);
ExpertRemove();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
{
static int previous_open_positions = 0;
int current_open_positions = PositionsTotal();
// a position just got closed:
if(current_open_positions < previous_open_positions)
{
previous_open_positions = current_open_positions;
HistorySelect(TimeCurrent()-300, TimeCurrent()); // 5 minutes ago
int All_Deals = HistoryDealsTotal();
if(All_Deals < 1) Print("Some nasty shit error has occurred :s");
// last deal (should be an DEAL_ENTRY_OUT type):
ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1);
// here check some validity factors of the position-closing deal
// (symbol, position ID, even MagicNumber if you care...)
}
// a position just got opened:
else if(current_open_positions > previous_open_positions)
previous_open_positions = current_open_positions;
}
```

you don't need to call OnTrade() from OnTick() !!!

```OnTick()
{
// open/close your positions here as usual
}

{
static int previous_open_positions = 0;
int current_open_positions = PositionsTotal();
if(current_open_positions < previous_open_positions)             // a position just got closed:
{
previous_open_positions = current_open_positions;
HistorySelect(TimeCurrent()-300, TimeCurrent()); // 5 minutes ago
int All_Deals = HistoryDealsTotal();
if(All_Deals < 1) Print("Some nasty shit error has occurred :s");
// last deal (should be an DEAL_ENTRY_OUT type):
ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1);
// here check some validity factors of the position-closing deal
// (symbol, position ID, even MagicNumber if you care...)
}
else if(current_open_positions > previous_open_positions)       // a position just got opened:
previous_open_positions = current_open_positions;
}```

Also those Sleeps aren't needed

Code2219 or probably 2319

It works. Thank you very much.

Hi

Allthough its an old thread ,  I'm struggling with this code..

```        static int previous_open_positions = 0;
int current_open_positions = PositionsTotal();
if(current_open_positions < previous_open_positions)         ```

Since your set  previous_open_positions  to 0, this "if" is only true when     PositionsTotal() is negative !?!

Can that be possible?

grobbrecht:

Hi

Allthough its an old thread ,  I'm struggling with this code..

Since your set  previous_open_positions  to 0, this "if" is only true when     PositionsTotal() is negative !?!

Can that be possible?

It is a static variable
https://www.mql5.com/en/docs/basis/variables/static
So it will keep last assigned value. It has, however, a few flaws. First there is no symbol check. But mainly, if the EA is reinitialized with an active trade, it might miss a trade closure.

Thanks for the info!

Didnt notice the "static" keyword, nor did I knew of if.

Whats the difference with using a global variable? Don't see any to be honest.

Thanks

As a general rule of thumb, the fewer global variables the better. No chances that a change inside of a function affects other functions or naming overlaps.

This wont work on a live account!

You have no chance when you do not deal with delayed, async processing. The only chance to get the results is to use OnTradeTransaction(), cause you will face the challenge, that from time to time you wont receive the valid/final results when your EA doesn´t wait for the postponed transaction results. Like that it may work in 80-90% of the cases, maybe less, maybe more, but surely not in all cases!

See: https://www.mql5.com/en/forum/357245