Posts

Showing posts with the label EMA crossover

Pine Script 'if' Statements: A Complete Guide to Conditional Logic for Buy Signals and Color Changes

Image
Conditional logic is the backbone of any algorithmic trading script — without it, indicators and strategies cannot respond dynamically to market conditions. In Pine Script v6, the if statement provides a structured, deterministic mechanism to evaluate boolean expressions and execute specific branches of code, enabling everything from simple color changes to complex multi-condition buy/sell signal generation. 1. The Anatomy of an if Statement in Pine Script At its core, an if statement evaluates a boolean condition and executes a block of code only when that condition resolves to true . The general syntax follows this structure: //@version=6 // Basic if-else structure (conceptual) // if condition // // executed when condition is true // else // // executed when condition is false Pine Script uses indentation-based block scoping . Any statements belonging to an if branch must be consistently indented relative to the if key...

Pine Script v6: indicator() vs strategy() — Core Functional Differences Explained

Image
In Pine Script v6, every script must declare its type using either indicator() or strategy() as its first executable statement. Understanding the precise functional boundary between these two declarations is essential for building reliable tools — one is designed purely for visual analysis, while the other drives a full backtesting and order-execution engine. 1. Architectural Overview At the compiler level, indicator() and strategy() are mutually exclusive script-type declarations. They share the same bar-by-bar execution model but expose entirely different namespaces and capabilities. The table below summarizes the top-level differences: Feature indicator() strategy() Primary Purpose Visual overlays & signal drawing Backtesting & order simulation Order Functions ❌ Not available ✅ strategy.entry() , strategy.exit() , strategy.close() ...