# Standard deviation of last 10 days close

I am trying calculate volatility and got struck . Could be a silly question .

``````Volatality = StDev(ln(C/Ref(C,-1)), 10)*100*sqrt(252); // calculating volatility of last 1o closes
``````

how do i get it for last 10 days Daily closes ?

I tried this way but no use

``````DayC = 	TimeFrameGetPrice("C", inDaily, -1);	// yesterdays close
VolatalityDays = StDev(ln(DayC/Ref(DayC,-1)), 10)*100*sqrt(252);

``````

appreciate you help

Use TimeFrameSet then.

``````// Code if using shorter interval than EOD (e.g. 1-min, 5-min, ...., Hourly, ...)
tmfrm = inDaily;

TimeFrameSet( tmfrm );
VolatalityDays = StDev(ln(C/Ref(C,-1)), 10)*100*sqrt(252);
TimeFrameRestore();

VolatalityDays = TimeFrameExpand( VolatalityDays, tmfrm, expandLast);
``````

If you just use (as you did)

``````DayC = 	TimeFrameGetPrice("C", inDaily, -1);	// yesterdays close
``````

then only Close array will be compressed to EOD but not the calculations you do after that.

6 Likes

Thank you very much .

Hi @fxshrat

I am trying to do find one standard deviation of last say "X day" range and add it to the current days open(subtract) price to get the expected range for the day..

``````// Code if using shorter interval than EOD (e.g. 1-min, 5-min, ...., Hourly, ...)

_SECTION_BEGIN("Expected_Intra_Move");
dtDayStartComp = TimeFrameCompress( DateTime(), inDaily, compressOpen );
dtDayStartExp = TimeFrameExpand( dtDayStartComp, inDaily, expandFirst );
dtDayEndComp = TimeFrameCompress( DateTime(), inDaily, compressLast );
dtDayEndExp = TimeFrameExpand( dtDayEndComp, inDaily, expandFirst );
DayCond = DateTime() >= dtDayStartExp AND DateTime() < dtDayEndExp;

SetBarsRequired(sbrAll);
Todays_Open = TimeFrameGetPrice( "O", inDaily, 0 );

//TimeFrameSet(inDaily);

tmfrm = inDaily;
TimeFrameSet( tmfrm );
Range = ATR(14);

VolatalityDays = StDev(ln(Range/Ref(Range,-1)), 30)*100*sqrt(252);
printf( "\n Range = %.2f \n", Range );
printf( "\n VolatalityDays = %.2f \n", VolatalityDays );

TimeFrameRestore();

VolatalityDays = TimeFrameExpand( VolatalityDays, tmfrm, expandLast);

Upper_Range=Todays_Open+(VolatalityDays);
Plot( IIf( DayCond, Upper_Range, Null ), "Upper Range", colorGreen, styleDashed, Null, Null, 0 );
Lower_Range=Todays_Open-(VolatalityDays);
Plot( IIf( DayCond, Lower_Range, Null ), "Lower Range", colorRed, styleDashed, Null, Null, 0 );

``````

But the value it is not working as expected.. at some places, the value of standard deviation is negative..

Can you please suggest where I am going wrong?

sorry,forgot to give credit in Alf I reused you logic