Plot horizontal dynamic Standard Deviatin Lines on price chart

Hello,

How to plot horizontal One and Two Standard Deviation moves Upmove and Down move away from the closing price as shown in the attached picture?

I tried this but it seems not to be correct
Periods = 50;
difference = C - MA(C,Periods);
mean = MA(difference,Periods);
lvmean = LastValue(mean);
sdmean = StDev(mean, Periods);
top = LastValue(mean + sdmean);
bottom = LastValue(mean - sdmean);

Plot(lvmean, _DEFAULT_NAME(),colorGreen,styleLeftAxisScale);
Plot(top, _DEFAULT_NAME(),colorGreen,styleLeftAxisScale);
Plot(bottom, _DEFAULT_NAME(),colorGreen,styleLeftAxisScale);
GraphLabelDecimals = 2;

Thank you

Untitled

1 - use the code button when you add a code.
2 - A standard deviation already calculates the mean (see Standard deviation - Wikipedia). Are you sure that you need a mean calculation before the standard deviation? You can add each field of your code in an exploration and see the result. Based om the result you can verify the result and move on to the next step.

1 Like

Thank you for the reply. I just want to plot one sigma and two sigma horizontal lines from the current price. i.e +1 S.D , +2 S.D ,-1 S.D , -2 S.D

// Plot Standard Deviations from the Current price //

How to calculate the sigma values?

Plot(,"+1 S.D",colorRed,styleLine);
Plot(,"+2 S.D",colorOrange,styleLine);
Plot(,"-1 S.D",colorGreen,styleLine);
Plot(,"-2 S.D",colorWhite,styleLine);

That is incorrect. The first argument to StDev function is a input DATA array,
so it should be:

sdmean = StDev( Close, Periods);

https://www.amibroker.com/f?stdev

1 Like

Thank you.

How to calculate and plot horizontal lines in the price chart
+1 Sigma
+2 Sigma
-1 Sigma
-2 Sigma from the current price as can be seen the attached image they are demarcated by vertical blue lines.

For Example:-
Current price is 469.15
+1 Sigma = 501
+2 Sigma = 540
-1 Sigma = 430
-2 Sigma = 400

// Plot Standard Deviations from the Current price //
Periods = 50;
sdmean = StDev( Close, Periods);

Plot(,"+1 S.D",colorRed,styleLine);
Plot(,"+2 S.D",colorOrange,styleLine);
Plot(,"-1 S.D",colorGreen,styleLine);
Plot(,"-2 S.D",colorWhite,styleLine);

Instead of repeating the same post twice, you have to show YOUR OWN EFFORT. Did you even bother to read my answer and use the code (replace faulty line with correct line in the original post code)?

2 Likes

Sorry, I was trying to explain clearly. I tried this , I got the horizontal line, but I am not sure whether the calculations are correct. I am not from math background. Trying to overlay SD lines on Price chart

Periods = 50;
difference = C - MA(C,Periods);
mean = MA(difference,Periods);
lvmean = LastValue(mean);
sdmean = StDev(Close, Periods);
top = LastValue(mean + sdmean);
bottom = LastValue(mean - sdmean);

Plot(lvmean, "+1SD",colorGreen,styleLeftAxisScale);
Plot(top	, "SD",colorGreen,styleLeftAxisScale);
Plot(bottom, "-1SD",colorGreen,styleLeftAxisScale);
GraphLabelDecimals = 2;

image

Thank you

Are you trying to put the 3 stDev lines above and below the last close @amisur? So you can see if the close of the bar fits within a certain StDev? If so then just add and subtract your values from the last close and I think that you will get what you want.

1 Like

Thankyou. I tried to add and subtract the values from the last close. But I am getting bands instead of horizontal lines above and below the closing price.

Periods = 20;
difference = C - MA(C,Periods);
mean = MA(difference,Periods);
lvmean = LastValue(mean);
sdmean = StDev(Close, Periods);
top = LastValue(mean + sdmean);
bottom = LastValue(mean - sdmean);


Plot(lvmean, "+1SD",colorGreen,styleLeftAxisScale);
Plot(C+top	, "SD",colorGreen,styleLeftAxisScale);
Plot(C-bottom, "-1SD",colorGreen,styleLeftAxisScale);
GraphLabelDecimals = 2;

image

@amisur - just use the 'last value' of your close - so LastValue(C) instead of the full array

1 Like

Thank you so much. Now the values are getting displayed. But still not on the price chart. What should be added in the above code, so that the lines are plotted on regular candlestick price chart?

image

@amisur - you just have to put the code that you wrote in the same chart as the price chart that you want it displayed on or you can plot the price Plot(Close,"Close",colorBlack,styleCandle); within the chart that you have written the StDev code.

1 Like

I tried this and getting the result. When I scroll back the price chart the values are fixed and not changing. Can I make it dynamic?

Periods = 20;
difference = C - MA(C,Periods);
mean = MA(difference,Periods);
lvmean = LastValue(mean);
sdmean = StDev(Close, Periods);
top = LastValue(mean + sdmean);
bottom = LastValue(mean - sdmean);


Plot(LastValue(C)+lvmean, "+1SD",colorGreen,styleLeftAxisScale);
Plot(LastValue(C)+top	, "SD",colorGreen,styleLeftAxisScale);
Plot(LastValue(C)-bottom, "-1SD",colorGreen,styleLeftAxisScale);
Plot(Close,"Close",colorBlack,styleCandle);
GraphLabelDecimals = 2;

sure @amisur - check out AFL Function Reference - LASTVISIBLEVALUE

2 Likes

Finally I came up with the below code. However I feel it needs improvement. Hope some seniors give suggestions in this regard. when I select the previous close the values are not changing.

//No.of Periods
Periods = 18;

//Formula for Standard Deviation
stdn = StDev(Close, Periods);

//Plot the closing Price
Plot(Close,"Close",colorBlack,styleCandle);

//Plot Two Standard Deviation Up from close price
Plot(LastValue(C)+LastValue(stdn), "+1SD",colorRed,styleDashed);
Plot(LastValue(C)+LastValue(stdn*2), "+2SD",colorRed,styleDashed);

//Plot Two Standard Deviation Down from close price
Plot(LastValue(C)-LastValue(stdn), "-1SD",colorGreen,styleDashed);
Plot(LastValue(C)-LastValue(stdn*2), "-2SD",colorGreen,styleDashed);
GraphLabelDecimals = 2;

image

Thank you for the support

@amisur ,

From your comments, I am not sure what you are wanting.

In one part you are talking about Horizontal lines, but then you are talking about dynamic when scrolling back.

Here is a slight modification of your code to give you the dynamic plot (not horizontal line) of the SD levels.

//No.of Periods
Periods = 18;

//Formula for Standard Deviation
stdn = StDev(Close, Periods);

//Plot the closing Price
Plot(Close,"Close",colorBlack,styleCandle);

//Plot Two Standard Deviation Up from close price
Plot((C)+(stdn), "+1SD",colorRed,styleDashed);
Plot((C)+((stdn) * 2), "+2SD",colorRed,styleDashed);

//Plot Two Standard Deviation Down from close price
Plot((C)-(stdn), "-1SD",colorGreen,styleDashed);
Plot((C)-((stdn) * 2), "-2SD",colorGreen,styleDashed);
GraphLabelDecimals = 2;

Now you will have to work on describing what you really want for us to help you any more.

1 Like

@amisur you wrote,

Then several answers were given to you (using the CURRENT PRICE) and you wrote,

So you were given an answer for the SCROLLING and use of "LastVisiblePrice". But that does not appear to be what you wanted either?

Perhaps you need SelectedValue( )
http://www.amibroker.com/guide/afl/selectedvalue.html

And are you looking for something that changes when you "select" a bar? And do you want the lines to go all across the chart (future and past bars) or only in the past? Something like this perhaps?
image

I assume it was not deliberate but you see that you keep changing your question and have yet to be clear and specific about what you are trying to achieve.

Good Luck!

2 Likes

Yes this is what I am trying to get. I am sorry I didn't change the questions intentionally. They were part of trail and error of learning things It's only when I tried practically I understood the differences and what I was exactly looking for.

I tried this after reading the selected value

//No.of Periods
Periods = 18;

//Formula for Standard Deviation
stdn = SelectedValue(StDev(Close, Periods));

//Plot the closing Price
Plot(Close,"Close",colorBlack,styleCandle);

//Plot Two Standard Deviation Up from close price
Plot(SelectedValue(Close)+(stdn), "+1SD",colorRed,styleDashed);
Plot(SelectedValue(Close)+((stdn) * 2), "+2SD",colorRed,styleDashed);

//Plot Two Standard Deviation Down from close price
Plot(SelectedValue(Close)-(stdn), "-1SD",colorGreen,styleDashed);
Plot(SelectedValue(Close)-((stdn) * 2), "-2SD",colorGreen,styleDashed);
GraphLabelDecimals = 2;

I am not from mathematical or programming background. Can you please look if the above formula of standard deviation and the graphical plots are correct?
image

The selected value in the above image is 32

Thank you for the guidance.