//@Name:MA Rising/Falling Periods //@Description:Calculates the number of consecutive days the MA has been rising or falling. The script returns a variable which is the number of trading days. If the MA is falling then the variable is negative. //@Returns:Number //@Width:100 //@Env:Production //@Update:Intraday // 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 // Modified by: Paul Hall, ShareScope Support var dataSource = 0; var useIntra = 0; var dataList = ["Daily","Weekly","Monthly"]; var titleList = ["TDs","Wks","Mnths"] var maPeriod = 5 var maType = 0 var lookback = 0; var maList = ["SMA","EMA","WMA","TMA","VVHF","VCMO","Hull"]; function init(status) { if (status == Loading || status == Editing) { maPeriod = storage.getAt(0); maType = storage.getAt(1); dataSource = storage.getAt(2); useIntra = storage.getAt(3); lookback = storage.getAt(4); } if (status == Adding || status == Editing) { var dlg = new Dialog("Select Settings:", 260, 60); dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("VAL1",60, 5, -1, -1, "Moving Average:","",maPeriod,2,9999); dlg.addDropList("VAL2", 95,5,-1,-1,maList,"","",maType); dlg.addDropList("VAL3",48,22,-1,-1,dataList,"Data Source:","",dataSource); dlg.addTickBox("VAL4",118,24,80,-1,"Include Intraday data",useIntra); dlg.addIntEdit("VAL5",60,40,-1,-1,"Check back from","periods ago",lookback,0,9999); if (dlg.show() == Dialog.Cancel) return false; maPeriod = dlg.getValue("VAL1"); maType = dlg.getValue("VAL2"); dataSource = dlg.getValue("VAL3"); useIntra = dlg.getValue("VAL4"); lookback = dlg.getValue("VAL5"); storage.setAt(0, maPeriod); storage.setAt(1,maType); storage.setAt(2, dataSource); storage.setAt(3, useIntra); storage.setAt(4, lookback); } setTitle("No of "+titleList[dataSource]+" "+maPeriod+maList[maType]+" abv / blw price"); } function getVal(share) { var data = getData(share,dataSource,useIntra) if (data == undefined || data.length<(maPeriod*2)) return; var maCalc = new MA(maPeriod,maType); var maRes = new Array(); var output; for (var i=0;i=1;i--) { if(i == data.length-1-lookback) { if(maRes[i] > maRes[i-1]) { count = 1; continue; } else if(maRes[i] < maRes[i-1]) { count = -1; continue; } else return 0; } if(count > 0) { if(maRes[i] > maRes[i-1]) count ++; else return count; } if(count < 0) { if(maRes[i] < maRes[i-1]) count --; else return count; } } return; } 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