How to store previous account balance into a variable?

 
if(Total_sell_pos() == 0 && Total_buy_pos() == 0) {
      double previous_balance = AccountBalance(); //usd1000
}

if (AccountEquity() > previous_balance + (previous_balance *0.05)){ //usd1000 + 50 = usd1050
      CloseSellOrders();
      CloseBuyOrders();
      Delete_Pendings();
}

if Equity more than usd1050 then delete pending and orders.

But why when run the code, it keep delete pending and orders immediately even when Equity is less than previous balance?

The following code is the problem, and I replace it :

AccountEquity() > previous_balance + (previous_balance *0.05)

with

AccountEquity() > 1050

then only it works. I did try to check the value :

double check_value = previous_balance + (previous_balance *0.05);
printf (check_value); //1050

May I know why I cannot use the following code?

AccountEquity() > previous_balance + (previous_balance *0.05)
 
Eng Keat Ang:


Before everything else, move the double declaration out of the 'if'...:

if(Total_sell_pos() == 0 && Total_buy_pos() == 0) {
      double previous_balance = AccountBalance(); //usd1000
}

like this:

double previous_balance = 0;

if(Total_sell_pos() == 0 && Total_buy_pos() == 0) {
      previous_balance = AccountBalance(); //usd1000
}

 
Seng Joo Thio:

Before everything else, move the double declaration out of the 'if'...:

like this:


ok done

 
Eng Keat Ang:

ok done

Er... problem solved, right?

 
Seng Joo Thio:

Er... problem solved, right?

not yet

 

You need to declare

double previous_balance

variable outside (the best - before) OnTick, or OnTimer (depends which one you use), in global section.

In that way it will keep the state of calculation on next algo iterations.

If you dont want to do this, add static keyword before double, like this:

static double previous_balance

This will have similar effect.

 
Eng Keat Ang:

not yet

Are these lines within the same function?

double previous_balance = 0;

if(Total_sell_pos() == 0 && Total_buy_pos() == 0) {
      double previous_balance = AccountBalance(); //usd1000
}

if (AccountEquity() > previous_balance + (previous_balance *0.05)){ //usd1000 + 50 = usd1050
      CloseSellOrders();
      CloseBuyOrders();
      Delete_Pendings();
}
If they are, it should work. If they are not, then you have to sort out the number of duplicate previous_balance declarations you must be having.
 
Bartlomiej Gorski:

You need to declare

variable outside (the best - before) OnTick, or OnTimer (depends which one you use), in global section.

In that way it will keep the state of calculation on next algo iterations.

If you dont want to do this, add static keyword before double, like this:

This will have similar effect.

Perfect! It works. You are my hero. Thank you very much.

 
  1.       double previous_balance = AccountBalance(); //usd1000
    

    That isn't the previous balance, it's the current balance. Previous must be calculated
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  2. if (AccountEquity() > previous_balance + (previous_balance *0.05)){ //usd1000 + 50 = usd1050
    
    This includes all open orders, not only orders on the current chart, not only orders from the EA. Code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

    profit=Σ OrderProfit+OrderCommission+OrderSwap

 
Eng Keat Ang:

Perfect! It works. You are my hero. Thank you very much.

You welcome :)
Reason: