Help about closing a deal

Hello to everybody,

My goal is to write an EA which react in a particular way once I close a specified deal; i.e. suppose I have just closed a specified deal by the following code

ticket = PositionGetTicket(i);

      print("Opened order ticket no.: "+IntegerToString(ticket)+" closed");


where the index ‘i’ refers to the selected openend position

Then I want in my OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) detect the deal just closed in order, for example, to detect the gain or the loss.

Advanced thank to everybody will help me.

Hi, what do you want to do after detected profit or loss position? send emai? save info to file, journal? for what do you need this info. Regards Greg

My immediate goal is only to detect profit or loss position.

Hi, your statement show you profit and loss position. Regards Greg

Could you explain how exactly to do ?


double pprofit = PositionGetDouble(POSITION_PROFIT);

here are some other examples of information you can get about a position:

 #define xPOSITION_COMMENT        (PositionGetString(POSITION_COMMENT))
   #define xPOSITION_SYMBOL         (PositionGetString(POSITION_SYMBOL))
   #define xPOSITION_TYPE           (PositionGetInteger(POSITION_TYPE))
   #define xPOSITION_MAGIC          (PositionGetInteger(POSITION_MAGIC))
   #define xPOSITION_OPENTIME       (PositionGetInteger(POSITION_TIME))
   #define xPOSITION_TICKET         (PositionGetInteger(POSITION_TICKET))

   #define xPOSITION_PRICE_OPEN     (PositionGetDouble(POSITION_PRICE_OPEN))
   #define xPOSITION_PROFIT         (PositionGetDouble(POSITION_PROFIT))
   #define xPOSITION_PROFIT_LIQUID  (PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP))
   #define xPOSITION_SL             (PositionGetDouble(POSITION_SL))
   #define xPOSITION_TP             (PositionGetDouble(POSITION_TP))   
   #define xPOSITION_SWAP           (PositionGetDouble(POSITION_SWAP))  
   #define xPOSITION_VOLUME         (PositionGetDouble(POSITION_VOLUME))
   #define xPOSITION_ELAPSEDTIME    (TimeCurrent()-PositionGetInteger(POSITION_TIME))
   #define xPOSITION_TYPE_STRING    ((PositionGetInteger(POSITION_TYPE)) ? "Sell" : "Buy ")
   #define xPOSITION_TICKVALUE      (SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE))
   #define xPOSITION_SPREAD         (SymbolInfoInteger(_Symbol,SYMBOL_SPREAD))

   #define xMARKET_PIPVALUE         (SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE))

First select any position with:


Then you have access to every info about it using my definitions above.

// ---

            ProftInPoints = (int)NormalizeDouble( xPOSITION_PRICE_OPEN/_Point 
                                             - xPOSITION_PRICE_CURRENT/_Point
            ProftInPoints = (int)NormalizeDouble( xPOSITION_PRICE_CURRENT/_Point
                                             - xPOSITION_PRICE_OPEN/_Point 

double profit_money  = xPOSITION_PROFIT;
double profit_points = ProftInPoints;
double position_lots = xPOSITION_VOLUME;

// etc.. etc etc..