Lower than Low, Higher than High

Topics about the AmiBroker platform
User avatar
Posts: 948
Joined: Thu Jul 26, 2007 7:20 pm

Lower than Low, Higher than High

Postby progster » Thu Dec 31, 2009 5:21 pm

On the AB Yahoo List a user mentioned observing trade prices at the low and wondered how this could come about.

TJ wrote the following, illustrating two example cases:

If you write in your formula things like that:

BuyPrice = Ref( C, -1 );

It will assign BUYPRICE variable to be CLOSE OF YESTERDAY.

But the trade occurs TODAY (because you have set "delay of zero" as you

Now if YESTERDAY CLOSE was lower than TODAYS LOW, the backtester will
use todays LOW price (lowest possible price) because by default it
protects from using prices outside H-L range as described in the manual

The other example can be:

BuyPrice = C - ATR(1);

This will SUBTRACT true range from todays close price and use it as
buyprice, but if result is lower than todays low, the backtester will
use lowest possible price (i.e. todays low).

The above is just example of what can happen if you manipulate
BuyPrice/SellPrice/ShortPrice/CoverPrice in the formula without really
knowing what you are doing.

The key sentence from the docs page referenced above is:

During back-testing AmiBroker will check if the values you assigned to buyprice, sellprice, shortprice, coverprice fit into high-low range of given bar. If not, AmiBroker will adjust it to high price (if price array value is higher than high) or to the low price (if price array value is lower than low).

So, if you are seeing backtest trades at the high or low and you didn't put them there explicitly, then it is likely that you put them there implicitly as per above.

User avatar
Posts: 948
Joined: Thu Jul 26, 2007 7:20 pm

Re: Lower than Low, Higher than High

Postby progster » Thu Dec 31, 2009 5:29 pm

In a later message, Herman offers some very helpful code for detecting this situation:

Code: Select all


   Plots a big Red vertical bar whenever you have a pricing error.

   _01    Provided by Herman


// Buy, Sell, Short and Cover must be defined for this code to work.
// In use this code would be appended to the very end of your code.
// When working on complex system's code where BuyPrice, SellPrice, etc.
// are modified in different sections it is easy to introduce a pricing
// error so that you are trading outside the bar. Especially so since
// SetOption("PriceBoundChecking",TRUE) is the default and Amibroker
// will fit your trades within the bar limits and hide the problem.
// This code check the limits of your tradeprices, display the number
// of errors in the chart title and plot a red marker at the bottom of
// chart where the error occurs.

SetOption( "PriceBoundChecking", False );
BuyError = ( BuyPrice > H OR BuyPrice < L ) AND Buy;
SellError = ( SellPrice > H OR SellPrice < L ) AND Sell;
ShortError = ( ShortPrice > H OR ShortPrice < L ) AND Short;
CoverError = ( CoverPrice > H OR CoverPrice < L ) AND Cover;
PricingErrors = BuyError OR SellError OR ShortError OR CoverError;
Plot( PricingErrors , "", 4, styleArea | styleOwnScale, 0, 10 );
Title = "# Price Bound Errors: " + NumToStr( Cum( PricingErrors ), 1.0, False ) + "\n";
(1.4 KiB) Downloaded 200 times

Posts: 30
Joined: Fri Mar 30, 2012 9:15 pm

Re: Lower than Low, Higher than High

Postby trash » Tue Apr 03, 2012 2:55 pm

A side note,
Price bound checking can be turned off by using

Code: Select all

SetOption("PriceBoundChecking", False);

i.e. if you are working with bid data and want to buy at ask.

Another note by Marcin


You can calculate custom BuyPrice / SellPrice variables in your code and then - instead of using ApplyStop - calculate your stops in a loop, so you could define custom price levels for each type of exit the way you need. See for example:
http://www.amibroker.com/kb/2007/03/24/ ... ice-chart/

Note that if your trading prices would go outside H-L range of the bar - then you can disable automatic adjustment for trading prices by using:

SetOption("PriceBoundChecking", False);

Return to “AmiBroker Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest