Maxbarsback and trailing stop

Questions about MultiCharts and user contributed studies.
champski
Posts: 71
Joined: 28 Jun 2011
Has thanked: 20 times
Been thanked: 1 time

Maxbarsback and trailing stop

Postby champski » 12 Nov 2019

Hi,

I'm having challenges with my system and the maxbarsback.
My system uses a 20 MA, so my maxbarsback needs to be set to 20. I understand that much.
I then added a trailing stop to my system which now requires a very large maxbarsback e.g. 1000, or else it errors, and now I notice my backtest results miss a heap of trades in the early part of my chart period.
Why would the code below require me to change my maxbarsback setting to such a high number?
Does anyone have any workarounds or solutions?

Here's the code causing the problem:

If MarketPosition = 1 and High >= highest(high,BarsSinceEntry) then begin
mystopprice = High - (High * TrailingStopAmount);
if low <= mystopprice then begin
Sell ("TrailX ") next bar at market ;
end;
end;

Regards
Champski

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Maxbarsback and trailing stop

Postby ABC » 12 Nov 2019

champski,

you introduced a variable length with BarsSinceEntry. This can become quite large depending on how many bars ago this happened and in turn this will affect the required max bars back.
It might make sense to avoid that and to track the highest high during your position yourself.

Regards,

ABC

champski
Posts: 71
Joined: 28 Jun 2011
Has thanked: 20 times
Been thanked: 1 time

Re: Maxbarsback and trailing stop

Postby champski » 13 Nov 2019

champski,

you introduced a variable length with BarsSinceEntry. This can become quite large depending on how many bars ago this happened and in turn this will affect the required max bars back.
It might make sense to avoid that and to track the highest high during your position yourself.

Regards,

ABC
Thanks for highlighting the issue. I need to be able to automate my backtesting so I'm not sure how I can do this by tracking the highest high during the position myself. Any ideas?

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Maxbarsback and trailing stop

Postby ABC » 13 Nov 2019

champski,

by "yourself" I meant write code to do that. You can for example reset a tracking variable when you are flat or directly after the entry and every time a high is higher than the value stored in the variable you update this variable's value to that high.

Regards,

ABC

champski
Posts: 71
Joined: 28 Jun 2011
Has thanked: 20 times
Been thanked: 1 time

Re: Maxbarsback and trailing stop

Postby champski » 13 Nov 2019

ABC,

I've changed my code but I still have the maxbarsback issue. Some of my trades trail for 1000 day bars. Any ideas?

Code:

// Reset my variables
if marketposition = 0 and CurrentHighestHigh <> 0 then begin
CurrentHighestHigh = 0;
mystopprice = 0;
end;

// Set my variables
If marketposition = 1 and high > CurrentHighestHigh then begin
CurrentHighestHigh = high;
mystopprice = CurrentHighestHigh - (CurrentHighestHigh * TrailingStopAmount);
if low <= mystopprice then begin
Sell ("TrailX ") next bar at market ;
end;
end;

Regards
Champski

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Maxbarsback and trailing stop

Postby ABC » 13 Nov 2019

champski,

the problem is likely not coming from the code snippet you posted and there might be other parts causing the issue.

Regards,

ABC

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: Maxbarsback and trailing stop

Postby wilkinsw » 14 Nov 2019

Champski,

With the greatest respect....

" I need to be able to automate my backtesting so I'm not sure how I can do this by tracking the highest high during the position myself. Any ideas?"

That really made me lol!

There will be something in your code used to populate the highest / lowest /average (etc) function, causing this error.

Have a read through your code and ask yourself at every line: - How many bars back could the script potentially be looking to give me answer.

Well coded script will rarely ever look back more than 1 bar and will be faster too.

If you uncover large lookbacks, think how to code it instead by tracking/updating values bar by bar going forward instead.


Return to “MultiCharts”