flag_and_pennant
Download scriptThis script is a specialized technical analysis tool designed to identify specific geometric price patterns—specifically Flags and Pennants—on a trading chart. It automates the process of detecting these formations by analyzing price trends, slopes, and historical price ranges.
Core Operational Logic
The logic functions by breaking down market movement into three primary layers:
1. Trend and Slope Analysis The tool first calculates the "health" and direction of the current market trend. It uses a mathematical technique called linear regression to determine the slope of the price over a specific period. By doing this, it can distinguish between a steady trend, a sideways consolidation, and an accelerating movement. It establishes a "Median Buffer," which acts as a baseline for these calculations.
2. Structural Pattern Detection Once the general trend is understood, the script looks for two specific structural behaviors:
- The Flag: This is identified by looking for a period of consolidation (a small range of movement) that occurs immediately following a strong directional move. The logic checks if the price has "rested" within a tight enough boundary to qualify as a flag.
- The Pennant: This is identified by a symmetrical contraction in price. The script looks for a specific "narrowing" effect where the high and low points of the price movement are converging toward a center point, creating the triangular shape characteristic of a pennant.
3. Pattern Projection (Drawing Logic) When the script successfully identifies either a Flag or a Pennant, it doesn't just mark the point of origin; it projects the expected future path of that pattern based on the calculated angles:
- Extension Lines: It draws lines representing the "upper" and "lower" boundaries of the pattern.
- Proportional Scaling: The tool calculates how far the price moved during the initial breakout and applies those same proportions to the projected "extension" area. This helps visual learners see where the price might realistically head if the current pattern completes.
Summary of Visual Output
For the user, this script transforms raw price data into a structured visual map:
- Blue Lines: Typically represent the boundaries of a Flag formation.
- Green Lines: Typically represent the boundaries of a Pennant formation.
- Regression Line: A gray line (or similar) that provides the underlying mathematical trend of the current price action.
In essence, the script acts as an automated "chart pattern hunter," filtering out the noise of the market to highlight specific zones where traders often look for breakout opportunities.
ÿþ/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
/ / | F l a g _ a n d _ P e n n a n t . m q 4 |
/ / | F l a g _ a n d _ P e n n a n t C o p y r i g h t 2 0 1 5 , f x b o r g |
/ / | h t t p : / / f x b o r g - l a b o . h a t e b l o . j p / |
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
# p r o p e r t y c o p y r i g h t "