GannSwingsVIII_v1
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform. Its primary purpose is to automatically identify and visualize "swings"—key price turning points—on a trading chart to help users understand the underlying trend of the market.
Core Logic Overview
The script scans historical price data to detect significant peaks (Highs) and valleys (Lows). Instead of just marking every high or low, it uses a systematic logic to determine when a price movement has fundamentally changed direction, allowing it to filter out minor market "noise."
Key Functional Components
1. Trend Identification The script maintains an internal memory of the current market trend. It differentiates between upward and downward movements by monitoring whether new highs or new lows are being formed. By tracking these consecutive price actions, the tool can distinguish between a temporary pullback and a sustained shift in market momentum.
2. Swing Point Detection
- Highs and Lows: The logic identifies specific points where the price hits a peak or a trough.
- Dynamic Updates: It features a mechanism to handle "equal" highs or lows. If the price remains stagnant at a certain level across multiple bars, the script can be configured to move the swing point to the most recent bar, ensuring the visual data stays current.
3. Visual Indicators The script populates the chart with several visual elements based on its calculations:
- Trend Lines/Sections: It draws sections that represent the flow of the market.
- Arrows: It places specific markers (arrows) to highlight key price extremes.
- Outside Bar Detection: The logic includes a specialized check for "Outside Bars" (where the current bar's high is higher and its low is lower than the previous bar). If detected, it calculates the midpoint of these bars to provide additional visual context on the chart.
4. Configuration Options The user can customize how the script behaves through several parameters:
- Kind: This allows the user to define the sensitivity or "depth" of the swing detection (e.g., whether it looks for immediate turns or more significant structural shifts).
- MoveLastSwing: A toggle that determines if the script should automatically update the visual marker when the market hits a new high or low that is equal to the previous one, preventing "cluttered" markers on flat price action.
Summary of Behavior
When you apply this script, it looks back at your price history and begins plotting the structural "skeleton" of the chart. It identifies where the trend is breaking (reversals) and where it is continuing (extensions), marking these critical levels with visual cues so traders can more easily see the current market structure at a glance.
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//-----
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Aqua
#property indicator_color5 HotPink
//----
extern int kind=1;// òèï òåíäåíöèè, 1- ìàëàÿ, 2 - ïðîìåæóòî÷íàÿ, 3 - îñíîâíàÿ
extern bool MoveLastSwing=false; // åñëè ðàâíî true, òî êîí÷èê õâîñòà áóäåò ñäâèãàòüñÿ áëèæå ê íóëåâîìó áàðó äëÿ äâóõ îäèíàêîâûõ áàðîâ ïî High èëè Low
//---- buffers
double SwingsBuffer[];
double HighsBuffer[];
double LowsBuffer[];
double TrendBuffer[];
double UpCloseOutSideBuffers[];
double DownCloseOutSideBuffers[];
double SwingHigh,SwingLow;
int prevSwing,LastSwing,myBars,lowCounter,highCounter;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(6);
SetIndexStyle(0,DRAW_SECTION);
SetIndexBuffer(0,SwingsBuffer);
SetIndexEmptyValue(0,0.0);
SetIndexBuffer(1,HighsBuffer);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,159);
SetIndexBuffer(2,LowsBuffer);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,159);
SetIndexBuffer(3,UpCloseOutSideBuffers);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,108);
SetIndexBuffer(4,DownCloseOutSideBuffers);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,108);
SetIndexBuffer(5,TrendBuffer);
for (int i=Bars-1; i>=0; i--)
{
SwingsBuffer[i]=0.0;
HighsBuffer[i]=0.0;
LowsBuffer[i]=0.0;
TrendBuffer[i]=0.0;
UpCloseOutSideBuffers[i]=0.0;
DownCloseOutSideBuffers[i]=0.0;
}
SwingHigh=0.0;
SwingLow=0.0;
prevSwing=0;
LastSwing=0.0;
myBars=0;
lowCounter=0;
highCounter=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| return shift of last swing |
//+------------------------------------------------------------------+
int GetLastSwing()
{
//----
int point=1;
while (SwingsBuffer[point]==0.0 && point<Bars) point++;
//----
return(point);
}
//+------------------------------------------------------------------+
//| Check prev swing |
//+------------------------------------------------------------------+
void SetPrevSwing()
{
//----
int LastPoint;
LastPoint=GetLastSwing();
SwingsBuffer[0]=0.0;
SwingHigh=High[LastPoint];
SwingLow=Low[LastPoint];
LastSwing=LastPoint;
//----
return;
}
//+------------------------------------------------------------------+
//| check Out side bar |
//+------------------------------------------------------------------+
bool isOutSideSwingBar(int shift)
{
bool res=false;
//----
res=((High[shift]>High[shift+1])&&(Low[shift]<Low[shift+1]));
if (res)
{
if (Close[shift]>Open[shift]) UpCloseOutSideBuffers[shift]=(High[shift]+Low[shift])/2.0;
else DownCloseOutSideBuffers[shift]=(High[shift]+Low[shift])/2.0;
}
//----
return(res);
}
//+------------------------------------------------------------------+
//| Check new low |
//+------------------------------------------------------------------+
void SetSwings(int shift)
{
//----
//----
return;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int limit;
bool nonOutSide;
if (counted_bars==0)
{
limit=Bars-1;
LowsBuffer[limit]=Low[limit];
SwingsBuffer[limit]=Low[limit];
SwingHigh=High[limit];
SwingLow=Low[limit];
LastSwing=limit;
TrendBuffer[limit]=-1.0;
}
if (counted_bars>0) limit=Bars-counted_bars;
if ( (counted_bars<0)|| ((counted_bars>0)&&(Bars-myBars>1)) ) init();
limit--;
//Print("Bars=",Bars," counted_bars=",counted_bars,);
//----
for (int cnt=limit;cnt>=0;cnt--)
{
if (limit<3)Print("cnt=",cnt," myBars=",myBars," LastSwing=",LastSwing);
//if (cnt==0) SetPrevSwing();
nonOutSide=!isOutSideSwingBar(cnt);
if (High[cnt]>SwingHigh) {highCounter++;SwingHigh=High[cnt];HighsBuffer[cnt]=SwingHigh;}
if (Low[cnt]<SwingLow) {lowCounter++;SwingLow=Low[cnt];LowsBuffer[cnt]=SwingLow;}
//isOutSideSwingBar(cnt);
TrendBuffer[cnt]=TrendBuffer[cnt+1];// åñëè íå áóäóò èçìåíåíèé - òðåíä ñîõðàíèòñÿ
if ((TrendBuffer[cnt+1]==-1.0) && (lowCounter==1))// && nonOutSide)
{
//if (LastSwing=!cnt)
if (LastSwing!=Bars-1) SwingsBuffer[LastSwing]=0.0; // çàòðåì ïðåäûäóùèé ñâèíã
LastSwing=cnt;
SwingHigh=High[cnt];
SwingLow=Low[cnt];
//TrendBuffer[cnt]=-1.0;
highCounter=0;
lowCounter=0;
SwingsBuffer[cnt]=Low[cnt];
HighsBuffer[cnt]=SwingHigh;
//Print("ïðîäîëæåíèå äàóíòðåíäà");
}
else
{
if (TrendBuffer[cnt+1]==-1.0 &&highCounter==kind)
{
//if (LastSwing=!cnt)
LastSwing=cnt;
SwingHigh=High[cnt];
SwingLow=Low[cnt];
TrendBuffer[cnt]=1.0;
highCounter=0;
lowCounter=0;
SwingsBuffer[cnt]=High[cnt];
LowsBuffer[cnt]=SwingLow;
//Print("ðàçâîðîò äàóíòðåíäà");
}
else
{
if (TrendBuffer[cnt+1]==-1.0 && Low[cnt]==SwingLow && MoveLastSwing)
{
SwingsBuffer[LastSwing]=0.0;
LastSwing=cnt;
/* SwingLow=Low[cnt];
SwingHigh=High[cnt];
highCounter=0;
lowCounter=0;
*/ SwingsBuffer[cnt]=Low[cnt];
}
}
}
if ((TrendBuffer[cnt+1]==1.0) && (highCounter==1))// && nonOutSide)
{
//if (LastSwing=!cnt)
if (LastSwing!=Bars-1) SwingsBuffer[LastSwing]=0.0; // çàòðåì ïðåäûäóùèé ñâèíã
LastSwing=cnt;
SwingHigh=High[cnt];
SwingLow=Low[cnt];
//TrendBuffer[cnt]=1.0;
highCounter=0;
lowCounter=0;
SwingsBuffer[cnt]=High[cnt];
LowsBuffer[cnt]=SwingLow;
//Print("ïðîäîëæåíèå àïòðåíäà");
}
else
{
if (TrendBuffer[cnt+1]==1.0 &&lowCounter==kind)
{
//if (LastSwing=!cnt)
LastSwing=cnt;
SwingHigh=High[cnt];
SwingLow=Low[cnt];
TrendBuffer[cnt]=-1.0;
highCounter=0;
lowCounter=0;
SwingsBuffer[cnt]=Low[cnt];
HighsBuffer[cnt]=SwingHigh;
//Print("ðàçâîðîò àïòðåíäà");
}
else
{
if (TrendBuffer[cnt+1]==1.0 && High[cnt]==SwingHigh && MoveLastSwing)
{
SwingsBuffer[LastSwing]=0.0;
LastSwing=cnt;
/* SwingLow=Low[cnt];
SwingHigh=High[cnt];
highCounter=0;
lowCounter=0;
*/ SwingsBuffer[cnt]=High[cnt];
}
}
}
}
myBars=Bars;
//----
return(0);
}
//+------------------------------------------------------------------+
Comments