ZigZagFibonacci
Download scriptThis script is designed to automate the identification of key market structure points and automatically plot a Fibonacci retracement tool based on the most recent price movements. Here is a breakdown of how the logic works:
Core Functionality
The primary goal of this script is to filter through "market noise" to find significant historical highs and lows (peaks and valleys). Once it identifies these major structural points, it automatically draws a Fibonacci retracement from the most recent completed swing.
Key Logic Steps
1. Identifying Structural Peaks and Valleys The script scans the price history over a specific depth (defined by the user) to find local highs and lows. It doesn't just pick every minor move; it uses three layers of filtering:
- Depth & Deviation: It looks for points that stand out significantly from the surrounding price action. If a price movement is too small, it is ignored as noise.
- Backstep Validation: Once a potential peak or valley is found, the script looks back at a few preceding bars to ensure this point is indeed a turning point and not just a minor fluctuation within a trend.
- Unique Point Filtering: It ensures that multiple points at the same price level aren't counted as separate significant events, keeping the data clean.
2. Dynamic Swing Tracking The script maintains a "memory" of the most recent high and low points. As new price data comes in:
- If a new high is found that is higher than the previous one, it updates the "Current High."
- If a lower low is formed, it updates the "Current Low."
- This creates a chain of structural pivots (similar to a ZigZag indicator) that defines the current trend's boundaries.
3. Automated Fibonacci Drawing This is the final action of the script. Every time the market completes a new structural pivot:
- The script identifies the two most recent significant points in the sequence.
- It automatically deletes the old drawing to keep the chart clean.
- It places a new Fibonacci retracement object on the chart, connecting the start and end of the latest major price swing.
User Benefits
Instead of a trader having to manually click on charts to find "where the trend started" or "where the last peak was," this script does the heavy lifting. It continuously monitors the market and repositions the Fibonacci tool in real-time as new structural highs and lows are confirmed, providing an automated way to visualize potential support and resistance levels.
ÿþ/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
/ / | f u k i n a g a s h i |
/ / | C o p y r i g h t © 2 0 0 0 - 2 0 0 7 , M e t a Q u o t e s S o f t w a r e C o r p . |
/ / | h t t p : / / w w w . m e t a q u o t e s . r u |
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
# p r o p e r t y i n d i c a t o r _ c h a r t _ w i n d o w
# p r o p e r t y i n d i c a t o r _ b u f f e r s 1
# p r o p e r t y i n d i c a t o r _ c o l o r 1 R e d
/ / - - - - i n d i c a t o r p a r a m e t e r s
e x t e r n i n t E x t D e p t h = 1 2 ;
e x t e r n i n t E x t D e v i a t i o n = 5 ;
e x t e r n i n t E x t B a c k s t e p = 3 ;
/ / - - - - i n d i c a t o r b u f f e r s
d o u b l e E x t M a p B u f f e r [ ] ;
d o u b l e E x t M a p B u f f e r 2 [ ] ;
i n t O l d L a s t Z i g Z a g , O l d P r e v i o u s Z i g Z a g ;
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
/ / | C u s t o m i n d i c a t o r i n i t i a l i z a t i o n f u n c t i o n |
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
i n t i n i t ( )
{
I n d i c a t o r B u f f e r s ( 2 ) ;
/ / - - - - d r a w i n g s e t t i n g s
S e t I n d e x S t y l e ( 0 , D R A W _ S E C T I O N ) ;
/ / - - - - i n d i c a t o r b u f f e r s m a p p i n g
S e t I n d e x B u f f e r ( 0 , E x t M a p B u f f e r ) ;
S e t I n d e x B u f f e r ( 1 , E x t M a p B u f f e r 2 ) ;
S e t I n d e x E m p t y V a l u e ( 0 , 0 . 0 ) ;
A r r a y S e t A s S e r i e s ( E x t M a p B u f f e r , t r u e ) ;
A r r a y S e t A s S e r i e s ( E x t M a p B u f f e r 2 , t r u e ) ;
/ / - - - - i n d i c a t o r s h o r t n a m e
I n d i c a t o r S h o r t N a m e ( "