//@LibraryID:1057,4 //@LibraryID:1057,3 //@Name:MA Close Arrows //@Description:Draws arrows each time the close crosses a chosen MA. Does not draw the moving average line. //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 maPeriod1 = 20; var maType1 = 0; var fillCol1 = Colour.Green; var fillCol2 = Colour.Red; var maTitleList = ["SMA","EMA","WMA","TMA","VVHF","VCMO","VIDYA","HMA","TEMA"]; var maList = ["Simple","Exponential","Weighted","Triangular","VariableVHF","VariableCMO","VIDYA","Hull","TEMA"]; var dataList = ["Open","High","Low","Close","Typical","Median","Weighted"]; var dataSource = 3; function init(status) { if (status == Loading || status == Editing) { maPeriod1 = storage.getAt(0); maType1 = storage.getAt(2); fillCol1 = storage.getAt(4); fillCol2 = storage.getAt(5); dataSource = storage.getAt(6); } if(status == Adding|| status == Editing) { drawDialogBox(); } buttonHandle = createButton("Settings", onButton0); setInfoText(maPeriod1+" "+maTitleList[maType1]+"\n"+dataList[dataSource]); } function onButton0() { maPeriod1 = storage.getAt(0); maType1 = storage.getAt(2); fillCol1 = storage.getAt(4); fillCol2 = storage.getAt(5); dataSource = storage.getAt(6); drawDialogBox(); setInfoText(maPeriod1+" "+maTitleList[maType1]+"\n"+dataList[dataSource]); draw(); } function drawDialogBox() { var dlg = new Dialog("Crossover Arrows", 250, 120); dlg.addOkButton(); dlg.addCancelButton(); dlg.addGroupBox(10,5,180,35,"Moving Average"); dlg.addDropList("MA1Val1",35,20,-1,-1,maList,"Type","",maType1); dlg.addIntEdit("MA1Val3",155,20,-1,-1,"Period length","",maPeriod1,2,1000); dlg.addGroupBox(10,45,180,50,"Arrow colours"); dlg.addColPicker("MA1Val2",20,55,-1,-1,"","price above MA",fillCol1); dlg.addColPicker("MA2Val2",20,75,-1,-1,"","price below MA",fillCol2); dlg.addDropList("dataSource1",60,100,-1,-1,dataList,"Data Source","",dataSource); if (dlg.show() == Dialog.Cancel) return false; maPeriod1 = dlg.getValue("MA1Val3"); maType1 = dlg.getValue("MA1Val1"); fillCol1= dlg.getValue("MA1Val2"); fillCol2 = dlg.getValue("MA2Val2"); dataSource = dlg.getValue("dataSource1"); storage.setAt(0, maPeriod1); storage.setAt(2, maType1); storage.setAt(4, fillCol1); storage.setAt(5, fillCol2); storage.setAt(6, dataSource); } function onNewChart() { draw(); } function onNewBarUpdate() { draw() } function draw() { clearDisplay(); if (maType1 == 8) var ma1 = new TEMA(maPeriod1); else var ma1 = new MA(maPeriod1,maType1); var maValues = []; var closeLine = []; for (var i=0;i=line2[i]) { setPenStyle(0,1,fillCol2); setBrushColour(getBackColour()); //x & y are the the coordinates of the exact crossing point of the two lines var x = (line1[i] - line2[i] + i * (line2[i]-line2[i-1]-line1[i]+line1[i-1])) / (line2[i]-line2[i-1]-line1[i]+line1[i-1]); var y = (line1[i]-line1[i-1]) * (x - i) + line1[i]; drawSymbol(x,y*1.01,Symbol.TriangleDown,"",BoxAlign.Centre|BoxAlign.Above); } if (line1[i-1]>line2[i-1] && line1[i]<=line2[i]) { setPenStyle(0,1,fillCol1); setBrushColour(getBackColour()); //x & y are the the coordinates of the exact crossing point of the two lines var x = (line1[i] - line2[i] + i * (line2[i]-line2[i-1]-line1[i]+line1[i-1])) / (line2[i]-line2[i-1]-line1[i]+line1[i-1]); var y = (line1[i]-line1[i-1]) * (x - i) + line1[i]; drawSymbol(x,y*0.99,Symbol.TriangleUp,"",BoxAlign.Centre|BoxAlign.Below); } } } 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; }