//@Name:Four MAs //@Description:Draws up to four MAs on the chart. Use this to extend the program's 8 MA limit. // 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: Richard Chiesa // This is a hacked version of the Four MAs Per Share script. The value in the file reserved for the shareID is always set to 0 so all shares use the same settings. //Globals - accessable by any part of the script. var settingsArray = new Array(); var settingsFile; //Arrays for the drop down lists in the dialog box. var maTitleList = ["SMA","EMA","WMA","TMA","VVHF","VCMO","VIDYA","TEMA","HMA"]; var maList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","VIDYA","TEMA","Hull"]; var dataList = ["Open","High","Low","Close","Typical","Median","Weighted"]; var channelList = ["Envelopes","Bolinger Bands"]; var chanButtonList = ["Env","Boll"]; function init() { setTitle("Four MAs"); //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("Four_MAs.txt",File.ReadMode); for (var a=0;text = settingsFile.readLine();a++) { if (text != "") settingsArray[a] = text.split(","); } settingsFile.close("Four_MAs.txt"); } catch (e) { settingsFile.open("Four_MAs.txt",File.WriteMode); settingsFile.close("Four_MAs.txt"); } //Creates a button on the Study Manager buttonHandle = createButton("Set 1", onButton0); buttonHandle = createButton("Set 2", onButton1); buttonHandle = createButton("Set 3", onButton2); buttonHandle = createButton("Set 4", onButton3); } //What happens if the set button is clicked: function onButton0() { //Load Settings for the share if possible var MAsettings=getSettings(1); //Call the dialog function which in turn will also draw/re-draw the lines on the chart setSettings(MAsettings,1); } function onButton1() { //Load Settings for the share if possible var MAsettings=getSettings(2); //Call the dialog function which in turn will also draw/re-draw the lines on the chart setSettings(MAsettings,2); } function onButton2() { //Load Settings for the share if possible var MAsettings=getSettings(3); //Call the dialog function which in turn will also draw/re-draw the lines on the chart setSettings(MAsettings,3); } function onButton3() { //Load Settings for the share if possible var MAsettings=getSettings(4); //Call the dialog function which in turn will also draw/re-draw the lines on the chart setSettings(MAsettings,4); } //This defines what happens when he study is added to a chart or we select a new share. function onNewChart() { clearDisplay(); var MAsettings=getSettings(1); if (MAsettings.length>0) { setButtonText(0,MAsettings[4]+maTitleList[MAsettings[3]]+(MAsettings[10]>0?(", "+MAsettings[12]+(MAsettings[12]%1?"":".0")+(MAsettings[11]==0?"% ":" ")+chanButtonList[MAsettings[11]]):"")); draw(MAsettings); } else setButtonText(0,"Set 1"); var MAsettings=getSettings(2); if (MAsettings.length>0) { setButtonText(1,MAsettings[4]+maTitleList[MAsettings[3]]+(MAsettings[10]>0?", "+MAsettings[12]+(MAsettings[12]%1?"":".0")+(MAsettings[11]==0?"% ":" ")+chanButtonList[MAsettings[11]]:"")); draw(MAsettings); } else setButtonText(1,"Set 2"); var MAsettings=getSettings(3); if (MAsettings.length>0) { setButtonText(2,MAsettings[4]+maTitleList[MAsettings[3]]+(MAsettings[10]>0?", "+MAsettings[12]+(MAsettings[12]%1?"":".0")+(MAsettings[11]==0?"% ":" ")+chanButtonList[MAsettings[11]]:"")); draw(MAsettings); } else setButtonText(2,"Set 3"); var MAsettings=getSettings(4); if (MAsettings.length>0) { setButtonText(3,MAsettings[4]+maTitleList[MAsettings[3]]+(MAsettings[10]>0?", "+MAsettings[12]+(MAsettings[12]%1?"":".0")+(MAsettings[11]==0?"% ":" ")+chanButtonList[MAsettings[11]]:"")); draw(MAsettings); } else setButtonText(3,"Set 4"); } //Updates the graph if the data is updated. function onNewBarUpdate() { clearDisplay(); var MAsettings=getSettings(1); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(2); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(3); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(4); if (MAsettings.length>0) draw(MAsettings); } //Updates the graph if the data is updated. function onZoom() { clearDisplay(); var MAsettings=getSettings(1); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(2); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(3); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(4); if (MAsettings.length>0) draw(MAsettings); } //Draws a new line depending which button has been clicked on function draw(MAsettings) { var MA1choice = MAsettings[2]; var MA1type = MAsettings[3]; var MA1period = MAsettings[4]; var MA1colour1 = MAsettings[5]; var MA1pen1 = MAsettings[6]; var MA1width1 = MAsettings[7]; var MA1downColour = MAsettings[8]; var MA1colour2 = MAsettings[9]; var MA1channelChoice = MAsettings[10]; var MA1channelType = MAsettings[11]; var MA1channelValue = MAsettings[12]; var MA1channelColour = MAsettings[13]; var MA1channelPen = MAsettings[14]; var MA1channelWidth = MAsettings[15]; var MA1channelFill = MAsettings[16]; var MA1channelFillColour = MAsettings[17]; var MA1dataSource = MAsettings[18]; var mainMAarray = new Array(); if (MA1type == 8) { var sqrtperiod1 = Math.round(Math.sqrt(MA1period)); var ma1 = new MA(MA1period/2, MA.Weighted); var ma2 = new MA(MA1period, MA.Weighted); var ma3 = new MA(sqrtperiod1, MA.Weighted); for (var i=0; i=0;j--) { lineTo(j,mainMAarray[j]*(1-(MA1channelValue/100))); } lineTo(0,mainMAarray[0]*(1-(MA1channelValue/100))); endPath(); fillPath(); } } //draw bolinger bands if (MA1channelType==1) { var upBB = new Array(); var downBB = new Array(); var sumPmMA=0 setPenStyle(MA1channelPen,MA1channelWidth,MA1channelColour); { for(var i=0;i=MA1period-1;j--) { lineTo(j,downBB[j]); } lineTo(MA1period-1,upBB[MA1period-1]); endPath(); fillPath(); } } } } function TEMA(p) { this.period = p; this.emaVal1; this.emaVal2; this.emaVal3; } TEMA.prototype.getNext = function (price) { var ma1 = new MA(this.period, MA.Exponential); var ma2 = new MA(this.period, MA.Exponential); var ma3 = new MA(this.period, MA.Exponential) if (this.emaVal1 != undefined) this.emaVal1 = ma1.getNext(this.emaVal1); this.emaVal1 = ma1.getNext((price.high+price.low+price.close)/3); if (this.emaVal2 != undefined) this.emaVal2 = ma2.getNext(this.emaVal2); this.emaVal2 = ma2.getNext(this.emaVal1); if (this.emaVal3 != undefined) this.emaVal3 = ma3.getNext(this.emaVal3); this.emaVal3 = ma3.getNext(this.emaVal2); var tema = 3*this.emaVal1-3*this.emaVal2+this.emaVal3; return tema; } //returns the correct type of data to be used in the MA calculation function getData(x, type) { if (type == 0) return x.open; else if (type == 1) return x.high; else if (type == 2) return x.low; else if (type == 3) return x.close; else if (type == 4) return (x.high+x.low+x.close)/3; else if (type == 5) return (x.high+x.low)/2; else return (x.open+x.high+x.low+x.close)/4; } function getSettings(button) { //temporary storage of the settings array var tempSettings = new Array(); //Scan the settings array for data for the current share and for the button that was clicked for (var b=0;b0) {//if there are settings use them to set the dialog defaults var MA1choice = MAsettings[2]; var MA1type = MAsettings[3]; var MA1period = MAsettings[4]; var MA1colour1 = MAsettings[5]; var MA1pen1 = MAsettings[6]; var MA1width1 = MAsettings[7]; var MA1downColour = MAsettings[8]; var MA1colour2 = MAsettings[9]; var MA1channelChoice = MAsettings[10]; var MA1channelType = MAsettings[11]; var MA1channelValue = MAsettings[12]; var MA1channelColour = MAsettings[13]; var MA1channelPen = MAsettings[14]; var MA1channelWidth = MAsettings[15]; var MA1channelFill = MAsettings[16]; var MA1channelFillColour = MAsettings[17]; var MA1dataSource = MAsettings[18]; } else {//If no settings set defaults var MA1choice = 1; var MA1type = 0; var MA1period = 25; var MA1colour1 = Colour.RGB(0,255,0); var MA1pen1 = 0; var MA1width1 = 1; var MA1downColour = 1; var MA1colour2 = Colour.RGB(255,0,0); var MA1channelChoice = 0; var MA1channelType = 0; var MA1channelValue = 2.5; var MA1channelColour = Colour.RGB(0,0,255); var MA1channelPen = 2; var MA1channelWidth = 0; var MA1channelFill = 1; var MA1channelFillColour = Colour.RGB(198,198,255); var MA1dataSource = 3; } var dlg = new Dialog("Moving Average "+button, 250, 125); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(15,5,175,50,""); dlg.addTickBox("MA1Val1",20,5,65,-1,"Moving Average",MA1choice); dlg.addDropList("MA1Val2",40,20,-1,-1,maList,"Type","",MA1type); dlg.addColLinePicker("MA1Val3",155,20,-1,-1,"","",MA1colour1,MA1pen1,MA1width1); dlg.addTickBox("MA1Val4",125,38,30,-1,"Down",MA1downColour); dlg.addColPicker("MA1Val5",155,35,-1,-1,"","",MA1colour2); dlg.addIntEdit("MA1Val6",40,38,-1,-1,"","MA period",MA1period,2,1000); dlg.addGroupBox(15,55,175,50,""); dlg.addTickBox("MA1Val7",20,55,40,-1,"Channel",MA1channelChoice); dlg.addDropList("MA1Val8",40,70,-1,-1,channelList,"Type","",MA1channelType); dlg.addColLinePicker("MA1Val9",155,70,-1,-1,"","",MA1channelColour,MA1channelPen,MA1channelWidth); dlg.addNumEdit("MA1Val10",40,85,-1,-1,"","%/devs",MA1channelValue,0.01,100); dlg.addTickBox("MA1Val11",125,88,30,-1,"Fill",MA1channelFill); dlg.addColPicker("MA1Val12",155,85,-1,-1,"","",MA1channelFillColour); dlg.addDropList("MA1Val13",60,110,-1,-1,dataList,"Data Source","",MA1dataSource); if (dlg.show() == Dialog.Cancel) return false; MA1choice = dlg.getValue("MA1Val1")==true?1:0; MA1type = dlg.getValue("MA1Val2"); MA1width1 = dlg.getValue("MA1Val3").width; MA1colour1 = dlg.getValue("MA1Val3").colour; MA1pen1 = dlg.getValue("MA1Val3").pen; MA1downColour = dlg.getValue("MA1Val4")==true?1:0; MA1colour2= dlg.getValue("MA1Val5"); MA1period = dlg.getValue("MA1Val6"); MA1channelChoice = dlg.getValue("MA1Val7")==true?1:0; MA1channelType = dlg.getValue("MA1Val8"); MA1channelColour = dlg.getValue("MA1Val9").colour; MA1channelWidth = dlg.getValue("MA1Val9").width; MA1channelPen = dlg.getValue("MA1Val9").pen; MA1channelValue = dlg.getValue("MA1Val10"); MA1channelFill = dlg.getValue("MA1Val11")==true?1:0; MA1channelFillColour = dlg.getValue("MA1Val12"); MA1dataSource = dlg.getValue("MA1Val13"); var tempSettings=[0,button,MA1choice,MA1type,MA1period,MA1colour1,MA1pen1,MA1width1,MA1downColour,MA1colour2,MA1channelChoice,MA1channelType, MA1channelValue,MA1channelColour,MA1channelPen,MA1channelWidth,MA1channelFill,MA1channelFillColour,MA1dataSource] if(MAsettings.length==0) { settingsArray[settingsArray.length]=tempSettings; } else { for (var c=0;c0) draw(MAsettings); var MAsettings=getSettings(2); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(3); if (MAsettings.length>0) draw(MAsettings); var MAsettings=getSettings(4); if (MAsettings.length>0) draw(MAsettings); }