What is the purpose of this line of backtest code?

I am looking at some code related to backtesting.

What is the purpose of this line of code?
BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

Is it some kind of initialisation code for all backtesting code? What if I do not have this line of code in my backtest code? Shouldn't BuyPrice, SellPrice be automatically set when buy, sell orders are triggered?

It is found here.

This is a shorthand for initialising variables.

Initialising variables is a "good programming practice" and is not specific to AFL.

When variables are declared, this is the only sure way to ascertain that a residual or garbage value is not contained in the variable.

so as far as this question goes "What if I do not have this line of code...." you can see for yourself and don't disregard any unexpected results.
If you are assigning a value before using any variable, and can guarantee that,.you will get right results.

1 Like

@thankyou18 if you look at the entire example you'll see that in that case, it is used in combination with SetTradeDelays()

TradeDelay = 1; // set it to 0 for no delays
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay );
//  ...
BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

These instructions will execute (when properly triggered) any BUY / SELL / SHORT or COVER on the next day (bar) Close price.
(Later in the code. also ApplyStop() is used to close the trade: in such a case, the price is determined by the Stop rule.)

Specifying the BuyPrice/SellPrice/etc. in your formulas OVERRIDES the default values that are defined in the backtester setting dialog:


So, actually, there is nothing truly automatic: the backtester uses ALL the predefined settings (not only the said variables) except if you override them in your code.

Back-testing your trading ideas explains quite well the all the reserved variables that you can use to fine tune your backtests.

1 Like