<html>
	<head>
		<title>Changing technical indicators params dynamically</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>	
		<script type="text/javascript" language="javascript" src="./js/jquery.min.js"></script>
		<script type="text/javascript" language="javascript" src="./../js/AnyChartStock.js?v=1.9.0r9317"></script>
	
		<!-- chart size settings -->
		<style type="text/css">
			#chartContainer {
				width: 650px;
				height: 580px;
				float: left;
				padding: 5px;
			}
		</style>
		
		<script type="text/javascript" language="javascript">
			// Creating new chart object. 
			var chart = new AnyChartStock("./../swf/AnyChartStock.swf?v=1.9.0r9317", "./../swf/Preloader.swf?v=1.9.0r9317");
			// Setting XML config file.
			chart.setXMLFile("config.xml");
			// NOTE: needConfig flag set to true, provides an ability to work with chart objectModel
			chart.needConfig = true; 
			// Writing the flash object into the page DOM.
			chart.write("chartContainer");
			
			//---------------------------------------------------------------------------------------------------
			//		Reset Settings
			//---------------------------------------------------------------------------------------------------
			
			// Reset settings to initial values
			function resetToDefault() {
				// SMA Indicator
				configSMA(20);
				$("#smaPeriod").val(20);

				// BBands Indicator
				configBollingerBandsIndicator(20, 2);
				$("#bBandsPeriod").val(20);
				$("#bBandsDeviation").val(2);

				// Stochastic Oscillator Indicator
				configStochasticOscillatorIndicator(14, 3, 1, "SMA");
				$("#stochasticKPeriod").val(14);
				$("#stochasticDPeriod").val(3);
				$("#stochasticKSmoothingPeriod").val(1);
				$("#stochasticMAType").get(0).selectedIndex = 0;

				// MACD Indicator
				configMACDIndicator(26,12,9);
				$("#macdSlowPeriod").val(26);
				$("#macdFastPeriod").val(12);
				$("#macdSignalPeriod").val(9);
				
				// Apply Changes
				chart.applySettingsChanges();
			}
			
			//---------------------------------------------------------------------------------------------------
			//		SMA
			//---------------------------------------------------------------------------------------------------
			
			// Function to update SMA indicator settings
			function configSMA(period) {
				if (isNaN(period)) return false; // if period is NaN - settings are invalid
				
				// Get indicator object from chart objectModel.
				var smaIndicator = chart.getTechIndicatorById("idMainChart", "idSMAIndicator");
				// Setting new period value.
				smaIndicator.smaIndicator.period = period;
				// Changing series name to fit new period setting.
				smaIndicator.smaIndicator.series.name = "SMA("+period+")";
				
				return true;
			}
			
			// Event on change of SMA settings
			function updateSMAIndicator() {
				if (configSMA($("#smaPeriod").val()))
					chart.applySettingsChanges(); // If settinggs are valid - Apply changes.
			}
			
			//---------------------------------------------------------------------------------------------------
			//		BBands
			//---------------------------------------------------------------------------------------------------
			
			// Function to change Bollinger Bands indicator
			function configBollingerBandsIndicator(period, deviation) {
				if (isNaN(period) || isNaN(deviation)) return false; // Validation
				
				// Get indicator object from chart object model.
				var bBandsIndicator = chart.getTechIndicatorById("idMainChart", "idBBandsIndicator");
				
				// Setting new indicator parameters.
				bBandsIndicator.bbandsIndicator.period = period;
				bBandsIndicator.bbandsIndicator.deviation = deviation;
				
				// Change the name of upper series.
				bBandsIndicator.bbandsIndicator.upperSeries.name = "BBands(" + period + "," + deviation + ")";
				
				return true;
			}
			
			// Function to appply Bollinger Bands indicator settings.
			function updateBollingerBandsIndicator() {
				if (configBollingerBandsIndicator($("#bBandsPeriod").val(), $("#bBandsDeviation").val()))
					chart.applySettingsChanges(); // If settinggs are valid - apply changes.
			}
			
			//---------------------------------------------------------------------------------------------------
			//		Stochastic Oscillator
			//---------------------------------------------------------------------------------------------------
			
			// Function to change Stochastic Oscillator indicator settings
			function configStochasticOscillatorIndicator(kPeriod, dPeriod, kSmoothingPeriod, maType) {
				if (isNaN(kPeriod) || isNaN(dPeriod) || isNaN(kSmoothingPeriod) || maType == null) return false; //Validation
				
				// Get indicator object from chart object model.
				var soIndicator = chart.getTechIndicatorById("idStochasticOscillatorChart", "idStochasticOscillatorIndicator");
				
				// Setting new indicator parameters.
				soIndicator.stochasticOscillatorIndicator.kPeriod = kPeriod;
				soIndicator.stochasticOscillatorIndicator.dPeriod = dPeriod;
				soIndicator.stochasticOscillatorIndicator.kSmoothingPeriod = kSmoothingPeriod;
				soIndicator.stochasticOscillatorIndicator.maType = maType;

				// Changing names of k and d series.
				soIndicator.stochasticOscillatorIndicator.kSeries.name = "Full Stochastic: %K("+kPeriod+","+kSmoothingPeriod+")";
				soIndicator.stochasticOscillatorIndicator.dSeries.name= "%D("+dPeriod+")";
				
				return true;
			}

			// Function to apply Stochastic Oscillator indicator settings.
			function updateStochasticOscillatorIndicator() {
				if (configStochasticOscillatorIndicator(
						$("#stochasticKPeriod").val(),
						$("#stochasticDPeriod").val(),
						$("#stochasticKSmoothingPeriod").val(),
						$("#stochasticMAType").val()
					)) {
					
					chart.applySettingsChanges(); // If settings are valid - apply changes.
				}
			}
			
			//---------------------------------------------------------------------------------------------------
			//		MACD
			//---------------------------------------------------------------------------------------------------
			
			// Function to change MACD indicator settings
			function configMACDIndicator(slowPeriod, fastPeriod, signalPeriod) {
				if (isNaN(slowPeriod) || isNaN(fastPeriod) || isNaN(signalPeriod)) return false; // parameters validation
				
				// Get indicator object from chart object model.
				var macdIndicator = chart.getTechIndicatorById("idMACDIndicatorChart", "idMACDIndicator");
				
				// Set new values for indicator parameters.
				macdIndicator.macdIndicator.slowPeriod = slowPeriod;
				macdIndicator.macdIndicator.fastPeriod = fastPeriod;
				macdIndicator.macdIndicator.signalPeriod = signalPeriod;

				// Change name of macd and signal series.
				macdIndicator.macdIndicator.macdSeries.name = "MACD(" + slowPeriod + "," + fastPeriod + ")";
				macdIndicator.macdIndicator.signalSeries.name = "EMA(" + signalPeriod + ")";
				
				return true;
			}

			// Function to apply new settings of MACD indicator.
			function updateMACDIndicator() {

				if (configMACDIndicator(
						$("#macdSlowPeriod").val(),
						$("#macdFastPeriod").val(),
						$("#macdSignalPeriod").val()
					)) {
					
					chart.applySettingsChanges(); // If settings are valid  - apply changes.	
				}
			}
			
			//---------------------------------------------------------------------------------------------------
			//		html events binding
			//---------------------------------------------------------------------------------------------------

			$(function() {
				$("#smaPeriod").change(updateSMAIndicator);
				
				$("#bBandsPeriod").change(updateBollingerBandsIndicator);
				$("#bBandsDeviation").change(updateBollingerBandsIndicator);
				
				$("#stochasticMAType").change(updateStochasticOscillatorIndicator);
				$("#stochasticKSmoothingPeriod").change(updateStochasticOscillatorIndicator);
				$("#stochasticDPeriod").change(updateStochasticOscillatorIndicator);
				$("#stochasticKPeriod").change(updateStochasticOscillatorIndicator);
				
				$("#macdSignalPeriod").change(updateMACDIndicator);
				$("#macdFastPeriod").change(updateMACDIndicator);
				$("#macdSlowPeriod").change(updateMACDIndicator);
			});
			
			//---------------------------------------------------------------------------------------------------
			//		Enable html controls
			//---------------------------------------------------------------------------------------------------
			
			chart.onChartDraw = function() {
				$("#smaPeriod").removeAttr("disabled");
				
				$("#bBandsPeriod").removeAttr("disabled");
				$("#bBandsDeviation").removeAttr("disabled");
				
				$("#stochasticMAType").removeAttr("disabled");
				$("#stochasticKSmoothingPeriod").removeAttr("disabled");
				$("#stochasticDPeriod").removeAttr("disabled");
				$("#stochasticKPeriod").removeAttr("disabled");
				
				$("#macdSignalPeriod").removeAttr("disabled");
				$("#macdFastPeriod").removeAttr("disabled");
				$("#macdSlowPeriod").removeAttr("disabled");
				
				$("#btnResetToDefault").removeAttr("disabled");
				
				chart.onChartDraw = null;
			}
		</script>
		
		<!-- table settings -->
		<style type="text/css">
		
			table.settings {
				border-style: solid;
				border-width: 1px;
				border-color: #D0CDC9;
				width: 300px;
			}
			
			table.settings input, table.settings select {
				width: 130px;
			}

			table.settings tr th {
				font:normal 60% Verdana;
				background-color: #DCD9D5;
				font-weight:bold;
				padding-bottom:5px;
				padding-top:5px;
				padding-left:10px;
				text-align:left;
			}
			
			table.settings tr td {
				background-color: #F8F4F0;
				font:normal 70% Verdana;
				padding-bottom:2px;
				padding-top:2px;
				padding-left:10px;
				text-align:left;	
			}
		</style>
	</head>
	
	<body>
		<div id="chartContainer">	<!-- Chart Container --></div>
		
		<table class="settings">
			<tr>
				<td colspan="2">Type new value for any parameter and press "Enter" key (or "Tab" key in Opera) to update the chart:</td>
			</tr>
			
			<!-- SMA Indicator parameters -->
			<tr><th colspan="2">SMA Params:</th></tr>
			<tr>
				<td>Period:</td>
				<td><input type="text" id="smaPeriod" disabled="disabled" value="20" autocomplete="off"/></td>
			</tr>
			
			<!-- BBands Indicator parameters -->
			<tr><th colspan="2">Bollinger Bands Params:</th></tr>
			<tr>
				<td>Period:</td>
				<td><input type="text" id="bBandsPeriod" disabled="disabled" value="20" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>Deviation:</td>
				<td><input type="text" id="bBandsDeviation" disabled="disabled" value="2" autocomplete="off"/></td>
			</tr>
			
			<!-- Stochastic Oscillator Indicator parameters -->
			<tr><th colspan="2">Full Stochastic Oscillator:</th></tr>
			<tr>
				<td>K Period:</td>
				<td><input type="text" id="stochasticKPeriod" disabled="disabled" value="14" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>K Smoothing Period:</td>
				<td><input type="text" id="stochasticKSmoothingPeriod" disabled="disabled" value="1" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>D Period:</td>
				<td><input type="text" id="stochasticDPeriod" disabled="disabled" value="3" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>MA Type:</td>
				<td><select id="stochasticMAType" disabled="disabled" autocomplete="off">
					<option id="maTypeSMA" value="SMA">SMA</option>
					<option id="maTypeEMA" value="EMA">EMA</option>
					</select></td>
			</tr>
			
			<!-- MACD Indicator parameters -->
			<tr><th colspan="2">MACD:</th></tr>
			<tr>
				<td>Slow period:</td>
				<td><input type="text" id="macdSlowPeriod" disabled="disabled" value="26" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>Fast period:</td>
				<td><input type="text" id="macdFastPeriod" disabled="disabled" value="12" autocomplete="off"/></td>
			</tr>
			<tr>
				<td>Signal period:</td>
				<td><input type="text" id="macdSignalPeriod" disabled="disabled" value="9" autocomplete="off"/></td>
			</tr>
			
			<!-- reset -->
			<tr>
				<td colspan="2" style="text-align:right;">
					<input type="button" style="width:auto;" disabled="disabled" id="btnResetToDefault" value="Reset all params to default" onclick="resetToDefault()"/>
				</td>
			</tr>
		</table>
	</body>
