UpdateStats() with TimeInsideBar = 1

I noticed a change in the bo.Equity value in _TRACE() output, even though no trades have taken place between the outputs:

The only thing that may have caused this change is this line of code:
The "Portfolio Backtester Interface Reference Guide" says "TimeInsideBar = 1 means middle of the bar". What price is used for the "middle of the bar" to update the value of bo.Equity?

UpdateStats calculates equity based on current price. On opening of the bar, open price is used. If price changes (for example close is used instead of open), then calculated equity will change too.

Thanks Tomasz for the quick reply. Just to confirm my understanding, do you mean that "TimeInsideBar = 1" may have the same effect as "TimeInsideBar = 0"?

To be clear, I'd like to know after a call to UpdateStats(bar,1) what prices are used to value portfolio non-cash positions. I noticed a change in total equity value after the call in the DebugView log. Is it, for example, (High + Low) / 2? Thanks, Tomasz.

Things are way more complex and well beyond forum explanations because to fulfill your curiosity I would need to publish my proprietary source code, and I won't do that. UpdateStats() does not only update equity, it does lots of other calculations, including for example calculating exposure, updating bars in trade, accounting for interest for both margin loan and interest on cash left in the account. To directly answer your question the OPEN price is used for time in bar == 1. Close is only used when time inside bar is == 2.

Thanks again for the answer. However I still cannot reconcile for the change in bo.equity.


It appears to me that the difference between the two values inside red rectangles is approximately equal to the commission for 1204 shares.

As @mradtke says, one equity is just before entering trade, the other is after trade has been opened, the difference is commission

Sorry, I meant to compare the Equity value $99,975.92 $to $100,000. The difference, $24.08, is exactly twice the commission on 1,204 shares @ $0.01/share. It seems the commission is charged twice. What causes this intriguing behavior, Matt?
Thanks a lot.

Moderator comment: The post below contains incorrect information. Round trip commission is NOT charged at entry.

I believe AmiBroker deducts the round trip commissions from Cash when the trade is opened, and deducts no commissions when the trade is closed. This prevents a scenario where there is no Cash available to pay commissions when it's time to exit. On a Long trade, you could obviously pay the commissions from the sale proceeds, but on a losing Short trade this might not be the case.

That is not true. Commission is charged TWICE but NOT at the entry bar.

It is charged ONCE at the entry and ONCE at the EXIT.

Calculation for EXIT commission is SEPARATE from entry. And it totally depends on exit bar (since value of trade changes usually), so it definitely can not be charged at entry.

BUT.... Equity represents so called "closed equity". It is the value of cash plus value of all OPEN POSITIONS AS IF THEY WERE CLOSED. It is mark-to-market equity with all trading costs included.

So for the purpose of calculation of closed equity, EXIT commission value calculated at given bar is virtually subtracted.

And I would appreciate if before giving false information, a FORUM SEARCH was done because it was explained before:

Also quite clearly the exact amount of commission charged at entry and exit is listed in the Detailed Log, so there is really no room for guessing. All information is there right before your eyes: How do I debug my formula?

1 Like

I've finally confirmed my understanding from this thread by checking the equity changes in the DebugView log. Thanks very much for Tomasz and Matt's help.

For the benefit of a future reader of this thread, here is the summary of what I've learned:
UpdateStats(), with parameter TimeInsideBar = 1 uses open prices to calculate the position values.
bo.Equity is the sum of cash and all the position values marked with either open or close prices as determined by TimeInsideBar value, minus the total commission amount that would have incurred if all positions were liquidated.

1 Like