Bars Since Entry issues in portfolio backtester

Questions about MultiCharts and user contributed studies.
smhuggins
Posts: 12
Joined: 15 Mar 2015
Has thanked: 1 time

Bars Since Entry issues in portfolio backtester

Postby smhuggins » 16 Aug 2015

Hi there

Can someone please help me fix this problem?

I am trading the ASX, long, on daily bars.

I coded a strict ATR trailing stop (as the inbuilt ATR trailing exit is not strict, it can still trail down).

This works perfectly in a single chart however 'BarsSinceEntry' creates massive problems in portfolio backtester. It requires me to ensure the maxbarsback setting is as long as BarsSinceEntry, and therefore I cannot properly test the strategy over, for example 5 years, in portfolio backtester (because the maxbarsback setting needs to be set to the number of bars back that equals BarsSinceEntry).

Firstly, I have to guess what this maxbarsback/BarsSinceEntry figure might be (it could be 1000 days!). Secondly, if it is 1000 days, I cannot backtest the strategy 5 years back (ie ~1100 bars back) to see how robust it is, because this means you are only getting a portfolio backtest duration of 100 days which is almost pointless.

There must be another way to code a strict trailing exit without using the barssinceentry...?

Plllllleeaasse can someone help? Anything would be greatly appreciated.

Code: Select all


Input: TrailingATRs (4.5) ;
Input: ATRLength (10) ;

Var: ATRVal (0) ;

ATRVal = Highest (high, barssinceentry) - (AvgTrueRange(ATRLength) * TrailingATRs) ;

If MarketPosition = 1 Then Begin
If High [0] is < ATRval then
Sell ( "ATRtrail" ) next bar at ATRval stop;
End ;

Thank you in advance

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Bars Since Entry issues in portfolio backtester

Postby JoshM » 16 Aug 2015

This works perfectly in a single chart however 'BarsSinceEntry' creates massive problems in portfolio backtester. It requires me to ensure the maxbarsback setting is as long as BarsSinceEntry, and therefore I cannot properly test the strategy over, for example 5 years, in portfolio backtester (because the maxbarsback setting needs to be set to the number of bars back that equals BarsSinceEntry).

Firstly, I have to guess what this maxbarsback/BarsSinceEntry figure might be (it could be 1000 days!). Secondly, if it is 1000 days, I cannot backtest the strategy 5 years back (ie ~1100 bars back) to see how robust it is, because this means you are only getting a portfolio backtest duration of 100 days which is almost pointless.

Code: Select all


Input: TrailingATRs (4.5) ;
Input: ATRLength (10) ;

Var: ATRVal (0) ;

ATRVal = Highest (high, barssinceentry) - (AvgTrueRange(ATRLength) * TrailingATRs) ;

If MarketPosition = 1 Then Begin
If High [0] is < ATRval then
Sell ( "ATRtrail" ) next bar at ATRval stop;
End ;
I didn't knew that `BarsSinceEntry` was dependent on the MaxBarsBack setting. Does this portfolio behaviour also occur if you test that there was an actual trade before requesting `BarsSinceEntry`?

Like this:

Code: Select all

if (TotalTrades > 0) then
ATRVal = Highest (high, barssinceentry) - (AvgTrueRange(ATRLength) * TrailingATRs) ;

smhuggins
Posts: 12
Joined: 15 Mar 2015
Has thanked: 1 time

Re: Bars Since Entry issues in portfolio backtester

Postby smhuggins » 17 Aug 2015

Thanks JoshM for the suggestion, but unfortunately it doesn't work.

TotalTrades refers to the total number of closed-out trades, so I tried CurrentShares but that doesn't work either.

Also I tested this across many other different insturments, and this DOES NOT work on a single chart either, despite what previously thought.

There is still the maxbarsback/barssinceenty looping issue.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Bars Since Entry issues in portfolio backtester

Postby Fredi » 18 Aug 2015

May be this Workaround is a help ...

Code: Select all

if barssinceentry <= MaxBarsBack then
MaxHigh = Highest (high, barssinceentry)
else MaxHigh = maxlist(MaxHigh, Highest (high, MaxBarsBack));

ATRVal = MaxHigh - (AvgTrueRange(ATRLength) * TrailingATRs);

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Bars Since Entry issues in portfolio backtester

Postby TJ » 18 Aug 2015

one idea:

you can put the entry bar in a variable,
then add bar increments to each subsequent bars
to find out "bars sine entry".

smhuggins
Posts: 12
Joined: 15 Mar 2015
Has thanked: 1 time

Re: Bars Since Entry issues in portfolio backtester

Postby smhuggins » 18 Aug 2015

Thanks Fredi and TJ

Fredi - I have tried your solution and it seems to definitely solve that problem, so thank you. TJ, I will try yours too.

Though the exit price is not triggering correctly. I want the exit to trigger at 5ATRs below the Highest High, calculated using the ATR value from the day/bar of the Highest High. I suspect it is calculating an ATR from a different bar.

Do you have even the slightest idea how I would specify using the ATR value from the day that the Highest High occurred??

Massive thanks again.

User avatar
signalworks
Posts: 65
Joined: 06 Oct 2013
Location: Germany.Solingen
Has thanked: 23 times
Been thanked: 2 times
Contact:

Re: Bars Since Entry issues in portfolio backtester

Postby signalworks » 04 Mar 2016

Hi,

it seems, it is a general problem with barsSinceEntry and MaxbarsBack. Unfortunately, Fredi's solution is not practical... When I want to check large positions with more bars as defined in MaxBarsBack, it can only check i.e. highest(h, MaxbarsBack). TJ's solution works, but eats code-lines...

There should be a MC intern solution that barsSinceEntry not correlate with maxbarsback. Because Entries where even generate if barnumber is greater than maxbarsback.

Thanks.

regards,
Lutz

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Bars Since Entry issues in portfolio backtester

Postby JoshM » 04 Mar 2016

it seems, it is a general problem with barsSinceEntry and MaxbarsBack. Unfortunately, Fredi's solution is not practical... When I want to check large positions with more bars as defined in MaxBarsBack, it can only check i.e. highest(h, MaxbarsBack). TJ's solution works, but eats code-lines...
I'm not sure if it should eat code lines; I think it can be done in one statement. You might even put it into a function to get a keyword-like behaviour.

Have you tried something like this?

Code: Select all

barsSinceEntry = CurrentBar - PosTradeEntryBar(0, 0); // Bars since entry in current position

Code: Select all

barsSinceEntry = CurrentBar - PosTradeEntryBar(1, 0); // Bars since entry of previous position

User avatar
signalworks
Posts: 65
Joined: 06 Oct 2013
Location: Germany.Solingen
Has thanked: 23 times
Been thanked: 2 times
Contact:

Re: Bars Since Entry issues in portfolio backtester

Postby signalworks » 04 Mar 2016

Hi Josh,

thanks. Not a problem, to work with barsSinceEntry in other cases than in "period"-scenarios (like highest(h, barsSinceEntry). In Period-Scenarios, seems it have to go "by feet":

I.e. to get the distance highestHigh - lowestLow since entry, determine high (hh) and low(ll) at entry, change that and calculate (hh-ll). No other way found til now.
Thanks.

regards,
Lutz


Return to “MultiCharts”