EntryFxRate and ExitFxRate logic

I am currently evaluating the free trial version of AmiBroker 6.00.2. I am interested in forex backtesting.

I have set up USDCHF symbol, in Symbol Information the currency is set to CHF, and the same USDCHF symbol is used for dynamic rate (with inverse flag) in the Currencies settings.

I use the following test code to evaluate the numbers, this code adds EntryFxRate and ExitFxRate custom metrics to trades. There is a single trade to keep the test simple:

SetCustomBacktestProc( "" );

if( Status( "action" ) == actionPortfolio )
    bo = GetBacktesterObject();
    bo.Backtest( True );

    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
        trade.AddCustomMetric( "EntryFxRate", trade.EntryFxRate, 5 );
        trade.AddCustomMetric( "ExitFxRate", trade.ExitFxRate, 5 );


SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = Close;
SellPrice = Close;

Buy = BarIndex() == 14;
Sell = BarIndex() == 15;

Please note, I trade USDCHF and the same symbol is used to derive Entry/Exit Fx rates (inverse of USDCHF).
Here are my two questions:

  1. When the trade is closed on SELL bar CLOSE, ExitFxRate is derived from the previous bar CLOSE. Why? Is this a bug? In my test scenario the trade is entered on close of the bar with index 14 and exited on the next bar close. These are two different closes, but EntryFxRate and ExitFxRate are reported equal.
  2. Is it possible to control EntryFxRate in the code? If I want to enter trades on NEXT bar OPEN, how can I use the dynamic rate symbol OPEN on that bar?

Read pages 10 to 11 here


I have read that tutorial before, it's very useful. Thank you @fxshrat for that additional documentation!

But my two questions still stand, I will rephrase them:

  1. To get EntryFxRate the Close of the corresponding dynamic rate bar is used. Why is ExitFxRate derived from the Close of the previous dynamic rate bar?
  2. I understand that in case when BuyPrice = SellPrice = Open; we can generate a custom dynamic rate symbol with Open prices in "Close" field. But how to use different fields of dynamic rate symbol for entries and exits. I am talking about BuyPrice = Open; SellPrice = Close; case.

It might complicate things but you can try to modify entry/exit price instead:

modified_exit_price = exit_price * ( default_fx_rate/ true_fx_rate )

It would be nice to have the 'FX Rate' as an additional argument in enter/exit trade functions in CBI though.

1 Like

There are no bugs in AmiBroker.

Everything is so for the purpose and works so because it was designed to work that way.
It uses previous bar FX close by design because inside candle you don't know WHEN exactly you enter/exit the trade (because setting the price variable does NOT set the time of trade*) and the latest known FX rate is previous bar close. Otherwise you would have risked future leak in the system. It is also in line with how FX accounting works for tax purposes (required by law).

1 Like

@Tomasz, I don't understand your explanation and here's why:

  1. When the trade is entered on bar's CLOSE, fx rate uses THIS bar CLOSE.
  2. When the trade is exited on bar's CLOSE, it uses PREVIOUS bar CLOSE.

Here's my example:

  • on some USDCHF bar close of 1.24715 go Long, buy 1 lot, (buy 100000 USD, sell 124715 CHF);
  • on the NEXT USDCHF bar close of 1.28325 exit the trade, (sell 100000 USD, buy 128325 CHF);
  • trade's profit is 3610 CHF; that is 3610/1.28325 = 2813.17 USD;
  • use of PREVIOUS bar CLOSE for fx rate leads to profit of 3610/1.24715 = 2894.60 USD.

I am not here to prove something is wrong or incorrect. I am evaluating Amibroker specifically for Forex trading. My impression is that the program is mostly oriented for stocks, and not all design decisions can be clearly mapped to Forex. Your product is great, and I'd like to help make it better by discussing some aspects specific for FX trading.

@aron 's suggestion to:

It would be nice to have the 'FX Rate' as an additional argument in enter/exit trade functions in CBI though.

is one possible solution, although leading to low-level CBI.
What are your thoughts on this?