accoount balance history - page 2

 
Daniel Bouchard:
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() );

 
Adiasa:

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())

string  comment = DoubleToString(AccountBalance(),2); double my_balance_history = StringToDouble( OrderComment() ); if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==True);

res=OrderSend(Symbol(),OP_SELL,lot2,Bid,3,Ask+stoploss*Point,0,comment,MAGICMA,0,Red);

My code so far

does parameter 1 in OrderSelect()mean AccountBalance of the last trade

 
Daniel Bouchard:

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:

string  comment = DoubleToString(AccountBalance(),2);

res=OrderSend(Symbol(),OP_SELL,lot2,Bid,3,Ask+stoploss*Point,0,comment,MAGICMA,0,Red);

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

OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
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...
 
Adiasa:

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 

what is my_balance_history if it prints out 0?


thank you

 
this code will print the  account balance of your last 10 trades.
string comment=DoubleToString(AccountBalance(),2);
   int trade_to_read=OrdersHistoryTotal()-11;

   for(int i=OrdersHistoryTotal(); i>trade_to_read; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderMagicNumber()==MAGICMA)
           {
           double my_balance_history=StringToDouble(OrderComment());
            if ( my_balance_history!=0 ) Print(my_balance_history);
          }
        }
     }

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.


   int total=OrdersHistoryTotal()-1;

   for(int i=total;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderCloseTime()>=date_start_ && OrderType()<2)
           {
            profit+=OrderProfit()+OrderCommission()+OrderSwap();
           }
     }
   return NormalizeDouble(AccountBalance()-profit,2);
 
Marcin Madrzak:

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())
         return 0; 
      return OrderProfit() + OrderCommission() + OrderSwap(); 
   }
   datetime close_time() const {
      if (!this.select()) 
         return WRONG_VALUE;
      return OrderCloseTime();
   }
   virtual int Compare(const CObject *node, const int mode=0) const override {
      const XOrder *other = node;
      if (this.close_time() > other.close_time())
         return 1;
      if (this.close_time() < other.close_time())
         return -1;
      return 0;
   }
   bool select() const { 
      return OrderSelect(m_ticket, SELECT_BY_TICKET);
   }
   
};


void OnStart()
{
   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
      );
   }
}
 
Daniel Bouchard:

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--)

//NOTE: MQL5 code
//+------------------------------------------------------------------+
struct Struct_AccInfo
//+------------------------------------------------------------------+
  {
   datetime          startTime;
   datetime          endTime;
   double            DealsSumUSD;
   double            SwapSumUSD;
   double            FeeSumUSD;
   int               dealStartPoint;
   int               dealEndPoint;
   int               ticketCount;
   double            accProfitUSD;
   double            accDepositUSD;
   double            accBalanceUSD;
   double            aveProfitPerTicket;

   //+------------------------------------------------------------------+
   void              Struct_AccInfo() //Constructor
   //+------------------------------------------------------------------+
     {
      getDepositOnDate();
     }

   //+------------------------------------------------------------------+
   double            getDepositOnDate(datetime iv_periodStart = 0)
   //+------------------------------------------------------------------+
     {
      startTime          = iv_periodStart;
      endTime            = TimeLocal();
      DealsSumUSD        = 0;
      SwapSumUSD         = 0;
      FeeSumUSD          = 0;
      dealStartPoint     = 0;
      dealEndPoint       = 0;
      ticketCount        = 0;
      accProfitUSD       = 0;
      accDepositUSD      = 0;
      accBalanceUSD      = AccountInfoDouble(ACCOUNT_BALANCE);
      aveProfitPerTicket = 0;

      if((startTime != 0) && (startTime < endTime)) //Only execute if valid dates entered - otherwise default to entire period
        {
         HistorySelect(0, iv_periodStart);
         dealStartPoint = HistoryDealsTotal();
        }

      HistorySelect(0, endTime);
      dealEndPoint = HistoryDealsTotal();

      long dealID = 0;
      ENUM_DEAL_ENTRY entry;

      for(int i = dealEndPoint; i >= dealStartPoint; i--)
        {
         dealID  = (long)HistoryDealGetTicket(i);
         entry = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(dealID, DEAL_ENTRY);
         if(entry != DEAL_ENTRY_IN)
           {
            DealsSumUSD += HistoryDealGetDouble(dealID, DEAL_PROFIT);
            SwapSumUSD  += HistoryDealGetDouble(dealID, DEAL_SWAP);
            FeeSumUSD   += HistoryDealGetDouble(dealID, DEAL_FEE);
            ticketCount++;
           }
         else
           {
            startTime   = (datetime) HistoryDealGetInteger(dealID, DEAL_TIME);
           }
        }

      accProfitUSD  = DealsSumUSD + SwapSumUSD + FeeSumUSD;
      accDepositUSD = accBalanceUSD  - accProfitUSD;
      aveProfitPerTicket = (ticketCount > 0) ? (accProfitUSD / ticketCount) : 0;
      return(accDepositUSD);
      //+------------------------------------------------------------------+
     } //end - getDepositOnDate()
   //+------------------------------------------------------------------+

   //+------------------------------------------------------------------+
  }; //end - Struct_AccInfo
//+------------------------------------------------------------------+
 
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?

I can delete my post if you prefer