//@Name:PVI Value //@Description: Returns the value of the Positive Volume Index(PVI) or it's signal line //@Returns:Number //@Width:60 //@Env:Production //Author: Phil Tolhurst, ShareScript Support // 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. var period = 9; var outputList = ["PVI","Signal"]; var outputType = 0; var dataList = ["Daily","Weekly","Monthly"]; var dataSource = 0 function init(status) { if (status == Loading || status == Editing) { period = storage.getAt(0); outputType = storage.getAt(1); dataSource = storage.getAt(2); } if (status == Adding || status == Editing) { dlg = new Dialog("Positive Volume Index Settings", 200, 65); dlg.addOkButton(); dlg.addCancelButton(); dlg.addIntEdit("VAL1",5,22,-1,-1, "","Signal Period", period, 1, 5000); dlg.addDropList("VAL2",47,5,40,-1,outputList,"Return the "," value.",outputType); dlg.addDropList("VAL3",47,39,40,-1,dataList,"Data Source","",dataSource); if (dlg.show()==Dialog.Cancel) return false; period= dlg.getValue("VAL1"); outputType = dlg.getValue("VAL2"); dataSource = dlg.getValue("VAL3"); storage.setAt(0, period); storage.setAt(1, outputType); storage.setAt(2, dataSource); } if (outputType==0) setTitle("PVI value( "+dataList[dataSource]+")"); else setTitle("PVI Signal value( "+period+" "+dataList[dataSource]+")"); } function getVal(share) { if (dataSource==0) var data=share.getPriceArray(); else if (dataSource==1) var data=share.getWeeklyBarArray(); else if (dataSource==2) var data=share.getMonthlyBarArray(); var PVI = []; var maCalc = new MA(period,MA.Simple) var maRes = []; var calcStart = 0; for(i=0;i0) { calcStart==1; PVI[i]=data[i].close;; maRes[i]=maCalc.getNext(PVI[i]); continue; } if (data[i].volume==0 && calcStart==0) { PVI[i]=null; maRes[i] = null; continue } if (i>0 && data[i-1].volume==0 && data[i].volume>0 && calcStart==0) { calcStart==1; PVI[i]=data[i].close; maRes[i]=maCalc.getNext(PVI[i]); continue; } else { if (data[i].volume>data[i-1].volume) { PVI[i] = PVI[i-1]+(((data[i].close - data[i-1].close ) / data[i-1].close) * PVI[i-1]); } else { PVI[i]=PVI[i-1]; } maRes[i]=maCalc.getNext(PVI[i]); } } if (outputType==0) return PVI[PVI.length-1]; else return maRes[maRes.length-1]; }