Account Balance During Backtesting

To add comments, please log in or register
tyrenw461
44
tyrenw461  
Hi, I have an EA that uses lot sizes based on the account balance. During back testing I set an initial deposit of $1000 and it calculated the correct lot size(.16 lots) based off that value, but as the backtesting went on, the lot sizes never changed. During back testing the account balance would reach $3000 but would still use the same lot size calculated from the initial deposit of $1000. Could someone help me fix this to where the account balance will update during backtesting so that the lot sizes can be calculated?  Maybe somehow get the profit/loss from each trade and combine that with the initial deposit?
Alain Verleyen
36050
Alain Verleyen  
tyrenw461:
Hi, I have an EA that uses lot sizes based on the account balance. During back testing I set an initial deposit of $1000 and it calculated the correct lot size(.16 lots) based off that value, but as the backtesting went on, the lot sizes never changed. During back testing the account balance would reach $3000 but would still use the same lot size calculated from the initial deposit of $1000. Could someone help me fix this to where the account balance will update during backtesting so that the lot sizes can be calculated?  Maybe somehow get the profit/loss from each trade and combine that with the initial deposit?

In your EA code probably.

tyrenw461
44
tyrenw461  
Alain Verleyen:

In your EA code probably.

Yeah more than likely, I don''t have any ideas how to though. 

Alain Verleyen
36050
Alain Verleyen  
tyrenw461:

Yeah more than likely, I don''t have any ideas how to though. 

Check lines 112 to 127.
tyrenw461
44
tyrenw461  
Alain Verleyen:
Check lines 112 to 127.
extern int MagicNumber=10001;
extern double StopLoss=30;
extern double TakeProfit=0;
double money =(((AccountBalance()* 0.05) / 30.0 ) / 10.0);
extern int TrailingStop=5;
extern int Slippage=3;
datetime PreviousBarTime;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0) 
  {
     int result=0;
     if((Close[1]>Close[2])&&NewBar()) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,money,Ask,Slippage,0,0,MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((Close[1]<Close[2])&&NewBar()) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,money,Bid,Slippage,0,0,MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

bool NewBar()
{
if(PreviousBarTime<Time[0])
{
PreviousBarTime = Time[0];
return(true);
}
return(false); // in case if - else statement is not executed
}

b-b-b-but I only have .43 lines of code sir.

whroeder1
17944
whroeder1  
  1. tyrenw461: b-b-b-but I only have .43 lines of code sir.
    Your posted code has 112 lines

  2. double money =(((AccountBalance()* 0.05) / 30.0 ) / 10.0);
    This global variable is never updated. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.

tyrenw461
44
tyrenw461  
whroeder1:
  1. Your posted code has 112 lines

  2. This global variable is never updated. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.

yeah lol I was kidding about the whole .43 thing. So the code you replied back with I should change? What variable could I use instead that can be updated? And since this is an EA I don't use OnCalculate I'm assuming. 

Alain Verleyen
36050
Alain Verleyen  
tyrenw461:

b-b-b-but I only have .43 lines of code sir.

Good one

You deserve an answer. Move this line

double money =(((AccountBalance()* 0.05) / 30.0 ) / 10.0);

just after this one

  if( TotalOrdersCount()==0) 
Edit : also after the opening bracket {
tyrenw461
44
tyrenw461  
Alain Verleyen:

Good one

You deserve an answer. Move this line

just of this one

haha thanks man, works like a charm. 

To add comments, please log in or register