need help for calculated total pips for order closed today (Mql5) - page 2

 
forextime8:

this is still mql4, isnt it ??

MQL5.

 
fxsaber:

MQL5.


thanks, will try ur way.

since there is no solution for ordercloseprice for mql5.

 
forextime8:

since there is no solution for ordercloseprice for mql5.

Forum on trading, automated trading systems and testing trading strategies

need help for calculated total pips for order closed today (Mql5)

fxsaber, 2017.10.25 01:00

Res += OrderType() ? PriceToInteger(OrderOpenPrice(), point) - PriceToInteger(OrderClosePrice(), point)
                   : PriceToInteger(OrderClosePrice(), point) - PriceToInteger(OrderOpenPrice(), point);
 
fxsaber:

is there a another code if i dont want to use include #include <MT4Orders.mqh> ??

 
forextime8:

is there a another code if i dont want to use include #include <MT4Orders.mqh> ??

Of course, using "standard" mql5 code. Please post your attempt if you need coding help.
 
Alain Verleyen:
Of course, using "standard" mql5 code. Please post your attempt if you need coding help.
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[],const double &high[],
                const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[])
   {
//+------------------------------------------------------------------+
//+- Info For Position Closed Today ---------------------------------+
   TotalPipClosed = 0;
   SelectStart = iTime(_Symbol, PERIOD_D1, 0);
   SelectEnd = TimeCurrent();
   
   HistorySelect(SelectStart, SelectEnd);
//+------------------------------------------------------------------+
   for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
   {
   if (Deal_Info_For_AllSymbols || PositionGetSymbol(i) == _Symbol)
   {
   if (HistoryDealGetTicket(i) > 0)
   {
      Deal_Ticket       = HistoryDealGetTicket(i);
      Deal_Magic        = HistoryDealGetInteger(Deal_Ticket, DEAL_MAGIC);
      Deal_Time         = (datetime)HistoryDealGetInteger(Deal_Ticket, DEAL_TIME);
      Deal_Type         = (ENUM_DEAL_TYPE)HistoryDealGetInteger(Deal_Ticket, DEAL_TYPE);
      Deal_Entry_Type   = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(Deal_Ticket, DEAL_ENTRY);
      Deal_ID           = HistoryDealGetInteger(Deal_Ticket, DEAL_POSITION_ID);
      Deal_Volume       = HistoryDealGetDouble(Deal_Ticket, DEAL_VOLUME);
      Deal_Price        = HistoryDealGetDouble(Deal_Ticket, DEAL_PRICE);
      Deal_Comment      = HistoryDealGetString(Deal_Ticket, DEAL_COMMENT);
      Deal_Symbol       = HistoryDealGetString(Deal_Ticket, DEAL_SYMBOL);
      Deal_Profit       = HistoryDealGetDouble(Deal_Ticket, DEAL_PROFIT);
      Deal_Swap         = HistoryDealGetDouble(Deal_Ticket, DEAL_SWAP);
      Deal_Commission   = HistoryDealGetDouble(Deal_Ticket, DEAL_COMMISSION);
      Position_Type     = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      
      if (HistoryOrderGetTicket(i) > 0)
      {
         Order_Ticket         = HistoryOrderGetTicket(i);
         Order_Magic          = HistoryOrderGetInteger(Order_Ticket, ORDER_MAGIC);
         Order_Time_Setup     = (datetime)HistoryOrderGetInteger(Order_Ticket, ORDER_TIME_SETUP);
         Order_Time_Done      = (datetime)HistoryOrderGetInteger(Order_Ticket, ORDER_TIME_DONE);
         Order_Type           = (ENUM_ORDER_TYPE)HistoryOrderGetInteger(Order_Ticket, ORDER_TYPE);
         Order_ID             = HistoryOrderGetInteger(Order_Ticket, ORDER_POSITION_ID);
         Order_Volume_Initial = HistoryOrderGetDouble(Order_Ticket, ORDER_VOLUME_INITIAL);
         Order_Price_Open     = HistoryOrderGetDouble(Order_Ticket, ORDER_PRICE_OPEN);
         Order_Price_Current  = HistoryOrderGetDouble(Order_Ticket, ORDER_PRICE_CURRENT);
         Order_Comment        = HistoryOrderGetString(Order_Ticket, ORDER_COMMENT);
         Order_Symbol         = HistoryOrderGetString(Order_Ticket, ORDER_SYMBOL);
      
      if(_Digits == 2 || _Digits == 4) SymbolPoint = _Point * 10;
      else if (_Digits == 3 || _Digits == 5) SymbolPoint = _Point;
      {
      if ((Deal_Type == DEAL_TYPE_BUY)) if ((Deal_Entry_Type == DEAL_ENTRY_OUT))
      {
         TotalPipClosed += (HistoryDealGetDouble(Deal_Ticket, DEAL_PRICE) - HistoryOrderGetDouble(Order_Ticket, ORDER_PRICE_OPEN)) / SymbolPoint;
      }
      
      if ((Deal_Type == DEAL_TYPE_SELL)) if ((Deal_Entry_Type == DEAL_ENTRY_OUT))
      {
         TotalPipClosed += (HistoryOrderGetDouble(Order_Ticket, ORDER_PRICE_OPEN) - HistoryDealGetDouble(Deal_Ticket, DEAL_PRICE)) / SymbolPoint;
      }
      }
      
      }
   }
   }
   }
//+------------------------------------------------------------------+
   return(rates_total);
   }
