// JenScript - 1.3.2 2017-06-10 // http://jenscript.io - Copyright 2017 Sébastien Janaud. All Rights reserved !function(){JenScript.StockPlugin=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.StockPlugin,JenScript.Plugin),JenScript.Model.addMethods(JenScript.StockPlugin,{_init:function(a){a=a||{},this.stocks=[],this.boundedStocks=[],this.stockLayers=[],a.priority=1e4,a.name=void 0!==a.name?a.name:"StockPlugin",JenScript.Plugin.call(this,a),this.bearishColor=void 0!==a.bearishColor?a.bearishColor:"red",this.bullishColor=void 0!==a.bullishColor?a.bullishColor:"green"},getBearishColor:function(){return this.bearishColor},setBearishColor:function(a){this.bearishColor=a},getBullishColor:function(){return this.bullishColor},setBullishColor:function(a){this.bullishColor=a},getStocks:function(){return this.stocks},getBoundedStocks:function(){if(this.requestBoundStock){this.boundedStocks=[];for(var a=0;a=this.getProjection().minX&&b.fixing.getTime()<=this.getProjection().maxX&&(void 0!==c&&c.fixing.getTime()this.getProjection().maxX&&(this.boundedStocks[this.boundedStocks.length]=d))}this.requestBoundStock=!1}return this.boundedStocks},getBoundedStocksAdjustedIndex:function(a,b){for(var c=[],d=-1,e=-1,f=0;f=this.getProjection().minX&&-1==d&&(d=f),g.fixing.getTime()>=this.getProjection().maxX&&-1==e&&(e=f)}return d-a>0&&e+b=a.getTime()&&e.fixing.getTime()<=b.getTime()&&(c[c.length]=e)}return c},getLayers:function(){return this.stockLayers},addLayer:function(a){a.plugin=this,this.stockLayers[this.stockLayers.length]=a,this.repaintPlugin()},removeLayer:function(a){for(var b=[],c=0;cd?1:-1}),this.repaintPlugin()},addStock:function(a){this.stocks[this.stocks.length]=a,this.setStocks(this.stocks)},paintPlugin:function(a,b){if("Device"===b){this.requestBoundStock=!0;for(var c=0;cthis.open}})}(),function(){JenScript.StockGeometry=function(a){this.init(a)},JenScript.Model.addMethods(JenScript.StockGeometry,{init:function(a){a=a||{},this.name=a.name,this.layer},setLayer:function(a){this.layer=a},getLayer:function(){return this.layer},solveGeometry:function(){}}),JenScript.StockItemGeometry=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.StockItemGeometry,JenScript.StockGeometry),JenScript.Model.addMethods(JenScript.StockItemGeometry,{_init:function(a){a=a||{},this.stock,this.deviceLow,this.deviceHigh,this.deviceOpen,this.deviceClose,this.deviceVolume,this.deviceVolumeBase,this.deviceFixing,this.deviceFixingStart,this.deviceFixingEnd,this.deviceFixingDuration,JenScript.StockGeometry.call(this,{name:"StockItemGeometry"})},setStock:function(a){this.stock=a},getStock:function(){return this.stock},solveGeometry:function(){var a=this.stock;this.deviceLow=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),a.getLow())),this.deviceHigh=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),a.getHigh())),this.deviceOpen=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),a.getOpen())),this.deviceClose=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),a.getClose())),this.deviceVolume=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),a.getVolume())),this.deviceVolumeBase=this.getProjection().userToPixel(new JenScript.Point2D(a.getFixing().getTime(),0)),this.deviceFixingStart=this.getProjection().userToPixelX(a.getFixing().getTime()-a.getFixingDurationMillis()/2),this.deviceFixingEnd=this.getProjection().userToPixelX(a.getFixing().getTime()+a.getFixingDurationMillis()/2),this.deviceFixingDuration=Math.abs(this.deviceFixingEnd-this.deviceFixingStart),this.deviceFixing=this.getProjection().userToPixelX(a.getFixing().getTime()),this.solveItemGeometry()},solveItemGeometry:function(){},getProjection:function(){return this.getLayer().plugin.getProjection()}}),JenScript.StockGroupGeometry=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.StockGroupGeometry,JenScript.StockGeometry),JenScript.Model.addMethods(JenScript.StockGroupGeometry,{_init:function(a){a=a||{},this.stockItemGeometries=[],JenScript.StockGeometry.call(this,{name:"StockGroupGeometry"})},setStockItemGeometries:function(a){this.stockItemGeometries=a},addStockItemGeometries:function(a){this.stockItemGeometries[this.stockItemGeometries.length]=a},getStockItemGeometries:function(){return this.stockItemGeometries},solveGeometry:function(){}})}(),function(){JenScript.StockLayer=function(a){this.init(a)},JenScript.Model.addMethods(JenScript.StockLayer,{init:function(a){a=a||{},this.Id="layer"+JenScript.sequenceId++,this.name=a.name,this.plugin,this.geometries=[],this.stockListeners=[]},clearGeometries:function(){this.geometries=[]},getGeometries:function(){return this.geometries},addGeometry:function(a){this.geometries[this.geometries.length]=a},getHost:function(){return this.plugin},solveLayer:function(){},paintLayer:function(){},onMove:function(){},onPress:function(){},onRelease:function(){},addStockListener:function(a,b,c){if(void 0===c)throw new Error("Stock listener, listener name should be supplied.");var d={action:a,onEvent:b,name:c};this.stockListeners[this.stockListeners.length]=d},fireStockEvent:function(a,b){for(var c=0;cthis.getStock().getClose()?(new JenScript.SVGRect).origin(e,c.y).size(f,Math.abs(c.y-d.y)):(new JenScript.SVGRect).origin(e,d.y).size(f,Math.abs(c.y-d.y))},getBound2D:function(){return this.deviceOpenCloseGap.getBound2D()}}),JenScript.CandleStickLayer=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.CandleStickLayer,JenScript.StockLayer),JenScript.Model.addMethods(JenScript.CandleStickLayer,{_init:function(a){a=a||{},this.lowHighColor=void 0!==a.lowHighColor?a.lowHighColor:"black",JenScript.StockLayer.call(this,{name:"CandleStickLayer"})},setLowHighColor:function(a){this.color=a},getLowHighColor:function(){return this.color},solveLayer:function(){this.geometries=[];for(var a=0;a400){var e=(new JenScript.SVGText).Id(this.Id).location(200,30).fill("white").fontSize("8").textAnchor("start").textContent("BUSY CANDLESTICK, two much shapes");c.child(e.toSVG())}else for(var f=0;fthis.getStock().getClose()?(new JenScript.SVGRect).origin(e,c.y).size(f,Math.abs(c.y-d.y)):(new JenScript.SVGRect).origin(e,d.y).size(f,Math.abs(c.y-d.y)),this.deviceLowHighGap=(new JenScript.SVGLine).from(a.x,a.y).to(b.x,b.y),this.deviceOpenTick=(new JenScript.SVGLine).from(c.x-f/2,c.y).to(c.x,c.y),this.deviceCloseTick=(new JenScript.SVGLine).from(d.x,d.y).to(d.x+f/2,d.y)}}),JenScript.OhlcLayer=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.OhlcLayer,JenScript.StockLayer),JenScript.Model.addMethods(JenScript.OhlcLayer,{_init:function(a){a=a||{},this.markerColor=void 0!==a.markerColor?a.markerColor:"black",this.markerWidth=void 0!==a.markerWidth?a.markerWidth:1.5,JenScript.StockLayer.call(this,{name:"OhlcLayer"})},setMarkerColor:function(a){this.markerColor=a},setMarkerWidth:function(a){this.markerWidth=a},solveLayer:function(){this.geometries=[];for(var a=0;a400){var e=(new JenScript.SVGText).Id(this.Id).location(200,40).fill("white").fontSize("8").textAnchor("start").textContent("BUSY OHLC, two much shapes");c.child(e.toSVG())}else for(var f=0;fd&&e>h&&(a[a.length]={x:g.getFixing().getTime(),y:g.getClose()})}this.points=a}}),JenScript.StockFixingLayer=function(a){this.__init(a)},JenScript.Model.inheritPrototype(JenScript.StockFixingLayer,JenScript.StockCurveLayer),JenScript.Model.addMethods(JenScript.StockFixingLayer,{__init:function(a){a=a||{},a.name="StockFixingLayer",JenScript.StockCurveLayer.call(this,a)},getGeomInstance:function(){return new JenScript.StockFixingGeometry}})}(),function(){JenScript.StockMovingAverageGeometry=function(a){this.___init(a)},JenScript.Model.inheritPrototype(JenScript.StockMovingAverageGeometry,JenScript.CurveStockGeometry),JenScript.Model.addMethods(JenScript.StockMovingAverageGeometry,{___init:function(a){a=a||{},JenScript.CurveStockGeometry.call(this,a)},solveGeometry:function(){for(var a=this.getLayer().getHost().getProjection(),b=a.minX,c=a.maxX,d=[],e=this.getLayer().getHost().getStocks(),f=this.moveCount;f=b&&c>=l&&(d[d.length]=new JenScript.Point2D(g.getFixing().getTime(),k))}this.points=d}}),JenScript.StockMovingAverageLayer=function(a){this.__init(a)},JenScript.Model.inheritPrototype(JenScript.StockMovingAverageLayer,JenScript.StockCurveLayer),JenScript.Model.addMethods(JenScript.StockMovingAverageLayer,{__init:function(a){a=a||{},a.name="StockMovingAverageLayer",JenScript.StockCurveLayer.call(this,a)},getGeomInstance:function(){var a={moveCount:this.moveCount};return new JenScript.StockMovingAverageGeometry(a)}})}(),function(){JenScript.StockWeightedMovingAverageGeometry=function(a){this.___init(a)},JenScript.Model.inheritPrototype(JenScript.StockWeightedMovingAverageGeometry,JenScript.CurveStockGeometry),JenScript.Model.addMethods(JenScript.StockWeightedMovingAverageGeometry,{___init:function(a){a=a||{},JenScript.CurveStockGeometry.call(this,a)},solveGeometry:function(){var a=this.getLayer().getHost().getProjection(),b=a.minX,c=a.maxX,d=[],e=this.getLayer().getHost().getStocks();e&&e.sort(function(a,b){return a.getFixing().getTime()>b.getFixing().getTime()?1:-1});for(var f=this.moveCount;f=b&&c>=m&&(d[d.length]=new JenScript.Point2D(g.getFixing().getTime(),l))}this.points=d}}),JenScript.StockWeightedMovingAverageLayer=function(a){this.__init(a)},JenScript.Model.inheritPrototype(JenScript.StockWeightedMovingAverageLayer,JenScript.StockCurveLayer),JenScript.Model.addMethods(JenScript.StockWeightedMovingAverageLayer,{__init:function(a){a=a||{},a.name="StockWeightedMovingAverageLayer",JenScript.StockCurveLayer.call(this,a)},getGeomInstance:function(){var a={moveCount:this.moveCount};return new JenScript.StockWeightedMovingAverageGeometry(a)}})}(),function(){JenScript.StockExponentialMovingAverageGeometry=function(a){this.___init(a)},JenScript.Model.inheritPrototype(JenScript.StockExponentialMovingAverageGeometry,JenScript.CurveStockGeometry),JenScript.Model.addMethods(JenScript.StockExponentialMovingAverageGeometry,{___init:function(a){a=a||{},JenScript.CurveStockGeometry.call(this,a)},solveGeometry:function(){for(var a=this.getLayer().getHost().getProjection(),b=a.minX,c=a.maxX,d=[],e=this.getLayer().getHost().getStocks(),f=2/(this.moveCount+1),g=this.moveCount;g=b&&c>=n&&(d[d.length]=new JenScript.Point2D(h.getFixing().getTime(),m))}this.points=d}}),JenScript.StockExponentialMovingAverageLayer=function(a){this.__init(a)},JenScript.Model.inheritPrototype(JenScript.StockExponentialMovingAverageLayer,JenScript.StockCurveLayer),JenScript.Model.addMethods(JenScript.StockExponentialMovingAverageLayer,{__init:function(a){a=a||{},JenScript.StockCurveLayer.call(this,a)},getGeomInstance:function(){var a={moveCount:this.moveCount};return new JenScript.StockExponentialMovingAverageGeometry(a)}})}(),function(){JenScript.BollingerStockGeometry=function(a){this.__init(a)},JenScript.Model.inheritPrototype(JenScript.BollingerStockGeometry,JenScript.StockGroupGeometry),JenScript.Model.addMethods(JenScript.BollingerStockGeometry,{__init:function(a){a=a||{},JenScript.StockGroupGeometry.call(this,a),this.moveCount=void 0!==a.moveCount?a.moveCount:20,this.upperPoint=[],this.bottomPoint=[]},setMoveCount:function(a){this.moveCount=a},getMoveCount:function(){return this.moveCount},getCurveUp:function(){return this.upperPoint},getCurveAverage:function(){return this.stockMAs},getCurveBottom:function(){return this.bottomPoint},solveGeometry:function(){for(var a=this.getLayer().getHost().getProjection(),b=a.minX,c=a.maxX,d=[],e=[],f=[],g=this.getLayer().getHost().getStocks(),h=this.moveCount;h=b&&c>=j&&(f[f.length]=new JenScript.Point2D(i.getFixing().getTime(),n));for(var o=0,l=0;l=b&&c>=j&&(d[d.length]=new JenScript.Point2D(i.getFixing().getTime(),n+2*p),e[e.length]=new JenScript.Point2D(i.getFixing().getTime(),n-2*p))}this.upperPoint=d,this.bottomPoint=e,this.stockMAs=f}}),JenScript.StockBollingerLayer=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.StockBollingerLayer,JenScript.StockLayer),JenScript.Model.addMethods(JenScript.StockBollingerLayer,{_init:function(a){a=a||{},this.lineColor=void 0!==a.lineColor?a.lineColor:"black",this.lineOpacity=void 0!==a.lineOpacity?a.lineOpacity:1,this.lineWidth=void 0!==a.lineWidth?a.lineWidth:1,this.bandColor=void 0!==a.bandColor?a.bandColor:"orange",this.bandOpacity=void 0!==a.bandOpacity?a.bandOpacity:.4,this.Id="StockBollinger"+JenScript.sequenceId++,this.bandId=this.Id+"_band",this.upId=this.Id+"_up",this.bottomId=this.Id+"_bottom",JenScript.StockLayer.call(this,{name:"StockBollingerLayer"})},solveLayer:function(){this.clearGeometries();var a=new JenScript.BollingerStockGeometry;a.setLayer(this),a.solveGeometry(),this.addGeometry(a)},paintCurve:function(a,b,c,d,e){for(var f=this.plugin.getProjection(),g=(new JenScript.SVGPath).Id(e),h=[],i=0;i=0;n--){var o=m[n];g.lineTo(o.x,o.y)}d.length>0&&e.length>0&&g.close();var p=g.strokeNone().fill(this.bandColor).fillOpacity(this.bandOpacity).toSVG();a.child(p)},paintLayer:function(a,b){if("Device"===b)for(var c=0;c=30;e--){for(var f=c[e],g=f.getClose(),h=1,i=1;b>=i;i++){var j=c[e-i];g+=Math.pow(1-d,i)*j.getClose(),h+=Math.pow(1-d,i)}var k=g/h;"min"===a&&(this.getFixing(f).min=k),"max"===a&&(this.getFixing(f).max=k)}},_solveSignal:function(a){for(var b=this.getLayer().stocksAdjusted,c=2/(a+1),d=b.length-1;d>=30+this.moveCountSignal;d--){for(var e=b[d],f=this.getFixing(e),g=f.macd,h=1,i=1;a>=i;i++){var j=b[d-i],k=this.getFixing(j);g+=Math.pow(1-c,i)*k.macd,h+=Math.pow(1-c,i)}var l=g/h;f.signal=l}},solveGeometry:function(){this.fixingMap=[],this._solveGeometry("min",this.moveCountMin),this._solveGeometry("max",this.moveCountMax);for(var a=this.getLayer().stocksAdjusted,b=a.length-1;b>=30;b--){var c=a[b],d=this.getFixing(c);if(void 0!==d.min&&void 0!==d.max){var e=d.min-d.max;d.macd=e}}this._solveSignal(this.moveCountSignal)},getMACD:function(){return this.getPoints("macd",30,this.getLayer().stocksAdjusted.length-1)},getSignal:function(){return this.getPoints("signal",30+this.moveCountSignal,this.getLayer().stocksAdjusted.length-1)},getPoints:function(a,b,c){for(var d=[],e=this.getLayer().getHost().getProjection(),f=e.minX,g=e.maxX,h=this.getLayer().stocksAdjusted,i=b;c>=i;i++){var j=h[i],k=j.getFixing().getTime(),l=this.getFixing(j);if(k>=f&&g>=k){var m="signal"===a?l.signal:l.macd;d[d.length]=new JenScript.Point2D(j.getFixing().getTime(),m)}}return d}}),JenScript.StockMACDLayer=function(a){this._init(a)},JenScript.Model.inheritPrototype(JenScript.StockMACDLayer,JenScript.StockLayer),JenScript.Model.addMethods(JenScript.StockMACDLayer,{_init:function(a){a=a||{},this.macdId="macdlayer"+JenScript.sequenceId++,this.signalId="signallayer"+JenScript.sequenceId++,this.lineColor=void 0!==a.lineColor?a.lineColor:"black",this.lineOpacity=void 0!==a.lineOpacity?a.lineOpacity:1,this.lineWidth=void 0!==a.lineWidth?a.lineWidth:1,this.signalColor=void 0!==a.signalColor?a.signalColor:"red",this.signalOpacity=void 0!==a.signalOpacity?a.signalOpacity:1,this.signalWidth=void 0!==a.signalWidth?a.signalWidth:1,this.macdColor=void 0!==a.macdColor?a.macdColor:"blue",this.macdOpacity=void 0!==a.macdOpacity?a.macdOpacity:1,this.macdWidth=void 0!==a.macdWidth?a.macdWidth:1,this.moveCountSignal=void 0!==a.moveCountSignal?a.moveCountSignal:9,this.moveCountMin=void 0!==a.moveCountMin?a.moveCountMin:12,this.moveCountMax=void 0!==a.moveCountMax?a.moveCountMax:26,a.name="StockMACDLayer",JenScript.StockLayer.call(this,a)},selectStocks:function(){this.stocksAdjusted=this.getHost().getBoundedStocksAdjustedIndex(30,30)},solveLayer:function(){this.selectStocks(),this.clearGeometries();var a={moveCountSignal:this.moveCountSignal,moveCountMin:this.moveCountMin,moveCountMax:this.moveCountMax},b=new JenScript.StockMACDGeometry(a);b.setLayer(this),b.solveGeometry(),this.addGeometry(b)},paintCurve:function(a,b,c,d,e,f,g,h){for(var i=this.plugin.getProjection(),j=(new JenScript.SVGPath).Id(e),k=[],l=0;l