I would like to perform a backtest. The backtest is in the daily timeframe and I have used the closing price as the buy price. This works very well.
I would now like to use the daily low price as the buy price, but only if tomorrow's daily low price is lower than today's.
Attached is a backtest code that unfortunately does not work:
> // backtest
> // daily timeframe
>
> days = 3 ; // 3 days - holding time
>
> setup = Ref(Close, -1) > EMA(Close, 20) ; // setup rules
>
> trigger = close > Ref(High, -1); // trigger rules
>
> Buy = setup AND trigger; // buysignal
>
> BuyPrice = low; // buyprice is not close therefore filter
>
> buy = buy and Ref(low, 1) < low // filter for trades who do not match
>
> Sell = 0;
>
> // trade management
> bars = days; // exit after n bars
> ApplyStop( stopTypeNBar, stopModeBars, bars);
You are totally right. My plan is to trade with a Limit order the next day. The next day I know where the low was today. This low price is my Limit order trade for the next day. Sorry, for the confusion.
With the backtest I would like to understand if it is better to use the close or the low price for my limit order next day. So where do I get the better expectancy with close or low price? Sorry, for the confusion.
You need to reconsider you logic and move it to today. Today you want to buy LMT at yesterday's low. That implies adding the posibility of a gap down.
Also, in your logic for today you cannot use any closing price because your order will be executed during the session. This is the most realistic backtest that you can perform:
days = 3 ; // 3 days - holding time
setup = Ref( Close, -1 ) > EMA( Close, 20 ) ; // setup rules
trigger = close > Ref( High, -1 ); // trigger rules
total_setup = setup AND trigger; // buysignal
Buy = Ref( total_setup, -1 ) AND L < Ref( L, -1 );
BuyPrice = Min( Open, Ref( L, -1 ) );
Sell = 0;
// trade management
bars = days; // exit after n bars
ApplyStop( stopTypeNBar, stopModeBars, bars);
If you use an approach like the one presented by @Armin_Tamzarian , you need to be very careful if you run the backtest against a watchlist of symbols. This logic basically assumes that you have sufficient capital in your account to place limit orders for ALL symbols that generate a setup condition.
For example, consider the following scenario. Your strategy allows 5 open positions, each allocated 20% of your equity. Yesterday, there were 12 setups so today you would need to place 12 orders, each at 20% of equity. Will your broker allow you to place orders totaling 240% of equity? What if they all get filled, will you hold them all? If you’re planning to cancel the extra orders once you get 5 fills, how do you know which of the 12 symbols got fills in live trading?
Note that AmiBroker will stop taking trades when you reach max positions, but most of the problems above still exist.
A more conservative approach is to only place as many orders as you have current open slots. For example, if you already have 3 open positions, then in the scenario above you would only consider the top 2 setups. Those may or may not get filled.
yes, I agree with you. I am using positionscore in my backtest. You must use CBT for your trading system to get the same functionality. So I do it by hand, because CBT code is from my point of view harder to test and could have side effects.