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.
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:
Build a collection (set) of position IDs from history
Use position ids to create an object containing all orders for that position.
Calculate the volume-weighted average price for all orders in, and out.
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 |//+------------------------------------------------------------------+voidOnStart()
{
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(constlong 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(constulong any_deal_ticket);
bool SelectPositionByID(constlong position_id);
//bool OpenPrice(double &value);double OpenPrice();
double ClosePrice();
void ToLog();
int Compare(const CObject *node, constint mode = 0) constoverride;
};
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+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(constlong element)
{
Sort();
if (Search(element) < 0)
return CArrayLong::InsertSort(element);
returnfalse;
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+long CHistoryPosition::PositionID() const
{
return m_pos_id;
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+bool CHistoryPosition::SelectPositionByAnyDealTicket(constulong any_deal_ticket)
{
if (!HistorySelect(0, TimeCurrent()))
returnfalse;
CDealInfo deal;
for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
if (deal.SelectByIndex(i) && deal.Ticket() == any_deal_ticket)
return SelectPositionByID(deal.PositionId());
returnfalse;
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+bool CHistoryPosition::SelectPositionByID(constlong position_id)
{
CDealInfo deal;
m_pos_id = position_id;
m_in.Clear();
m_out.Clear();
if (!HistorySelectByPosition(m_pos_id))
returnfalse;
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, constint mode = 0) constoverride
{
const CHistoryPosition *other = node;
if (this.PositionID() > other.PositionID())
return1;
if (this.PositionID() < other.PositionID())
return -1;
return0;
}
//+------------------------------------------------------------------+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;
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
this is still mql4, isnt it ??
MQL5.
MQL5.
thanks, will try ur way.
since there is no solution for ordercloseprice for mql5.
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
is there a another code if i dont want to use include #include <MT4Orders.mqh> ??
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.
Yeah there are a bunch of bugs.
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:
Here is an example.
Here is an example.
Anti-advertising of MQL5-EA.
Anti-advertising of MQL5-EA.
Thanks, but that's how it's done.