Weekly Backtest

If I run a Backtest with SetTradeDelays(1,1,1,1) and select a Periodicity of Weekly Amibroker will use the price from the preceeding week to place the trade. If I have the first day of week in Weekly Time-compression (interday settings) set to Friday I would like to have the price data from Monday use to place the trade. Is there a way to set this as an option?

Yes, use

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

I am not getting that result. Here is my code and result follow 1) From Back test 2) Explore showing detail data:::

// First Day of Week set to "Friday"
SetTradeDelays( 1, 1, 1, 1 );  // Setting this here.  Trade on the next bar
BuyPrice = SellPrice = Open;   // Buy/Sell on open of next bar.
ShortPrice = CoverPrice = Open; 

Buy = Open > Ref(Close,-1);
Sell = Open < ma(Close,5);
Buy = ExRem( buy, sell );
Sell = ExRem( sell, buy );
Filter = 1;
AddColumn(DayOfWeek(),"Date",1.0);
AddColumn(O,"Open",1.2);
AddColumn(H,"High",1.2);
AddColumn(L,"Low",1.2);
AddColumn(C,"Close",1.2);
AddColumn(Buy,"Buy",1.0);
AddColumn(Sell,"Sell",1.0);

SetTradeDelays works in Backtest only.

To activate backtest mode in Exploration you have to use Equity function.
Read carefully what that function does and when it should not be used
https://www.amibroker.com/guide/afl/equity.html
Note: Portfolio backtest will not work in Exploration.

// First Day of Week set to "Friday"
SetPositionSize( 1, spsShares );

SetTradeDelays( 1, 1, 1, 1 );  // Setting this here.  Trade on the next bar

BuyPrice = SellPrice = Open;   // Buy/Sell on open of next bar.
ShortPrice = CoverPrice = Open; 
Buy = Open > Ref(Close,-1);
Sell = Open < ma(Close,5);


if ( Status( "actionex" ) == actionExplore )
{
	e = Equity( 2, -1 );

	Filter = Buy OR Sell;

	AddColumn(DayOfWeek(),"Date",1.0);
	AddColumn(O,"Open",1.2);
	AddColumn(H,"High",1.2);
	AddColumn(L,"Low",1.2);
	AddColumn(C,"Close",1.2);
	AddColumn(Buy,"Buy",1.0);
	AddColumn(Sell,"Sell",1.0);
}
  1. Symbol Trade Date Price Ex. date Ex. Price % chg Profit % Profit Shares Position value Cum. Profit # bars Profit/bar MAE MFE Scale In/Out
    SPY Open Long 6/15/2017 244.09 6/20/2017 243.01 -0.44% -442.46 -0.44% 409.685 100000.00 -442.46 3 -147.49 -1.01% 0.38% 0/0
    2)Ticker Date/Time Date Open High Low Close Buy Sell
    SPY 6/1/2017 4 241.97 243.38 241.64 243.36 0 0
    SPY 6/2/2017 5 243.42 244.35 243.08 244.17 0 0
    SPY 6/5/2017 1 243.97 244.30 243.76 243.99 0 0
    SPY 6/6/2017 2 243.34 243.98 243.12 243.21 0 0
    SPY 6/7/2017 3 243.60 243.92 242.83 243.66 0 1
    SPY 6/8/2017 4 243.77 244.33 243.17 243.78 1 0
    SPY 6/9/2017 5 244.09 245.01 241.95 243.41 0 0
    SPY 6/12/2017 1 243.13 243.42 242.38 243.36 0 1
    SPY 6/13/2017 2 243.98 244.61 243.58 244.55 1 0
    SPY 6/14/2017 3 244.86 244.87 243.29 244.24 0 0
    SPY 6/15/2017 4 242.68 243.91 242.36 243.77 0 1
    SPY 6/16/2017 5 242.77 242.83 241.63 242.64 0 0
    SPY 6/19/2017 1 243.59 244.73 243.48 244.66 1 0
    SPY 6/20/2017 2 244.25 244.26 242.99 243.01 0 0
    Notice that the date of trade is 6/15/2017 at a price of 244.09 which is the open price on 6/9/2017 The previous Friday.

Ooops: Forgot to press the Reply button. New to this site… So you can see when I run the code in BACKTESTER the trade price is 244.09 which corresponds to the SPY price the previous week. I Can’t trade on old price data unless someone has a way to activate Back to the Future.

Exploration is NOT backtest.

Exploration displays user-defined columns. RAW values as they appear in arrays. It has NOTHING to do with trades and nothing to do with backtesting and nothing to do with all backtester settings like trade delays.

SetTradeDelays() is an INSTRUCTION FOR THE BACKTESTER to trade LATER than the signal appears in array.

You should be running BACKTEST, not Exploration, and look at TRADE LIST or DETAILED LOG.

GBord, I recommend that you start with some simple system and practice using the backtester and the exploration capabilities.

