Login Page - Create Account

Support Board


Date/Time: Fri, 07 Nov 2025 01:14:17 +0000



[Programming Help] - Sequence of evaluation of interdependent studies during chart load?

View Count: 267

[2025-10-04 19:28:19]
rajeshh - Posts: 50
Hello - I have a case where studies are used by other studies, so lets say study A is used by B which is used by C. All of them calculate values on a bar close.
When an intra day chart is loaded the first time, how do the studies get evaluated? Does A get evaluated for all bars in the chart, then B for all bars, and then C, or does the evaluation happen A->B->C for each bar in the chart?
I suspect its the former, because for one of the values calculated in study B, the value at a bar X in the chart can be different if called by C at close of bar X (without seeing any future bars), vs if it is being called by C after study B has seen all the bars (because that value could be reset based on future bars), and for reload of a chart, I see the value reset as if study B has seen all the bars, by the time C calls it for bar X.

This is not a problem for me in real trading or replay, because C calls B for a bar X before future bars are printed. But it does show wrong results for historical data in the intra-day chart.

Thanks
[2025-10-06 19:52:47]
ForgivingComputers.com - Posts: 1154
Answer:
A->B->C for each bar in the chart.

You can see the "Calc Order" next to each study in the Study Settings window.

When the study is first added to the chart, or a recalc is done, the study starts at Bar 0 and calculates the values for each study, then goes onto the next bar.

Here is the reason why it is done this way. Let's say study A also depended on the previous result of study C. You could not evaluate A for every bar, then B, then C, because A needs data from a previous C value.

Also, when live, each study on each bar is evaluated periodically in real-time before the bar closes.
[2025-10-08 23:18:51]
rajeshh - Posts: 50
or a recalc is done, the study starts at Bar 0 and calculates the values for each study, then goes onto the next bar.

So I dont think the above is correct, as I suspected in my original post. I have written a sample study A, and B below. study A calculates a subgraph value when a bar closes, such that for bar X, it reassigns the subgraph value of every bar upto that bar to the index of X. IOW, when bar 1 closes, bar 0 subgraph value will be 1 and so on. So at the end of it, all bars in the chart will have the subgraph value of the last index in the chart.

Study B just gets the value of subgraph from study A for a bar X, and prints it below the low of the bar X. If the calculation is happening as you say where A->B are calculated for every bar on chart loading, then one should see values 1, 2, 3, 4 etc, but B shows the value of the last index in the chart for all the bars. When one does a replay mode of the chart, then the values are correctly showing the current index of the bar.


SCSFExport scsf_A(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_a = sc.Subgraph[0];


  if (sc.SetDefaults)
  {
    // Set the configuration and defaults

    sc.GraphName = "Test Sequence of Study Evaluation A";

    sc.CalculationPrecedence = LOW_PREC_LEVEL;

    sc.AutoLoop = true;
    sc.GraphRegion = 0;
    sc.ValueFormat = VALUEFORMAT_INHERITED;

    // Set the name of the first subgraph
    Subgraph_a.Name = "Index of bar";
    Subgraph_a.DrawStyle = DRAWSTYLE_SUBGRAPH_NAME_AND_VALUE_LABELS_ONLY;
    

    sc.ScaleRangeType = SCALE_SAMEASREGION;


    return;

  }
  int n = sc.Index;

  if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)


    for (int tbl = n ; tbl >= 0; tbl--)
    {
      Subgraph_a[tbl] = n;


    }

}


SCSFExport scsf_B(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_a = sc.Subgraph[0];

  SCInputRef Input_AArrayRef = sc.Input[0];


  if (sc.SetDefaults)
  {
    // Set the configuration and defaults

    sc.GraphName = "Test Sequence of Study Evaluation B";

    sc.CalculationPrecedence = LOW_PREC_LEVEL;

    sc.AutoLoop = true;
    sc.GraphRegion = 0;
    sc.ValueFormat = VALUEFORMAT_INHERITED;

    // Set the name of the first subgraph
    Subgraph_a.Name = "Index of bar";
    Subgraph_a.DrawStyle = DRAWSTYLE_CUSTOM_VALUE_AT_Y;
    Subgraph_a.PrimaryColor = RGB(0, 255, 0);
    Subgraph_a.LineWidth = 10;
    Subgraph_a.DrawZeros = false;

    Input_AArrayRef.Name = "Study A reference";
    Input_AArrayRef.SetStudyID(2);


    sc.ScaleRangeType = SCALE_SAMEASREGION;


    return;

  }
  int n = sc.Index;
  SCFloatArray indexArray;

  if (sc.GetStudyArrayUsingID(Input_AArrayRef.GetStudyID(), 0, indexArray) > 0
    && indexArray.GetArraySize() > 0);

  if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
    //Copy the study data that we retrieved using GetStudyArrayUsingID, into a subgraph data output array
    Subgraph_a.Arrays[0][n] = sc.Low[n] - 2 * sc.TickSize;
    Subgraph_a[n] = (float)indexArray[n];
    
  }

}

[2025-10-09 20:00:53]
Sierra_Chart Engineering - Posts: 21324
To see the calculation order of studies, refer to:
Chart Studies: How Calculation Order of Studies Is Determined

It is this way but not necessarily in this order:
? Does A get evaluated for all bars in the chart, then B for all bars, and then C

Refer to:
Working with ACSIL Arrays and Understanding Looping
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, use the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2025-10-09 20:03:49
[2025-10-09 21:35:37]
rajeshh - Posts: 50
Thanks for the response.
It is this way
Is it possible to complete calculations for all studies for a bar before proceeding to the next bar during initial load? This then mimics as closely as possible to what happens live/replay mode.
[2025-10-09 21:44:29]
ForgivingComputers.com - Posts: 1154
Is it possible to complete calculations for all studies for a bar before proceeding to the next bar during initial load? This then mimics as closely as possible to what happens live/replay mode.

