//@LibraryID:819,0 //@Name:Global Stop-Loss. //@Description:Calculates and displays the percentage/value/ATR stop-loss. //@Returns:Number //@Width:60 //Author: Richard Chiesa, ShareScope 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 slType = 0; var slTypeList = ["Stop-loss","Stop-gain"]; var slValue = 10; var slStart = getShare("UKI:UKX").getPrice().date; var sl_sg = 0; function init(status) { if (status == Loading || status == Editing) { slType = storage.getAt(0); sl_sg = storage.getAt(1); slValue = storage.getAt(2); slStart = new Date(dateNumGetYear(storage.getAt(3)),dateNumGetMonth(storage.getAt(3)),dateNumGetDay(storage.getAt(3))); } if (status == Adding || status == Editing) { dlg = new Dialog("Stop-loss settings", 145, 78); dlg.addOkButton(); dlg.addCancelButton(); dlg.addDropList("DL1",8,-1,80,-1,["Percent","Value","ATR"],"","",slType); dlg.addDropList("DL2",8,-1,80,-1,slTypeList,"","",sl_sg); dlg.addNumEdit("NUM1",8,-1,-1,-1,"","Stop-loss Value",slValue,0.1,1000); dlg.addDatePicker("DP1",8,-1,-1,-1, "", "Start date", slStart); if (dlg.show()==Dialog.Cancel) return false; slType = dlg.getValue("DL1"); sl_sg = dlg.getValue("DL2"); slValue = dlg.getValue("NUM1"); slStart = dlg.getValue("DP1"); storage.setAt(0, slType); storage.setAt(1, sl_sg); storage.setAt(2, slValue); storage.setAt(3, dateNum(slStart)); } setTitle(slValue+(slType==0?"%":(slType==1?"p":"ATR"))+" "+slTypeList[sl_sg]); } function getVal(share) { var data = share.getPriceArrayDates(slStart); var atr1 = new ATR(14); var sl; for (var i=0;isl) sl = data[i].close*(1-slValue/100); else if (slType==1&&sl_sg==0&&data[i].close-slValue>sl) sl = data[i].close-slValue; else if (slType==2&&sl_sg==0&&data[i].close-(slValue*atrValue)>sl) sl = data[i].close-(slValue*atrValue); else if (slType==0&&sl_sg==1&&data[i].close*(1+slValue/100)