//@Name:Quick Change MA //@Description:Allows on the fly changing of MA Values. //@Env:Production //@Editable:No // 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 //Declare Variables var ma; var ma1=10; var ma2=20; var ma3=30; var ma4=40; var ma5=50; var maType1=0; var maType2=0; var maType3=0; var maType4=0; var maType5=0; var maBol1=1; var maBol2=1; var maBol3=0; var maBol4=0; var maBol5=0; var ma1details=0; var ma2details=0; var ma3details=0; var ma4details=0; var ma5details=0; var colour1=0; var width1=0; var pen1=0; var MAList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","Vidya"]; var MAAccList = ["SMA","EMA","WMA","TMA","VVHF","VCMO","VIDYA"]; function init(status) { //If you Edit the study then it will load your values that store when you first add it to the graph or change the graph via the buttons. if (status == Loading || status == Editing) { ma1details = storage.getAt(0); ma2details = storage.getAt(1); ma3details = storage.getAt(2); ma4details = storage.getAt(3); ma5details = storage.getAt(4); colour1 = storage.getAt(5); width1 = storage.getAt(6); pen1 = storage.getAt(7); //The details for each of the 5 moving averages are compressed into single numbers for ease of storage, this section decompresses them. ma1details = ma1details.toString(); ma1 = parseInt(ma1details.substr(2,3),10); maType1 =(parseInt(ma1details.substr(1,1),10))-1; maBol1 = (parseInt(ma1details.substr(0,1),10))-1; ma2details = ma2details.toString(); ma2 = parseInt(ma2details.substr(2,3),10); maType2 = (parseInt(ma2details.substr(1,1),10))-1; maBol2 = (parseInt(ma2details.substr(0,1),10))-1; ma3details=ma3details.toString(); ma3=parseInt(ma3details.substr(2,3),10); maType3=(parseInt(ma3details.substr(1,1),10))-1; maBol3=(parseInt(ma3details.substr(0,1),10))-1; ma4details=ma4details.toString(); ma4=parseInt(ma4details.substr(2,3),10); maType4=(parseInt(ma4details.substr(1,1),10))-1; maBol4=(parseInt(ma4details.substr(0,1),10))-1; ma5details=ma5details.toString(); ma5=parseInt(ma5details.substr(2,3),10); maType5=(parseInt(ma5details.substr(1,1),10))-1; maBol5=(parseInt(ma5details.substr(0,1),10))-1; } //Creates the initial dialog window if (status == Adding || status == Editing) { var dlg = new Dialog("Moving Average Details", 220, 130); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(5,5,155,110,"Properties"); dlg.addIntEdit("VAL1",8,15,-1,-1, "","",ma1,1,999); dlg.addDropList("VAL2",48,15,-1,-1, MAList, "","",maType1); dlg.addIntEdit("VAL3",8,35,-1,-1, "","",ma2,1,999); dlg.addDropList("VAL4",48,35,-1,-1, MAList, "","",maType2); dlg.addIntEdit("VAL5",8,55,-1,-1, "","",ma3,0,999); dlg.addDropList("VAL6",48,55,-1,-1, MAList, "","",maType3); dlg.addTickBox("VAL12",118,55,-1,-1,"",maBol3); dlg.addIntEdit("VAL7",8,75,-1,-1, "","",ma4,0,999); dlg.addDropList("VAL8",48,75,-1,-1, MAList, "","",maType4); dlg.addTickBox("VAL13",118,75,-1,-1,"",maBol4); dlg.addIntEdit("VAL9",8,95,-1,-1, "","",ma5,0,999); dlg.addDropList("VAL10",48,95,-1,-1, MAList, "","",maType5); dlg.addTickBox("VAL14",118,95,10,10,"",maBol5); dlg.addColLinePicker("VAL11",118,15,-1,-1,"","",colour1,pen1,width1); if (dlg.show() == Dialog.Cancel) return false; ma1 = dlg.getValue("VAL1"); maType1 = dlg.getValue("VAL2"); ma2 = dlg.getValue("VAL3"); maType2 = dlg.getValue("VAL4"); ma3 = dlg.getValue("VAL5"); maType3 = dlg.getValue("VAL6"); maBol3 = dlg.getValue("VAL12"); ma4 = dlg.getValue("VAL7"); maType4 = dlg.getValue("VAL8"); maBol4 = dlg.getValue("VAL13"); ma5 = dlg.getValue("VAL9"); maType5 = dlg.getValue("VAL10"); maBol5 = dlg.getValue("VAL14"); colour1 = dlg.getValue("VAL11").colour; pen1 = dlg.getValue("VAL11").pen; width1 = dlg.getValue("VAL11").width; ma1details=((maBol1+1)*10000)+((maType1+1)*1000)+ma1; ma2details=((maBol2+1)*10000)+((maType2+1)*1000)+ma2; ma3details=((maBol3+1)*10000)+((maType3+1)*1000)+ma3; ma4details=((maBol4+1)*10000)+((maType4+1)*1000)+ma4; ma5details=((maBol5+1)*10000)+((maType5+1)*1000)+ma5; storage.setAt(0, ma1details); storage.setAt(1, ma2details); storage.setAt(2, ma3details); storage.setAt(3, ma4details); storage.setAt(4, ma5details); storage.setAt(5, colour1); storage.setAt(6, width1); storage.setAt(7, pen1); } //Sets the style of the line based on what was entered in the diaglog. setPenStyle(pen1,width1,colour1); //Draws one button for each of the MAs that were setup by the user. if (maBol1==1) buttonHandle1 = createButton(ma1+" "+MAAccList[maType1], onButton1); if (maBol2==1) buttonHandle2 = createButton(ma2+" "+MAAccList[maType2], onButton2); if (maBol3==1) buttonHandle3 = createButton(ma3+" "+MAAccList[maType3], onButton3); if (maBol4==1) buttonHandle4 = createButton(ma4+" "+MAAccList[maType4], onButton4); if (maBol5==1) buttonHandle5 = createButton(ma5+" "+MAAccList[maType5], onButton5); //Draws the Reset button. buttonHandle6 = createButton("Reset", onButton6); maPeriod = storage.getAt(9); maType = storage.getAt(10); if (!maPeriod) { maPeriod = ma1; storage.setAt(9, maPeriod); } if (!maType) { maType = maType1; storage.setAt(10, maType); } setTitle("QuickChange ("+maPeriod+" "+MAAccList[maType]+")"); } //Tells ShareScope what MA values to use when you click on a button, sets the Study title and calls the Draw function. function onButton1() { storage.setAt(9, ma1); storage.setAt(10, maType1); setTitle("QuickChange ("+ma1+" "+MAAccList[maType1]+")"); draw(); } function onButton2() { storage.setAt(9, ma2); storage.setAt(10, maType2); setTitle("QuickChange ("+ma2+" "+MAAccList[maType2]+")"); draw(); } function onButton3() { storage.setAt(9, ma3); storage.setAt(10, maType3); setTitle("QuickChange ("+ma3+" "+MAAccList[maType3]+")"); draw(); } function onButton4() { storage.setAt(9, ma4); storage.setAt(10, maType4); setTitle("QuickChange ("+ma4+" "+MAAccList[maType4]+")"); draw(); } function onButton5() { storage.setAt(9, ma5); storage.setAt(10, maType5); setTitle("QuickChange ("+ma5+" "+MAAccList[maType5]+")"); draw(); } function onButton6() { //Load data ma1details = storage.getAt(0); ma2details = storage.getAt(1); ma3details = storage.getAt(2); ma4details = storage.getAt(3); ma5details = storage.getAt(4); colour1 = storage.getAt(5); width1 = storage.getAt(6); pen1 = storage.getAt(7); //Decompress data ma1details = ma1details.toString(); ma1 = parseInt(ma1details.substr(2,3),10); maType1 =(parseInt(ma1details.substr(1,1),10))-1; maBol1 = (parseInt(ma1details.substr(0,1),10))-1; ma2details = ma2details.toString(); ma2 = parseInt(ma2details.substr(2,3),10); maType2 = (parseInt(ma2details.substr(1,1),10))-1; maBol2 = (parseInt(ma2details.substr(0,1),10))-1; ma3details=ma3details.toString(); ma3=parseInt(ma3details.substr(2,3),10); maType3=(parseInt(ma3details.substr(1,1),10))-1; maBol3=(parseInt(ma3details.substr(0,1),10))-1; ma4details=ma4details.toString(); ma4=parseInt(ma4details.substr(2,3),10); maType4=(parseInt(ma4details.substr(1,1),10))-1; maBol4=(parseInt(ma4details.substr(0,1),10))-1; ma5details=ma5details.toString(); ma5=parseInt(ma5details.substr(2,3),10); maType5=(parseInt(ma5details.substr(1,1),10))-1; maBol5=(parseInt(ma5details.substr(0,1),10))-1; //Load dialog box var dlg = new Dialog("Moving Average Details", 220, 130); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(5,5,155,110,"Properties"); dlg.addIntEdit("VAL1",8,15,-1,-1, "","",ma1,1,999); dlg.addDropList("VAL2",48,15,-1,-1, MAList, "","",maType1); dlg.addIntEdit("VAL3",8,35,-1,-1, "","",ma2,1,999); dlg.addDropList("VAL4",48,35,-1,-1, MAList, "","",maType2); dlg.addIntEdit("VAL5",8,55,-1,-1, "","",ma3,0,999); dlg.addDropList("VAL6",48,55,-1,-1, MAList, "","",maType3); dlg.addTickBox("VAL12",118,55,-1,-1,"",maBol3); dlg.addIntEdit("VAL7",8,75,-1,-1, "","",ma4,0,999); dlg.addDropList("VAL8",48,75,-1,-1, MAList, "","",maType4); dlg.addTickBox("VAL13",118,75,-1,-1,"",maBol4); dlg.addIntEdit("VAL9",8,95,-1,-1, "","",ma5,0,999); dlg.addDropList("VAL10",48,95,-1,-1, MAList, "","",maType5); dlg.addTickBox("VAL14",118,95,10,10,"",maBol5); dlg.addColLinePicker("VAL11",118,15,-1,-1,"","",colour1,pen1,width1); if (dlg.show() == Dialog.Cancel) return false; //Assign variable from dialog box input ma1 = dlg.getValue("VAL1"); maType1 = dlg.getValue("VAL2"); ma2 = dlg.getValue("VAL3"); maType2 = dlg.getValue("VAL4"); ma3 = dlg.getValue("VAL5"); maType3 = dlg.getValue("VAL6"); maBol3 = dlg.getValue("VAL12"); ma4 = dlg.getValue("VAL7"); maType4 = dlg.getValue("VAL8"); maBol4 = dlg.getValue("VAL13"); ma5 = dlg.getValue("VAL9"); maType5 = dlg.getValue("VAL10"); maBol5 = dlg.getValue("VAL14"); colour1 = dlg.getValue("VAL11").colour; pen1 = dlg.getValue("VAL11").pen; width1 = dlg.getValue("VAL11").width; //Compress data ma1details=((maBol1+1)*10000)+((maType1+1)*1000)+ma1; ma2details=((maBol2+1)*10000)+((maType2+1)*1000)+ma2; ma3details=((maBol3+1)*10000)+((maType3+1)*1000)+ma3; ma4details=((maBol4+1)*10000)+((maType4+1)*1000)+ma4; ma5details=((maBol5+1)*10000)+((maType5+1)*1000)+ma5; //Store data storage.setAt(0, ma1details); storage.setAt(1, ma2details); storage.setAt(2, ma3details); storage.setAt(3, ma4details); storage.setAt(4, ma5details); storage.setAt(5, colour1); storage.setAt(6, width1); storage.setAt(7, pen1); //Sets the style of the line based on what was entered in the diaglog. setPenStyle(pen1,width1,colour1); //Deletes the existing buttons. deleteButtons(); //Draws one button for each of the MAs that were setup by the user. if (maBol1==1) buttonHandle1 = createButton(ma1+" "+MAAccList[maType1], onButton1); if (maBol2==1) buttonHandle2 = createButton(ma2+" "+MAAccList[maType2], onButton2); if (maBol3==1) buttonHandle3 = createButton(ma3+" "+MAAccList[maType3], onButton3); if (maBol4==1) buttonHandle4 = createButton(ma4+" "+MAAccList[maType4], onButton4); if (maBol5==1) buttonHandle5 = createButton(ma5+" "+MAAccList[maType5], onButton5); buttonHandle6 = createButton("Reset", onButton6); //Resets the graph values to that of the same details as the first button. maPeriod = ma1; maType = maType1; storage.setAt(9, maPeriod); storage.getAt(10, maType); setTitle("QuickChange ("+maPeriod+" "+MAAccList[maType]+")"); draw(); } //Draws the first line based on the first MA value when no buttons have been clicked on. function onNewChart() { draw(); } //Updates the graph if the data is updated. function onNewBarUpdate() { draw(); } //Draws a new line depending which button has been clicked on function draw() { clearDisplay(); maPeriod = storage.getAt(9); maType = storage.getAt(10); var ma = new MA(maPeriod, maType); beginPath(); for (var i=0; i