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
Maxbarsback and trailing stop
- ABC
- Posts: 721
- Joined: 16 Dec 2006
- Location: www.abctradinggroup.com
- Has thanked: 125 times
- Been thanked: 409 times
- Contact:
Re: Maxbarsback and trailing stop
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
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
Re: Maxbarsback and trailing stop
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?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
- ABC
- Posts: 721
- Joined: 16 Dec 2006
- Location: www.abctradinggroup.com
- Has thanked: 125 times
- Been thanked: 409 times
- Contact:
Re: Maxbarsback and trailing stop
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
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
Re: Maxbarsback and trailing stop
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
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
- ABC
- Posts: 721
- Joined: 16 Dec 2006
- Location: www.abctradinggroup.com
- Has thanked: 125 times
- Been thanked: 409 times
- Contact:
Re: Maxbarsback and trailing stop
champski,
the problem is likely not coming from the code snippet you posted and there might be other parts causing the issue.
Regards,
ABC
the problem is likely not coming from the code snippet you posted and there might be other parts causing the issue.
Regards,
ABC
Re: Maxbarsback and trailing stop
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.
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.