Backtest with buyprice = low

Good evening!

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);

Thank you for help!

You have it in the wrong order, try;

Low < Ref(Low, -1)

I don’t understand how do you want to trade today based on tomorrow’s price. That is impossible. Totally unrealistic.

1 Like

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.

That's why you have AmiBroker so you can backtest it and see.

1 Like

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);

This is how it looks:

Thank you for your reply, Armin. Very helpful, and I understand your point. I will reconsider my backtest.

1 Like

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.

1 Like

I agree with you. I prefer the conservative approach like you mentioned it.

My position sizing based on Van Tharp risk-based method.

The placement of my orders is controlled by two parameters: buying power and max open position.

SetOption("InitialEquity", 100000 );
SetOption("AccountMargin", 100/my_leverage );
SetOption("AllowPositionShrinking", False);
SetOption("MaxOpenPositions", my_position);

Developing a trading system is a very solitary activity. It is so good to be part of such a great community. thank you for help.

1 Like

It should be noted that to implement the more conservative approach correctly requires a CBT.

Do you mean "correctly" for the backtest or do you mean "correctly" for the life system in my case amibroker scan, brokerIB and trader workstation?

If you want your backtest results to match your live trading, then you will most likely need to use a CBT.

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.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.