//@LibraryID:977,0 //@Name:Stop Loss - ATR //@Description: Creates a stop loss based on ATR multiples but allows the period of the ATR to be specified as well. // 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 //Globals - accessable by any part of the script. var settingsArray = new Array(); var settingsFile; //shareID will be set later to give a unique identifier for every share/instrument in the program var share; var shareID; function init() { setTitle("Stop Loss: Not Set"); //Open the settings file and pass all of the data into an array and then close the file again. //We use try and catch to check if the file exists already and if not to create it. settingsFile = new File(); var text; try { settingsFile.open("ATRStopLossSettings.txt",File.ReadMode); for (var a=0;text = settingsFile.readLine();a++) { if (text != "") settingsArray[a] = text.split(","); } settingsFile.close("ATRStopLossSettings.txt"); } catch (e) { settingsFile.open("ATRStopLossSettings.txt",File.WriteMode); settingsFile.close("ATRStopLossSettings.txt"); } //Creates a button on the Study Manager buttonHandle = createButton("Set", onButton0); } //What happens if the set button is clicked: function onButton0() { //Load Settings for the share if possible var SLsettings=getSettings(shareID); //if there are settings use them to set the dialog defaults if (SLsettings.length>0) { var SLvalue=SLsettings[1]; var SLmultiplier=SLsettings[2]; var SLcolour=SLsettings[3]; var SLpen=SLsettings[4]; var SLwidth=SLsettings[5]; var SLstartDate=SLsettings[6]*1; var steppedLine = SLsettings[7]; } //if there are no settings use the defaults and the first date of data available for the share. else { var SLvalue=14; var SLmultiplier=3; var SLcolour=0; var SLpen=0; var SLwidth=0; var SLstartDate = new Date(); var steppedLine = 0; } //Calculate how many years can be selected in the dialog var years = new Array(); years[0] = dateNumGetYear(bars[0].dateNum); var firstDate = bars[0].date; //Create a dialog box to set the values of the script do { var dlg = new Dialog("Stop Loss Settings", 200, 115); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(5,5,130,50,"ATR Properties"); dlg.addIntEdit("VAL1",60,17,-1,-1, "ATR Periods:","",SLvalue,2,1000); dlg.addIntEdit("VAL2",60,34,-1,-1, "Multiplier:","",SLmultiplier,1,999); dlg.addColLinePicker("VAL6",100,17,-1,-1,"","",SLcolour,SLpen,SLwidth); dlg.addGroupBox(5,65,130,40,"Start Date"); dlg.addDatePicker("VAL3",15,77,-1,-1, "","",new Date(SLstartDate)); dlg.addGroupBox(140,65,55,40,""); dlg.addTickBox("VAL4",143,75,40,-1,"Draw",steppedLine); dlg.addText(155,83,30,10,"Stepped"); dlg.addText(155,92,30,10,"Line."); if (dlg.show() == Dialog.Cancel) return false; SLvalue = dlg.getValue("VAL1"); SLmultiplier = dlg.getValue("VAL2"); SLstartDate = dlg.getValue("VAL3"); SLcolour = dlg.getValue("VAL6").colour; SLpen = dlg.getValue("VAL6").pen; SLwidth = dlg.getValue("VAL6").width; steppedLine = dlg.getValue("VAL4"); } while (SLstartDate>new Date() || SLstartDate0) { setTitle("Stop Loss: ATR("+SLvalue+")*"+SLmultiplier); draw(SLsettings); } else setTitle("Stop Loss: Not Set"); } //Updates the graph if the data is updated. function onNewBarUpdate() { share = getCurrentShare(); try { shareID = share.getShareScopeID()+":"+share.getShareNum(); } catch (e) { return; } var SLsettings=getSettings(shareID); if (SLsettings.length>0) draw(SLsettings); } //Draws a new line depending which button has been clicked on function draw(SLsettings) { clearDisplay(); var SLvalue = (SLsettings[1]*1); var SLmultiplier = (SLsettings[2]*1); var atrCalc = new ATR(SLvalue); var atrRes = [] for (var i = 0;i= SLstartDateNum && bars[i-1].dateNum < SLstartDateNum) { slArray[i] = bars[i].close-atrRes[i]; continue; } else if(i>0 && slArray[i-1]>0 && (bars[i].close-atrRes[i]>slArray[i-1])) { slArray[i] = bars[i].close-atrRes[i]; continue; } else if(i>0) { slArray[i] = slArray[i-1] } } if(SLsettings[7] == 0) { //Line colour and style setPenStyle(SLsettings[4],SLsettings[5],SLsettings[3]); beginPath(); var start = 0; for(var i = 0;i=SLstartDateNum && start == 0) { moveTo (i,slArray[i]); start = 1; continue; } else { lineTo (i,slArray[i]); } } endPath(); drawPath(); } else { //Line colour and style setPenStyle(SLsettings[4],SLsettings[5],SLsettings[3]); beginPath(); var start = 0; for(var i = 0;i=SLstartDateNum && start == 0) { moveTo (i,slArray[i]); start = 1; continue; } else { lineTo (i,slArray[i-1]); lineTo(i,slArray[i]); } } endPath(); drawPath(); } } function getSettings(shareID) { //temporary storage of the settings array var tempSettings = new Array(); //Scan the settings array for data for the current share for (var b=0;b