Evaluating Account Balance

 

Does anyone have code or an idea how to evaluate the accountbalance(for example, at the start of the day or some hour) and set that value to be used later?


For example:


if(Hour()==0) get accountbalance and set that to a value like SB1(starting balance)


then if equity = SB1 + arbitrary value(such as 2% for example), then close all open positions and disable trade.



This would be extremely helpful for me! Thank you all for your consideration..... Daniel

 
forexman05:

Does anyone have code or an idea how to evaluate the accountbalance(for example, at the start of the day or some hour) and set that value to be used later?


For example:


if(Hour()==0) get accountbalance and set that to a value like SB1(starting balance)


then if equity = SB1 + arbitrary value(such as 2% for example), then close all open positions and disable trade.



This would be extremely helpful for me! Thank you all for your consideration..... Daniel

If I am not mistaken...


extern int Check_Hour=2; //Hour to check Account balance

extern double arbitrary_value=2.0; // value of account balance in %


double sb1,sb2,arbitrary_value1; // declare variables


arbitrary_value1=(AccountBalance()/100)*arbitrary_value; // Work out the % of your account you want it to grow by

sb2=sb1+arbitrary_value1; // Account balance + %growth


if(Hour()==Check_Hour){AccountBalance()=sb1;}

if(AccountEquity()>=sb2){OrderClose(...); // If Account equity is greater than (in case of gaps) or = account balance + % growth...then close orders

 
forexman05:

Does anyone have code or an idea how to evaluate the accountbalance(for example, at the start of the day or some hour) and set that value to be used later?


For example:


if(Hour()==0) get accountbalance and set that to a value like SB1(starting balance)


then if equity = SB1 + arbitrary value(such as 2% for example), then close all open positions and disable trade.



This would be extremely helpful for me! Thank you all for your consideration..... Daniel


It sounds as though you're after something like the partial example in the attached file. The key is to compare the current time to the time of the previous tick, and see if they're in different days, rather than using something such as Hour() == 0 which contains an assumption that the first tick of the day will be within the first hour (or minute, or second, or however fine-grained you make the comparison).

 

if(Hour()==Check_Hour){AccountBalance()=sb1;} 


... Adding to my earlier message, the problem with a comparison such as (Hour() == Whatever) is that it will be met throughout the hour rather than just at the start of it. The storage of the account balance will end up containing the value at the end of the hour, rather than at the start of the day/period. You could do a further check along the lines of "is Hour() equal to what I want, and is it also different to the last value I saw?", but it's still more generally reliable to compare the current time to the time of the most recent tick.

 
jjc:


... Adding to my earlier message, the problem with a comparison such as (Hour() == Whatever) is that it will be met throughout the hour rather than just at the start of it. The storage of the account balance will end up containing the value at the end of the hour, rather than at the start of the day/period. You could do a further check along the lines of "is Hour() equal to what I want, and is it also different to the last value I saw?", but it's still more generally reliable to compare the current time to the time of the most recent tick.



Could this work?


extern double PT=200;//profit target for daily trades
extern double SB1=10000;//starting account balance for evaluation
int Slippage=5;
int i;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+


int start()
{
double SB2;//account balance + % profit in dollars
SB2 = SB1 + PT;
if (AccountEquity() >= SB2)
{
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close pending orders
case OP_BUYLIMIT : result = OrderDelete( OrderTicket() );
case OP_BUYSTOP : result = OrderDelete( OrderTicket() );
case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );

//Close opened positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Blue);
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Blue);
}

if(result == false)
{
Sleep(1000);
}
}
Print ("All Trades Have Been Closed");
Comment("Risk has been removed..all trades closed!");
return(0);
}

 
forexman05:

Could this work?

[...]


Looks largely fine in terms of trade closure. However, from sight, and without actually trying it out, two potential problems spring to mind:


  • In any potentially long-running piece of code, you need a RefreshRates(). Otherwise, the price returned by MarketInfo() for trade-closures may be out of date, and closures will fail.
  • If a closure does fail you're recording the fact in the variable result, going into a sleep to give the problem a chance to remedy itself, and then... ignoring the problem and moving on. Let's say that there are 10 orders at outset. You start processing at number 9. Closure fails, and you do a Sleep(1000). However, after that you'll then go round the for loop again, and i will get decremented to 8. Therefore, trade number 9 will be left open. If you want the code to have another go after a failure, you need to do something like i++ in the same section where you do the sleep (so that i gets incremented, and then decremented again immediately, leaving it at 9 on the next pass round the loop). However, if you do these retries, then you definitely need a RefreshRates(). Otherwise, you can end up in an infinite loop where a trade closure keeps failing over and over again because the price is out of date. 

Could be wrong about this; not actually tried out the code.


And, being very pedantic, you've not declared the loop variable i.

 
jjc:


Looks largely fine in terms of trade closure. However, from sight, and without actually trying it out, two potential problems spring to mind:

[...]


... Oh, and also... Unless MQL departs rather more from the C language than I think it does, you need a break statement for each case in the switch block. Otherwise, processing "falls through" from one case to another. For example if type is OP_BUYLIMIT then the code will end up trying to delete the same order 4 times, and then close it twice.


If that's not clear, try out the following in a script and see what get's displayed versus what you might be expecting:


   int j = 1;

   switch (j) {

      case 1: MessageBox("Case 1");

      case 2: MessageBox("Case 2");

   }


 
jjc:


... Oh, and also... Unless MQL departs rather more from the C language than I think it does, you need a break statement for each case in the switch block. Otherwise, processing "falls through" from one case to another. For example if type is OP_BUYLIMIT then the code will end up trying to delete the same order 4 times, and then close it twice.


If that's not clear, try out the following in a script and see what get's displayed versus what you might be expecting:


   int j = 1;

   switch (j) {

      case 1: MessageBox("Case 1");

      case 2: MessageBox("Case 2");

   }


JJC is quite right in terms of this piece of code.

However, just a slight amendment when thinking about the switch command in a more generic context. For each case in the switch block you need a break OR A RETURN on the assumption that you wish to execute just one line of the switch block, rather than the corresponding case line and all subsequent lines.

Reason: