Get profit of last closed position

 

Hi Guys, 

I want to use a "score" called "MFSCore" in my EA that depends on the success of the last trades. 
My Problem is simple but annoying. The Code doesn't work at all.
Am doing somethin obviously wrong?

 int MarkFisherScore (ulong LastMainTradeTicketNr, char SetOrGet) //0 = Set new Mark Fisher score 1 = Get Current Mark Fisher Score
   {
      static int MFScore;
      
      if(SetOrGet==1) //Get current Mark Fisher Score  
        {
            return MFScore;
        }
        
      if(SetOrGet==0) //Set New Mark Fisher Score after Main Trade is closed 
        {
            HistoryDealSelect(LastMainTradeTicketNr);

            
            if (HistoryDealGetInteger(LastMainTradeTicketNr,DEAL_TYPE)==DEAL_TYPE_BUY)
               {
                  if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)>0) MFScore=MFScore+2;
                  if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)<0) MFScore=MFScore-2;
               }  
                  
            if (HistoryDealGetInteger(LastMainTradeTicketNr,DEAL_TYPE)==DEAL_TYPE_SELL)
               {
                  if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)>0) MFScore=MFScore-2;
                  if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)<0) MFScore=MFScore+2;
               }   
             
            if(MFScore>6)MFScore=6;
            if(MFScore<-6)MFScore=-6;
                              
            return MFScore;
        }  
        
      return MFScore;            
   }

The ticketnumber I send to the function is the one from the latest Position that was just closed. 

The function Always Returns 0.

Thanks in Advance for your help.

 
What is 'LastMainTradeTicketNr'? How do you get this value? Show in the terminal where this value comes from.
 

Here's an example:

//+------------------------------------------------------------------+
//|                                                     Expert 1.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//---
ulong    m_last_deal_ticket=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_last_deal_ticket=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Comment("m_last_deal_ticket: ",IntegerToString(m_last_deal_ticket),", Mark Fisher Score: ",MarkFisherScore(m_last_deal_ticket,0));
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
        {
         m_last_deal_ticket=trans.deal;
        }
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
     }
  }
//+------------------------------------------------------------------+
//| Mark Fisher Score                                                |
//|   0 = Set new Mark Fisher score                                  |
//|   1 = Get Current Mark Fisher Score                              |
//+------------------------------------------------------------------+
int MarkFisherScore(ulong LastMainTradeTicketNr, char SetOrGet)
  {
   static int MFScore;
   if(SetOrGet==1) // get current Mark Fisher Score
     {
      return(MFScore);
     }
   if(SetOrGet==0) // set New Mark Fisher Score after Main Trade is closed
     {
      HistoryDealSelect(LastMainTradeTicketNr);
      if(HistoryDealGetInteger(LastMainTradeTicketNr,DEAL_TYPE)==DEAL_TYPE_BUY)
        {
         if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)>0)
            MFScore=MFScore+2;
         if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)<0)
            MFScore=MFScore-2;
        }
      if(HistoryDealGetInteger(LastMainTradeTicketNr,DEAL_TYPE)==DEAL_TYPE_SELL)
        {
         if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)>0)
            MFScore=MFScore-2;
         if(HistoryDealGetDouble(LastMainTradeTicketNr,DEAL_PROFIT)<0)
            MFScore=MFScore+2;
        }
      if(MFScore>6)
         MFScore=6;
      if(MFScore<-6)
         MFScore=-6;
      //---
      return(MFScore);
     }
//---
   return(MFScore);
  }
//+------------------------------------------------------------------+
Files:
Expert_1.mq5  8 kb
 

But this code will catch deals ( ENUM_DEAL_ENTRY )

DEAL_ENTRY_OUT

Entry out

DEAL_ENTRY_INOUT

Reverse

DEAL_ENTRY_OUT_BY

Close a position by an opposite one


***
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
        {
         ENUM_DEAL_ENTRY deal_entry=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         if(deal_entry!=DEAL_ENTRY_IN)
            m_last_deal_ticket=trans.deal;
        }
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
     }
  }
***
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Deal Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Expert_1.mq5  8 kb
 
Vladimir Karputov:
What is 'LastMainTradeTicketNr'? How do you get this value? Show in the terminal where this value comes from.
Hi,
thanks for your good help and cleaning up my code.

I get the ticket number like this, when I open a new position:


if(PositionsOpenOld==0  && PositionsOpenNew>0)  //Main Trade was just opened 
     {

      PositionsOpenOld=PositionsOpenNew;
      LastMainTicketNumber = PositionGetTicket(0); 

     }
Reason: