//@LibraryID:811,10 //@Name:Trend Line //@Description: Draw a Linear or Raff Regression line with optional confidence lines and options to extend the line forwards or backwards. // 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: Phil Tolhurst, ShareScope Support //Globals - accessable by any part of the script. var trendOptions = ["Trend of displayed bars","Trend of all bars","Trend of last X periods","Trend since date"] var calcOptions = ["Linear Regression","Raff Regression"]; var raffOptions = ["Close","High/Low"]; //Trend Line settings var TLswitch1=1; var TLperiod1=250; var TLtype1=0; var TLpen1=0; var TLwidth1=0; var TLcolour1=255; //Confidence Line 1 settings var CLswitch1=1; var CLstd1=1.5; var CLpen1=0; var CLwidth1=0; var CLcolour1=65025; //Confidence Line 2 settings var CLswitch2=1; var CLstd2=1.5; var CLpen2=0; var CLwidth2=0; var CLcolour2=65025; //Other options var TSdate = 56835; var extFWD = 1; var extBCK = 0; var calculation = 0; var useClose = 1; function init(status) { if (status==Loading) { if (storage.getAt(0) != undefined) { TLswitch1= storage.getAt(0); TLperiod1= storage.getAt(1); TLtype1= storage.getAt(2); CLswitch1= storage.getAt(3); CLstd1= storage.getAt(4); TLpen1= storage.getAt(5); TLwidth1= storage.getAt(6); TLcolour1= storage.getAt(7); CLpen1= storage.getAt(8); CLwidth1= storage.getAt(9); CLcolour1= storage.getAt(10); TSdate = storage.getAt(11); extFWD = storage.getAt(12); extBCK = storage.getAt(13); CLswitch2= storage.getAt(14); CLstd2= storage.getAt(15); CLpen2= storage.getAt(16); CLwidth2= storage.getAt(17); CLcolour2= storage.getAt(18); calculation = storage.getAt(19); useClose = storage.getAt(20); setHeader(); } } buttonHandle = createButton("Set", onButton0); } //What happens if the set button is clicked: function onButton0() { var tempDate = new Date(dateNumGetYear(TSdate),dateNumGetMonth(TSdate),dateNumGetDay(TSdate)); var dlg = new Dialog("Trend Settings", 360, 275); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(15,5,285,75,"Trend Line Settings"); //Trend line tickbox, option, periods and line colour etc dlg.addTickBox("VAL1",20,17,65,-1,"Trend Line",TLswitch1); dlg.addDropList("VAL2",90,17,95,-1,trendOptions,"","",TLtype1); dlg.addColLinePicker("VAL5",220,17,-1,-1,"","",TLcolour1,TLpen1,TLwidth1); dlg.addIntEdit("VAL3",65,34,-1,-1, "No of periods","(Only applies when 'Trend of last X periods' is selected)",TLperiod1,0,2500); dlg.addDatePicker("VAL4",65,51,-1,-1, "Select Date", "(Only applies when 'Trend since date' is selected)",tempDate); //Calculation options dlg.addGroupBox(15,80,285,50,"Trend Line Calculation"); dlg.addDropList("VAL14",100,92,75,-1,calcOptions,"Calculation Method:","",calculation); dlg.addDropList("VAL15",100,109,75,-1,raffOptions,"For Raff Regression use:","",useClose); dlg.addGroupBox(15,130,285,80,"Confidence Lines"); //Confidence line 1 tickbox, standard devs and line colour etc. dlg.addTickBox("VAL6",20,142,85,-1,"Upper Confidence Line",CLswitch1); dlg.addNumEdit("VAL7",20,159,-1,-1, "","Devs (Linear Regression only)",CLstd1,0,5.0); dlg.addColLinePicker("VAL8",220,142,-1,-1,"","",CLcolour1,CLpen1,CLwidth1); //Confidence line 2 tickbox, standard devs and line colour etc. dlg.addTickBox("VAL11",20,176,85,-1,"Lower Confidence Line",CLswitch2); dlg.addNumEdit("VAL12",20,193,-1,-1, "","Devs (Linear Regression only)",CLstd2,0,5.0); dlg.addColLinePicker("VAL13",220,176,-1,-1,"","",CLcolour2,CLpen2,CLwidth2); dlg.addGroupBox(15,210,285,50,"Line Extensions"); //Tick boxes for extending forwards and backwards dlg.addTickBox("VAL9",100,222,75,-1,"Extend Forwards",extFWD); dlg.addTickBox("VAL10",20,222,75,-1,"Extend Backwards",extBCK); dlg.addText(20,239,250,20,"(Extend Backwards is only an applicable when Trend of last X periods or Trend since date are selected)"); if (dlg.show() == Dialog.Cancel) return false; TLswitch1= dlg.getValue("VAL1")==true?1:0; TLtype1= dlg.getValue("VAL2"); TLperiod1= dlg.getValue("VAL3"); tempDate = dlg.getValue("VAL4"); TLcolour1=dlg.getValue("VAL5").colour; TLwidth1=dlg.getValue("VAL5").width; TLpen1=dlg.getValue("VAL5").pen; CLswitch1= dlg.getValue("VAL6")==true?1:0; CLstd1= dlg.getValue("VAL7"); CLcolour1=dlg.getValue("VAL8").colour; CLwidth1=dlg.getValue("VAL8").width; CLpen1=dlg.getValue("VAL8").pen; extFWD = dlg.getValue("VAL9")==true?1:0; extBCK = dlg.getValue("VAL10")==true?1:0; CLswitch2= dlg.getValue("VAL11")==true?1:0; CLstd2= dlg.getValue("VAL12"); CLcolour2=dlg.getValue("VAL13").colour; CLwidth2=dlg.getValue("VAL13").width; CLpen2=dlg.getValue("VAL13").pen calculation = dlg.getValue("VAL14"); useClose = dlg.getValue("VAL15"); TSdate = dateNum(tempDate.getFullYear(),tempDate.getMonth(),tempDate.getDate()); //Save Settings to storage storage.setAt(0,TLswitch1); storage.setAt(1,TLperiod1); storage.setAt(2,TLtype1); storage.setAt(3,CLswitch1); storage.setAt(4,CLstd1); storage.setAt(5,TLpen1); storage.setAt(6,TLwidth1); storage.setAt(7,TLcolour1); storage.setAt(8,CLpen1); storage.setAt(9,CLwidth1); storage.setAt(10,CLcolour1); storage.setAt(11,TSdate ); storage.setAt(12,extFWD); storage.setAt(13,extBCK); storage.setAt(14,CLswitch2); storage.setAt(15,CLstd2); storage.setAt(16,CLpen2); storage.setAt(17,CLwidth2); storage.setAt(18,CLcolour2); storage.setAt(19,calculation); storage.setAt(20,useClose); setHeader(); draw(); } function onNewChart() { draw(); } function onBarClose(preExisting) { if (!preExisting) draw(); } function onNewBarUpdate(preExisting) { if (!preExisting) draw(); } function onZoom() { draw(); } //Draws a new line depending which options have been selected function draw() { var checkPeriod = 0; var channel = 0; var trendVal; var trendSlp; var trendStd; clearDisplay(); if (TLtype1==0) { //Calculate the trend of displayed bars var period = getMaxVisibleBarIndex()-getMinVisibleBarIndex()+1; var trendCalc = new Trend(period); for (var i=getMinVisibleBarIndex();i<=getMaxVisibleBarIndex();i++) { trendCalc.next(bars[i].close) } } else if (TLtype1==1) { //Calculate the trend of all bars var period = bars.length; var trendCalc = new Trend(period); for (i=0;i=0;i--) { if (bars[i].dateNum<=TSdate) { checkPeriod = i; break; } } if (checkPeriod > 0) var period = bars.length-(checkPeriod); else var period = bars.length; var trendCalc = new Trend(period); for (i=0;i=getMinVisibleBarIndex();j--) { trendValue = trendCalc.getValue() - trendCalc.getSlope()*(n); trendSlp = trendCalc.getSlope(); if (useClose==1) channel = Math.max(Math.abs(bars[j].high-trendValue),Math.abs(bars[j].low-trendValue),channel) else channel = Math.max(Math.abs(bars[j].close-trendValue),channel) n++ } } else { for (var j=1;j=firstBar;j--) { if (j==lastBar) { moveTo(j,position); continue; } position = position-trendSlp lineTo(j,position); } endPath(); drawPath(); if(extFWD) { var position = trendVal; beginPath(); for(var j = lastBar;j=firstBar;j--) { if (j==lastBar) { moveTo(j,position); continue; } position = position-trendSlp lineTo(j,position); } endPath(); drawPath(); if(extFWD) { var position = trendVal+(calculation==0?(trendStd*CLstd1):channel); beginPath(); for(var j = lastBar;j=firstBar;j--) { if (j==lastBar) { moveTo(j,position); continue; } position = position-trendSlp lineTo(j,position); } endPath(); drawPath(); if(extFWD) { var position = trendVal-(calculation==0?(trendStd*CLstd2):channel); beginPath(); for(var j = lastBar;j