That is exactly what it is doing. This has already been stated.

sc.Index initially starts at 0 and increments up to sc.ArraySize -1 when the study is fully recalculated. This happens when the chart is loaded or reloaded. Each time it increments, your study function is called again.

[2025-10-10 02:29:36]
rajeshh - Posts: 50
No disrespect, ForgivingComputers, but did you even take a look at the sample studies code i put in. I dont think you are understanding the problem i am stating.
[2025-10-10 11:32:55]
User431178 - Posts: 806
I dont think you are understanding the problem i am stating.

You are correct, there seems to be some misunderstanding.
The SC team already confirmed how it is calculated - all bars for study x, then all bars for study y etc, and definitely not all studies at bar 1, all studies at bar 2 etc.


Is it possible to complete calculations for all studies for a bar before proceeding to the next bar during initial load? This then mimics as closely as possible to what happens live/replay mode.

My guess, that would be a no - it would be completely incompatible with manual looping.
Many studies use manual looping, whereby they loop from sc.UpdateStartIndex (e.g. 0 initially) to sc.ArraySize - 1 in a single function call, how would you reconcile that with calculating every study at every bar in sequence?
Maybe you need to reconsider how your study is written, bearing in mind how the studies are calculated.
[2025-10-10 14:58:24]
ForgivingComputers.com - Posts: 1154
@rajeshh I understand the problem. I don't know what you are trying to accomplish with your code.

I not only looked at it, I built the studies and added them to a chart.

The first one is overcomplicated:


for (int tbl = n ; tbl >= 0; tbl--)
{
Subgraph_a[tbl] = n;
}

Every time you add a bar, you renumber all previous bars to their index number, which is the same every time. This is very inefficient. You could replace the above with the following, resulting in the same result and each bar is touched only once.

Subgraph_a[n] = n;

I don't know what you are trying to accomplish with B. It results in all zeros.

The if statement does the same thing if true or false, it gets the array:

if (sc.GetStudyArrayUsingID(Input_AArrayRef.GetStudyID(), 0, indexArray) > 0
&& indexArray.GetArraySize() > 0);

It could be replaced with this:
sc.GetStudyArrayUsingID(Input_AArrayRef.GetStudyID(), 0, indexArray)

I don't see how your code proves anything.
[2025-10-10 15:54:23]
rajeshh - Posts: 50
@User431178, thanks for your response, and understanding of the problem.

Wrt manual vs automatic looping, I dont see how that affects SC calling the study on a bar close, since the manual looping is happening inside the study, and we are talking about calling the study as a black box.

FWIW, since the replay mode already works as desired, the initial chart load could have been implemented as a really fast replay. Wrt, a workaround for my study - yes, so happens my subgraph value is a function of 2 bars at any given time, so if I used an intermediate persistent 2 dimensional array to store my values, it would get around this problem.

@ForgivingComputers, since you have built the studies, can I request you to compare the values shown below each bar when first added to a chart, and then replay the chart for a few bars, and see whether the values below the bar are the same for each bar or different for each bar. IOW, the values calculated for study B are not the same on initial chart load vs in replay mode for a given bar. The sample study is not trying to accomplish anything more than illustrate this side-effect.

Since SC has already agreed with my understanding of the problem, we can lay this discussion to rest. The real resolution is in whether SC will decide to make the behavior consistent.
[2025-10-10 16:13:09]
ForgivingComputers.com - Posts: 1154
The real resolution is in whether SC will decide to make the behavior consistent.

I would not expect any modifications to a fundamental thing like calculation sequencing based on this thread. You have not made a case for any change.
[2025-10-10 16:28:32]
User431178 - Posts: 806

Wrt manual vs automatic looping, I dont see how that affects SC calling the study on a bar close, since the manual looping is happening inside the study, and we are talking about calling the study as a black box.

I am speaking with regards to full recalculation of chart (and any chart update where more than one bar is processed (sc.UpdateStartIndex < sc.ArraySize - 2) - "When an intra day chart is loaded the first time, how do the studies get evaluated?"

Automatic = study called once for each bar in the chart (or update)
Manual = study called once for all bars in the chart (or update)

The point being that with manual looping, as all bar are calculated in a single pass, there is no way to calculate all studies at each individual bar in sequence during a full recalculation.
Once you are live updating at then no difference.
If you had to calculate each study in turn at each bar during recalculation, then that would mean "automatic looping" would be the default and only case.


The real resolution is in whether SC will decide to make the behavior consistent.
You'll be better off rewriting your studies to account for this, it's surely not so hard.
Date Time Of Last Edit: 2025-10-10 16:29:23
[2025-10-10 17:30:21]
rajeshh - Posts: 50
The point being that with manual looping, as all bar are calculated in a single pass, there is no way to calculate all studies at each individual bar in sequence during a full recalculation.

how would you reconcile that with calculating every study at every bar in sequence?

Either way you look at it, if there are mixed studies - manual looped and auto-looped, the manual looped studies are called once for all bars, and the auto-looped is called once per bar. That is not changing. The point I am making is to call all auto-looped studies for a bar, before going to the next bar. Hope you see the distinction.
[2025-10-24 04:05:08]
rajeshh - Posts: 50
Hello Sierra team - can you please look at my post in message #13 for a precise request of what I would like? I have tried working around, but it's just not possible in my case if all the auto-looped studies aren't called for a bar before proceeding to the next bar during the initial load (just like it happens in live/replay). As a result, for my intra-day chart, my study results for the historical bars are wrong compared to what they are (correct) for live bars. Thanks

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account