Get the profit in pips

Fernando Carreiro  
Tiofelo Teles: Hello! How can i get the profit in pips??

Subtract the current closing price of the open position from its opening price. It is that simple!

In your code you are using the CPositionInfo class, so use the methods PriceCurrent and PriceOpen of the selected position for this.

Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
  • www.mql5.com
CPositionInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Vladimir Karputov  
Tiofelo Teles :
Hello! How can i get the profit in pips??

If you will work on all symbols, then you only need to change OnTick:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   for(int i=PositionsTotal()-1; i>=0; i--)  // returns the number of current positions
      if(m_position.SelectByIndex(i))        // selects the position by index for further access to its properties
        {
         //---
         ResetLastError();
         if(!m_symbol.Name(m_position.Symbol())) // sets symbol name
           {
            Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
            continue;
           }
         //---
         double profit=m_position.PriceCurrent()-m_position.PriceOpen();
         double profit_points=profit/m_symbol.Point();
        }
  }


If you will work only on the current symbol, then you need to make changes to both OnInit and OnTick:

//+------------------------------------------------------------------+
//|                                                   Get profit.mq5 |
//|                                 Copyright © 2022, Tiofelo Teles. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| EA Instatiates                                                   |
//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh>    // instatiate library for positions information          
#include <Trade\Trade.mqh>           // instatiate library for trades execution 
#include <Trade\SymbolInfo.mqh>      // instatiate library for symbol information 
//+------------------------------------------------------------------+
//| EA library                                                       |
//+------------------------------------------------------------------+
CPositionInfo  m_position;           // library for all position features and information
CTrade         m_trade;              // library for Orders information
CSymbolInfo    m_symbol;             // library for symbol informaton
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ResetLastError();
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name()/* && m_position.Magic()==InpMagic*/)
           {
            double profit=m_position.PriceCurrent()-m_position.PriceOpen();
            double profit_points=profit/m_symbol.Point();
           }
  }
//+------------------------------------------------------------------+
Tiofelo Da Olga Gilbert Teles  
Vladimir Karputov #:

If you will work on all symbols, then you only need to change OnTick:


If you will work only on the current symbol, then you need to make changes to both OnInit and OnTick:

This is my first programming language to learn. Thank you so much for your patience and for the teachings you have always given me.

Reason: