Intermediate Trading Tutorial | ACSIL C++
Two markets can cover the same total distance over twenty bars while exhibiting very different directional behavior. In one market, several upward closes are followed by a sustained downward sequence. In another, nearly every upward close is followed immediately by a downward close. A measure of cumulative price change or total range alone does not distinguish these patterns reliably.
This tutorial builds a Sierra Chart custom study that answers a narrower question: among adjacent non-flat close-to-close changes, how often does the sign switch? The output is a percentage from zero to one hundred, calculated only from completed chart bars. A separate supporting subgraph records the number of eligible pairs. Together they describe directional alternation and the amount of evidence behind that observation, without making assumptions about future price.
1. A precise definition of a sign transition
Take N completed close-to-close changes. The change associated with a bar is its close minus its predecessor's close, measured in raw price units rather than percentage returns. Classify each change as positive, negative or flat. With the default tolerance of zero ticks, an unchanged close is flat. A positive tolerance treats changes whose absolute size is less than or equal to that threshold as flat.
For each adjacent pair, count it only if both changes have nonzero signs. If their signs differ, record one flip. The sign transition rate is 100 multiplied by the flip count divided by the eligible pair count. With N changes there are at most N minus one candidate pairs. A flat change breaks adjacency: the algorithm does not connect two non-flat moves across a flat one.
This distinction matters. Skipping a zero observation and joining the surrounding signs measures the sequence of non-flat events, not successive chart bars. Both definitions can be defensible, but they answer different questions. This indicator uses literal adjacency throughout so that the time resolution of the chart remains part of the measurement.

