Sierra Chart - Referencing Other Time Frames and Symbols When Using the ACSIL

Referencing Other Time Frames and Symbols When Using the ACSIL


Introduction

When using the Advanced Custom Study Interface and Language (ACSIL) and you want to reference bar data, custom or built-in studies from other time frames, such as accessing 5 minute data from a 1 minute chart, then this is possible with 2 different methods.

All of this information applies whether you are creating a study that displays value data through lines or a trading system that generates order signals and displays Buy/Sell signals on the chart.

First, it must be understood that it is necessary in Sierra Chart to have the charts open for each timeframe/period per bar you wish to work with. For example, if you have a 1 minute chart and you want to access the price bar data or study data from a 5 minute chart, then you need to have the 1 minute chart open and the 5 minute chart open. All of these charts can be contained in the same chartbook and saved, so the setup can be immediately opened from the File menu.

Another use of the methods described here is to access data from different symbols rather than different time periods per bar. So these methods can also apply to accessing data from charts for different symbols.

The two methods are explained in the sections below along with the different ways that each method can be used.

Referencing Data from Other Time Frames by Using the Overlay Study

The simplest method to reference data from other Time Frames, whether this is bar data or study data, is to overlay that data on the destination timeframe by using the Study/Price Overlay study. The destination timeframe chart will be the chart where your custom study is applied to. You can then directly access the study data from within the code in your custom study. For complete documentation to use the Study/Price Overlay study, please refer to help topic #28. Once you have overlaid the studies on the destination chart, they can be hidden by enabling the Hide Study setting in the Study Settings window for the study. In this way they will not interfere with the view if you do not need to see them.

Once you have all of your bars and/or Studies overlayed on the destination chart, then the next step is in your custom study to reference that data. Below is a code example which demonstrates this. The primary function that is used is sc.GetStudyArrayUsingID. This code below can be found in studies7.cpp in the ACS_Source folder in the Sierra Chart program folder.



SCSFExport scsf_ReferenceStudyData(SCStudyGraphRef sc)
{
	SCSubgraphRef Average = sc.Subgraph[0];
	SCInputRef Study1 = sc.Input[0];
	SCInputRef Study1Subgraph = sc.Input[1];
	
	

	if (sc.SetDefaults)
	{
 
		sc.GraphName = "Reference Study Data";
		sc.StudyDescription = "This study function is an example of referencing data from other studies on the chart.";

		
		sc.AutoLoop = 1;

		// We must use a low precedence level to ensure
		// the other studies are already calculated first.
		sc.CalculationPrecedence = LOW_PREC_LEVEL;

		sc.FreeDLL=0; 

		Average.Name = "Average";
		Average.DrawStyle = DRAWSTYLE_LINE;

		Study1.Name = "Input Study 1";
		Study1.SetStudyID(0);

		Study1Subgraph.Name = "Study 1 Subgraph";
		Study1Subgraph.SetSubgraphIndex(0);

		return;
	}

	// Get the subgraph specified with the Study 1
	// Subgraph input from the study specified with
	// the Input Study 1 input.
	SCFloatArray Study1Array;
	sc.GetStudyArrayUsingID(Study1.GetStudyID(),Study1Subgraph.GetSubgraphIndex(),Study1Array);

	// We are getting the value of the study subgraph
	// at sc.Index. For example, this could be
	// a moving average value if the study we got in
	// the prior step is a moving average.
	float RefStudyCurrentValue = Study1Array[sc.Index];

	// Here we will add 10 to this value and compute 
	// an average of it. Since the moving average
	// function we will be calling requires an input
	// array, we will use one of the internal arrays
	// on a subgraph to hold this intermediate 
	// calculation. This internal array could be 
	// thought of as a Worksheet column where you 
	// are performing intermediate calculations.

	sc.Subgraph[0].Arrays[9][sc.Index] = RefStudyCurrentValue + 10;

	sc.SimpleMovAvg(sc.Subgraph[0].Arrays[9],sc.Subgraph[0],15);
}

Referencing Data from Other Time Frames By Direct Referencing of the Other Timeframe

If you need to reference bar data or study data from another timeframe, then this is possible by directly accessing the subgraph arrays of the chart with a different timeframe than the study that you are referencing the data from. For example, if you have a trading system study on chart #1 and you want to access a particular study with results needed for your system from chart #2, which has a different time frame per bar, then you would make a function call to get the subgraph arrays from chart #2. If you need to, for example, get the results of a moving average on a different timeframe, then that average must be on the other timeframe chart and you will need to make a function call to get the Subgraph Data array that contains the moving average data. This study on the other timeframe does not have to be visible. In the Study Settings window for the study, Hide Study can be enabled. Or, if you are referencing data from a custom study that you created, then that study can just use sc.Subgraphs that have no sc.Subgraph[].Name set in which case the study will not even be visible.

