Australian (ASX) Stock Market Forum

Amibroker backtester settings with IB forex

Joined
30 November 2011
Posts
4
Reactions
0
Hello,

Looking for someone to help with Amibroker backtester settings to use/simulate Interactive Brokers forex if it`s overall possible to get precise results using Amibroker software. As most of us know that Interactive Brokers forex is actually cash market ( IdealPro ) there is no lots and probably this is the main reason why settings should be different and doesn`t work in normal way. Whole month trying to get AB backtester working. No results, lost completely. Checked Amibroker yahoo group, but unfortunately not much info about AB with IB forex there. Checked all possible docs from AB site ( inc. Amibroker backtester by ChrisB. Jan 2008 ) about forex all info and examples given is about normal forex.

What I wanna do is to trade main fx pairs basket - eurusd, gbpusd, usdjpy, usdcad, usdchf, audusd, nzdusd, eurchf, eurgbp and later maybe to add a few more. My base currency is GBP. To get life easier I`d like to trade in "virtual lots" which is IB IdealPro minimum position size required (e.g. EURxxx pairs - 20K, AUDxxx, USDxxx - 25K, GBPxxx - 17K, NZDxxx - 35K ).
Also there are some questions regarding IB commissions. It`s $2.49/per trade and always are converted to base currency in reality, but fx rates(GBPUSD) changing time after time and in my case probably there is no chance to get right commissions.
Also I doubt that interest can be included into AB backtest as Interactive Broker always buys one currency and sells other as well as P&L after every trade stays in it`s respective currency, so, it means interest counts all the time when position open and when it closed also from your Profit or Loss.
I`d like to add ~0.0001 spread into account as well to get more realistic results, but I think I saw an example somewhere in AB group, so, I`ll sort it out myself later. Now I want to get the main backtester settings (margin deposit, point value, position size and other important settings) correctly working with Interactive Brokers forex (cash) market.

A few details and some info from Interactive Brokers:
Interactive Brokers Regulation T margin type account Buying Power is 4:1, but I`m not sure it works for forex and futures. Should I set AB Account Margin to 25 ?
Margin Requirement for USD, EUR, JPY, GBP, AUD, CAD, CHF - 2.5%. NZD - 3.33%
They calculate it this way: Margin Requirement % = 1/Leverage Rate. (Leverage Rate for all currencies mentioned above - 40:1, nzd - 30:1).

