indicatorproblem with different chartdayback

Questions about MultiCharts and user contributed studies.
User avatar
siscop
Posts: 197
Joined: 09 Jan 2011
Has thanked: 34 times
Been thanked: 29 times

indicatorproblem with different chartdayback

Postby siscop » 12 May 2012

Hi,
I have a problem that I can’t get solved. Hope someone could help me.
Problem:
I have a Indicator _iVolaP that just shows me the length of an ATR(20) related/compared to the last 200 days and shows me the comparisment in percent.

Code: Select all

inputs:
Glaettung(20),
Dauer(200);
Plot1(_fVolaP(Glaettung,Dauer));
Function _fVolaP

Code: Select all

inputs:
Glaettung(NumericSimple),
Dauer(NumericSimple);
variables:
VolaGlaettung(0),
VHigh(0),
VLow(0),
count(0),
Zwischen(0),
Ergebnis(0);

VolaGlaettung=AvgTrueRange(Glaettung);

VHigh=VolaGlaettung;
VLow=VolaGlaettung;
for count=0 to Dauer-1
begin
if VHigh<VolaGlaettung[count] then
VHigh=VolaGlaettung[count];
if VLow>VolaGlaettung[count] then
VLow=VolaGlaettung[count];
end;

if VHigh<>VLow then
Ergebnis=((VolaGlaettung-VLow)/(VHigh-VLow)*100);
if VHigh=VLow then
Ergebnis=0;
_fVolaP=Ergebnis;
The code is quite simple but including it to a chart of S&P500 over the last 300 days gives a other result then putting it on a chart on S&P500 over 600 days.
The upper indicator is based on the 300 days chart and the lower indi on the 600 days chart. Bothe indicator uses the same input parameter so WHY are the result different?
B1.png
(12.74 KiB) Downloaded 412 times
b3.png
(12.77 KiB) Downloaded 398 times
b2.png
(115.72 KiB) Downloaded 399 times
I have been looking at this for days and can't find the error.... Please someone help..

User avatar
siscop
Posts: 197
Joined: 09 Jan 2011
Has thanked: 34 times
Been thanked: 29 times

Re: indicatorproblem with different chartdayback

Postby siscop » 15 May 2012

swz168 was so kind and helped me on my problem. I should use the function of the indicator directly in my code instead a variable that just stores the indicator. This workaround did solve my problem but I do NOT understand it.

Code: Select all

inputs:
Glaettung(NumericSimple),
Dauer(NumericSimple);
variables:
// VolaGlaettung(0),
VHigh(0),
VLow(0),
count(0),
Zwischen(0),
Ergebnis(0);


// VolaGlaettung=AvgTrueRange(Glaettung);
VHigh=AvgTrueRange(Glaettung);
VLow=AvgTrueRange(Glaettung);


for count=0 to Dauer-1
begin
if VHigh<AvgTrueRange[count](Glaettung) then
VHigh=AvgTrueRange[count](Glaettung);
if VLow>AvgTrueRange[count](Glaettung) then
VLow=AvgTrueRange[count](Glaettung);



end;

if VHigh<>VLow then
Ergebnis=((AvgTrueRange(Glaettung) -VLow)/(VHigh-VLow)*100);
if VHigh=VLow then
Ergebnis=0;
_fVolaP=Ergebnis;

This code works perfectly. It just left out the variable “VolaGlaettung” and the code uses directly the “AvgTrueRange[count](Glaettung) ” instead of “VolaGlaettung[count]”.

Can someone please tell me why

Code: Select all

VolaGlaettung=AvgTrueRange(Glaettung);
...
VHigh<VolaGlaettung[count];
is not the same as

Code: Select all

...
VHigh<AvgTrueRange[count](Glaettung);
With the Length of 200 the code with the variable needs more than 600 bars to sync. Without the variable it just uses the 220 bars to sync.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: indicatorproblem with different chartdayback

Postby Henry MultiСharts » 15 May 2012

Code: Select all

VolaGlaettung=AvgTrueRange(Glaettung);
VHigh<VolaGlaettung[count];
In this code you are referring to the historical values of VolaGlaettung[count] variable in a cycle.
This history starts at the first bar of the calculation. Historical values will be zero until CurrentBar becomes >Count (i.e. Dauer-1). That means the indicator will stabilize from the chart bar ~ ( MaxBarsBack+Dauer).

Code: Select all

VHigh<AvgTrueRange[count](Glaettung);
AvgTrueRange[count](Glaettung) function is calculated in a cycle. The calculation is possible only when MaxBarsBack = Dauer. The indicator will stabilize from the chart bar = MaxBarsBack. For earlier stabilization you pay with excess function calls.

There are multiple possibilities to improve the code:
1) AvgTrueRange is called once, postponed indicator stabilization (from MaxBarsBack+Dauer), incorrect plot values are not shown.
Indicator code:

Code: Select all

if currentbar > Dauer then
plot1(_fVolaP(Glaettung,Dauer));
Function code remains the same as in the first post:

Code: Select all

inputs:
Glaettung(NumericSimple),
Dauer(NumericSimple);
variables:
VolaGlaettung(0),
VHigh(0),
VLow(0),
count(0),
Zwischen(0),
Ergebnis(0);

VolaGlaettung=AvgTrueRange(Glaettung);

VHigh=VolaGlaettung;
VLow=VolaGlaettung;
for count=0 to Dauer-1
begin
if VHigh<VolaGlaettung[count] then
VHigh=VolaGlaettung[count];
if VLow>VolaGlaettung[count] then
VLow=VolaGlaettung[count];
end;

if VHigh<>VLow then
Ergebnis=((VolaGlaettung-VLow)/(VHigh-VLow)*100);
if VHigh=VLow then
Ergebnis=0;
_fVolaP=Ergebnis;
2) AvgTrueRange is called twice (compared to six calls suggested by swz168). The indicator will stabilize from the chart bar = MaxBarsBack, all plot values are plotted.
Indicator code remains the same as in the first post:

Code: Select all

inputs:
Glaettung(20),
Dauer(200);
Plot1(_fVolaP(Glaettung,Dauer));
_fVolaP function is modified according to the suggested method (cutting excess AvgTrueRange calls):

Code: Select all

inputs:
Glaettung(NumericSimple),
Dauer(NumericSimple);
variables:
VolaGlaettung(0),
VHigh(0),
VLow(0),
count(0),
Zwischen(0),
Ergebnis(0);

VolaGlaettung=AvgTrueRange(Glaettung);
VHigh=VolaGlaettung;
VLow=VolaGlaettung;

for count=0 to Dauer-1
begin
//VALUE2 = AvgTrueRange(Glaettung)[count];
VALUE2 = VolaGlaettung[count];
if VHigh<VALUE2 then VHigh=VALUE2;
if VLow>VALUE2 then VLow=VALUE2;
end;


if VHigh<>VLow then
Ergebnis=((VolaGlaettung-VLow)/(VHigh-VLow)*100);
if VHigh=VLow then
Ergebnis=0;
_fVolaP=Ergebnis;


Return to “MultiCharts”