Below is a code example which demonstrates getting both the base data arrays and the study arrays from another chart. For example, this could be a moving average study or whatever. We also demonstrate different methods of finding the corresponding index. We use inputs to specify the chart number, the study number and the subgraph number. Although all three of those parameters could be hardcoded. In a future Sierra Chart release, there will be a special type of input to easily choose a particular study subgraph from a list of charts and studies on the chart. A Subgraph is an individual line within a study.

This code below can be found in studies7.cpp in the ACS_Source folder in the Sierra Chart program folder.



SCSFExport scsf_ReferenceDataFromAnotherChart(SCStudyGraphRef sc)
{
	SCInputRef ChartNumber = sc.Input[0];
	SCInputRef StudyNumber = sc.Input[1];

	if (sc.SetDefaults)
	{

		sc.GraphName = "Reference Data";
		sc.StudyDescription = "This is an example of referencing data from another chart.";

		sc.AutoLoop = 1;
		

		sc.FreeDLL=0; 

		ChartNumber.Name = "Chart Number";
		ChartNumber.SetChartNumber(1);

		StudyNumber.Name = "Study Number";
		StudyNumber.SetInt(1);


		return;
	}

	// The following code is for getting the High array
	// and corresponding index from another chart.
	
	// Define a graph data object to get all of the base graph data
	SCGraphData BaseGraphData;
	
	// Get the base graph data from the specified chart
	sc.GetChartBaseData(ChartNumber.GetChartNumber(), BaseGraphData);
	
	// Define a reference to the High array
	SCFloatArrayRef HighArray = BaseGraphData[SC_HIGH];
	
	// Array is empty. Nothing to do.
	if(HighArray.GetArraySize() == 0)
		return;
	
	
	// Get the index in the specified chart that is 
	// nearest to current index.
	int RefChartIndex = sc.GetNearestMatchForDateTimeIndex(ChartNumber.GetChartNumber(), sc.Index);
	float NearestRefChartHigh = HighArray[RefChartIndex];

	// Get the index in the specified chart that contains
	// the DateTime of the bar at the current index.
	RefChartIndex = sc.GetContainingIndexForDateTimeIndex(ChartNumber.GetChartNumber(), sc.Index);
	float ContainingRefChartHigh = HighArray[RefChartIndex];

	// Get the index in the specified chart that exactly
	// matches the DateTime of the current index.
	RefChartIndex = sc.GetExactMatchForSCDateTime(ChartNumber.GetChartNumber(), sc.BaseDateTimeIn[sc.Index]);
	if(RefChartIndex != -1)
	{
		float ExactMatchRefChartHigh = HighArray[RefChartIndex];
	}
	
	
	// The following code is for getting a study subgraph array
	// and corresponding index from another chart. 
	// For example, this could be a moving average study subgraph.

	// Define a graph data object to get all of the study data
	SCGraphData StudyData;
	
	// Get the study data from the specified chart
	sc.GetStudyArraysFromChart(ChartNumber.GetChartNumber(), StudyNumber.GetInt(),StudyData);
	
	// Define a reference to the first subgraph array
	SCFloatArrayRef SubgraphArray = StudyData[0];
	
	// Array is empty. Nothing to do.
	if(SubgraphArray.GetArraySize() == 0)
		return;
	
	
	// Get the index in the specified chart that is nearest
	// to current index.
	RefChartIndex = sc.GetNearestMatchForDateTimeIndex(ChartNumber.GetChartNumber(), sc.Index);
	float NearestSubgraphValue = SubgraphArray[RefChartIndex];

	// Get the index in the specified chart that contains 
	// the DateTime of the bar at the current index.
	RefChartIndex = sc.GetContainingIndexForDateTimeIndex(ChartNumber.GetChartNumber(), sc.Index);
	float ContainingSubgraphValue = SubgraphArray[RefChartIndex];

	// Get the index in the specified chart that exactly
	// matches the DateTime of the current index.
	RefChartIndex = sc.GetExactMatchForSCDateTime(ChartNumber.GetChartNumber(), sc.BaseDateTimeIn[sc.Index]);
	if(RefChartIndex != -1)
	{
		float ExactMatchSubgraphValue = SubgraphArray[RefChartIndex];
	}
}


[Top]