//@Name:Days Since Price Crossed MA //@Description:Calculates the number of days since the price has crossed the MA. //@Returns:Number //@Width:100 //@Env:Production // 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 var MAtype1 = 1 var maList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","VIDYA"]; var maListShort = ["SMA","EMA","WMA","TMA","V-VHF","V-CMO","VIDYA"]; var MAperiod1 = 13; var outputList = ["Days since Price Crossed Above MA","Days since Price Crossed Below MA"]; var outputType = 0; var dataList = ["Daily","Weekly","Monthly"]; var dataSource = 0; var titleDataList = ["Days","Weeks","Months"]; function init(status) { if (status == Loading || status == Editing) { MAperiod1 = storage.getAt(0); MAtype1 = storage.getAt(1); outputType = storage.getAt(2); dataSource = storage.getAt(3); } if (status == Adding || status == Editing) { var dlg = new Dialog("Select Settings:", 250, 65); dlg.addOkButton(); dlg.addCancelButton(); dlg.addDropList("Val1", 90,5,-1,-1,maList,"","",MAtype1); dlg.addIntEdit("Val2",45, 5, -1, -1, "MA Period","",MAperiod1,2,1000); dlg.addDropList("Val3", 45,22,130,-1,outputList,"Return","",outputType); dlg.addDropList("Val4",45,39,48,-1,dataList,"Data Source","",dataSource); if (dlg.show() == Dialog.Ok) { MAtype1 = dlg.getValue("Val1"); MAperiod1=dlg.getValue("Val2"); outputType=dlg.getValue("Val3"); dataSource=dlg.getValue("Val4"); storage.setAt(0, MAperiod1); storage.setAt(1, MAtype1); storage.setAt(2, outputType); storage.setAt(3, dataSource); } else { return false; } } setTitle(titleDataList[dataSource]+" since price crossed "+(outputType==0?"above":"below")+" "+MAperiod1+" "+maListShort[MAtype1]); } function getVal(share) { if (dataSource==0) var data = share.getPriceArray(); if (dataSource==1) var data = share.getWeeklyBarArray(); if (dataSource==2) var data = share.getMonthlyBarArray(); var ma1 = new MA(MAperiod1,MAtype1); var ma1res = new Array(); var gcDays; var dcDays; for (var i=0;idata[y].close && ma1res[t]data[t].close) { dcDays = i; break; } } } if (outputType==0) return gcDays; if (outputType==1) return dcDays; }