Creating a Technical Indicator Using JavaScript Chart | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Creating a Technical Indicator By Using JavaScript Chart

This blog post discusses how to create a Technical Indicator by using JavaScript Chart. Technical Indicators forecast the direction of prices in Financial sectors through the study of past market data, primarily price and volume. There are more than ten widely used technical indicators, but we will discuss how to create a Simple Moving Average (SMA) technical indicator here.

Before we start, you must understand the calculation behind creating a Simple Moving Average technical indicator. Financial Charts­apart from focusing on the price and volume—also focus on the high, low, opening, and closing prices of a certain period. For this reason, we need to take into account these values while creating a technical indicator. The following list shows the required data for SMA calculation:

·        Date

·        High (Double)

·        Low (Double)

·        Open (Double)

·        Close (Double)

Let us consider the following JSON Data Collection for SMA Calculation.

window.chartData = [ 
{"xDate": new Date(2011,7,28), "Open":22.91, "High":23.06, "Low":22.52, "Close":22.56},
{"xDate": new Date(2011,7,29), "Open":22.38, "High":22.64, "Low":22.05, "Close":22.25},
{"xDate": new Date(2011,8,1), "Open":22.41, "High":22.46, "Low":21.76, "Close":21.95},
{"xDate": new Date(2011,8,2), "Open":21.78, "High":22.19, "Low":21.2, "Close":21.21},
{"xDate": new Date(2011,8,3), "Open":21.18, "High":21.4, "Low":20.72, "Close":21.34},
{"xDate": new Date(2011,8,4), "Open":21.04, "High":21.14, "Low":20.14, "Close":20.15},
{"xDate": new Date(2011,8,5), "Open":20.29, "High":20.49, "Low":19.03, "Close":20},
…
…
…
]

Now that the data is ready, we can start creating an SMA.

Simple Moving Average (SMA)

 

Firstly, to create a Simple Moving Average technical indicator, we must compute the average of prices over a specific period of time. This period can range from two days to an infinite number of days.

Calculation

 

Daily Closing Prices: 1,2,3,4,5,6,7 
First day of 5-day SMA: (1 + 2 + 3 + 4 + 5) / 5 = 3
Second day of 5-day SMA: (2 + 3 + 4 + 5 + 6) / 5 = 4
Third day of 5-day SMA: (3 + 4 + 5 + 6 + 7) / 5 = 5

Notice that each SMA value is lesser than the last price. For example, the SMA value for the first day is three and the last price is five. As the prices prior four days are lesser, this causes the SMA to lag.

Secondly, let us learn how to compute SMA points of data. To start with, let us create an array “smaPoints”, and use the CalculateSMAPoints method to store the values in the array. The following code example illustrates this.

var smaPoints[];
function CalulateSMAPoints(movingAveragePeriod) 
{

  var sum = 0.0;
  var limit = chartData.length;
  var length = Math.round(movingAveragePeriod);
  for (var i = 0; i < length; i++)
  smaPoints[i] = { "X": chartData[i].xDate, "Y": chartData[i].Close };
  for (var i = 0; i < limit; ++i) 
  {
    if (i >= length - 1 && i < limit) 
    {
      if (i - movingAveragePeriod >= 0) 
      {
        sum += chartData[i].Close - chartData[i - length].Close; 
      }
      else 
      {
        sum += chartData[i].Close;
      }
      smaPoints[i] = { "X": chartData[i].xDate, "Y": sum / movingAveragePeriod 

      };
    }
    else 
    {
      if (i < movingAveragePeriod - 1)
      sum += chartData[i].Close;
    }
  }
}

After computing the SMA points in a separate Array collection, you can set this collection as the data source to a Line Series as shown in the following code example.

$(function()
{
  $("#container").ejChart(
  {
    series: [
    {
      name: 'Series 1', type: 'candle',
      animation: false,
      dataSource :{ data: window.indicatorData, xName: "xDate", high: "High", 

      open: "Open", low: "Low", close: "Close" }
    },
    {
      name: 'SMA 5', type: 'line',
      style: { interior: "#01D002" },
      animation: false,
      dataSource :{ data: smaPoints, xName: "X", yName: "Y"}
    },
    {
      name: ''SMA 25', type: 'line',
      style: { interior: "#6501F5" },
      animation: false,
      dataSource :{ data: smaPoints, xName: "X", yName: "Y"}
    },
    {
      name: ''SMA 50', type: 'line',
      style: { interior: "lightblue" },
      animation: false,
      dataSource :{ data: smaPoints, xName: "X", yName: "Y"}

    }],
  });
});

In the preceding code, you will be able to see four Chart Series (one Candle Series and three Line Series). The Line Series indicate SMA indicator values over a period of 5, 25, and 50 days.

Sales Analysis with SMA Indicator

To help you replicate what you just learned, we have also provided a sample you can download here.

Content Contributor: Michael Prabhu Content Editor: GeeGee Inekeya

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed