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);
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.
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);
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)?
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;
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.
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;
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?
@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.
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;
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.
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?
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.
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?