So, if you buy/sell EURUSD - 20K(reminder!!!min. amount to buy in IDEALPro) they take deposit EUR500 and convert it to your base currency ( e.g. today was GBP418 ), if you buy/sell USDCHF - 25K, deposit is $625(US dollars), converted from USD today was GBP398. They always convert deposit to Base currency!!!
One thing to mention : if you BUY EURGBP - 20K, deposit was 0 (zero, I guess its because I have enough money to cover 20K euros from my base cash otherwise would be deposited, I`m not sure here???), when you SELL EURGBP - 20K, deposit GBP418 converted to base from EUR as usually. Same with GBPUSD pair (17K min.IdealPro) only vise versa, if you BUY it deposit was GBP425, if SELL it - no deposit taken (zero).No idea how to sort all those things out in Amibroker.

Btw, as I now if you set AB symbol info window currency for e.g. if you trade EURUSD ( currency settings should be set to USD ) or e.g USDJPY pair ( currency set to JPY ), but in reality EURUSD - you buy/sell 20K euros and deposit counted in euros and then converted to base currency in my case GBP, or other example buy/sell USDJPY 25K, deposit counted in USD and then converted to GBP. So, its nothing to do in first example with USD, in second with JPY as by settings in AB. Amibroker as I understand takes deposit in USD in first example and later probably converts to base currency, accordingly in second example it takes deposit in JPY and should convert to base. So, it works differntly comparing with Interactive Brokers. If I`m wrong somewhere please correct me.

Any help would be really appreciated.

Thank you in advance.
 
IAB FX margin is 2.5% so MarginDeposit = -2.5;

Sample settings

SetOption("AllowPositionShrinking", False);
SetOption("AllowSameBarExit", True);
SetOption("FuturesMode", 1);
SetOption("InitialEquity", 100000);
SetOption("InterestRate",0);
SetOption("MaxOpenPositions", 1);
SetOption("MinPosValue", 0);
SetOption("MinShares", 20000);
SetOption("PriceBoundChecking", True );
SetOption("ReverseSignalForcesExit", False);
SetOption("UsePrevBarEquityForPosSizing", False );
SetTradeDelays(0, 0, 0, 0);

SetPositionSize( 20000, spsShares ); // min lot at Interactive Brokers
MarginDeposit = -2.5; // 100: 1 leverage, 1%
PointValue = 1;
RoundLotSize = 1;
Ticksize = 0.0001; //as for EUR.USD, USD.JPY would be 0.01

And there are messages on the AB Yahoo list regarding Forex and Interactive Brokers.
You just didn't look carefully.

There is also at least one message not at the main list but at Yahoo AmiBroker-at in regards to IAB and FX by T.J. http://finance.groups.yahoo.com/group/AmiBroker-at/message/4109

If there should be any other difficulties you should also contact support. support@amibroker.com
 
Additional note besides others ... i.e. in Symbol Information window set Currency to USD for EUR.USD and to JPY for USD.JPY and so on ....
 
There is also at least one message not at the main list but at Yahoo AmiBroker-at in regards to IAB and FX by T.J. http://finance.groups.yahoo.com/group/AmiBroker-at/message/4109

from there:

Wrong.
EUR.USD is denominated in USD and deposit is in USD, not in EUR.
See: http://finance.groups.yahoo.com/group/amibroker/message/134212

Also, you can't buy 2500 EUR with IB using IDEALPRO.
The minimum amount is 25000. 100000 is normal lot.

The formula and settings you are using is simply incorrect.

EUR.USD should NOT have EUR entered as currency in the information box.
This symbol is denominated in USD.

Now, you need to make up your mind whenever you want
to treat IB FOREX as "regular" symbol (then a "share" is just one euro and
your position size should be 100000 "shares" for 100000 Euros) and margin
deposit set to 2.5%,
or whenever you want to treat it as "contract" where 1 contract represents
100000 EUR.

Note that IB IDEALPRO is not "regular" forex dealer, i.e. they do not trade
treat one lot as one contract.
See this:
http://www.elitetrader.com/vb/showthread.php?s=&postid=2107439&highlight=idealpr\
o+ideal#post2107439
and this:
http://www.elitetrader.com/vb/showthread.php?s=&threadid=129325&highlight=idealp\
ro+ideal
they treat opened positions as actual cash and you can earn interest on it
so after few days you will get some "remainings" of different currencies on your
account.
That is one of the reasons why it is better to use currency futures instead of
forex when trading @IB.

In first case:
FOREX MarginDeposit can be defined as percentage. For example
if you are using 1:100 margin you should use MarginDeposit = -1; // 1%
if you are using 1:50 margin, you should use MarginDeposit = -2; // 2%

In case of IB, their forex margin is 2.5% so
MarginDeposit = -2.5;
SetPositionSize( 100000, spsShares ); // in that case one "share" is one euro

For the second case:
please read this:
http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/
 
Another one from here http://finance.groups.yahoo.com/group/amibroker/message/134235

Did you read what I wrote on -at list?
Interactive Brokers is NOT a typical Forex dealer.
The instructions on forex on KB were mere example of one of many possibilities
and actual settings of course vary from broker/dealer to broker/dealer as each broker/dealer on this
planet sets its own margin rules.

Interactive Brokers do not operate on full 100'000 lots only (they allow you to buy for example 25000 EUR) and
report actual CASH positions, instead of lots. If you actually DO the trading with them
you will quickly find out that you end up with EUR33.12 and JPY2314 "remainings" on your account
if you trade EURUSD or JPY because of the fact that those CASH positions earn interest.
That would never happen with true *Forex* dealer that would do overnight rollover.
Also IB does NOT use fixed deposit. They use percentage deposit (2.5%),
therefore you should always use MarginDeposit = -2.5 as negative sign means percent.

As instructed on the -at list the settings for IB are

SetPositionSize( 100000, spsShares );
MarginDeposit = -2.5;

and currency set to USD.

Typical Forex dealer as GFT trades in 100'000 lots and *reports* lots, unlike IB.
That's why you can use 1 "share" = 1 lot paradigm.

Of course you can insist on using lots with IB, however, it is not the way they operate.

You are so much concerned about deposit which in fact does not matter at all because
it is eventually returned in full to your account once you close the position, so dollar profit
does NOT depend on deposit (only percent profit does).
However if you set incorrect currency for the symbol, you are risking gettting your profits incorrect
because your profits will be transfered back to base currency via conversion.

Best regards,
Tomasz Janeczko
amibroker.com
 
Hi Joshmi,

Thanks for trying to help me. Really appreciated.
Yes, I`ve read those topics in amibroker group before a few weeks ago (except elitetrader, I`ll do it tomorrow), but problem is I couldn`t set my AB correctly anyway following those instructions. Example from ab group and you gave me here don`t work for me at all, it doesn`t open any trades. I tryed exactly the same settings you wrote here, but without success. Tryed to change some setting as well, no success. Probably it confused me even more, because I couldn`t find the reason why.
However, finally I gave up and started building it my own way. And yesterday looks like I found what I wanted. Still checking some details.
Settings I`m trying to use are :

SetOption("InitialEquity", 100000 );
SetOption("MinShares",1);
SetOption("MinPosValue", 1);
SetOption("FuturesMode",True);
RoundLotSize = 1;
MDall = (2.5*PointValue)/100;
MDnzd = (3.33*PointValue)/100;
MarginDeposit = IIf(Name()=="NZDUSD.COMP" OR Name()=="NZDUSD.COMP_1",MDnzd,MDall);
PointValue = IIf(Name()=="USDCHF.COMP" OR Name()=="USDCHF.COMP_1" OR Name()=="USDCAD.COMP" OR Name()=="USDCAD.COMP_1" OR Name()=="AUDUSD.COMP" OR Name()=="AUDUSD.COMP_1",25000,
IIf(Name()=="EURUSD.COMP" OR Name()=="EURUSD.COMP_1" OR Name()=="EURGBP.COMP" OR Name()=="EURGBP.COMP_1" OR Name()=="EURCHF.COMP" OR Name()=="EURCHF.COMP_1",20000,
IIf(Name()=="GBPUSD.COMP" OR Name()=="GBPUSD.COMP_1",17000,
IIf(Name()=="NZDUSD.COMP" OR Name()=="NZDUSD.COMP_1",35000,
IIf(Name()=="USDJPY.COMP" OR Name()=="USDJPY.COMP_1",25000,0)))));
TickSize = IIf(StrFind(Name(),"JPY"),0.01,0.0001);
PositionSize = MarginDeposit;
MaxPos = Optimize("MaxPos",1,1,5,1);
SetOption( "MaxOpenPositions", MaxPos );

Filter = 1;
AddColumn(GetOption("FuturesMode"),"FM",1.0);
AddColumn(RoundLotSize,"RLS",1.2);
AddColumn(MarginDeposit,"MarginDeposit");
AddColumn(PointValue,"PointValue");
AddColumn(TickSize,"TickSize",1.4);
AddColumn(PositionSize,"PositionSize");

SetOption("AllowPositionShrinking", False);
SetOption("ActivateStopsImmediately",False);
SetOption("ReverseSignalForcesExit",False);
SetOption("AllowSameBarExit",True);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",2.50);
SetOption("InterestRate",0);
SetOption("AccountMargin",100);
SetOption("PriceBoundChecking",True);
SetOption("UsePrevBarEquityForPosSizing", False );

SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;
ApplyStop(stopTypeLoss,stopModePercent,0.7,1,False,1);
ApplyStop(stopTypeProfit,stopModePercent,3.1,1,False,1);

Base currency USD. All setup in Tools-Preferences-Curreencies.
Also All currencies set up in symbol info currency field following instructions from amibroker group(e.g. USDCAD-CAD,EURGBP-GBP,EURUSD-USD and etc.)

I`ve got one question here. Does AB calculate Position Value and Profit in Result List in base currency, in currency denominated in symbol info (e.g. USDCHF pair, currency CHF) or always in USD?

