How is Amibroker calcuating profit?

When running a backtest, how is Amibroker calculating the Profit for a trade that is displayed in the analysis window?

I am backtesting a simple breakout system on various futures markets. I have my profit size set to 1 contract in my code.

_SECTION_BEGIN("Breakout System-Test");
// This is a simple breakout/breakdown system using daily data.

// Turn off Quick AFL to use all bars

// System Parameters

// PositionSize=MarginDeposit=1;

The Profit that Amibroker calculates is different from what I calculate by hand. Following is an example.

The trade in question is in the 3-Month Sterling contract. The signal was given on 8/3/2003 and the trade was entered on 8/4/2003 per my system rules. The signal to exit was given on 1/9/2004 and the trade was exited on 1/12/2004 per my system rules. I checked the entry and exit prices from the backtest and they match what I would calculate by hand.


When I run the backtest, I get the following result for this trade.


The loss of -$28,299.74 is way more than I calculate.

The point loss on the trade is 94.06 - 93.98 or 0.08 points.

The point value for the 3-Month Sterling contract is 1,250 pounds, which I have in my symbol information.


Therefore, I would calculate the loss in pounds on this trade as 1,250 * 0.08 or 100 pounds.

My account is in US dollars. You can see in the symbol information above for 3-Month Sterling I have the currency set to GBP. I have GBP set up in my preferences per the image below.


If I go to a chart of the GBPUSD for the time period, I find that the exchange rate was 1.8511.


Therefore, the loss in USD would be $185.11.

This is wildly different than the Profit that Amibroker calculates for this trade and I am completely confused as to how to Amibroker arrived at a $28k loss on this trade.

This seems to be only happening with contracts denominated in a foreign currency.

I have read the Amibroker guide about trading in a foreign currency.

In case of symbols denominated in foreign currency, the profit calculations can be done two ways depending on "Trade using FX cash conversion" box in Tools->Preferences, "Currencies" tab.

As documentation says:

Trade using FX cash conversion option - handles different ways to calculate profits depending on whenever foreign currency is borrowed or cash actualy exchanged

When you actually exchange the cash when entering a trade on security traded in foreign currency then you should have this option turned ON
when you use a loan in a foreign currency, turn this option OFF (the pre 5.71-behavior)
See this for more info: Mechanics of an Overseas Stock Transaction Tour | Interactive Brokers

For multiple currency backtesting see this: Portfolio-level back testing


See also:

@stevekline2, I saw a similar problem in the past, and in my case (even if the discrepancy was not so evident), the mistake was due to a wrong settings about the commissions.
Do you set them in code or in the preferences? I suggest checking them.

Did you also examine the DETAILED LOG for additional information about this trade?

Thank you for the links. I read through all of them.

I already had all of my currencies set up in Tools->Preferences, Currencies tab as the guide states. And, I already had "Trade using FX cash conversion" checked because I exchange currencies prior to initiating the trade.

So, I think I have everything correct from a settings perspective.

I'm now looking at the detailed log. I was able to calculate the change in the cash balance at the start of the trade, but I am struggling to calculate the equity changes listed in the detailed log.

Post entries from detailed log pointing out the lines you are struggling with.

I did the commissions in the preferences in the backtest. I set them at $7.50 per contract.

Let's look at just a few lines of the detailed log from this trade.


I set up a spreadsheet to try to calculate the cash and equity changes on my own to understand how Amibroker is calculating them. The cash and equity from Amibroker's detailed log are in columns B and D with their daily changes in C and E. The entry price is from the detailed log. The close price I looked up on my chart. The point value and the margin are from the symbol information. The commission is the value I set per contract in the preferences for the backtester. The FX rate on the day of the trade is from the detailed log and the other days I looked up the value on my Amibroker chart.

I then tried to calculate the changes in cash and equity that the detailed log shows. You can see the formulas I am using in Excel in row 8. I was able to get the change in cash to match. But, I cannot figure out how Amibroker is calculating the change in equity. It seems like it should be the change in the price of the contract times the point value, which is in pounds (column M) times the FX rate (column N). But, that is clearly not what Amibroker is doing since the values for the four days shown do not match at all. And, I can't see any pattern to the differences between Amibroker and my attempts to calculate the equity change.


@stevekline2, do you still see the above result?

Anyway, to better help @Tomasz provide you an answer, I suggest you also post the section of the detailed log where there is the "exit" of the position.

I'm still seeing this trade with a loss of $28k. (This instrument and others denominated in foreign currencies have several trades like this still).

Below is the exit from the detailed trade log.


The only thing I can think of that would cause the loss is that the GBP-USD exchange rate moved against me that much during the trade. The exit FX rate is 1.8472 versus the entry FX rate of 1.608.

But, how is AB calculating that?

Shouldn't the profit of the trade be entry price * fx Rate - exit price * fx rate?

The margin is only 269 pounds for one contract. In dollars, the margin difference between the entry and exit is about $65. (I realize that doesn't affect the equity, but I'm trying to think about any way that AB might be using the change in the FX rate to affect the profit of the trade).

As I wrote previously, you are using incorrect settings. " Trade using FX cash conversion" should be ON, if and ONLY if you are exchanging ENTIRE value of position (not using margin). That does not happen with futures. With futures you are trading on margin, therefore this setting should be OFF.

If you turn this option off, your profit will be -199.72
It is the result of the following calculation: (93.98 - 94.06 ) * 1 contract * 1250 (point value) * exit fx rate (1.8472) - 7.5 (entry commission ) - 7.5 (exit commission) = -184.72 - 15 = -199.72


Thank you.

I misunderstood the interpretation of that checkbox. I was thinking that I use cash for the maintenance margin so I needed to have it checked. I had it completely backwards.

@Tomasz, maybe this is another opportunity to reconsider the suggestions that were discussed in this previous thread.

1 Like

Yes, this setting will be moved to be per-APX (analysis project).


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.