DMI is affected by Max Bars Back  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

DMI is affected by Max Bars Back

Postby pcrespo » 20 Nov 2016

MC version 10 Beta 2
OS Win 7 Ultimate 64-bit

I noticed some odd behavior on a chart, so I did a test:

Code: Select all

input: len(5);
vars:
filename("C:\Users\user\Desktop\" + NumToStr(len,0) + " & " + NumToStr(maxbarsback,0) + ".txt"),
plus(0),
minus(0);

if barstatus=2 and currentbar<21 then begin
DirMovement(High, Low, Close, len, plus, minus, value1, value2, value3, value4);
print(file(filename),currentbar," ",date," ",time," ",plus," ",minus);
end;
  • With len=5 and Max Bars Back = 5
    ...
    6.00 1070702.00 500.00 21.63 20.28
    7.00 1070702.00 600.00 16.87 22.11
    8.00 1070702.00 700.00 31.98 14.63
    9.00 1070702.00 800.00 52.10 9.83
    10.00 1070702.00 900.00 44.76 8.44
    ...
  • Len=5 and MBB=10
    1.00 1070702.00 500.00 9.38 21.88
    2.00 1070702.00 600.00 7.36 23.31
    3.00 1070702.00 700.00 25.38 15.56
    4.00 1070702.00 800.00 47.41 10.51
    5.00 1070702.00 900.00 40.80 9.05
    ...

Unlike, say, SMA, the DMI calculation returns different values for the same hour when the Max Bars Back is different. Two questions:

1. Why is this? (As of yet I haven't found a reason in the DirMovement code.)

2. In MC, you can set a different MBB for each indicator (or Auto-detect will do that if necessary). But when using Portfolio Trader, only one Max Bars Back can be set. This means if I have a signal using one indicator with a length of 10, and DMI with length 5, I have to set MBB=10, which will apparently screw up the DMI. Is there a solution?

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

Re: DMI is affected by Max Bars Back

Postby TJ » 20 Nov 2016

MC version 10 Beta 2
OS Win 7 Ultimate 64-bit

I noticed some odd behavior on a chart, so I did a test:

Code: Select all

input: len(5);
vars:
filename("C:\Users\user\Desktop\" + NumToStr(len,0) + " & " + NumToStr(maxbarsback,0) + ".txt"),
plus(0),
minus(0);

if barstatus=2 and currentbar<21 then begin
DirMovement(High, Low, Close, len, plus, minus, value1, value2, value3, value4);
print(file(filename),currentbar," ",date," ",time," ",plus," ",minus);
end;
  • With len=5 and Max Bars Back = 5
    ...
    6.00 1070702.00 500.00 21.63 20.28
    7.00 1070702.00 600.00 16.87 22.11
    8.00 1070702.00 700.00 31.98 14.63
    9.00 1070702.00 800.00 52.10 9.83
    10.00 1070702.00 900.00 44.76 8.44
    ...
  • Len=5 and MBB=10
    1.00 1070702.00 500.00 9.38 21.88
    2.00 1070702.00 600.00 7.36 23.31
    3.00 1070702.00 700.00 25.38 15.56
    4.00 1070702.00 800.00 47.41 10.51
    5.00 1070702.00 900.00 40.80 9.05
    ...

Unlike, say, SMA, the DMI calculation returns different values for the same hour when the Max Bars Back is different. Two questions:

1. Why is this? (As of yet I haven't found a reason in the DirMovement code.)

2. In MC, you can set a different MBB for each indicator (or Auto-detect will do that if necessary). But when using Portfolio Trader, only one Max Bars Back can be set. This means if I have a signal using one indicator with a length of 10, and DMI with length 5, I have to set MBB=10, which will apparently screw up the DMI. Is there a solution?

Your input len might be 5,
but the indicator looks further back than 5.

You have a misunderstanding of the purpose of the MBB. Many people (esp newbies) make the same mistake.
Please go back to the manual and study the MBB explanation again.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 20 Nov 2016

You have a misunderstanding of the purpose of the MBB. Many people (esp newbies) make the same mistake.
Please go back to the manual and study the MBB explanation again.
I'm guilty of being a newbie, but none of the manuals (including "mc_users_manual") lend any insight on this issue. I haven't yet found the answer in old threads either. My manual is from October 19, 2015; if there is a newer one with a more thorough explanation of MBB's behavior, I'd appreciate a link.

The EL/PL FAQ only says the following on MBB (which I understand, but it does not explain the DMI behavior):
MaxBarsBack value totally depends on your script. For example, if your script references 50 bars back in its calculation, then the MaxBarsBack value should be set at least to 50. If it is set to 100, first 50 bars will be in fact ignored, next 50 bars will be considered in script calculation and its results on bar # 101.
Returning to what you said,
Your input len might be 5,
but the indicator looks further back than 5.
To me this isn't obvious from the code itself, but rather is due to a process happening behind the scenes that I haven't seen explained anywhere.

But not only would I like to understand how/why it's happening, I'd like to know if there's a way to make DirMovement not look back further than 5. Do I need to write a custom indicator with a few tweaks of DirMovement?

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

Re: DMI is affected by Max Bars Back

Postby TJ » 20 Nov 2016

You have a misunderstanding of the purpose of the MBB. Many people (esp newbies) make the same mistake.
Please go back to the manual and study the MBB explanation again.
I'm guilty of being a newbie, but none of the manuals (including "mc_users_manual") lend any insight on this issue. I haven't yet found the answer in old threads either. My manual is from October 19, 2015; if there is a newer one with a more thorough explanation of MBB's behavior, I'd appreciate a link.
::
Please copy and paste the manual you have and we can go through it together.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 20 Nov 2016

Please copy and paste the manual you have and we can go through it together.
The Maximum number of bars a study will reference
A study's script can refer to the values of previous bars for use in evaluation of the current bar.
When a value of a bar N bars ago is referenced in a script, the study can only be visualized starting with the N + 1
bar.
The number of previous bars that must be available for a script in order to start performing calculations is called
Maximum number of bars a study will reference, or MaxBarsBack.
Which is stuff I know. The next few sentences talk about auto-detect mode, which is not applicable to Portfolio Trader.

The manual says the same thing about MBB for Indicators and Signals.

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

Re: DMI is affected by Max Bars Back

Postby TJ » 20 Nov 2016

Please copy and paste the manual you have and we can go through it together.
The Maximum number of bars a study will reference
A study's script can refer to the values of previous bars for use in evaluation of the current bar.
When a value of a bar N bars ago is referenced in a script, the study can only be visualized starting with the N + 1
bar.
The number of previous bars that must be available for a script in order to start performing calculations is called
Maximum number of bars a study will reference, or MaxBarsBack.
Which is stuff I know. The next few sentences talk about auto-detect mode, which is not applicable to Portfolio Trader.

The manual says the same thing about MBB for Indicators and Signals.

Did you get the N+1 part?

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 20 Nov 2016

Did you get the N+1 part?
I took it to mean that when MBB = N, the calculation can't begin until the (N+1)th bar.

But I see that the phrasing is different: N is the number of bars ago referenced. So if the calculation begins at bar N+1, does that imply that the Nth bar back is being used in the equations?

I know that with MBB=10, the 11th bar of the chart is the first bar where DMI is calculated. However, I don't know how many bars are back are used in the actual calculation. It's evidently more than 5, like you said. But is it using 10 bars back? When I set Len=10, that gets different results, so that would suggest no.

Why does it seem like MBB behaves differently for DMI than SMA? With SMA, the "first" bar is MBB+1, but from that bar it only looks back the number of bars specificed by the sma length. So when varying MBB, the only difference in the values is that they're shifted. I can map each element from one list to an element of the other list.

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

Re: DMI is affected by Max Bars Back

Postby TJ » 20 Nov 2016

Did you get the N+1 part?
I took it to mean that when MBB = N, the calculation can't begin until the (N+1)th bar.

But I see that the phrasing is different: N is the number of bars ago referenced. So if the calculation begins at bar N+1, does that imply that the Nth bar back is being used in the equations?

I know that with MBB=10, the 11th bar of the chart is the first bar where DMI is calculated. However, I don't know how many bars are back are used in the actual calculation. It's evidently more than 5, like you said. But is it using 10 bars back? When I set Len=10, that gets different results, so that would suggest no.

Why does it seem like MBB behaves differently for DMI than SMA? With SMA, the "first" bar is MBB+1, but from that bar it only looks back the number of bars specificed by the sma length. So when varying MBB, the only difference in the values is that they're shifted. I can map each element from one list to an element of the other list.

Have you looked into the codes in DirMovement?

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 21 Nov 2016

Have you looked into the codes in DirMovement?
Yes, several times now. Nothing I see makes me think it should exceed Len when looking back. Everywhere "Len" appears in the code, nothing jumps out at me. I also looked at the function "TrueRangeCustom", and still no clues.

Below is the code, if you want to highlight anything that should have caught my eye.

Code: Select all

inputs:
PriceValueH( numericseries ),
PriceValueL( numericseries ),
PriceValueC( numericseries ),
Len( numericsimple ),
oDMIPlus( numericref ),
oDMIMinus( numericref ),
oDMI( numericref ),
oADX( numericref ),
oADXR( numericref ),
oVolty( numericref ) ;

variables:
var0( 0 ),
var1( 0 ),
var2( 0 ),
var3( 0 ),
var4( 0 ),
var5( 0 ),
var6( 0 ),
var7( 0 ),
var8( 0 ),
var9( 1 / Len ),
var10( 0 ) ;

if CurrentBar = 1 then
begin
for Value1 = 0 to Len - 1
begin
var0 = 0 ;
var1 = 0 ;
var2 = PriceValueH[Value1] - PriceValueH[ Value1 + 1 ] ;
var3 = PriceValueL[ Value1 + 1 ] - PriceValueL[Value1] ;
condition1 = var2 > var3 and var2 > 0 ;
if condition1 then
var0 = var2
else
begin
condition1 = var3 > var2 and var3 > 0 ;
if condition1 then
var1 = var3 ;
end;
var4 = var4 + var0 ;
var5 = var5 + var1 ;
var6 = var6 + TrueRangeCustom( PriceValueH, PriceValueL, PriceValueC )[Value1] ;
end ;
var7 = var4 / Len ;
var8 = var5 / Len ;
oVolty = var6 / Len ;
end
else
begin
var0 = 0 ;
var1 = 0 ;
var2 = PriceValueH - PriceValueH[1] ;
var3 = PriceValueL[1] - PriceValueL ;
condition1 = var2 > var3 and var2 > 0 ;
if condition1 then
var0 = var2
else
begin
condition1 = var3 > var2 and var3 > 0 ;
if condition1 then
var1 = var3 ;
end;
var7 = var7[1] + var9 * ( var0 - var7[1] ) ;
var8 = var8[1] + var9 * ( var1 - var8[1] ) ;
oVolty = oVolty[1] + var9 * ( TrueRangeCustom( PriceValueH, PriceValueL, PriceValueC )
- oVolty[1] ) ;
end ;

if oVolty > 0 then
begin
oDMIPlus = 100 * var7 / oVolty ;
oDMIMinus = 100 * var8 / oVolty ;
end
else
begin
oDMIPlus = 0 ;
oDMIMinus = 0 ;
end ;

var10 = oDMIPlus + oDMIMinus ;
if var10 > 0 then
oDMI = 100 * AbsValue( oDMIPlus - oDMIMinus ) / var10
else
oDMI = 0 ;

condition1 = CurrentBar <= Len and CurrentBar > 0 ;
if condition1 then
begin
oADX = Cum( oDMI ) / CurrentBar ;





oADXR = ( oADX + oADX[ CurrentBar - 1 ] ) * .5 ;

end
else
begin
oADX = oADX[1] + var9 * ( oDMI - oADX[1] ) ;
oADXR = ( oADX + oADX[ Len - 1 ] ) * .5 ;
end ;

DirMovement = 1 ;

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

Re: DMI is affected by Max Bars Back

Postby TJ » 21 Nov 2016

Every time you see an [1] appended to the end of a variable,
you are extending the lookback by 1.

If there is a function inside the function,
and inside the function, there is an [1] appended to one of the variables,
you are looking at MBB = Len+1+1... and so on.

Why do you want to set the MBB to 5? or 10?
There is a reason the auto setting is at 50 -- so that you do not run into calculation errors or inconsistencies.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 22 Nov 2016

If there is a function inside the function,
and inside the function, there is an [1] appended to one of the variables,
you are looking at MBB = Len+1+1... and so on.
Ahh ok, I didn't notice recursion in the code. I saw the [1] but thought it was only going back 1, not 1+1+...

I'll have to play around writing my own similar code to get a better understanding. Thanks for the help so far.
Why do you want to set the MBB to 5? or 10?
I don't; that was only an experiment to observe what MBB was doing. I have an indicator with a length of 240 and DMI with length 100. I'm forced to set MBB >= 240, but from what I gather, that will make DMI look back longer than 100, which I don't want (I made the DMI length 100 because I want it to look back 100 -- no more, no less).

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

Re: DMI is affected by Max Bars Back

Postby TJ » 22 Nov 2016

Why do you want to set the MBB to 5? or 10?
I don't; that was only an experiment to observe what MBB was doing. I have an indicator with a length of 240 and DMI with length 100. I'm forced to set MBB >= 240, but from what I gather, that will make DMI look back longer than 100, which I don't want (I made the DMI length 100 because I want it to look back 100 -- no more, no less).

Here lies your misunderstanding...

If your DMI code is required to look back 100 bars to calculate the result,
it will only look back 100 bars. It will not look back further than 100 bars.

Considerf this simple example -- a simple 100 period moving average.
No matter what you set your MBB to be, it can be 10, it can be 1000,
the indicator will only give you a 100 period moving average. Not more, Not less.
In order to give you a 100 period moving average,
the indicator will look back exactly 100 period to perform the calculation, regardless of your MBB.
The indicator will NOT look back 99 period to perform the calculation.
The indicator will NOT look back 101 period to perform the calculation.
The indicator will look back exactly 100 period to perform the calculation.

Got it?
Give it a try and see.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 22 Nov 2016

If your DMI code is required to look back 100 bars to calculate the result,
it will only look back 100 bars. It will not look back further than 100 bars.
But then why, when I set the lookback (Len) to 5, do I get different DMI+ and DMI- numbers with MBB=5 vs MBB=10 (as shown in the two lists in my first post, in green and red)?
Considerf this simple example -- a simple 100 period moving average.
Like I said, I don't have this problem with SMA, only DMI. I suspect it's because of what you said in your previous post about the [1]'s attached to the variables in the DMI code (which I'm about to experiment with, and it might all make sense once I do).

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

Re: DMI is affected by Max Bars Back

Postby TJ » 22 Nov 2016

If your DMI code is required to look back 100 bars to calculate the result,
it will only look back 100 bars. It will not look back further than 100 bars.
But then why, when I set the lookback (Len) to 5, do I get different DMI+ and DMI- numbers with MBB=5 vs MBB=10 (as shown in the two lists in my first post, in green and red)?

Why do you think when your simplistic singular input Len=5,
that your complex multi-layered indicator's ACTUAL total look back should also be 5?
I thought I have already explained to you in prev post.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 22 Nov 2016

Why do you think when your simplistic singular input Len=5,
that your complex multi-layered indicator's ACTUAL total look back should also be 5?
I thought I have already explained to you in prev post.
You did, and I'll fully grasp it after some more playing around. (Your last post made me wonder if I had misread your previous posts, but now I know I didn't. Your posts were consistent with one another.)

But one more question which is probably simple -- if the actual lookback is exceeding 5, then why doesn't PT throw the "exceeds MBB" error message when I have MBB=5 ?

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

Re: DMI is affected by Max Bars Back

Postby TJ » 22 Nov 2016

Why do you think when your simplistic singular input Len=5,
that your complex multi-layered indicator's ACTUAL total look back should also be 5?
I thought I have already explained to you in prev post.
You did, and I'll fully grasp it after some more playing around. (Your last post made me wonder if I had misread your previous posts, but now I know I didn't. Your posts were consistent with one another.)

But one more question which is probably simple -- if the actual lookback is exceeding 5, then why doesn't PT throw the "exceeds MBB" error message when I have MBB=5 ?

Don't know. You should just leave it on auto.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 22 Nov 2016

Don't know. You should just leave it on auto.
I'm using Portfolio Trader. I thought PT didn't have auto mbb?

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

Re: DMI is affected by Max Bars Back

Postby TJ » 22 Nov 2016

Don't know. You should just leave it on auto.
I'm using Portfolio Trader. I thought PT didn't have auto mbb?

Then take your best estimate and add 50 to it.

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back

Postby pcrespo » 22 Nov 2016

Then take your best estimate and add 50 to it.
My point is there are two seemingly contradictory facts:

1. DMI's true lookback exceeds the input length.
2. When I set MBB=Len, there is never an error message.

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

Re: DMI is affected by Max Bars Back

Postby ABC » 23 Nov 2016

pcrespo,

I don't think MBB is the real problem here. Try the following:
Set a date block in your code and only have it start when the chart date >= this date.
Set this date to the beginning of 2016 and start your chart a couple of month earlier.

Does changing the MBB value give you a different result now (provided you don't set a MBB value so high that
you delay the indicator start after the beginning of 2016)?

In my opinion you should get the same results now as the answer is in the indicator code. When you use indicators that use previous bar's results you can find traces of each data point (although this effect might be small) in your results. Changing the start point as you do with varying the MBB values might get rid of outliers and therefore create a different outcome. There might be other parameters that add to the result you get here, but the effect described is surely something you will have to some extend.
And you can test it with what I described above.

Regards,

ABC

User avatar
pcrespo
Posts: 49
Joined: 07 Feb 2015
Has thanked: 7 times
Been thanked: 4 times

Re: DMI is affected by Max Bars Back  [SOLVED]

Postby pcrespo » 23 Nov 2016

You're right!
Try the following:
Set a date block in your code and only have it start when the chart date >= this date.
Set this date to the beginning of 2016 and start your chart a couple of month earlier.
I took this concept and did something similar: I started calculating after currentbar > N. With a sufficient N, the MBB never matters.

Now I notice that without that restriction, the two lists do converge. They begin wildly different, but after about 25 bars they start getting very close, and later on are exactly equal.

Thanks!

And I think I finally know why they begin different: the DMI code uses the previous calculations if there are any. With MBB=5, the calculations begin earlier, so the calculation of the 5pm bar I pasted was based on those. With MBB=10 the 5pm bar was the 1st bar, so it was calculated from scratch.


Return to “MultiCharts”