//@Name:High/Low Line //@Description:Draws a line beween two points. Will snap to the closest high/low price. // 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. // Author: Phil Tolhurst, ShareScope Support var startPoint = []; var endPoint = []; var state; var settings = []; var HLsettings = []; function init(status) { if(status==Adding) { var dlg = new Dialog("Settings...", 225, 140); dlg.addOkButton(); dlg.addText(5,5,150,150,"To use this script click on the Set Start Point button and click on a bar/candle either close to the high or the low."); dlg.addText(5,39,150,150,"The script will work out which you are aiming for so there is no need to be 100% acurate. "); dlg.addText(5,63,150,150,"Repeat for the End Point."); dlg.addText(5,80,150,150,"The script will then draw a line between the two points."); dlg.addText(5,105,150,150,"The \"Settings...\" button allows you to determine the colour and style of the line and what values are shown on the line."); if (dlg.show() == Dialog.Ok) return; } //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. var settingsFile = new File(); try { settingsFile.open("High_Low_Line_Settings.txt",File.ReadMode); } catch (e) { settingsFile.open("High_Low_Line_Settings.txt",File.WriteMode); settingsFile.close("High_Low_Line_Settings.txt"); } //Create two buttons on the Study Manager buttonHandle = createButton("Set Start Point", onButton0); buttonHandle = createButton("Set End Point",onButton1); buttonHandle = createButton("Settings...",onButton2); } //onMouseClick is permanently running so we need to determine if one of the two buttons has been clicked. //To do so we set a variable called 'state' for the A button it is set to 1 and for the B button it is set to 2 function onButton0() { setButtonText(0, "Set Start Point"); state = 1; } function onButton1() { setButtonText(1, "Set End Point"); state = 2; } function onButton2() { var dlg = new Dialog("Settings...", 175, 100); dlg.addOkButton(); dlg.addCancelButton(); dlg.addColLinePicker("VAL1",5,5,-1,-1,"","",HLsettings[7]*1,HLsettings[8]*1,HLsettings[9]*1); dlg.addText(5,26,100,20,"Show on line:"); dlg.addTickBox("VAL2",5,39,80,-1,"Absolute Change",HLsettings[10]); dlg.addTickBox("VAL3",5,56,80,-1,"Percentage Change",HLsettings[11]); dlg.addTickBox("VAL4",5,73,80,-1,"Slope",HLsettings[12]); if (dlg.show() == Dialog.Cancel) return false; HLsettings[7] = dlg.getValue("VAL1").colour; HLsettings[8] = dlg.getValue("VAL1").pen; HLsettings[9] = dlg.getValue("VAL1").width; HLsettings[10] = dlg.getValue("VAL2")==true?1:0; HLsettings[11] = dlg.getValue("VAL3")==true?1:0; HLsettings[12] = dlg.getValue("VAL4")==true?1:0; writeSettings(); if(HLsettings[2]>0 && HLsettings[4]>0) draw(); } function onMouseClick(frame,date,value) { if(state == 1 && bar) { var high = bar.high var low = bar.low if(high-value0 && HLsettings[4]>0) draw(); } if(state == 2 && bar) { var high = bar.high var low = bar.low if(high-value0 && HLsettings[4]>0) draw(); } } function onNewChart() { share = getCurrentShare(); //creates a unique identifier for each share. This is important as it's used to get the setttings for that share from the settings file the script creates. shareID = share.getShareScopeID()+":"+share.getShareNum(); HLsettings=getSettings(shareID); if(HLsettings[0] == undefined || HLsettings[1]==0) { HLsettings = [shareID,0,0,0,0,0,0,255,0,1,0,1,0]; setButtonText(0, "Set Start Point"); setButtonText(1, "Set End Point"); } else { setButtonText(0, (HLsettings[3]==1?"High":"Low")+":"+(HLsettings[2]*1).toFixed(2)); setButtonText(1, (HLsettings[6]==1?"High":"Low")+":"+(HLsettings[5]*1).toFixed(2)); draw(); } } function draw() { clearDisplay(); setPenStyle((HLsettings[8]*1),(HLsettings[9]*1),(HLsettings[7]*1)); moveTo(HLsettings[1]*1, HLsettings[2]*1); lineTo(HLsettings[4]*1, HLsettings[5]*1); setFontColour(HLsettings[7]*1); var absChange = (HLsettings[5]*1-HLsettings[2]*1).toFixed(2) var percChange = (((HLsettings[5]*1-HLsettings[2]*1)/HLsettings[2]*1)*100).toFixed(2) var slope = ((HLsettings[2]*1-HLsettings[5]*1)/(HLsettings[1]*1-HLsettings[4]*1)).toFixed(2) if(Math.abs(percChange)<1) drawText((HLsettings[1]*1+HLsettings[4]*1)/2,(HLsettings[2]*1+HLsettings[5]*1)/2,((HLsettings[10]?absChange+" /":"")+(HLsettings[11]?" "+percChange+"%":"")+(HLsettings[12]?" ("+slope+"/bar)":"")),BoxAlign.Above,TextAlign.Centre,0,0); else drawText((HLsettings[1]*1+HLsettings[4]*1)/2,(HLsettings[2]*1+HLsettings[5]*1)/2,((HLsettings[10]?absChange+" /":"")+(HLsettings[11]?" "+percChange+"%":"")+(HLsettings[12]?" ("+slope+"/bar)":"")),BoxAlign.Right,TextAlign.Right,0,0); } function getSettings(shareID) { var settingsFile = new File(); //temport array that stores all of the settings loaded from the file. var tempSettingsArray = []; //temporary storage of the settings array that will be returned. var tempSettings = new Array(); //Load the file and pass the contents to the tempSettingsArray settingsFile.open("High_Low_Line_Settings.txt",File.ReadMode); for (var a=0;text = settingsFile.readLine();a++) { if (text != "") tempSettingsArray[a] = text.split(","); } settingsFile.close("High_Low_Line_Settings.txt"); //Scan the settings array for data for the current share and for the button that was clicked for (var b=0;b