//@Name:Alarm:Donchian Cross //@Description:An alarm that triggers when the mid price crosses the Donchian Channels //@Update:Periodic, 60 //@Env:Production //@Returns:Text //@Width:200 // 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: Richard Chiesa, ShareScript Support var dataList = ["Daily","Weekly","Monthly"]; var dataType = 0; var period = 20; function init(status) { if (status == Loading || status == Editing) { dataType = storage.getAt(0); period = storage.getAt(1); } if (status == Adding || status == Editing) { dlg = new Dialog("Donchian Channel Cross",150,45); dlg.addOkButton(); dlg.addCancelButton(); dlg.addDropList("DL1",8,-1,80,-1,dataList,"","",dataType); dlg.addIntEdit("INT1",8,-1,-1,-1,"","Period",period,2,1000); if (dlg.show()==Dialog.Cancel) return false; dataType = dlg.getValue("DL1"); period = dlg.getValue("INT1"); storage.setAt(0, dataType); storage.setAt(1, period); } } function getVal(share) { if (dataType == 0) { var data = share.getPriceArray(period*4); 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()) { var tclose = share.getIClose(); var topen = share.getIOpen(); data[data.length]={open:(topen==null?idata[0].open:topen),high:idata[0].high,low:idata[0].low,close:(tclose==null?share.getIMid():tclose),volume:idata[0].volume}; } } if (dataType == 1) var data = share.getWeeklyBarArray(period*4); if (dataType == 2) var data = share.getMonthlyBarArray(period*4); if (data.length<3) return; var price = data[data.length-1].close var hh,ll; for (var i=Math.max(0, data.length-(period+1));idata[i].low) ll = data[i].low; } //print(hh+" "+ll+" "+data[data.length-1].open+" "+data[data.length-1].high+" "+data[data.length-1].low+" "+data[data.length-1].close); var oldAlarm = getValueForShare(share); var alarmText; if (oldAlarm == undefined || !isAlarmContext) { if (price>hh) { setValueForShare(share, 1); alarmText = "Mid crossed top channel ("+period+")"; } if (pricell) setValueForShare(share, undefined); } return alarmText; }