</html>

Sample Description

How to use this sample?

Sample allows you to change parameters of technical indicators shown on the chart: SMA, Bollinger Bands, MACD and Full Stochastic Oscillator. Change params in input controls to the right of the chart and see how indicators are changed.

to top

How it works

All the changes in charts can be made using object model as described in Updating Chart using Object Model.

This sample uses auxiliary getTechIndicatorById function to obtain a link to technical indicator object and applySettingsChanges method to commit the changes made.

Note, that each indicator has the complex structure and parameters are very different, you can learn more about technical indicators available in AnyChart Stock in Technical Indicators section.

to top

AnyChartStock JavaScript API

This sample uses the following methods, properties and events from AnyChartStock JavaScript API:

Item Type Description
needConfig Property (Boolean) Defines whether the access to full chart configuration object model is required.
applySettingsChanges Method Commits changes made to settings element of the objectModel.
getTechIndicatorById Method Gets the link to technical indicator object by its id.
setXMLFile Method Sets chart XML configuration file path.
write Method Adds the chart to HTML DOM as a child of the specified container.
onChartDraw Event This event is dispatched when the AnyChart Stock is drawn.

to top

Prerequisites

This section lists all configuration, data and auxiliary files required for this sample.

Configuration file

CSV files

SWF files

  • AnyChartStock.swf - AnyChart Stock component.
  • Preloader.swf - AnyChart Stock helper component that loads the main component (AnyChartStock.swf) and displays loading progress.

JavaScript Libraries

  • jquery.min.js - A JavaScript jQuery library. Learn more at jQuery official site.
  • AnyChartStock.js - A JavaScript library that is shipped with AnyChart Stock component. It is used to embed the component into HTML DOM and to comunicate with the Flash part.

to top

The information contained in this website is for general information purposes only. All sample data provided on this site is for demonstration purposes only.

The logos and names of other companies and products mentioned on this site are copyright and/or trademarks of their respective owners.

The content on this site, including news, quotes, data and other information, is provided for your personal information only, and is intended for demonstration purposes only. Content on this site is not appropriate for the purposes of making a decision to carry out a transaction or trade. Nor does it provide any form of advice (investment, tax, legal) amounting to investment advice, or make any recommendations regarding particular financial instruments, investments or products.

In no event AnyChart will be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website.

This site may point to other Internet sites that may be of interest to you, however AnyChart does not endorse or take responsibility for the content on such other sites

Market data and News provided by and copyright RediNews, Incorporated.