//@LibraryID:958,0 //@Name:Displaced Donchian Channels //@Description: Replicates ShareScope's Donchian Channels so that it is displaced back in time by 1 period // 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. // Original Code by: Phil Tolhurst, ShareScope Support // Edited Code by: Paul Hall, ShareScope Support var highPeriod = 20; var lowPeriod = 20; var highPen = 0; var highWidth = 1; var highColour = 65025; var lowPen = 0; var lowWidth = 1; var lowColour = 255; var highSwitch = 1; var lowSwitch = 1; var fillSwitch = 1; var highSource = 1; var lowSource = 2; var fillColour = 12517375; var sourceList = ["Open","High","Low","Close"]; function init(status) { if (status == Loading || status == Editing) { highPeriod = storage.getAt(0); lowPeriod = storage.getAt(1); highColour = storage.getAt(2); highWidth = storage.getAt(3); highPen = storage.getAt(4); lowColour = storage.getAt(5); lowWidth = storage.getAt(6); lowPen = storage.getAt(7); highSwitch = storage.getAt(8); lowSwitch = storage.getAt(9); highSource = storage.getAt(10); lowSource = storage.getAt(11); fillSwitch = storage.getAt(12); fillColour = storage.getAt(13); } if (status == Adding || status == Editing) { dlg = new Dialog("Price/Displaced Donchian Channels",220,150); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(5,5,150,50); dlg.addTickBox("VAL1",8,5,40,-1,"High Line",highSwitch); dlg.addIntEdit("VAL2",45,22,-1,-1,"Period","",highPeriod,0,1000); dlg.addColLinePicker("VAL3",120,20,-1,-1,"High Line","",highColour,highPen,highWidth); dlg.addDropList("VAL4",45,39,40,-1,sourceList,"Source","",highSource); dlg.addGroupBox(5,55,150,50); dlg.addTickBox("VAL5",8,55,40,-1,"Low Line",lowSwitch); dlg.addIntEdit("VAL6",45,72,-1,-1,"Period","",lowPeriod,0,1000); dlg.addColLinePicker("VAL7",120,70,-1,-1,"Low Line","",lowColour,lowPen,lowWidth); dlg.addDropList("VAL8",45,89,40,-1,sourceList,"Source","",lowSource); dlg.addGroupBox(5,105,150,35); dlg.addTickBox("VAL9",8,105,50,-1,"Fill Channel",fillSwitch); dlg.addColPicker("VAL10",120,120,-1,-1,"Channel","",fillColour); if (dlg.show()==Dialog.Cancel) return false; highSwitch = dlg.getValue("VAL1"); highPeriod = dlg.getValue("VAL2"); highColour = dlg.getValue("VAL3").colour; highWidth = dlg.getValue("VAL3").width; highPen = dlg.getValue("VAL3").pen; highSoorce = dlg.getValue("VAL4"); lowSwitch = dlg.getValue("VAL5"); lowPeriod = dlg.getValue("VAL6"); lowColour = dlg.getValue("VAL7").colour; lowWidth = dlg.getValue("VAL7").width; lowPen = dlg.getValue("VAL7").pen; lowSource = dlg.getValue("VAL8"); fillSwitch = dlg.getValue("VAL9"); fillColour = dlg.getValue("VAl10"); storage.setAt(0, highPeriod); storage.setAt(1, lowPeriod); storage.setAt(2, highColour); storage.setAt(3, highWidth); storage.setAt(4, highPen); storage.setAt(5, lowColour); storage.setAt(6, lowWidth); storage.setAt(7, lowPen); storage.setAt(8, highSwitch); storage.setAt(9, lowSwitch); storage.setAt(10, highSource); storage.setAt(11, lowSource); storage.setAt(12, fillSwitch); storage.setAt(13, fillColour); } buttonHandle = createButton("Settings....", onButton0); } function onButton0() { dialog() } function onNewChart() { draw(); } function onBarClose(preExisting) { if (!preExisting) draw(); } function onNewBarUpdate(preExisting) { if (!preExisting) draw(); } function draw() { clearDisplay(); if (bars.length<2) return; var lowLow = new MinMax(lowPeriod); var highHigh = new MinMax(highPeriod); var highData; var lowData; highLine = []; lowLine = []; for (var i=0;i=0;j--) { lineTo(j,lowLine[j]); } lineTo(0,lowLine[0]); lineTo(0,highLine[0]); endPath(); fillPath(); } } function dialog() { dlg = new Dialog("Price/Displaced Donchian Channels",220,150); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(5,5,150,50); dlg.addTickBox("VAL1",8,5,40,-1,"High Line",highSwitch); dlg.addIntEdit("VAL2",45,22,-1,-1,"Period","",highPeriod,0,1000); dlg.addColLinePicker("VAL3",120,20,-1,-1,"High Line","",highColour,highPen,highWidth); dlg.addDropList("VAL4",45,39,40,-1,sourceList,"Source","",highSource); dlg.addGroupBox(5,55,150,50); dlg.addTickBox("VAL5",8,55,40,-1,"Low Line",lowSwitch); dlg.addIntEdit("VAL6",45,72,-1,-1,"Period","",lowPeriod,0,1000); dlg.addColLinePicker("VAL7",120,70,-1,-1,"Low Line","",lowColour,lowPen,lowWidth); dlg.addDropList("VAL8",45,89,40,-1,sourceList,"Source","",lowSource); dlg.addGroupBox(5,105,150,35); dlg.addTickBox("VAL9",8,105,50,-1,"Fill Channel",fillSwitch); dlg.addColPicker("VAL10",120,120,-1,-1,"Channel","",fillColour); if (dlg.show()==Dialog.Cancel) return false; highSwitch = dlg.getValue("VAL1"); highPeriod = dlg.getValue("VAL2"); highColour = dlg.getValue("VAL3").colour; highWidth = dlg.getValue("VAL3").width; highPen = dlg.getValue("VAL3").pen; highSoorce = dlg.getValue("VAL4"); lowSwitch = dlg.getValue("VAL5"); lowPeriod = dlg.getValue("VAL6"); lowColour = dlg.getValue("VAL7").colour; lowWidth = dlg.getValue("VAL7").width; lowPen = dlg.getValue("VAL7").pen; lowSource = dlg.getValue("VAL8"); fillSwitch = dlg.getValue("VAL9"); fillColour = dlg.getValue("VAl10"); storage.setAt(0, highPeriod); storage.setAt(1, lowPeriod); storage.setAt(2, highColour); storage.setAt(3, highWidth); storage.setAt(4, highPen); storage.setAt(5, lowColour); storage.setAt(6, lowWidth); storage.setAt(7, lowPen); storage.setAt(8, highSwitch); storage.setAt(9, lowSwitch); storage.setAt(10, highSource); storage.setAt(11, lowSource); storage.setAt(12, fillSwitch); storage.setAt(13, fillColour); draw(); }