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;