Sierra Chart | Study ACSIL

Sierra Chart | Referencing a Study with Sierra Chart ACSIL

Sometimes you need to reference a study from within your own study. As an example, say you have a study that uses the Sierra Chart built in “Swing High and Low” study for some calculation.With references, the study user will do the following:

  1. Adding the “Swing High and Low” study to the chart and configuring it as he or she wishes
  2. Adding the custom study to the chart
  3. In our custom study settings, the user will be presented with an input parameter through which he or she sets the reference to the “Swing High and Low”
  4. The drop down list will include all the studies that are currently on the chart
  5. The user will select the “Swing High and Low” study that was added in step 1

This last step provides us with a reference to the study. A reference is merely the study ID which is assigned by Sierra Chart when it is added to the chart. We later use this ID to retrieve the study subgraphs which we process.

Below is a short piece of code and an image what the use will see.

[codesyntax lang=”cpp” tab_width=”2″]

#include "sierrachart.h"

SCSFExport scsf_reference_example(SCStudyGraphRef sc)
{
	// In order to access the swing high 
	// and low study and subgraphs,
	// our study needs a reference - 
	// the reference is nothing more than the study ID which
	// is a number (the one that appears in the application UI
	// We retrieve that by having the user set it for us 
	// from the study settings.
	// The variable below will be used just for that.
	SCInputRef studyRef = sc.Input[0]; 

	if (sc.SetDefaults)
	{
		sc.GraphName = "Reference Example";
		sc.StudyDescription = "";
		sc.AutoLoop = 1;  // true
		sc.GraphRegion = 1;
		sc.DrawZeros = 1;

		// The following line is important 
		// It tells Sierra Chart that this study is calculated at a lower precedence
		// We want our study to be calculated after the swing high and low study is calculated
		// That ensures that our study is reading the most updated values from the swing high / low subgraphs
		sc.CalculationPrecedence = LOW_PREC_LEVEL; 

		studyRef.Name = "Study Reference";
		studyRef.SetStudyID(0);

		return;

	}

	// we use the sc.GetStudyArrayUsingID(studyRef.GetStudyID(), 0, swingHigh)
	// to gain access to the actual subgraph values
	SCFloatArray swingHigh;
	sc.GetStudyArrayUsingID(studyRef.GetStudyID(), 0, swingHigh);

	SCFloatArray swingLow;
	sc.GetStudyArrayUsingID(studyRef.GetStudyID(), 1, swingLow);

	// now we can use the swingHigh and swingLow arrays to retrieve swing point information
	// the code below simply iterates over the array and retrieves the last swingHigh value
	float lastSwingHighValue;
	for (int i = sc.Index; i >= 0; i--)
	{
		if (swingHigh[i] != 0)
		{
			lastSwingHighValue = swingHigh[i];
			break;
		}
	}
}

[/codesyntax]