//@Name:Multiple Trend Lines per Share //@Description: Add up to four trendlines specific to each share. // 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 settingsArray = new Array(); var settingsFile; var trendOptions = ["Trend of displayed data","Trend of all data","Trend of last X periods"] //shareID will be set late to give a unique identifier for every share/instrument in the program var share; var shareID; function init() { setTitle("Multiple Trend Lines"); //Open the settings file and pass all of the data into an array and then close the file again. //We use try and catch to check if the file exists already and if not to create it. settingsFile = new File(); var text; try { settingsFile.open("TrendLineSettings.txt",File.ReadMode); for (var a=0;text = settingsFile.readLine();a++) { if (text != "") settingsArray[a] = text.split(","); } settingsFile.close("TrendLineSettings.txt"); } catch (e) { settingsFile.open("TrendLineSettings.txt",File.WriteMode); settingsFile.close("TrendLineSettings.txt"); } //Creates a button on the Study Manager buttonHandle = createButton("Set", onButton0); } //What happens if the set button is clicked: function onButton0() { //Load Settings for the share if possible var TLsettings=getSettings(shareID); //if there are settings use them to set the dialog defaults if (TLsettings.length>0) { var TLswitch1=TLsettings[1] var TLperiod1=TLsettings[2]; var TLtype1=TLsettings[3]; var CLswitch1=TLsettings[4]; var CLstd1=TLsettings[5]; var TLpen1=TLsettings[6]; var TLwidth1=TLsettings[7]; var TLcolour1=TLsettings[8]; var CLpen1=TLsettings[9]; var CLwidth1=TLsettings[10]; var CLcolour1=TLsettings[11]; var MSwitch1=TLsettings[12]; var TLswitch2=TLsettings[13] var TLperiod2=TLsettings[14]; var TLtype2=TLsettings[15]; var CLswitch2=TLsettings[16]; var CLstd2=TLsettings[17]; var TLpen2=TLsettings[18]; var TLwidth2=TLsettings[19]; var TLcolour2=TLsettings[20]; var CLpen2=TLsettings[21]; var CLwidth2=TLsettings[22]; var CLcolour2=TLsettings[23]; var MSwitch2=TLsettings[24]; var TLswitch3=TLsettings[25] var TLperiod3=TLsettings[26]; var TLtype3=TLsettings[27]; var CLswitch3=TLsettings[28]; var CLstd3=TLsettings[29]; var TLpen3=TLsettings[30]; var TLwidth3=TLsettings[31]; var TLcolour3=TLsettings[32]; var CLpen3=TLsettings[33]; var CLwidth3=TLsettings[34]; var CLcolour3=TLsettings[35]; var MSwitch3=TLsettings[36]; var TLswitch4=TLsettings[37] var TLperiod4=TLsettings[38]; var TLtype4=TLsettings[39]; var CLswitch4=TLsettings[40]; var CLstd4=TLsettings[41]; var TLpen4=TLsettings[42]; var TLwidth4=TLsettings[43]; var TLcolour4=TLsettings[44]; var CLpen4=TLsettings[45]; var CLwidth4=TLsettings[46]; var CLcolour4=TLsettings[47]; var MSwitch4=TLsettings[48]; TLswitch1=TLswitch1=="0"?0:1; TLswitch2=TLswitch2=="0"?0:1; TLswitch3=TLswitch3=="0"?0:1; TLswitch4=TLswitch4=="0"?0:1; CLswitch1=CLswitch1=="0"?0:1; CLswitch2=CLswitch2=="0"?0:1; CLswitch3=CLswitch3=="0"?0:1; CLswitch4=CLswitch4=="0"?0:1; MSwitch1=MSwitch1=="0"?0:1; MSwitch2=MSwitch2=="0"?0:1; MSwitch3=MSwitch3=="0"?0:1; MSwitch4=MSwitch4=="0"?0:1; } //if there are not settings use the defaults and the first date of data available for the share. else { var TLswitch1 = 1; var TLperiod1 = 250; var TLtype1 = 0; var CLswitch1= 1; var CLstd1= 1.5; var TLcolour1=Colour.Red; var TLwidth1=0; var TLpen1=0; var CLcolour1=Colour.Green; var CLwidth1=0; var CLpen1=1; var MSwitch1=1; var TLswitch2 = 0; var TLperiod2 = 250; var TLtype2 = 0; var CLswitch2= 0; var CLstd2= 1.5; var TLcolour2=Colour.Red; var TLwidth2=0; var TLpen2=0; var CLcolour2=Colour.Green; var CLwidth2=0; var CLpen2=1; var MSwitch2=0; var TLswitch3=0; var TLperiod3=250; var TLtype3 =0; var CLswitch3=0; var CLstd3=1.5; var TLcolour3=Colour.Red; var TLwidth3=0; var TLpen3=0; var CLcolour3=Colour.Green; var CLwidth3=0; var CLpen3=1; var MSwitch3=0; var TLswitch4=0; var TLperiod4=250; var TLtype4=0; var CLswitch4=0; var CLstd4=1.5; var TLcolour4=Colour.Red; var TLwidth4=0; var TLpen4=0; var CLcolour4=Colour.Green; var CLwidth4=0; var CLpen4=1; var MSwitch4=0; } //Create a dialog box to set the values of the script do { var dlg = new Dialog("Trend Settings", 340, 280); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(15,5,265,60,""); //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.addIntEdit("VAL3",65,32,-1,-1, "No of periods","(Only applies when 'Trend of last X periods' selected)",TLperiod1,0,2500); dlg.addColLinePicker("VAL4",220,17,-1,-1,"","",TLcolour1,TLpen1,TLwidth1); //Confidence line tickbox, standard devs and line colour etc. dlg.addTickBox("VAL5",20,47,65,-1,"Confidence Lines",CLswitch1); dlg.addNumEdit("VAL6",90,47,-1,-1, "","Devs",CLstd1,0,5.0); dlg.addColLinePicker("VAL7",220,47,-1,-1,"","",CLcolour1,CLpen1,CLwidth1); dlg.addTickBox("VAL29",20,5,65,-1,"Trend Settings 1",MSwitch1); dlg.addGroupBox(15,75,265,60,""); //Trend line tickbox, option, periods and line colour etc dlg.addTickBox("VAL8",20,87,65,-1,"Trend Line",TLswitch2); dlg.addDropList("VAL9",90,87,95,-1,trendOptions,"","",TLtype2); dlg.addIntEdit("VAL10",65,102,-1,-1, "No of periods","(Only applies when 'Trend of last X periods' selected)",TLperiod2,0,2500); dlg.addColLinePicker("VAL11",220,87,-1,-1,"","",TLcolour2,TLpen2,TLwidth2); //Confidence line tickbox, standard devs and line colour etc. dlg.addTickBox("VAL12",20,117,65,-1,"Confidence Lines",CLswitch2); dlg.addNumEdit("VAL13",90,117,-1,-1, "","Devs",CLstd2,0,5.0); dlg.addColLinePicker("VAL14",220,117,-1,-1,"","",CLcolour2,CLpen2,CLwidth2); dlg.addTickBox("VAL30",20,75,65,-1,"Trend Settings 2",MSwitch2); dlg.addGroupBox(15,145,265,60,""); //Trend line tickbox, option, periods and line colour etc dlg.addTickBox("VAL15",20,157,65,-1,"Trend Line",TLswitch3); dlg.addDropList("VAL16",90,157,95,-1,trendOptions,"","",TLtype3); dlg.addIntEdit("VAL17",65,172,-1,-1, "No of periods","(Only applies when 'Trend of last X periods' selected)",TLperiod3,0,2500); dlg.addColLinePicker("VAL18",220,157,-1,-1,"","",TLcolour3,TLpen3,TLwidth3); //Confidence line tickbox, standard devs and line colour etc. dlg.addTickBox("VAL19",20,187,65,-1,"Confidence Lines",CLswitch3); dlg.addNumEdit("VAL20",90,187,-1,-1, "","Devs",CLstd3,0,5.0); dlg.addColLinePicker("VAL21",220,187,-1,-1,"","",CLcolour3,CLpen3,CLwidth3); dlg.addTickBox("VAL31",20,145,65,-1,"Trend Settings 3",MSwitch3); dlg.addGroupBox(15,215,265,60,""); //Trend line tickbox, option, periods and line colour etc dlg.addTickBox("VAL22",20,227,65,-1,"Trend Line",TLswitch4); dlg.addDropList("VAL23",90,227,95,-1,trendOptions,"","",TLtype4); dlg.addIntEdit("VAL24",65,242,-1,-1, "No of periods","(Only applies when 'Trend of last X periods' selected)",TLperiod4,0,2500); dlg.addColLinePicker("VAL25",220,227,-1,-1,"","",TLcolour4,TLpen4,TLwidth4); //Confidence line tickbox, standard devs and line colour etc. dlg.addTickBox("VAL26",20,257,65,-1,"Confidence Lines",CLswitch4); dlg.addNumEdit("VAL27",90,257,-1,-1, "","Devs",CLstd4,0,5.0); dlg.addColLinePicker("VAL28",220,257,-1,-1,"","",CLcolour4,CLpen4,CLwidth4); dlg.addTickBox("VAL32",20,215,65,-1,"Trend Settings 4",MSwitch4); if (dlg.show() == Dialog.Cancel) return false; TLswitch1= dlg.getValue("VAL1")==true?1:0; TLperiod1= dlg.getValue("VAL3"); TLtype1= dlg.getValue("VAL2"); CLswitch1= dlg.getValue("VAL5")==true?1:0; CLstd1= dlg.getValue("VAL6"); TLcolour1=dlg.getValue("VAL4").colour; TLwidth1=dlg.getValue("VAL4").width; TLpen1=dlg.getValue("VAL4").pen; CLcolour1=dlg.getValue("VAL7").colour; CLwidth1=dlg.getValue("VAL7").width; CLpen1=dlg.getValue("VAL7").pen; MSwitch1=dlg.getValue("VAL29")==true?1:0; TLswitch2=dlg.getValue("VAL8")==true?1:0; TLperiod2=dlg.getValue("VAL10"); TLtype2=dlg.getValue("VAL9"); CLswitch2=dlg.getValue("VAL12")==true?1:0; CLstd2=dlg.getValue("VAL13"); TLcolour2=dlg.getValue("VAL11").colour; TLwidth2=dlg.getValue("VAL11").width; TLpen2=dlg.getValue("VAL11").pen; CLcolour2=dlg.getValue("VAL14").colour; CLwidth2=dlg.getValue("VAL14").width; CLpen2=dlg.getValue("VAL14").pen; MSwitch2=dlg.getValue("VAL30")==true?1:0; TLswitch3=dlg.getValue("VAL15")==true?1:0; TLperiod3=dlg.getValue("VAL17"); TLtype3=dlg.getValue("VAL16"); CLswitch3=dlg.getValue("VAL19")==true?1:0; CLstd3=dlg.getValue("VAL20"); TLcolour3=dlg.getValue("VAL18").colour; TLwidth3=dlg.getValue("VAL18").width; TLpen3=dlg.getValue("VAL18").pen; CLcolour3=dlg.getValue("VAL21").colour; CLwidth3=dlg.getValue("VAL21").width; CLpen3=dlg.getValue("VAL21").pen; MSwitch3=dlg.getValue("VAL31")==true?1:0; TLswitch4=dlg.getValue("VAL22")==true?1:0; TLperiod4=dlg.getValue("VAL24"); TLtype4=dlg.getValue("VAL23"); CLswitch4=dlg.getValue("VAL26")==true?1:0; CLstd4= dlg.getValue("VAL27"); TLcolour4=dlg.getValue("VAL25").colour; TLwidth4=dlg.getValue("VAL25").width; TLpen4=dlg.getValue("VAL25").pen; CLcolour4=dlg.getValue("VAL28").colour; CLwidth4=dlg.getValue("VAL28").width; CLpen4=dlg.getValue("VAL28").pen; MSwitch4=dlg.getValue("VAL32")==true?1:0; } while (MSwitch1!=1 && MSwitch2!=1 && MSwitch3!=1 && MSwitch4!=1); //Record settings in array and save array to file. var tempSettings=[shareID ,TLswitch1,TLperiod1,TLtype1,CLswitch1, CLstd1,TLpen1,TLwidth1,TLcolour1,CLpen1,CLwidth1,CLcolour1,MSwitch1 ,TLswitch2,TLperiod2,TLtype2,CLswitch2, CLstd2,TLpen2,TLwidth2,TLcolour2,CLpen2,CLwidth2,CLcolour2,MSwitch2 ,TLswitch3,TLperiod3,TLtype3,CLswitch3, CLstd3,TLpen3,TLwidth3,TLcolour3,CLpen3,CLwidth3,CLcolour3,MSwitch3 ,TLswitch4,TLperiod4,TLtype4,CLswitch4, CLstd4,TLpen4,TLwidth4,TLcolour4,CLpen4,CLwidth4,CLcolour4,MSwitch4]; if(TLsettings.length==0) { settingsArray[settingsArray.length]=tempSettings; } else { for (var c=0;c0) { setInfoText((TLsettings[12]==0?"1. -":(TLsettings[4]==1?(TLsettings[3]==2?"1. Trend of "+TLsettings[2]+" periods\n "+TLsettings[5]+" std dev conf lines":"1. "+trendOptions[TLsettings[3]]+"\n "+TLsettings[5]+" std dev conf lines"):(TLsettings[3]==2?"1. Trend of "+TLsettings[2]+" periods":"1. "+trendOptions[TLsettings[3]])))+"\n"+ (TLsettings[24]==0?"2. -":(TLsettings[16]==1?(TLsettings[15]==2?"2. Trend of "+TLsettings[14]+" periods\n "+TLsettings[17]+" std dev conf lines":"2. "+trendOptions[TLsettings[15]]+"\n "+TLsettings[17]+" std dev conf lines"):(TLsettings[15]==2?"2. Trend of "+TLsettings[14]+" periods":"2. "+trendOptions[TLsettings[15]])))+"\n"+ (TLsettings[36]==0?"3. -":(TLsettings[28]==1?(TLsettings[27]==2?"3. Trend of "+TLsettings[26]+" periods\n "+TLsettings[29]+" std dev conf lines":"3. "+trendOptions[TLsettings[27]]+"\n "+TLsettings[29]+" std dev conf lines"):(TLsettings[27]==2?"3. Trend of "+TLsettings[26]+" periods":"3. "+trendOptions[TLsettings[27]])))+"\n"+ (TLsettings[48]==0?"4. -":(TLsettings[40]==1?(TLsettings[39]==2?"4. Trend of "+TLsettings[38]+" periods\n "+TLsettings[41]+" std dev conf lines":"4. "+trendOptions[TLsettings[38]]+"\n "+TLsettings[41]+" std dev conf lines"):(TLsettings[39]==2?"4. Trend of "+TLsettings[38]+" periods":"4. "+trendOptions[TLsettings[39]])))); draw(TLsettings); } else setInfoText(); } //Updates the graph if the data is updated. function onNewBarUpdate() { var TLsettings=getSettings(shareID); if (TLsettings.length>0) draw(TLsettings); } //Updates the graph if the data is updated. function onZoom() { var TLsettings=getSettings(shareID); if (TLsettings.length>0) draw(TLsettings); } //Draws a new line depending which button has been clicked on function draw(TLsettings) { clearDisplay(); for (var x=0;x<4;x++) { if(TLsettings[12+(12*x)]==0) continue if (TLsettings[3+(12*x)]==0) { var period = getMaxVisibleBarIndex()-getMinVisibleBarIndex()+1; var trendCalc = new Trend(period); for (var i=getMinVisibleBarIndex();i<=getMaxVisibleBarIndex();i++) { trendCalc.next(bars[i].close) } } else if (TLsettings[3+(12*x)] ==1) { var period = bars.length; var trendCalc = new Trend(period); for (i=0;i=0;j--) { if (j==bars.length-1) { moveTo(j,position); continue; } position = position-trendSlp lineTo(j,position); } endPath(); drawPath(); var position = trendVal; beginPath(); for(j=(TLsettings[3+(12*x)]==0?getMaxVisibleBarIndex():bars.length-1);j=0;j--) { if (j==bars.length-1) { moveTo(j,position); continue; } position = position-trendSlp lineTo(j,position); } endPath(); drawPath(); var position = (i==0?trendVal+(trendStd*TLsettings[5+(12*x)]):trendVal-(trendStd*TLsettings[5+(12*x)])); beginPath(); for(var j=(TLsettings[3+(12*x)]==0?getMaxVisibleBarIndex():bars.length-1);j