I was calculating the daily and historical volatility for a stock and displaying on the Chart to track the levels.

The below given is the code for Daily and historical volatility.

```
///// code starts here
HAV_Curr[0]=1;
d_Open = TimeFrameGetPrice("O",inDaily,0);
d_High = TimeFrameGetPrice("O",inDaily,0);
d_Low = TimeFrameGetPrice("O",inDaily,0);
d_Close = TimeFrameGetPrice("O",inDaily,0);
d_Close1 = TimeFrameGetPrice("O",inDaily,-1);
d_Close2 = TimeFrameGetPrice("O",inDaily,-2);
HAV = ln(d_close1/d_close2);
HAV_Curr = sqrt(0.94 *(Ref(HAV_Curr,-1)^2))+(.06* HAV^2);
HVOlt = HAV_Curr * sqrt(365);
pxHeight = Status( "pxchartheight") ;
x = Status( "pxchartwidth");
YstartPosition = 250;
Yincrementposition = 15;
GfxTextOut("Daily volt : "+ HAV_Curr,x-275,pxHeight-(580-(15*30)));
GfxTextOut("Hist volt : "+ HVOlt,x-275,pxHeight-(580-(15*31)));
/// code ends here
```

the above code is behaving differently from time to time. The numbers seems jumping between whole numbers and decimals.Attached are the screen shots.

Please let me know how to fix this.

As for your AFL question. All you need to do is reading below sample links and applying proper formatting accordingly in order to get constant decimal places.
https://www.amibroker.com/guide/afl/strformat.html
http://www.amibroker.com/kb/2016/01/18/how-to-fix-error-61-in-printfstrformat-calls/
http://www.amibroker.com/kb/2015/02/13/customizing-chart-titles/

So you would have to add formatting to

```
"Daily volt : "+ HAV_Curr
```

as well as to

```
"Hist volt : "+ HVOlt
```

Secondly (again), important read as far as forum usage is concerned:
http://forum.amibroker.com/t/how-to-use-this-site/?source_topic_id=785
You even have 10 minutes time left to edit your post. And the even more astonishing thing isâ€¦ there is even a post preview right of your forum editor. You just have to click â€ś<> â€ť button in forum editorâ€™s toolbar to insert your code within upper and lower three dots. There is even a place holding text saying â€śtype or paste code here â€ť . Please do not make moderator(s) wasting time on fixing your posts and please show some respect to your readers! Thank you!

Why is it important to add code tags? It is making post better readable and it avoids formatting issues, for example one of your code lines not being inserted within tags looks as follows

```
HAV_Curr = sqrt(0.94 (Ref(HAV_Curr,-1)^2)) + (.06 HAV^2);
```

Instead it would have to be like so

```
HAV_Curr = sqrt(0.94 * (Ref(HAV_Curr,-1)^2)) + (.06 * HAV^2);
```

So the multiplication sign has been removed by forum software in your case.

Tomasz
June 18, 2017, 1:05pm
3
Your issue is THE SAME as discussed two days ago:

I know that everybody thinks his issue is â€śuniqueâ€ť but it isnâ€™t. So use Search before posting.

Thanks fxshrat. I will adhere to the standards henceforth and make it more comprehensive.

I tried using the following code in version 6.00.1

```
HAV_Curr[0]=1;
d_Open = TimeFrameGetPrice("O",inDaily,0);
d_High = TimeFrameGetPrice("O",inDaily,0);
d_Low = TimeFrameGetPrice("O",inDaily,0);
d_Close = TimeFrameGetPrice("O",inDaily,0);
d_Close1 = TimeFrameGetPrice("O",inDaily,-1);
d_Close2 = TimeFrameGetPrice("O",inDaily,-2);
SetBarsRequired( sbrAll, sbrAll );
HAV = ln(d_close1/d_close2);
HAV_Curr = sqrt(0.94 *(Ref(HAV_Curr,-1)^2))+(.06* HAV^2);
HVOlt = HAV_Curr * sqrt(365);
pxHeight = Status( "pxchartheight") ;
x = Status( "pxchartwidth");
YstartPosition = 250;
Yincrementposition = 15;
GfxTextOut(StrFormat("Daily volt : %g", HAV_Curr),x-275,pxHeight-(580-(15*30)));
GfxTextOut(StrFormat("Hist volt : %g", HVOlt),x-275,pxHeight-(580-(15*31)));
```

The result still displays different random numbers(example 1.75,2048,9068,7.5 etc) for â€śHist Voltâ€ť and not providing any proper constant. I checked this from the first bar in the chart and found that the value for ref(hav_curr,-1) gets accumulated rather considering the previous value.

Please help me on the missing things here.

Tomasz
June 19, 2017, 9:39am
6
Your code is wrong because it self-references `HAV_Curr`

variable. You need to first read Understand AFL . It is essential reading.

This

```
HAV_Curr = sqrt(0.94 *(Ref(HAV_Curr,-1)^2))+(.06* HAV^2);
```

is incorrect.
You are trying just to exponentially smooth the HAV (1st order IIR). This should be properly done using EMA or AMA

```
HAV_Curr = sqrt( AMA( HAV ^ 2, 0.06 ) );
```

See `AMA()`

function.

1 Like

Thanks Tomasz for the guidance. I tried the same and it worked well as you instructed.

Yes, I read the AMA function and fixed the code as per Tomasz
recommendation.

This is new to me and apologize for disturbing experts on such petty
issues.