accoount balance history - page 2

To add comments, please log in or register
Adiasa
179
Adiasa  
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() );

Daniel Bouchard
61
Daniel Bouchard  
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

Adiasa
179
Adiasa  
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...
Daniel Bouchard
61
Daniel Bouchard  
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

Daniel Bouchard
61
Daniel Bouchard  
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

Marcin Madrzak
475
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.


   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);
nicholi shen
2388
nicholi shen  
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
      );
   }
}
12
To add comments, please log in or register