BuyPrice / SellPrice / Backtesting

Help please. I am trying to learn to program in order to automate some simple strategies.

Strategy: buy = cross (close, MA)

What buy price is assumed for back-testing?

Can I set Seetu backtesting to always buy & close at opening price of the following day?

Many thanks.

Trading prices are stored in 4 variables:
BuyPrice, SellPrice, ShortPrice, CoverPrice

Default values are Close for all.

You can fill these variables by your own values:

BuyPrice = Open;
SellPrice = Open;


You can shift values in all arrays to the left or right (it means to the past or to the future) using the REF function.

You can shift Close, MA(Close,10), a result of the CROSS function and even BUY, SELL arrays itself.

Here are some examples:

// Setup
SetOption("Symbols","MSFT");
SetOption("ApplyFromTime", "2014-09-21");

// (A) - BUY on 2014-09-24
Buy = Cross( Close,MA(Close,10) );

// (B) - BUY shifted to 2014-09-25 
// Buy = Cross( ref(Close,-1), ref(MA(Close,10),-1));

// (C) - BUY shifted to 2014-09-23
// Buy = Cross( ref(Close,1), ref(MA(Close,10),1));

// (D) A shortcut for Backtest: BUY on 2014-09-23 (Exploration shows 2014-09-24)
// SetTradeDelays(-1,-1,0,0);
// Buy = Cross(Close,MA(Close,10));

// (E) A shortcut for Backtest: BUY on 2014-09-25 (Exploration shows 2014-09-24)
// SetTradeDelays(1,1,0,0);
// Buy = Cross(Close,MA(Close,10));

// Shifted CROSS:

// (F) Shifted CROSS: BUY on 2014-09-25 
// Buy = Ref( Cross( Close, MA(Close,10) ) , -1);

// (G) Shifted CROSS: BUY on 2014-09-23 
// Buy = Ref( Cross(Close,MA(Close,10)) , 1);

// Shifted BUY:

// (H) BUY shifted to 2014-09-25 
// Buy = Cross(Close,MA(Close,10));
// Buy = Ref(Buy,-1);

Filter = 1;
AppendColumn(Buy,"Buy",format=1.0);

So it depends…

  • SetTradeDelays can lead to confusion (values are in diferent bars in Exploration and Backtest).
    But your source code is cleaner than with the REF function.

  • Exploration vs Backtest confusion can be removed using REF.
    But one needs to write more code.
    And it is easy to make errors.
    One can easily look to the future by a mistake: all you need is to use -1 instead of 1 (and vice versa) on the wrong place.
    So be carefull.


To answer your question

Use:
BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,0,0);
// Or
// Buy = Ref(Buy,-1);
// Sell = Ref(Sell,-1);

Bob . Extremely helpful answer - thank you.