//+------------------------------------------------------------------+
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//+------------------------------------------------------------------+
 
forextime8:

Yeah there are a bunch of bugs.

  for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
   {
   if (Deal_Info_For_AllSymbols || PositionGetSymbol(i) == _Symbol)
   {
   if (HistoryDealGetTicket(i) > 0)
   {
    ...
      if (HistoryOrderGetTicket(i) > 0)
      ...

Mainly you can't use an index (i) related to deals to get order data.

To get open/close price you should work with deals only, DEAL_IN gives you open price, DEAL_OUT (eventually INOUT and OUT_BY) gives you the close price. You have to check DEAL_POSITION_ID to be sure you are working with open/close of the same position.

 

Alain is correct.


First you need to understand what a position is in MT5. A position is any number of trades on an instrument that starts when the first trade is placed and ends when the net position size is 0, meaning there can be any number of deals that comprise a position. In order to do what you want you will need to complete the following steps:

  1. Build a collection (set) of position IDs from history
  2. Use position ids to create an object containing all orders for that position.
  3. Calculate the volume-weighted average price for all orders in, and out.
  4. Subtract VWAP in from VWAP out (for long positions) to calculate the net pips.

Here is an example.

#include <Trade\DealInfo.mqh>
#include <Arrays\ArrayObj.mqh>
#include <Arrays\ArrayLong.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{ 
   CHistoryPositionPool pool;
   for (int i = 0; i < pool.Total(); i++)
      pool[i].ToLog();
}
//+------------------------------------------------------------------+
// vector class 
template <typename T>
class vector : public CArrayObj
{
 public:
   T operator[](int i) { return At(i); }
   bool Add(T element) { return CArrayObj::Add(element); }
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CPositionIdSet : public CArrayLong
{
 public: bool Add(const long element);
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CHistoryPosition : public CObject
{
   vector<CDealInfo*> m_in;
   vector<CDealInfo*> m_out;
   long m_pos_id;

 public:
   CHistoryPosition() : m_pos_id(-1) {}
   int  TotalDeals()     { return m_in.Total() + m_out.Total(); }
   int  TotalIn()        { return m_in.Total(); }
   int  TotalOut()       { return m_out.Total(); }
   long PositionID() const;
   bool SelectPositionByAnyDealTicket(const ulong any_deal_ticket);
   bool SelectPositionByID(const long position_id);
   //bool                 OpenPrice(double &value);
   double OpenPrice();
   double ClosePrice();
   void ToLog();
   int Compare(const CObject *node, const int mode = 0) const override;
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CHistoryPositionPool : public vector<CHistoryPosition*>
{
 public:
   CHistoryPositionPool()
   {
      CPositionIdSet id_set;
      CDealInfo deal;
      if (HistorySelect(0, TimeCurrent()))
         for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
            if (deal.SelectByIndex(i))
               if(!id_set.Add(deal.PositionId()))
                  DebugBreak();
      for (int i = 0; i < id_set.Total(); i++)
      {
         CHistoryPosition *position = new CHistoryPosition;
         if (position.SelectPositionByID(id_set[i]))
            if (this.Add(position))
               continue;
         delete position;
      }
   }
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CPositionIdSet::Add(const long element)
{
   Sort();
   if (Search(element) < 0)
      return CArrayLong::InsertSort(element);
   return false;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
long CHistoryPosition::PositionID() const
{
   return m_pos_id;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CHistoryPosition::SelectPositionByAnyDealTicket(const ulong any_deal_ticket)
{
   if (!HistorySelect(0, TimeCurrent()))
      return false;
   CDealInfo deal;
   for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
      if (deal.SelectByIndex(i) && deal.Ticket() == any_deal_ticket)
         return SelectPositionByID(deal.PositionId());
   return false;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CHistoryPosition::SelectPositionByID(const long position_id)
{
   CDealInfo deal;
   m_pos_id = position_id;
   m_in.Clear();
   m_out.Clear();
   if (!HistorySelectByPosition(m_pos_id))
      return false;
   for (int j = HistoryDealsTotal() - 1; j >= 0; j--)
   {
      if (deal.SelectByIndex(j))
      {
         CDealInfo *info = new CDealInfo();
         info.Ticket(deal.Ticket());
         if (deal.Entry() == DEAL_ENTRY_IN)
            m_in.Add(info);
         else
            m_out.Add(info);
      }
   }
   return (m_in.Total() > 0 && m_out.Total() > 0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CHistoryPosition::ToLog(void)
{
   string in_tickets = "[", out_tickets = "[";
   for (int i = 0; i < m_in.Total(); i++)
      in_tickets += (string)m_in[i].Ticket() + (i >= m_in.Total() - 1 ? "" : ", ");
   for (int i = 0; i < m_out.Total(); i++)
      out_tickets += (string)m_out[i].Ticket() + (i >= m_out.Total() - 1 ? "" : ", ");
   in_tickets += "]";
   out_tickets += "]";
   for (int i = 0; i < 5; i++)
      Print("-----------------------");
   printf("position_id: %lld", m_pos_id);
   printf("in tickets (%d) = %s", m_in.Total(), in_tickets);
   printf("out tickets (%d) = %s", m_out.Total(), out_tickets);
   printf("net price in = %s",DoubleToString(OpenPrice(),(int)SymbolInfoInteger(m_in[0].Symbol(),SYMBOL_DIGITS)));
   printf("net price out = %s",DoubleToString(ClosePrice(),(int)SymbolInfoInteger(m_in[0].Symbol(),SYMBOL_DIGITS)));
   for (int i = 0; i < 5; i++)
      Print("-----------------------");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CHistoryPosition::Compare(const CObject *node, const int mode = 0) const override
{
   const CHistoryPosition *other = node;
   if (this.PositionID() > other.PositionID())
      return 1;
   if (this.PositionID() < other.PositionID())
      return -1;
   return 0;
}
//+------------------------------------------------------------------+
double CHistoryPosition::OpenPrice(void)
{
   double res = 0.0;
   double volume = 0.0;
   for(int i=0;i<m_in.Total();i++)
   {
      res+= m_in[i].Price() * m_in[i].Volume();
      volume+= m_in[i].Volume();
   }
   res/=volume;
   return res;
}

double CHistoryPosition::ClosePrice(void)
{
   double res = 0.0;
   double volume = 0.0;
   for(int i=0;i<m_out.Total();i++)
   {
      res+= m_out[i].Price() * m_out[i].Volume();
      volume+= m_out[i].Volume();
   }
   res/=volume;
   return res;
}

 
nicholishen:

Here is an example.

Anti-advertising of MQL5-EA.

 
fxsaber:

Anti-advertising of MQL5-EA.

Thanks, but that's how it's done. 

Reason: