//@Name:Days Since MA crossed //@Description:Calculates the number of days since a cross (dead or golden) as represented on the historical chart including the use of Intraday data. //@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 var MAtype1 = 1 var MAtype2 = 1 var maList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","VIDYA","Hull"] var maListShort = ["SMA","EMA","WMA","TMA","V-VHF","V-CMO","VIDYA","Hull"]; var MAperiod1 = 13; var MAperiod2 = 26; var outputList = ["Days since Golden Cross","Days since Dead Cross","Gold-Dead difference","Price at last Golden cross","Price at last Dead cross"]; var outputType = 0; function init(status) { if (status == Loading || status == Editing) { MAperiod1 = storage.getAt(0); MAperiod2 = storage.getAt(1); MAtype1 = storage.getAt(2); MAtype2 = storage.getAt(3); outputType = storage.getAt(4); } if (status == Adding || status == Editing) { var dlg = new Dialog("Select Settings:", 250, 65); dlg.addOkButton(); dlg.addCancelButton(); dlg.addDropList("Val1", 120,5,-1,-1,maList,"","",MAtype1); dlg.addDropList("Val2", 120,25,-1,-1,maList,"","",MAtype2); dlg.addIntEdit("Val3",45, 5, -1, -1, "MA Period 1","Days",MAperiod1,2,1000); dlg.addIntEdit("Val4",45, 25, -1, -1, "MA Period 2","Days",MAperiod2,2,1000); dlg.addDropList("Val5", 30,45,110,-1,outputList,"Return","",outputType); if (dlg.show() == Dialog.Ok) { MAtype1 = dlg.getValue("Val1"); MAtype2 = dlg.getValue("Val2"); MAperiod1=dlg.getValue("Val3"); MAperiod2=dlg.getValue("Val4"); outputType=dlg.getValue("Val5"); storage.setAt(0, MAperiod1); storage.setAt(1, MAperiod2); storage.setAt(2, MAtype1); storage.setAt(3, MAtype2); storage.setAt(4, outputType); } else { return false; } } setTitle(outputList[outputType]+" ("+MAperiod1+maListShort[MAtype1]+"/"+MAperiod2+maListShort[MAtype2]+")"); } function getVal(share) { var iClose=share.getIClose(); var iMid=share.getIMid(); var uClose=0; var data=share.getCloseArray(); if (data.length<(MAperiod1>MAperiod2?MAperiod1:MAperiod2)) return; if (iClose!=undefined) data[data.length]=iClose; else if (iMid!=undefined) data[data.length]=iMid; var ma1 = new MA(MAperiod1,MAtype1); var ma2 = new MA(MAperiod2,MAtype2); var ma1res = new Array(); var ma2res = new Array(); var gcDays; var dcDays; var closeAtCross; for (var i=0;ima2res[t]) { gcDays = i; closeAtCross = data[t]; break; } } } if (outputType==1 || outputType==2 || outputType==4) { for (var i=0;ima2res[y] && ma1res[t]