2. Verify the calculation with a small sample
Assume seven completed closes are 100, 102, 103, 101, 100, 103 and 103. Their six changes are +2, +1, -2, -1, +3 and 0. Use N = 6 and a zero-tick tolerance for this example.
| Adjacent changes | Eligible? | Flip? |
|---|---|---|
| +2, +1 | Yes | No |
| +1, -2 | Yes | Yes |
| -2, -1 | Yes | No |
| -1, +3 | Yes | Yes |
| +3, 0 | No | Excluded |
Two flips divided by four eligible pairs produces 50%. Dividing by all five candidate pairs would produce 40%, which is not the definition used here. Similarly, a sequence with five eligible pairs and no flips reports zero, whereas an entirely flat sequence also reports zero but has no eligible pairs. The supporting count is therefore essential for interpreting the result correctly.
3. Complete ACSIL C++ source
#include "sierrachart.h"
#include <cmath>
SCDLLName("PatternSmart Close Sign Transitions")
SCSFExport scsf_CloseSignTransitionRate(SCStudyInterfaceRef sc)
{
SCSubgraphRef Rate = sc.Subgraph[0];
SCSubgraphRef PairCount = sc.Subgraph[1];
SCInputRef Length = sc.Input[0];
SCInputRef FlatTicks = sc.Input[1];
if (sc.SetDefaults)
{
sc.GraphName = "Closed-Bar Close Sign Transition Rate";
sc.StudyDescription = "Adjacent non-flat close change sign flips.";
sc.AutoLoop = 1;
sc.GraphRegion = 1;
Rate.Name = "Flip Rate (%)";
Rate.DrawStyle = DRAWSTYLE_LINE;
Rate.PrimaryColor = RGB(58, 99, 201);
Rate.LineWidth = 2;
Rate.DrawZeros = true;
PairCount.Name = "Eligible Pair Count";
PairCount.DrawStyle = DRAWSTYLE_IGNORE;
Length.Name = "Completed Close Changes";
Length.SetInt(20);
Length.SetIntLimits(2, 500);
FlatTicks.Name = "Flat Tolerance (Ticks)";
FlatTicks.SetInt(0);
FlatTicks.SetIntLimits(0, 100);
return;
}
const int n = Length.GetInt();
const int i = sc.Index;
sc.DataStartIndex = n + 1;
Rate[i] = 0.0f;
PairCount[i] = 0.0f;
// Need n close changes from n+1 completed closes.
if (i < n + 1)
return;
if (FlatTicks.GetInt() > 0 && sc.TickSize <= 0)
return;
const double tolerance = FlatTicks.GetInt() * sc.TickSize;
int previousSign = 0;
int eligible = 0;
int flips = 0;
// Never read the forming candle at index i.
for (int j = i - n; j <= i - 1; ++j)
{
const double d = static_cast<double>(sc.Close[j])
- static_cast<double>(sc.Close[j - 1]);
int sign = 0;
if (d > tolerance)
sign = 1;
else if (d < -tolerance)
sign = -1;
// A flat change breaks adjacency; do not bridge it.
if (previousSign != 0 && sign != 0)
{
++eligible;
if (sign != previousSign)
++flips;
}
previousSign = sign;
}
PairCount[i] = static_cast<float>(eligible);
if (eligible > 0)
Rate[i] = 100.0f * flips / eligible;
}
The study uses automatic looping. Sierra Chart calls the study for the relevant chart index, and the loop inside the function visits only the N completed close changes preceding that index. Each change requires two close values, so the first valid index is N plus one. The default lookback is 20; the input accepts values between 2 and 500. Flat tolerance is specified in chart ticks and defaults to zero.
The second subgraph stores the count rather than drawing it on the main percentage scale. It is configured with DRAWSTYLE_IGNORE to avoid visually mixing quantities with percentages. If desired, expose that count in a separate chart region through a second study or reference it when building a downstream ACSIL filter. A value of zero in the rate subgraph is meaningful only when the count is positive.
4. Closed-bar timing without forward-looking data
At index i, the newest close consulted is i minus one. The first change in the window uses close i minus N and close i minus N minus one. The final change uses close i minus one and close i minus two. No calculation reads the close of the currently forming bar. Consequently, the output is displayed one bar after the most recent observation it describes. That shift is deliberate and is not a plotting mistake.
Sierra Chart also documents sc.GetBarHasClosedStatus for studies that prefer to calculate on the actual completed candle. This implementation uses prior indices instead, which simplifies the mapping between information availability and the displayed bar. Be careful when adapting it to a strategy that reacts on the measured candle: the final close is not known until that candle completes. A historical chart with fully populated OHLC arrays is not evidence that an earlier live decision could have used those values.
Recalculations and historical corrections can still change old results. Data reloads, changes to session templates, or reconstruction of non-time-based chart bars may alter the underlying series. Closed-bar indexing avoids ordinary intrabar fluctuation of the measured close; it cannot make revised market data immutable.
5. Build and add the study
- Save the source as a new .cpp file in the Sierra Chart ACS_Source directory.
- Open the custom studies build window, select the file, and build the DLL using the supported build method for your installation.
- Add the study from the custom studies list and set the lookback to six for the worked example, or leave the default of twenty for initial research.
- Use the chart values tools or a spreadsheet study to compare several calculated values with the underlying closes and the eligible count.
- Restore the desired lookback and test multiple chart types, sessions and market regimes.
The code follows Sierra Chart's documented ACSIL programming concepts. It has not been compiled or executed in Sierra Chart during preparation. Validate it with your installed version before relying on it.
6. Interpreting the lookback and flat tolerance
A short lookback reacts quickly to alternation but can produce unstable percentages when only a few pairs qualify. For example, one flip out of one eligible pair is 100%, but it provides far less evidence than eighteen flips out of nineteen. Longer windows smooth the response and increase the maximum available sample while introducing a slower reaction to changes. Consider a minimum eligible count as a separate research condition; for a twenty-change window, a threshold such as ten eligible pairs is a hypothetical starting point, not a universal recommendation.
Increasing flat tolerance removes small changes from the classification. This can help distinguish larger directional movements from very small price adjustments, but it may also remove substantial numbers of pairs. A one-tick tolerance means exactly one-tick changes are treated as flat. The tick size must correspond to the chart's instrument settings. If nonzero tolerance is selected and the tick size is unavailable, the study returns an invalid observation represented by zero count.
Do not interpret a high rate as a complete definition of a sideways market. A strongly trending market can still alternate between large advances and small pullbacks. Conversely, a low rate does not guarantee a strong trend: a series of tiny moves in the same direction also produces few transitions. The indicator deliberately ignores movement magnitude beyond the flat threshold. Combining this feature with an independent measure of displacement or volatility may be informative, provided each component is tested separately.
7. Testing checklist and limitations
- Verify the first N plus one chart indices do not produce a valid pair count.
- Test all-positive and alternating sequences; with no flat changes their rates should be 0% and 100% respectively.
- Check that a zero between positive and negative changes does not produce a cross-flat flip.
- Verify changes exactly equal to the tolerance are classified as flat.
- Compare the plotted value during an open candle and after a new candle appears, checking the intended one-bar alignment.
- Test the maximum lookback for acceptable chart recalculation performance; this direct implementation has O(N) work per evaluated index.
The study is descriptive and makes no execution, profitability or risk-control claims. Session gaps, illiquid bars and different aggregation methods affect close-change signs. OHLC data do not reveal order book conditions, execution costs or the sequence of intrabar events. Any trading rule derived from this indicator requires independent out-of-sample validation, realistic transaction cost assumptions and suitable risk management.
If you would like to modify this free indicator, add a minimum-sample filter or integrate the calculation with a trading system, contact us for Custom Sierra Chart Development Services.