Posts

Showing posts with the label indicator development

What Is an SMA Moving Average? Pine Script v6 Guide to Simple Moving Averages

Image
When analyzing price charts, raw candlestick data can appear noisy and difficult to interpret — the Simple Moving Average (SMA) is one of the most fundamental mathematical tools used to smooth that noise and reveal the underlying directional trend of a market. 1. What Is a Simple Moving Average (SMA)? A Simple Moving Average is a statistical calculation that computes the arithmetic mean of a price series over a fixed number of periods. At each bar, the SMA sums the most recent N closing prices and divides by N , producing a single smoothed value that "rolls" forward with each new bar. Mathematical Definition For a window of length $N$, the SMA at time $t$ is defined as: $$\text{SMA}_t = \frac{1}{N} \sum_{i=0}^{N-1} P_{t-i}$$ Where $P_{t-i}$ is the price at bar $t - i$. Every observation within the window receives an equal weight of $\frac{1}{N}$. Numerical Example Consider 5 consecutive closing prices: 10, 12, 11, 13,...

Pine Script Learning Roadmap: From First Indicator to Advanced Strategy Backtesting

Image
Learning Pine Script efficiently requires a structured progression — jumping directly into complex strategies without mastering the execution model and type system leads to subtle bugs that are difficult to diagnose. This roadmap organizes the learning path into five discrete stages, each building on verified language mechanics, so every concept introduced is grounded in the official Pine Script v6 Reference Manual. 🗺️ Overview: The Five-Stage Learning Path graph TD S1["Stage 1 Language Foundations & Execution Model"] --> S2["Stage 2 Type System & Built-in Series"] S2 --> S3["Stage 3 TA Functions & Plotting"] S3 --> S4["Stage 4 Arrays, UDTs & Drawing Objects"] S4 --> S5["Stage 5 Strategy Backtesting & request.security()"] style S1 fill:#4a90d9,color:#fff,stroke:#2c6fad style S2 fill:#5ba85a,color:#fff,stroke:#3d7a3c style S3 fill:#e8a838,col...

Pine Script Variable Declaration: Mastering var, varip, and Proper Initialization Across Historical Bars

Image
Variable declaration is the foundational building block of any Pine Script indicator or strategy. Understanding the precise behavioral differences between standard assignment, var , and varip — and knowing exactly when each value is initialized or updated across historical and real-time bars — is essential for writing correct, predictable, and performant Pine Script code. 1. The Pine Script Execution Model: Why Variable Scope Matters Pine Script executes your script once per bar, from the oldest historical bar to the most recent, and then continues on each new real-time tick. This bar-by-bar execution model is the core reason why variable declaration semantics matter so much. A variable declared without var is re-evaluated and re-initialized on every single bar execution. A variable declared with var persists its value across bars. Understanding this distinction prevents the most common logical errors in Pine Script development. gra...