Two complete studies walked through line by line — a moving-average overlay and a momentum sub-pane histogram — straight from the shipped examples.
These are complete, compilable studies — nothing elided. Paste either into the Study Editor and press Compile & Load. The first is the moving-average overlay the editor ships preloaded; the second is the momentum example that ships in the SDK examples folder.
A line on the price plane: the mean of the last period closes, computed with a rolling sum so it stays cheap on long series.
Line by line:
#include "sharpnel_study_sdk.h" — the only Sharpnel header. <new> is for std::nothrow.struct Inst — per-instance state behind the void* handle. An SMA is stateless, so it is empty; it exists so two copies on two charts stay independent.abi_version() returns SHARPNEL_STUDY_ABI_VERSION — the host calls this first and refuses a mismatch.describe() names the study and sets SN_TARGET_OVERLAY so it draws on the price plane.inputs() declares one integer period with a default of 20 and 2–400 bounds; the terminal builds the settings dialog from it. It returns 1 — the number of inputs it would write.create() / destroy() allocate and free that per-instance Inst.compute(): resolve period from the flat SnInput[] by matching the key; ask the sink for a line buffer named "SMA" (steel blue, 1.4px wide); bound the loop by b.len; keep a running sum, subtracting the bar that falls out of the window; and write SN_NOVALUE for warmup bars so the line only begins once period bars exist.A histogram in its own pane: the difference between the current close and the close period bars ago, with a zero reference line. Same six exports; two things change — the draw target and the plot style.
What differs from the SMA:
describe() sets SN_TARGET_SUBPANE — momentum oscillates around zero, so it belongs in its own pane below the price, not on top of it.subgraph("Mom", SN_STYLE_HISTOGRAM, …) asks for a histogram (amber) rather than a line — bars above/below zero read as positive/negative momentum.close[i] − close[i − period], with the first period bars left as SN_NOVALUE.api->level(sink, 0.0, …, "0") draws a semi-transparent grey zero line — the axis the histogram reads against.if (!s || !sink || !api), if (!buf.ptr)) — defensive, and cheap.example_momentum.cpp. Start from it, change the math in compute(), and you have your own study.