//@Name:ATR Value //@Description:Displays the latest ATR value or as expressed as a percentage of the current price //@Returns:Number //@Update:Periodic,15 //@Width:80 //@Env:Production // Author: ShareScript Support // 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. var period = 14; var dataChoiceList = ["end-of-day","intraday"]; var dataChoice = 0; var dataList = ["Daily","Weekly","Monthly"]; var intraDataList = ["1","2","2.5","3","4","5","6","10","15","20","30","60","120","180","240"]; var barSize = 0; var dataSource = 0; var useIntra = true; var outputChoice = 0; var outputList = ["ATR value","ATR % of Price"]; function init(status) { load("libraries\\SSLib.ssl"); if (status == Loading || status == Editing) { period = storage.getAt(0); dataSource = storage.getAt(1); useIntra = storage.getAt(2); dataChoice = storage.getAt(3); barSize = storage.getAt(4); outputChoice = storage.getAt(5); } if (status == Adding || status == Editing) { dlg = new Dialog("ATR Value",340,105); dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("VAL1",75,5,20,-1,"ATR Period","",period,2,1000); dlg.addDropList("VAL6",75,22,75,-1,outputList,"Return the ","",outputChoice); dlg.addDropList("VAL2",186,22,55,-1,dataChoiceList,"based on "," data.",dataChoice); dlg.addDropList("VAL3",75,39,30,-1,intraDataList,"Intraday bar size: "," minutes",barSize); dlg.addDropList("VAL4",75,56,40,-1,dataList,"End of day bar size: ","",dataSource); dlg.addTickBox("VAL5",120,58,150,-1,"Include intraday data (if available)",useIntra); dlg.addText(5,73,300,20,"Note: For intraday bar sizes greater than 30mins three days of intraday data is used, otherwise the script uses two days of intraday data."); if (dlg.show()==Dialog.Cancel) return false; period = dlg.getValue("VAL1"); dataChoice = dlg.getValue("VAL2"); barSize = dlg.getValue("VAL3"); dataSource = dlg.getValue("VAL4"); useIntra = dlg.getValue("VAL5"); outputChoice = dlg.getValue("VAL6"); storage.setAt(0, period); storage.setAt(1, dataSource); storage.setAt(2, useIntra); storage.setAt(3, dataChoice); storage.setAt(4, barSize); storage.setAt(5, outputChoice); } //sets the title of the column if(dataChoice==0) { setTitle(period+" ATR"+(outputChoice==0?" ":"/price % ")+(useIntra?"(i":"(")+dataList[dataSource]+")") } else { setTitle(period+" ATR"+(outputChoice==0?" (":"/price % (")+intraDataList[barSize]+"m bars)"); } } function getVal(share) { if(dataChoice==0) { var data = getData(share,dataSource,useIntra); } if(dataChoice==1 && barSize<10) { var data0 = share.getIBarArray(0,60*(intraDataList[barSize]*1)); var data1 = share.getIBarArray(1,60*(intraDataList[barSize]*1)); if(data0==undefined || data0.length<1) return; if(data1==undefined || data1.length<1) return; var data = data1.concat(data0); } if(dataChoice==1 && barSize>=10) { var data0 = share.getIBarArray(0,60*(intraDataList[barSize]*1)); var data1 = share.getIBarArray(1,60*(intraDataList[barSize]*1)); var data2 = share.getIBarArray(2,60*(intraDataList[barSize]*1)); if(data0==undefined || data0.length<1) return; if(data1==undefined || data1.length<1) return; if(data2==undefined || data2.length<1) return; var data = data2.concat(data1,data0); } if(data==undefined || data.length<1) return; //In certain circumstances, usually with US data, there can be no open and, therefore no current bar on the chart. //So I'm testing for that here. var cp = data.length-1; if(data[cp].open == undefined || data[cp].high == undefined || data[cp].low == undefined || data[cp].close == undefined) return; var atr1 = new ATR(period); var atrVal = atr1.getNext(data) if(outputChoice==0) return atrVal.toFixed(3); else return ((atrVal/data[data.length-1].close)*100).toFixed(3); } function getData(share,dataSource,useIntra) { if (dataSource == 0)var data = share.getPriceArray(); if (dataSource == 1)var data = share.getWeeklyBarArray(); if (dataSource == 2)var data = share.getMonthlyBarArray(); if (data.length<2) return; if (dataSource==0 && useIntra==1) { //get a 24hour intraday bar var idata = share.getIBarArray(0,86400); //Check the bar is not undefined and has the correct length. //Check the date of the intraday data is today's date. //Check the date of the end-of-day data is not today's date. if (idata!=undefined && idata.length==1 && new Date().getDate()==idata[0].date.getDate() && new Date().getDate()!=data[data.length-1].date.getDate()) {//Add a new bar to the end of the current data array that adds the intraday Open,High,Low & Close data[data.length]={ open:share.getIOpen(), high:idata[0].high, low:idata[0].low, close:(share.getIClose()==null?share.getIMid():share.getIClose()), volume:idata[0].volume, dateNum:idata[0].dateNum}; } } if (dataSource==1 && useIntra==1) { //get a 24hour intraday bar var idata = share.getIBarArray(0,86400); //Check the bar is not undefined and has the correct length. //Check the date of the intraday data is today's date. //Check the date of the end-of-day data is not today's date. if (idata!=undefined && idata.length==1 && new Date().getDate()==idata[0].date.getDate() && new Date().getDate()!=data[data.length-1].date.getDate()) { if (idata[0].date.getDay()data[data.length-1].high?idata[0].high:data[data.length-1].high), low:(idata[0].lowdata[data.length-1].high?idata[0].high:data[data.length-1].high), low:(idata[0].low