Knowing the profit of the last closed position

To add comments, please log in or register
Ziad El
123
Ziad El  

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:

//+------------------------------------------------------------------+
//| Ask: MQL4 emulation                                              |
//+------------------------------------------------------------------+
double Ask(string aPair)                                      //[MQL5]
{
MqlTick last_tick;
SymbolInfoTick(aPair,last_tick);
double ask=last_tick.ask;
return ask;
} //--- *

//+------------------------------------------------------------------+
//| CheckProfit: Checks If Last Postion Was Profitable               |
//| I am in HEDGING account.                                         |
//+------------------------------------------------------------------+
bool CheckProfit_MethodA()
{
//-- first open a position, for the exercice:
Trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,Ask(_Symbol),0,0);
//-- Immediately get the ticket number:
ulong lastTicket = Trade.RequestPosition();
Print("the last ticket was: " + IntegerToString(lastTicket));
//-- sleep some time
Sleep(4000);
//-- then close this position, to provide some data for coming code:
Trade.PositionClose(_Symbol,3);
//-- 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?

Code2219 or probably 2319
390
Code2219 or probably 2319  

Save/Update that double each time a position closes :

double LAST_TRADE_PROFIT=0;     // global variable

void OnTrade()
{
        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...)
                LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT);
        }
        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.

fxsaber
9849
fxsaber  
OrderCloseTime Expert Advisor MQL5
OrderCloseTime Expert Advisor MQL5
  • 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...
Ziad El
123
Ziad El  

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:
Trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,Ask(_Symbol),0,0);
Sleep(4000);
Trade.PositionClose(_Symbol,3);
Sleep(4000);
OnTrade();  // I had to put this here, otherwise no output
}
Sleep(12000);
ExpertRemove();
}
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
{
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...)
 LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT);
}
// a position just got opened:
else if(current_open_positions > previous_open_positions) 
 previous_open_positions = current_open_positions; 
Print ("Last Trade Profit " + DoubleToString(LAST_TRADE_PROFIT));
}



Code2219 or probably 2319
390
Code2219 or probably 2319  

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

OnTick()
{
    // open/close your positions here as usual
    // OnTrade() will be called automatically with each trade-related operation
}

void OnTrade()
{
        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...)
                LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT);
                Print("Last Trade Profit : ", DoubleToString(LAST_TRADE_PROFIT));
        }
        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

Ziad El
123
Ziad El  

Code2219 or probably 2319

It works. Thank you very much.

To add comments, please log in or register