(your system is not a good example because of it's rules you can signal a Long Entry even though the same bar meets the Sell conditions, ie. Buy = Open > Ref(Close,-1); Sell = Open < ma(Close,5); is silly because this bar's Open may be greater than the previous bar's Close (your BUY signal) AND this bar's open may be less than your 5 week moving average (your SELL signal).

In your original post you wrote "WEEKLY", but your later post shows the exploration results from a Daily exploration.

Check your settings,

And as TJ has written, if you want to find what prices your trades are filled at, run a back test and look at the TRADE LIST or DETAILED LOG,

OR

1 Like

Lets start over. MY QUESTION is that BACKTEST when run as WEEKLY is using price data from previous bars to place the the trade. I would like to know how to correct this.Here is the revised code and two runs: 1) as backtest with WEEKLY set as the periodicity and 2) a run using Explore on the same code using DAILY to show where the trades took place and what price was used. Look at the Explore run and you will see that the price used to place the trade comes BEFORE the date of the trade.

[code]// Set up: This afl is run in 1)Backtester using WEEKLY as Periodicity
// 2) Explore using DAILY as Periodicity
// First Day of Week set to ::: FRIDAY
// Output from Backtestr show buy on 5/11/17 at price 239.19
// and a sell on 6/1/17 at a price of 241.54
// **** See Output BELOW::::::
SetTradeDelays( 1, 1, 1, 1 ); // Setting this here. Trade on the next bar
BuyPrice = SellPrice = Open; // Buy/Sell on open of next bar.
ShortPrice = CoverPrice = Open;

Buy = Open > ma(Close,4);
Sell = Open < ma(Close,4);
Buy = ExRem( buy, sell );
Sell = ExRem( sell, buy );
Filter = 1;
AddColumn(DayOfWeek(),“Date”,1.0);
AddColumn(O,“Open”,1.2);

[code]Date Information
5/4/2017
Entry signals(score):
Exit signals:
0 Open Positions: , Market Value: 0.00, Equity: 100000.00, Cash: 100000.00, Margin: 0.00, Net Cash Balance: 100000.00,
5/11/2017
Entry signals(score):SPY=Buy(1),
Exit signals:
Enter Long, SPY, Price: 239.19, Shares: 418.0777, Commission: 0, Rank: 1, Equity 100000, Margin Loan: 0, Fx rate: 1
1 Open Positions: , SPY (+418.078), Market Value: 100079.43, Equity: 100079.43, Cash: 0.00, Margin: 0.00, Net Cash Balance: 0.00,
5/18/2017
Entry signals(score):
Exit signals:
1 Open Positions: , SPY (+418.078), Market Value: 98988.25, Equity: 98988.25, Cash: 0.00, Margin: 0.00, Net Cash Balance: 0.00,
5/25/2017
Entry signals(score):
Exit signals:
1 Open Positions: , SPY (+418.078), Market Value: 101074.46, Equity: 101074.46, Cash: 0.00, Margin: 0.00, Net Cash Balance: 0.00,
6/1/2017
Entry signals(score):
Exit signals:SPY=Sell,
Exit Long, SPY, Price: 241.54, (Avg. exit pr. 241.54), Shares: 418.0777, Commission: 0, (Total comm.: 0), Profit: 982.4825 (0.98 %), Entry rank:1, Equity: 100982.5, Fx rate: 1
0 Open Positions: , Market Value: 0.00, Equity: 100982.48, Cash: 100982.48, Margin: 0.00, Net Cash Balance: 100982.48,
Ticker Date/Time Date Open
SPY 5/4/2017 4 238.83
SPY 5/5/2017 5 239.19
SPY 5/8/2017 1 239.75
SPY 5/9/2017 2 239.96
SPY 5/10/2017 3 239.39
SPY 5/11/2017 4 239.35
SPY 5/12/2017 5 239.09
SPY 5/15/2017 1 239.47
SPY 5/16/2017 2 240.64
SPY 5/17/2017 3 238.10
SPY 5/18/2017 4 235.73
SPY 5/19/2017 5 237.33
SPY 5/22/2017 1 238.90
SPY 5/23/2017 2 239.95
SPY 5/24/2017 3 240.32
SPY 5/25/2017 4 241.20
SPY 5/26/2017 5 241.54
SPY 5/29/2017 1 241.71
SPY 5/30/2017 2 241.34
SPY 5/31/2017 3 241.84
SPY 6/1/2017 4 241.97

Have you managed to solve this? I also found that Backtest with weekly data set to "Trade on NEXT BAR ..." actually uses the starting price of the current bar. It does not make any sense to test this way.
Best I could do is use "Trade CURRENT BAR" and assume the next week starting price is actually the current week closing price. This is in the very least misleading from my point of view, or simply wrong.

In my experience, trade delays work exactly as expected with bars of any duration. Therefore, it is likely that there is an error in your implementation, but without some details (your code and results) it is pretty difficult for anyone to help you.