And please, could you have a look at my settings above I`m trying to use (still testing) and would like to here some comments about it.

Btw, backtester results for USDJPY symbol for some reason are wrong (unrealistic). Trying to find out why.

Thank you
 
Margin Deposit must be -2.5! NOT +2.5
Just write MarginDeposit = -2.5; Nothing else needed.
Or just write MDall = -2.5;
And MDnzd = -3.33;
Have you tried PointValue = 1;?

You can shorten the Margindeposit line by using StrFind.
Example MarginDeposit = IIf(StrFind(Name(), "NZDUSD"),MDnzd,MDall);
Same goes for Pointvalue

But you would not need that because you can automatically import that stuff using an ASCII import format file.

Example

$FORMAT Name,FullName,CURRENCY,WATCHLIST,MARKET,GROUP,INDUSTRY,TICKSIZE
$SKIPLINES 0
$BREAKONERR 1
$AUTOADD 1
$NOQUOTES 1
$OVERWRITE 1
$SEPARATOR ,

You can then import a csv file that provides data according to my sample format file

Example of a csv file

AUDCAD,AUDCAD,CAD,0,2,0,48,0.0001
AUDJPY,AUDJPY,JPY,0,2,0,48,0.01
AUDNZD,AUDNZD,NZD,0,2,0,48,0.0001
AUDUSD,AUDUSD,USD,0,2,0,48,0.0001
CADCHF,CADCHF,CHF,0,2,0,48,0.0001
CADJPY,CADJPY,JPY,0,2,0,48,0.01
CHFJPY,CHFJPY,JPY,0,2,0,48,0.01
EURAUD,EURAUD,AUD,0,2,0,48,0.0001
EURCAD,EURCAD,CAD,0,2,0,48,0.0001
EURCHF,EURCHF,CHF,0,2,0,48,0.0001
EURGBP,EURGBP,GBP,0,2,0,48,0.0001
EURJPY,EURJPY,JPY,0,2,0,48,0.01
EURNZD,EURNZD,NZD,0,2,0,48,0.0001
EURUSD,EURUSD,USD,0,2,0,48,0.0001
GBPAUD,GBPAUD,AUD,0,2,0,48,0.0001
GBPCAD,GBPCAD,CAD,0,2,0,48,0.0001
GBPCHF,GBPCHF,CHF,0,2,0,48,0.0001
GBPJPY,GBPJPY,JPY,0,2,0,48,0.01
GBPNZD,GBPNZD,NZD,0,2,0,48,0.0001
GBPUSD,GBPUSD,USD,0,2,0,48,0.0001
NZDCAD,NZDCAD,CAD,0,2,0,48,0.0001
NZDJPY,NZDJPY,JPY,0,2,0,48,0.01
NZDUSD,NZDUSD,USD,0,2,0,48,0.0001
USDCAD,USDCAD,CAD,0,2,0,48,0.0001
USDCHF,USDCHF,CHF,0,2,0,48,0.0001
USDDKK,USDDKK,DKK,0,2,0,48,0.0001
USDJPY,USDJPY,JPY,0,2,0,48,0.01
XAUUSD,XAUUSD,USD,0,2,0,48,0.01

If you want to add POINTVALUE and MARGINDEPOSIT just add it

$FORMAT Name,FullName,CURRENCY,WATCHLIST,MARKET,GROUP,INDUSTRY,MARGINDEPOSIT,TICKSIZE,POINTVALUE
$SKIPLINES 0
$BREAKONERR 1
$AUTOADD 1
$NOQUOTES 1
$OVERWRITE 1
$SEPARATOR ,

So the first line from csv example would change from

AUDCAD,AUDCAD,CAD,0,2,0,48,0.0001

to

AUDCAD,AUDCAD,CAD,0,2,0,48,-2.5,0.0001,1

So I've used MarginDeposit -2.5 and pointvalue 1 here

Aussiestockforums splits the first line of my sample format file. It must be INDUSTRY not IND USTRY. Its all one line till POINTVALUE. Same for the first example


Save the format file sample as IAB_FX_PAIRS.format
.format is the file type.
Additionally go to the FORMATS Folder of AB and open import.types file and add the line
IAB FX Pairs (*.csv)|*.csv|IAB_FX_Pairs.format

Then go to File>Import ASCII of AB Menu bar and choose IAB_FX_PAIRS as file type and browse to the csv file
 
Even if you input MarginDeposit -2.5 and Pointvalue 1 in Information window of AB you can still use

MDall = (2.5*PointValue)/100;
MDnzd = (3.33*PointValue)/100;
MarginDeposit = IIf(StrFind(Name(), "NZDUSD"),MDnzd,MDall);

if(StrFind(Name(), "USDCHF") OR StrFind(Name(), "USDCAD") OR StrFind(Name(), "USDJPY" ) OR StrFind(Name(), "AUDUSD") )
PointValue = 25000;
else if(StrFind(Name(), "EURUSD") OR StrFind(Name(), "EURGBP") OR StrFind(Name(), "EURCHF") )
PointValue = 20000;
else if (StrFind(Name(), "GBPUSD") )
PointValue = 17000;
else if(StrFind(Name(), "NZDUSD") )
PointValue = 35000;

Because code settings overwrite settings of Information window.
Same for SetOption function that overwrites settings of AA preferences.
 
Hi Joshsmi,

Thanks for you suggestions about auto import using ASCII import format file. I`ll try it.
About PointValue set to 1, yes, I`ve tryed it using your example. It didnt`t work for me, I don`t know why. Maybe there is problems with other parameters. No idea.
About MarginDeposit = -2.5, that was what I`ve tryed straight away following amibroker group instructions couple weeks ago, played with other settings at the same time changing them as well it didn`t work for me(tryed using it in AFL and in symbol info, I know that AFL owerwrite settings). My guess would be if we set MarginDeposit to -2.5 backtester thinks it is 2.5% from Equity or etc. I need deposit to be 2.5% from open position(e.g. EURUSD-buy 20K, 2.5% will be 500, that`s exatcly what I need).

Thank you
 
I suggest sending the whole code of yours to support and ask them. It could be a different problem in your code, a wrong line. They know it better than me. Also I don't use Interactive brokers. Also you could ask at yahoo groups of AB. There are users who trade with IAB. Report back please if you have found out the reason.
 
Thanks Joshsmi,

I`ve sent an email to support a few mins ago with all my settings and hopefully I`ll get an answer soon.
What was very interesting for me that there is not much info about IB cash market backtesting, actually only these you mentioned in this topic. Sometimes I think that nobody using forex with IB because of its complexity. Most people probably prefer "real" forex brokers I guess :)
However, if I get it working properly, I`ll definitely post all my settings here to brake that uncertainty.

Thank you
 
Top