implement moving average in stock chart - Javascript highcharts

Javascript examples for highcharts:Stock Chart

Description

implement moving average in stock chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <script type="text/javascript">
$(function() {/* w w w.  j ava 2 s  .  c  o m*/
    $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
        $('#container').highcharts('StockChart', {
            title : {
                text : 'Simple Moving Average (SMA) of AAPL stock price'
            },
            subtitle: {
                text: 'From may 15, 2006 to May 10, 2013'
            },
            xAxis: {
                type: 'datetime'
            },
            yAxis: {
                title : {
                    text : 'Price'
                }
            },
            tooltip: {
                crosshairs: true,
                shared: true
            },
         rangeSelector : {
            selected : 1
         },
            legend: {
                enabled: true,
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            plotOptions: {
                series: {
                    marker: {
                        enabled: false,
                    }
                }
            },
            series : [{
                name: 'AAPL Stock Price',
                type : 'line',
                id: 'primary',
                data : data
            }, {
                name: '15-day SMA',
                linkedTo: 'primary',
                showInLegend: true,
                type: 'trendline',
                algorithm: 'SMA',
                periods: 15
            }, {
                name: '40-day SMA',
                linkedTo: 'primary',
                showInLegend: true,
                type: 'trendline',
                algorithm: 'SMA',
                periods: 40
            }]
        });
    });
});

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/stock/highstock.js"></script> 
      <script src="https://code.highcharts.com/stock/modules/exporting.js"></script> 
      <script src="https://rawgithub.com/laff/technical-indicators/master/technical-indicators.src.js"></script> 
      <div id="container" style="min-width: 500x; height: 500px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials