//@LibraryID:926,2 //@Name:Alarm:Pivot Point Cross //@Description:Provides an alert if the price cross one of the pivot lines. //@Returns:Text //@Update:Periodic, 60 //@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 // Amended by: Paul Hall, ShareScope Support var dataList = ["Daily","Weekly","Monthly"]; var dataSource = 0; var useIntra = 0; var ppOption = 0; var ppUp = 1; var ppDown = 1; var sup1Up = 1; var sup1Down = 1; var sup2Up = 1; var sup2Down = 1; var sup3Up = 1; var sup3Down = 1; var res1Up = 1; var res1Down = 1; var res2Up = 1; var res2Down = 1; var res3Up = 1; var res3Down = 1; var pivotList = ["Pivot Point","Support 1","Support 2","Support 3","Resistance 1","Resistance 2","Resistance 3"] function init(status) { if (status == Loading || status == Editing) { dataSource = storage.getAt(0); useIntra = storage.getAt(1); ppOption = storage.getAt(2); ppUp = (ppOption & 8192)?true:false; ppDown = (ppOption & 4096)?true:false; sup1Up = (ppOption & 2048)?true:false; sup1Down = (ppOption & 1024)?true:false; sup2Up = (ppOption & 512)?true:false; sup2Down = (ppOption & 256)?true:false; sup3Up = (ppOption & 128)?true:false; sup3Down = (ppOption & 64)?true:false; res1Up = (ppOption & 32)?true:false; res1Down = (ppOption & 16)?true:false; res2Up = (ppOption & 8)?true:false; res2Down = (ppOption & 4)?true:false; res3Up = (ppOption & 2)?true:false; res3Down = (ppOption & 1)?true:false; } if (status == Adding || status == Editing) { var dlg = new Dialog("Settings...", 230, 160); dlg.addOkButton(); dlg.addCancelButton(); dlg.addText(5,5,100,40,"Trigger alert if the price crosses: Above Below "); dlg.addText(5,22,45,10,(pivotList[0]+":")) dlg.addTickBox("VAL1",50,22,10,10,"",ppUp); dlg.addTickBox("VAL2",80,22,10,10,"",ppDown); dlg.addText(5,39,45,10,(pivotList[1]+":")) dlg.addTickBox("VAL3",50,39,10,10,"",sup1Up); dlg.addTickBox("VAL4",80,39,10,10,"",sup1Down); dlg.addText(5,56,45,10,(pivotList[2]+":")) dlg.addTickBox("VAL5",50,56,10,10,"",sup2Up); dlg.addTickBox("VAL6",80,56,10,10,"",sup2Down); dlg.addText(5,73,45,10,(pivotList[3]+":")) dlg.addTickBox("VAL7",50,73,10,10,"",sup3Up); dlg.addTickBox("VAL8",80,73,10,10,"",sup3Down); dlg.addText(5,90,45,10,(pivotList[4]+":")) dlg.addTickBox("VAL9",50,90,10,10,"",res1Up); dlg.addTickBox("VAL10",80,90,10,10,"",res1Down); dlg.addText(5,107,45,10,(pivotList[5]+":")) dlg.addTickBox("VAL11",50,107,10,10,"",res2Up); dlg.addTickBox("VAL12",80,107,10,10,"",res2Down); dlg.addText(5,124,45,10,(pivotList[6]+":")) dlg.addTickBox("VAL13",50,124,10,10,"",res3Up); dlg.addTickBox("VAL14",80,124,10,10,"",res3Down); dlg.addDropList("VAL15",50,141,50,10,dataList,"Data Source:","",dataSource); dlg.addTickBox("VAL16",115,141,75,-1,"Use Intraday data",useIntra); if (dlg.show() == Dialog.Cancel) return false; ppUp = dlg.getValue("VAL1"); ppDown = dlg.getValue("VAL2"); sup1Up = dlg.getValue("VAL3"); sup1Down = dlg.getValue("VAL4"); sup2Up = dlg.getValue("VAL5"); sup2Down = dlg.getValue("VAL6"); sup3Up = dlg.getValue("VAL7"); sup3Down = dlg.getValue("VAL8"); res1Up = dlg.getValue("VAL9"); res1Down = dlg.getValue("VAL10"); res2Up = dlg.getValue("VAL11"); res2Down = dlg.getValue("VAL12"); res3Up = dlg.getValue("VAL13"); res3Down = dlg.getValue("VAL14"); ppOption = (ppUp?8192:0)+(ppDown?4096:0)+(sup1Up?2048:0)+(sup1Down?1024:0) +(sup2Up?512:0)+(sup2Down?256:0)+(sup3Up?128:0)+(sup3Down?64:0) +(res1Up?32:0)+(res1Down?16:0)+(res2Up?8:0)+(res2Down?4:0) +(res3Up?2:0)+(res3Down?1:0); dataSource = dlg.getValue("VAL15"); useIntra = dlg.getValue("VAL16"); storage.setAt(0, dataSource); storage.setAt(1, useIntra); storage.setAt(2, ppOption); } setTitle("Pivot Point Cross "+(useIntra?"(i":"(")+dataList[dataSource]+")") } function getVal(share) { var data = getData(share,dataSource,useIntra); if(data == undefined || data.length<2) return; if (dataSource == 0) { var price = data[data.length-1]; pivot = (price.high + price.low + price.close) / 3 res1 = pivot + (pivot - price.low); sup1 = pivot - (price.high - pivot); res2 = pivot + (price.high - price.low); sup2 = pivot - (price.high - price.low); res3 = pivot + 2*(price.high - price.low); sup3 = pivot - 2*(price.high - price.low); } else if (dataSource == 1) { var price = data[data.length-2]; pivot = (price.high + price.low + price.close) / 3 res1 = pivot + (pivot - price.low); sup1 = pivot - (price.high - pivot); res2 = pivot + (price.high - price.low); sup2 = pivot - (price.high - price.low); res3 = pivot + 2*(price.high - price.low); sup3 = pivot - 2*(price.high - price.low); } else if (dataSource == 2) { var price = data[data.length-2]; pivot = (price.high + price.low + price.close) / 3 res1 = pivot + (pivot - price.low); sup1 = pivot - (price.high - pivot); res2 = pivot + (price.high - price.low); sup2 = pivot - (price.high - price.low); res3 = pivot + 2*(price.high - price.low); sup3 = pivot - 2*(price.high - price.low); } var currPrice = data[data.length-1].close; var prevPrice = data[data.length-2].close; var trigger = 0; var pivotList = ["Pivot Point","Support 1","Support 2","Support 3","Resistance 1","Resitance 2","Resistance 3"] if(!isAlarmContext) { if(ppUp && (prevPrice < pivot && currPrice > pivot)) trigger = 1; if(ppDown && (prevPrice > pivot && currPrice < pivot)) trigger = -1; if(sup1Up && (prevPrice < sup1 && currPrice > sup1)) trigger = 2; if(sup1Down && (prevPrice > sup1 && currPrice < sup1)) trigger = -2; if(sup2Up && (prevPrice < sup2 && currPrice > sup2)) trigger = 3; if(sup2Down && (prevPrice > sup2 && currPrice < sup2)) trigger = -3; if(sup3Up && (prevPrice < sup3 && currPrice > sup3)) trigger = 4; if(sup3Down && (prevPrice > sup3 && currPrice < sup3)) trigger = -4; if(res1Up && (prevPrice < res1 && currPrice > res1)) trigger = 5; if(res1Down && (prevPrice > res1 && currPrice < res1)) trigger = -5; if(res2Up && (prevPrice < res2 && currPrice > res2)) trigger = 6; if(res2Down && (prevPrice > res2 && currPrice < res2)) trigger = -6; if(res3Up && (prevPrice < res3 && currPrice > res3)) trigger = 7; if(res3Down && (prevPrice > res3 && currPrice < res3)) trigger = -7; if(trigger!=0) { return ("Price crossed "+(trigger<0?"below ":"above ")+(pivotList[Math.abs(trigger)-1])); } return; } else { //Check for triggers and that the alarm has not previously triggered if(ppUp && (prevPrice < pivot && currPrice > pivot) && getValueForShare(share)!=1) { trigger = 1; } if(ppDown && (prevPrice > pivot && currPrice < pivot)&& getValueForShare(share)!=-1) { trigger = -1; } if(sup1Up && (prevPrice < sup1 && currPrice > sup1)&& getValueForShare(share)!=2) { trigger = 2; } if(sup1Down && (prevPrice > sup1 && currPrice < sup1)&& getValueForShare(share)!=-2) { trigger = -2; } if(sup2Up && (prevPrice < sup2 && currPrice > sup2)&& getValueForShare(share)!=3) { trigger = 3; } if(sup2Down && (prevPrice > sup2 && currPrice < sup2)&& getValueForShare(share)!=-3) { trigger = -3; } if(sup3Up && (prevPrice < sup3 && currPrice > sup3)&& getValueForShare(share)!=4) { trigger = 4; } if(sup3Down && (prevPrice > sup3 && currPrice < sup3)&& getValueForShare(share)!=-4) { trigger = -4; } if(res1Up && (prevPrice < res1 && currPrice > res1)&& getValueForShare(share)!=5) { trigger = 5; } if(res1Down && (prevPrice > res1 && currPrice < res1)&& getValueForShare(share)!=-5) { trigger = -5; } if(res2Up && (prevPrice < res2 && currPrice > res2)&& getValueForShare(share)!=6) { trigger = 6; } if(res2Down && (prevPrice > res2 && currPrice < res2)&& getValueForShare(share)!=-6) { trigger = -6; } if(res3Up && (prevPrice < res3 && currPrice > res3)&& getValueForShare(share)!=7) { trigger = 7; } if(res3Down && (prevPrice > res3 && currPrice < res3)&& getValueForShare(share)!=-7) { trigger = -7; } //TRIGGER ALARM if(trigger!=0 && (getValueForShare(share)==0 || getValueForShare(share)==undefined)) { setValueForShare(share,trigger) return ("Price crossed "+(trigger<0?"below ":"above ")+(pivotList[Math.abs(trigger)-1])); } //RESET ALARM if(getValueForShare(share)==1 && currPrice <= pivot) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-1 && currPrice > pivot) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==2 && currPrice < sup1) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-2 && currPrice > sup1) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==3 && currPrice < sup2) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-3 && currPrice > sup2) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==4 && currPrice < sup3) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-4 && currPrice > sup3) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==5 && currPrice < res1) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-5 && currPrice > res1) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==6 && currPrice < res2) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-6 && currPrice > res2) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==7 && currPrice < res3) { trigger = 0; setValueForShare(share,0) } if(getValueForShare(share)==-7 && currPrice > res3) { trigger = 0; setValueForShare(share,0) } 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