//@LibraryID:1099,0 //@Name:Donchian Legend //@Description:Draws a legend box for Donchian Channels // Care has been taken in preparing this code but it is provided without guarantee. // You are welcome to modify and extend it. Please add your name as a modifier if you distribute it. //Coded by: Richard Chiesa, ShareScript Support var upperPeriod= 20; //period high var lowerPeriod = 20; //period low var fontSize = 10; //period low var displayPos = 0; //legend position var textCol = Colour.Red; var backCol = Colour.RGB(200,200,200); function init(status) { if (status == Loading || status == Editing) { upperPeriod = storage.getAt(0); lowerPeriod = storage.getAt(1); fontSize = storage.getAt(2); displayPos = storage.getAt(3); textCol = storage.getAt(4); backCol = storage.getAt(5); } if (status == Adding || status == Editing) { dlg = new Dialog("Settings...", 180, 150); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(8,5,100,45,"Donchian Channel Periods"); dlg.addIntEdit("INT1",13,18,-1,-1,"","High period",upperPeriod,1,4000); dlg.addIntEdit("INT2",13,33,-1,-1,"","Low period",lowerPeriod,1,4000); dlg.addIntEdit("INT3",11,55,-1,-1,"","Font size",fontSize,1,4000); dlg.addColPicker("COL1",11,-1,-1,-1,"","Font Colour",textCol); dlg.addColPicker("COL2",11,-1,-1,-1,"","Background Colour",backCol); dlg.addDropList("DL1",11,-1,80,-1,["Bottom Left","Top Left","Bottom Right","Top Right"],"","Legend position",displayPos); if (dlg.show()==Dialog.Cancel) return false; upperPeriod = dlg.getValue("INT1"); lowerPeriod = dlg.getValue("INT2"); fontSize = dlg.getValue("INT3"); displayPos = dlg.getValue("DL1"); textCol = dlg.getValue("COL1"); backCol = dlg.getValue("COL2"); storage.setAt(0, upperPeriod); storage.setAt(1, lowerPeriod); storage.setAt(2, fontSize); storage.setAt(3, displayPos); storage.setAt(4, textCol); storage.setAt(5, backCol); } } function onNewChart() { clearDisplay(); draw(); } function onZoom() { draw(); } function draw() { clearDisplay(); setLayer(Layer.Top); var s = getMinVisibleBarIndex(); var f = getMaxVisibleBarIndex(); setAltRange(0,1000); useAltRange(true); var upperValue; var lowerValue; var a = Math.max(upperPeriod+1,lowerPeriod+1); for (var i=1;i<=a;i++) { if (i<=upperPeriod+1 && (upperValue==null || upperValuebars[bars.length-i].low)) lowerValue = bars[bars.length-i].low; } setPenStyle(0,1,Colour.RGB(255-getBackColour()%256,255-Math.floor(getBackColour()%65536/256),255-Math.floor(getBackColour()/65536))); setBrushColour(Colour.RGB(200,200,200)); setFontStyle("Arial",fontSize,Colour.Red); var xoffset = (f-s)/1000; var text = upperPeriod+" High: "+upperValue+"\n"+lowerPeriod+" Low: "+lowerValue; if (displayPos==0) { var x0 = s+xoffset*5; var y0 = 5; drawText(x0,y0,text,BoxAlign.Right|BoxAlign.Above,TextAlign.Left,1); } if (displayPos==1) { var x0 = s+xoffset*5; var y0 = 960; drawText(x0,y0,text,BoxAlign.Right|BoxAlign.Below,TextAlign.Left,1); } if (displayPos==2) { var x0 = s+xoffset*995; var y0 = 5; drawText(x0,y0,text,BoxAlign.Left|BoxAlign.Above,TextAlign.Left,1); } if (displayPos==3) { var x0 = s+xoffset*995; var y0 = 995; drawText(x0,y0,text,BoxAlign.Left|BoxAlign.Below,TextAlign.Left,1); } }