ones you have x trades, ex: 10 trades opened and closed, and stored in history automatically, use this script to read the history, and the accountBalance() when the trade were opened:
int trade_to_read = 10;
for (int i=0; i<trade_to_read; i++){
if ( OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)){
if ( OrderMagicNumber()==my_magic ){
double my_balance_history = StringToDouble( OrderComment() );
}
}
}
index, it depends on second parameter. https://docs.mql4.com/trading/orderselect
To find out from what list the order has been selected, its close time must be analyzed. If the order close time equals to 0, the order is open or pending and taken from the terminal open orders list. One can distinguish an opened order from a pending order by the order type. If the order close time does not equal to 0, the order is a closed...
my idea is to differentiate between program for open the trades and read the history.
something like that for open the trades:
ones you have x trades, ex: 10 trades opened and closed, and stored in history automatically, use this script to read the history, and the accountBalance() when the trade were opened:
index, it depends on second parameter. https://docs.mql4.com/trading/orderselect
thanks, heres what I did. I run this code
string comment = DoubleToString(AccountBalance(),2);
int trade_to_read = 10;
for (int i=0; i<trade_to_read; i++){
if ( OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)){
if ( OrderMagicNumber()==MAGICMA ){
double my_balance_history = StringToDouble( OrderComment() );
}
}
}
if (my_balance_history< 5000)
{Print(my_balance_history);
res=OrderSend(Symbol(),OP_SELL,lot,Bid,3,Bid+stoploss*Point,0,comment,MAGICMA,0,Red);
return;
}
when I look in the journal when running the code it prints out that my_balance_history is 0
Storing data in comments is bad practice, since comments can be overwritten by broker. I use this code to calculate historical balance - you can adopt it to work with x last trades instead of given date.
Storing data in comments is bad practice, since comments can be overwritten by broker. I use this code to calculate historical balance - you can adopt it to work with x last trades instead of given date.
I second this. Anything to do with order comments is almost guaranteed to fail. In order to track the account balance you just have to sort the orders by close time and then do the math backwards from the current balance.
#include <arrays/list.mqh>
class XOrder : public CObject {
int m_ticket;
public:
XOrder(int ticket=0){m_ticket=ticket;}
int ticket(){return m_ticket;};
void ticket(int ticket){m_ticket = ticket;}
double profit(){
if (!this.select())
return0;
returnOrderProfit() + OrderCommission() + OrderSwap();
}
datetime close_time() const {
if (!this.select())
returnWRONG_VALUE;
returnOrderCloseTime();
}
virtualint Compare(const CObject *node, constint mode=0) constoverride {
const XOrder *other = node;
if (this.close_time() > other.close_time())
return1;
if (this.close_time() < other.close_time())
return -1;
return0;
}
bool select() const {
returnOrderSelect(m_ticket, SELECT_BY_TICKET);
}
};
voidOnStart()
{
CList trades;
for (int i=OrdersHistoryTotal()-1; i>=0; --i) {
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderType() < 2) {
trades.Add(new XOrder(OrderTicket()));
}
}
trades.Sort(0);
double bal_now = AccountInfoDouble(ACCOUNT_BALANCE);
for (XOrder *order=trades.GetLastNode(); CheckPointer(order); order=order.Prev()) {
if (!order.select())
return;
double begin_bal = bal_now;
bal_now -= order.profit();
printf("The balance after %d closed was $%.2f -> $%.2f",
OrderTicket(), bal_now, begin_bal
);
}
}
I appreciate this is an old post but I was looking for an account balance rollback function - I ended up writing this, so I am posting it in case it helps someone in future.
For the original query, It is easy to modify to rollback for the last 10 trades - just use the same approach and loop back by 10 tickets.
e.g. for(int i = dealEndPoint; i > (dealEndPoint-10); i--)
R4tna C: I appreciate this is an old post but I was looking for an account balance rollback function - I ended up writing this, so I am posting it in case it helps someone in future.
Your code is for MQL5, but you are posting it on a thread in the MQL4 section. Your code will be useless for MetaTrader 4 users and coders.
Fernando Carreiro: Your code is for MQL5, but you are posting it on a thread in the MQL4 section. Your code will be useless for MetaTrader 4 users and coders.
Thanks for pointing that out - I was not able to see it was a MQL4 thread, is there a way to identify this?
so doubletostring(1) now means the accountbalance at the end of the last trade?
no, you need to use OrderSelect() specified MODE_HISTORY on parameter in order to access the history pool. Then you can use as :
double my_balance_history = StringToDouble( OrderComment() );
no, you need to use OrderSelect() specified MODE_HISTORY on parameter in order to access the history pool. Then you can use as :
double my_balance_history = StringToDouble( OrderComment())
My code so far
does parameter 1 in OrderSelect()mean AccountBalance of the last trade
My code so far
does parameter 1 in OrderSelect()mean AccountBalance of the last trade
my idea is to differentiate between program for open the trades and read the history.
something like that for open the trades:
ones you have x trades, ex: 10 trades opened and closed, and stored in history automatically, use this script to read the history, and the accountBalance() when the trade were opened:
index, it depends on second parameter. https://docs.mql4.com/trading/orderselect
my idea is to differentiate between program for open the trades and read the history.
something like that for open the trades:
ones you have x trades, ex: 10 trades opened and closed, and stored in history automatically, use this script to read the history, and the accountBalance() when the trade were opened:
index, it depends on second parameter. https://docs.mql4.com/trading/orderselect
when I look in the journal when running the code it prints out that my_balance_history is 0
what is my_balance_history if it prints out 0?
thank you
Now how can I add the numbers stored in my_balance_history togheter?
thanks
Storing data in comments is bad practice, since comments can be overwritten by broker. I use this code to calculate historical balance - you can adopt it to work with x last trades instead of given date.
Storing data in comments is bad practice, since comments can be overwritten by broker. I use this code to calculate historical balance - you can adopt it to work with x last trades instead of given date.
I second this. Anything to do with order comments is almost guaranteed to fail. In order to track the account balance you just have to sort the orders by close time and then do the math backwards from the current balance.
how to get the account balance of the last 10 trades?
thank you
I appreciate this is an old post but I was looking for an account balance rollback function - I ended up writing this, so I am posting it in case it helps someone in future.
For the original query, It is easy to modify to rollback for the last 10 trades - just use the same approach and loop back by 10 tickets.
e.g. for(int i = dealEndPoint; i > (dealEndPoint-10); i--)
Your code is for MQL5, but you are posting it on a thread in the MQL4 section. Your code will be useless for MetaTrader 4 users and coders.
Thanks for pointing that out - I was not able to see it was a MQL4 thread, is there a way to identify this?
I can delete my post if you prefer