Backtest - Calculate GBPUSD rate on an EURGBP chart when I use USD account.

 

Hi Programmers!


I would like to ask your help for Backtest of my Expert Advisor.

The currency of my account is USD. The Backtest runs on a 1 minute EURGBP chart. These different currencies are the resultant of my problem.


My EA opens a trade with 1 lot and 100 points stop loss and take profit levels. I would like to calculate in advance the amont of the stop loss and take profits in USD. That's why I need the GBPUSD rate.


Thought I calculate the rate with the help of AccountFreeMarginCheck() function. I tried this code:

double GBPUSD_rate ;

if ( AccountFreeMargin() - AccountFreeMarginCheck(Symbol(),OP_SELL,0.1) != 0 )
      GBPUSD_rate = 10 * ( AccountFreeMargin() - AccountFreeMarginCheck(Symbol(),OP_SELL,0.1)) / (1000*Ask) ;
else  GBPUSD_rate = 10 * ( AccountFreeMargin() - AccountFreeMarginCheck(Symbol(),OP_BUY ,0.1)) / (1000*Ask) ;

But this calculation give me diffrent rate result than the backtest use. When I run the backtest at 2008.01.08 my calculation gave 1.6967 rate. And when I saw the results of my account of the backtest, there used 1.5012 rate. Its so much diffrence.


So, how can I get the right GBPUSD rate in the backtest when I use USD account.


Thanks in advance.


Relative

 
Try using iMA indicator for GPBUSD with period 1.
 

What do you mean GBPUSD_rate ?

Price?

GBPUSD_Price = iClose(GBPUSD, 0,iBarShift(GBPUSD, 0, Time[0]));

 
Roger:
Try using iMA indicator for GPBUSD with period 1.

Don't know how do you think. How does the iMA indicator come here?

 
phy:

What do you mean GBPUSD_rate ?

Price?

GBPUSD_Price = iClose(GBPUSD, 0,iBarShift(GBPUSD, 0, Time[0]));

Yes, I would like to get the GBPUSD price.


I tried the iClose function, but it doesn't work. It gives 0 all time.


Here is an example: After I ran the backtest I got this trades result:


#                Time        Type   Order    Size    Price   S/L     T/P     Profit   Balance
1	2008.01.02 21:45	sell	1	4.60	0.7436	0.7526	0.7425	0.00	 10000.00
2	2008.01.03 01:29	t/p	1	4.60	0.7425	0.7526	0.7425	780.72	 10780.72
3	2008.01.03 16:00	sell	2	5.00	0.7459	0.7549	0.7448	0.00	 10780.72
4	2008.01.03 19:39	close	2	5.00	0.7476	0.7549	0.7448	-1276.70 9504.02
5	2008.01.03 19:39	sell	3	4.40	0.7474	0.7564	0.7463	0.00	 9504.02
6	2008.01.04 07:40	close	3	4.40	0.7471	0.7564	0.7463	204.86	 9708.89
7	2008.01.07 17:54	buy	4	4.50	0.7453	0.7363	0.7464	0.00	 9708.89
8	2008.01.08 00:18	t/p	4	4.50	0.7464	0.7363	0.7464	718.36	 10427.25

If you see the first order, you can calculate the GBPUSD rate like this: 780.72 / ( 0.7436 - 0.7425 ) / 10 / 4.6 = 1.5036

Second order: 1276.70 / ( 0.7459 - 0.7476 ) / 10 / 5.0 = 1.5020

Third order: 204.86 / ( 0.7474 - 0.7471 ) / 10 / 4.4 = 1.5520

Fourth order: 718.36 / ( 0.7453 - 0.7464 ) / 10 / 4.5 = 1.4512


I would like to calculate these rates in advance by the EA.


I hope I was understandable.

 
Relative wrote >>

Don't know how do you think. How does the iMA indicator come here?

Actually it is a trick. Tester doesn't want to work with alien currency, and iMA with period 1 is very close to real prices of this currency. In your case you better use phy advice, just fix a little:

GBPUSD_Price = iClose("GBPUSD", 0,iBarShift("GBPUSD", 0, Time[0]));

 

This works ( in an EA) if the history is available.

First, open a GBPUSD chart 1m timeframe, and note the earliest availble chart bar.

Start Strategy Tester in Visual Mode on 1m timeframe at that date, and the price of GBPUSD is valid in the comment.

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   double gbpPrice = iClose("GBPUSD", 1, iBarShift("GBPUSD", 1, Time[0]));
   Comment(" GBPUSD is now ", DoubleToStr(gbpPrice, 4), " at ", TimeToStr(Time[0], TIME_DATE|TIME_SECONDS));
   return(0);
  }
//+------------------------------------------------------------------+

 

Thank you Roger and Phy the help.


After I download the whole GBPUSD 1 minute chart data from the history center, both of your advice worked and give me the correct GBPUSD price.


Your calculations give about 1.920~1.930 prices in that times.

But that prices aren't equal with the prices are used in the Trades Result list - what I showed you above. There are 1.5036, 1.5020, 1.5520, 1.4512.


I think these prices depend on the recent GBPUSD price in the database. So, while I run my test in the Januar of 2008, the GBPUSD price is the recent in 18 of November. But, this isn't sure. Because there is relative big diffrence between that prices. For example the 1.5520 and 1.4512.


So, I don't know yet how does this work...

Reason: