//@LibraryID:70,1 //@Name: Alarm:Price Crossing MA //@Description:An alarm that triggers when either the High/Low or Close crosses a specified MA. //@Update:Periodic, 60 //@Env:Production //@Returns:Text //@Width:120 // 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 var period = 25; var MAType = 1; var MAList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","VIDYA"]; var MAreturnList = ["SMA","EMA","WME","TME","VarVHF","VarCMO","VIDYA"]; var dataType = 0; var dataSource = ["High/Low","Close"]; function init(status) { if (status == Loading || status == Editing) { period = storage.getAt(0); MAType = storage.getAt(1); dataType = storage.getAt(2); } if (status == Adding || status == Editing) { dlg = new Dialog("Price over MA", 235, 60); dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("VAL1", 50, -1,-1, -1, "MA Period","", period, 1, 5000); dlg.addDropList("VAL2",50,-1,-1,-1, MAList, "MA Type","",MAType); dlg.addDropList("VAL3", 50,-1,-1,-1,dataSource,"Check for the","crossing the MA.",dataType); if (dlg.show()==Dialog.Cancel) return false; period= dlg.getValue("VAL1"); MAType = dlg.getValue("VAL2"); dataType =dlg.getValue("VAL3"); storage.setAt(0, period); storage.setAt(1, MAType); storage.setAt(2,dataType); } } function getVal(share) { //Get data array and add intraday data as the last Open,High,Low,Close and Volume figures in the array var data = share.getPriceArray(); if (data.lengthdata[data.length-1].dateNum+5) 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()) { var tclose = share.getIClose(); data[data.length]={open:idata[0].open,high:idata[0].high,low:idata[0].low,close:(tclose==null?share.getIMid():tclose),volume:idata[0].volume}; } //Calculate MA var MAcalc=new MA(period,MAType) var MAres = new Array(); for (i=0;iMAres[t]) { setValueForShare(share, 1); return dataType==0?"High Crossed Above "+period+":"+MAreturnList[MAType]:"Close Crossed Above MA "+period+":"+MAreturnList[MAType]; } if (getValueForShare(share)!=-1 && (dataType==0?data[y].low:data[y].close)>MAres[y] && (dataType==0?data[t].low:data[t].close)MAres[t]) { setValueForShare(share, 0); } }