//@Name:Elder's AutoEnvelope //@Description:From Alexander Elder's book "Sell & Sell Short" //@Env:Production //Envelope channels are set parallel to a slow moving average. //The two channel lines should contain approximately 90-95% of all prices //for the past two or three months between them with only the extremes //protruding outside. Envelope channels provide attractive profit targets //- sell longs near the upper channel line and cover shorts near the lower //channel line. The AutoEnvelope is a custom indicator that automatically //sizes channels by calculating a standard deviation for the last 100 bars - //the 'lookback' period. It is designed to change value at most once a week, //making it suitable even for intra-day data. // Indicator built by ShareScope Support and Jurgen Whitehouse var period = 22; //MA period var sdPeriod = 100; //The lookback period var factor= 2; //number of standard deviations var colourMA = Colour.Green; var penMA = 0; var widthMA = 0; var colourENV = Colour.DarkGreen; var penENV = 0; var widthENV = 0; var lineStyles; //dialog box code function init(status) { if (status==Loading || status==Editing) { period = storage.getAt(0); sdPeriod = storage.getAt(1); factor = storage.getAt(2); colourMA = storage.getAt(3); colourENV = storage.getAt(4); lineStyles = storage.getAt(5); lineStyles = lineStyles.toString(); penMA=parseInt(lineStyles.substr(0,1),10)-1; widthMA=parseInt(lineStyles.substr(1,1),10)-1; penENV=parseInt(lineStyles.substr(2,1),10)-1; widthENV=parseInt(lineStyles.substr(3,1),10)-1 } if (status==Adding || status==Editing) { var dlg = new Dialog((status==Adding?"Add":"Edit")+" indicator",200,80) dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("INT1",8,5,-1,-1,"","EMA period",period,2,1000); dlg.addIntEdit("INT2",8,22,-1,-1,"","Lookback period",sdPeriod,2,1000); dlg.addNumEdit("INT3",8,39,-1,-1,"","Std Devs",factor,0.01,1000); dlg.addColLinePicker("VAL1",58,56,-1,-1,"MA Settings:","",colourMA,penMA,widthMA); dlg.addColLinePicker("VAL2",158,56,-1,-1,"Envelope Settings:","",colourENV,penENV,widthENV); if (dlg.show()==Dialog.Cancel) return false; period = dlg.getValue("INT1"); sdPeriod = dlg.getValue("INT2"); factor = dlg.getValue("INT3"); colourMA = dlg.getValue("VAL1").colour; penMA = dlg.getValue("VAL1").pen; widthMA = dlg.getValue("VAL1").width; colourENV = dlg.getValue("VAL2").colour; penENV = dlg.getValue("VAL2").pen; widthENV = dlg.getValue("VAL2").width; lineStyles = ((penMA+1)*1000) + ((widthMA+1)*100) + ((penENV+1)*10) + (widthENV+1); storage.setAt(0, period); storage.setAt(1, sdPeriod); storage.setAt(2, factor); storage.setAt(3, colourMA); storage.setAt(4, colourENV); storage.setAt(5, lineStyles); } setRange(Range.Parent); //Colour and style of the moving average. setSeriesColour(0, colourMA); setSeriesLineStyle(0, penMA, widthMA); //Colour and style of the upper and lower envelopes. setSeriesColour(1, colourENV); setSeriesLineStyle(1, penENV, widthENV); setSeriesColour(2, colourENV); setSeriesLineStyle(2, penENV, widthENV); } function getGraph(share, data) { var ma1 = new MA(period, MA.Exponential); //Setup of EMA, from which the envelopes will be set. var w = new Array(); var envelope //envelope size var x //standard deviation intermidiate value var av1 = new Array(); var upperEnvelope = new Array(); //the upper envelope array var lowerEnvelope = new Array(); //the lower envelope array for (var i=0;isdPeriod) return [av1,upperEnvelope,lowerEnvelope]; else return av1; }