//@Name:ROC/Momentum //@Description: Returns the Rate of Change or Momentum value over a specified period. //@Env:Production //@Returns:Number //@Width:120 //@Update:Periodic,15 // 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, ShareScript Support //Modified by: Paul Hall, ShareScope Support var returnType = 1; var returnList = ["Absolute Change","Percentage Change","Momentum"]; var titleList = ["Abs Chng","ROC","Momentum"]; var period = 20; var outputType = 0; var outputList = ["Current Value", "Value x periods ago", "Change since x periods ago","% Change since x periods ago"]; var outputTitleList = ["","","chng","% chng"]; var outputPeriod = 1; var dataList = ["1m","2m","2.5m","3m","4m","5m","6m","10m","15m","20m","30m","1h","2h","3h","4h","Daily","Weekly","Monthly"]; var dataChoice = 15; var useIntra = 0; function init(status) { if (status == Loading || status == Editing) { period = storage.getAt(0); returnType = storage.getAt(1); dataChoice = storage.getAt(2); useIntra = storage.getAt(3); outputType = storage.getAt(4); outputPeriod = storage.getAt(5); } if (status == Adding || status == Editing) { dlg = new Dialog("ROC Period", 275,80); dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("VAL1", 30, 5,-1, -1, "Period","", period, 1, 5000); dlg.addDropList("VAL2",110,5,80,-1, returnList, "Calculate the","",returnType); dlg.addDropList("VAL3",45,30,115,-1,outputList,"Output the","",outputType); dlg.addIntEdit("VAL4",180,30,-1,-1,"x=","",outputPeriod,1,9999); dlg.addDropList("VAL5",70,56,-1,-1,dataList,"Choose data type:","",dataChoice); dlg.addTickBox("VAL6",140,58,80,-1,"Include Intraday data",useIntra); if (dlg.show()==Dialog.Cancel) return false; period= dlg.getValue("VAL1"); returnType= dlg.getValue("VAL2"); outputType=dlg.getValue("VAL3"); outputPeriod=dlg.getValue("VAL4"); dataChoice=dlg.getValue("VAL5"); useIntra = dlg.getValue("VAL6"); storage.setAt(0, period); storage.setAt(1, returnType); storage.setAt(2, dataChoice); storage.setAt(3, useIntra); storage.setAt(4, outputType); storage.setAt(5, outputPeriod); } if(outputType==0) setTitle(titleList[returnType]+"("+period+") "+(useIntra?"i"+dataList[dataChoice]:dataList[dataChoice])); else setTitle(titleList[returnType]+"("+period+") "+outputTitleList[outputType]+" "+outputPeriod+" periods ago "+(useIntra?"(i"+dataList[dataChoice]+")":"("+dataList[dataChoice]+")")); } function getVal(share) { var data = getData(share,dataChoice,useIntra,period+outputPeriod+1); if (data==undefined || data.length=0;i--) { tempData = share.getIBarArray(i,dataPeriod*60); if (tempData==undefined || tempData.length<1) continue; data = data.concat(share.getIBarArray(i,dataPeriod*60)); } if (data==undefined || data[0]==undefined || data.length<2) return []; } else if (dataType==15 && useIntra==0) var data = share.getPriceArray(); else if (dataType==16 && useIntra==0) var data = share.getWeeklyBarArray(); else if (dataType==17 && useIntra==0) var data = share.getMonthlyBarArray(); //daily else if (dataType==15 && useIntra==1) { var data = share.getPriceArray(); if (data.length<2) return []; var idata = share.getIBarArray(0,86400); if (idata!=undefined && idata.length==1 && new Date().getDate()==idata[0].date.getDate() && new Date().getDate()!=data[data.length-1].date.getDate()) { data[data.length]={ open:idata[0].open, high:idata[0].high, low:idata[0].low, close:(share.getIClose()==null?share.getIMid():share.getIClose()), volume:idata[0].volume, dateNum:idata[0].dateNum}; } } //weekly else if (dataType==16 && useIntra==1) { var data = share.getWeeklyBarArray(); if (data.length<2) return []; var idata = share.getIBarArray(0,86400); 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].lowtempData[tempData.length-1].high?idata[0].high:tempData[tempData.length-1].high), low:(idata[0].lowdata[data.length-1].high?tempData[i].high:data[data.length-1].high), low:(tempData[i].low