Using reserved variables "Buyprice", "Sellprice", "Shortprice" and "Coverprice" at market

Hi guys,

Today, I am studying enter and exits in AFL, and I have a doubt. I am afraid I am not having a good understanding of how some reserved variables (buyprice, sellprice, shortprice and coverprice) really work.

Following example code is an extract of a Howard Bandy's book showing how to enter and exit at market work.


BuyPrice = C;

SellPrice = C;

MA1 = MA(C,5);

MA2 = MA(C,25);

Buy = Cross(MA1,MA2);

Sell = Cross(MA2,MA1);

and following code (another extract from the same author) shows how to enter with a limit order and exit with a market order.


BuyPrice = C;

SellPrice = C;

MA1 = MA(C,5);

MA2 = MA(C,25);

BuySig = Cross(MA1,MA2);

Buy = (Ref(BuySig,-1)==1) AND (L<0.99*Ref(C,-1));

BuyPrice = 0.99*Ref(C,-1);

Sell = Cross(MA2,MA1);

Please, English is not my first language and I'm new programming, so it could be possible I would not be interpreting his words correctly. If it is the case, it would be glad been warned.

There is a thing that misleads me. Why Dr Bandy use "BuyPrice = C" and "SellPrice = C" in the first code (enter at market) and "SellPrice = C" in the second one (exit at market)? I have done some probes backtesting both codes and comparing with the same ones erasing these instructions and I am getting the same results. I would think it would be not necessary to use them but I guess I am wrong. I mean, some reason must have.

So, I raise my doubt in another way... is necessary to use buyprice an sellprice if I am buying or selling at market? I guess it is, but I don't know why.


Read you will find out that in Trade tab you can define your enter price.
When you include buyprice, sellprice, etc in your AFL code you OVERHIDE this settings


@ awilson I did never think he just wants only change his setting with his AFL code. I was getting crazy thinking about other possibilities. Thanks a lot!

These reserved variables are omnipresent just like O,H,L,C in your AFL code.

Whether you use (call) them or not is up to you.
By default, they are assigned the Close price of the bar as you see in settings.

When you assign a value to them in your AFL code, you are merely changing that value to a new value.

BuyPrice = C;
SellPrice = C;

The above in layman terms already exists in the background and can be omitted.

You'd explicitly write them and override them in your code if you wanted to change them as mentioned by Anderson.

Just some trivia: If you debug the (buyprice, sellprice, shortprice and coverprice) for each bar, you'll find that its the same value for all the variables for each bar.



Thanks a lot. By writing it expressly, I thought it would be necessary and, wrongly, I did not think that it could be omitted. That's why I thought there must be something that escaped to me.

Thank you very much for your observation.

@BernieTGN you might find this article interesting:

When you scroll it down, you will find a list of all Reserved variables in AFL with descriptions.


No, trade prices such as BuyPrice, SellPrice, ... added to AFL code do not always override the ones of Backtest settings - Trades page!

There are exceptions that need to be kept in mind.

Trade prices of code do not override the ones of backtest/analysis settings dialog if:

1. using SetBacktestMode(backtestRotational) (or old method EnableRotationalTrading() )

The rotational trading mode uses "buy price" and "buy delay" from the Settings | Trade page as trade price and delay for both entries and exits (long and short)

2. using ApplyStop then:

ExitAtStop = 0 uses SellPrice/CoverPrice variables in backtestRegular mode only, in other modes it uses trade prices from the Backtest Settings | Trade page (not overridable via SellPrice/CoverPrice of code)


@Milosz Thanks. It was one of the first articles about AFL I read. Very interesting.

@fxshrat Thank you very much for your observation. I was a little worry about this kind of things. I mean, thinking Amibroker will do something one way (written in the code) when it will do it following instructions from settings (or vice versa). A very good contribution for me.

Do you know if there is some article about this (beyond this four reserved variables)? I agree. It will be good keep in mind this kind of exceptions.


This was very helpful to me. Thanks a lot.