//This swing is NOT based on percentage OR logarithmic changes of the Price.
//Swing segments are triggered by the current Price (Close) > OR < than an average Price (Close) for a specified Period.
//Additionally, a minimum number of bars can be set to make a swing segment valid.
//Triggers can be turned on/ off (small triangles above High OR Low).
//Swing Pivots can be annotated (on/off) with: change in value, % change, AND length of the swing segment in bars.
//Swing color is NOT based on segments.
//If the resistance is taken out (previous Peak) the Trend is up. If the support is taken out (previous Trough) the Trend is down.
RequestTimedRefresh(1);
Len=Param("Period Length ?",13,1,100);
Min_Bars=Param("Minimum No of Bars in a Swing Segment ?",3,1,10,1);
Up_Color = ParamColor("Trend Up Color ?",colorBrightGreen);
Dn_Color = ParamColor("Trend Dn Color",colorRed);
Style = ParamStyle("Up Style",styleNoLabel|styleThick);
Annotate = ParamToggle("Annotate Pivots ?", "No|Yes",defaultval=1 );
Anno_color = ParamColor("Annotation Color ?", colorBlue);
Top_Pct=Param("Start Top Annotation as % of Y axis",5,1,100,1);
Bottom_Pct=Param("Start Bottom Annotation as % of Y axis",5,1,100,1);
Triggers = ParamToggle("Mark Swing Pivot Trigger Bars ?", "No|Yes",defaultval=1 );
Trigger_color = ParamColor("Trigger Color ?", colorYellow);
Min_Y = Status("axisminy");
Max_Y = Status("axismaxy");
Top_start = Max_Y - (Max_Y - Min_Y) * Top_Pct * 0.01;
Bottom_Start = Min_Y + (Max_Y - Min_Y) * Bottom_Pct * 0.01;
Dir = MA(Close,Len);
First_Visible_Bar =Status("FirstVisibleBar");
Last_Visible_Bar = Status("LastVisibleBar");
Pivot_No = 0;
Direction = 0;
Trend = 0;
Current_running_pivot_Bar = 0;
Current_running_pivot_Val = Close[0];
Last_Top_Val = H[0];
Last_Top_Bar = 0;
Last_Bottom_Bar = 0;
Last_Bottom_Val = L[0];
Swing_Array = Null;
Trend_Array = 1;
Trigger_Dn = 0;
Trigger_Up = 0;
for (barnum = Len+1; barnum < BarCount; barnum++)
{
if (Direction >= 0 )
{
if (H[barnum] > H[Current_running_pivot_Bar] AND Direction > 0)
{
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = H[barnum];
}
if (Close[barnum] < Dir[barnum-1] AND (barnum - Current_running_pivot_Bar) >= Min_Bars)
{
if (direction == 0)
{
Direction = -1;
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = Low[barnum];
}
else
{
Direction = -1;
Pivot_No++;
Prev_Top_Val = Last_Top_Val;
Prev_Top_Bar = Last_Top_Bar;
y1 = Last_Top_Val = Current_running_pivot_Val;
x1 = Last_Top_Bar = Current_running_pivot_Bar;
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = Low[barnum];
y0 = Last_Bottom_Val;
x0 = Last_Bottom_Bar;
a = (y1 - y0) / (x1 - x0);
b = (y0 * x1 - y1 * x0) / (x1 - x0);
Trigger_Dn[barnum] = 1;
if (barnum >= First_Visible_Bar AND barnum <= Last_Visible_Bar AND Annotate)
{
no_of_bars = x1 - x0;
delta_val = y1 - y0;
delta_pct = 100 * delta_val / y0;
PlotText(NumToStr(delta_val,1.2) + "\n" + NumToStr(delta_pct,1.1) + "%\n"+NumToStr(no_of_bars,1.0) +"b",x1,Top_start,Anno_color,bkcolor = colorDefault);
}
for (j = x0;j <= x1;j++)
{
Swing_Array[j]= a * j + b;
if (Pivot_No == 1)
Trend = 1;
else
if (Swing_Array[j] > Prev_Top_Val)
Trend = 1;
Trend_Array[j] = Trend;
}
}
}
}
if (Direction <= 0)
{
if (L[barnum] < L[Current_running_pivot_bar] AND direction < 0)
{
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = L[barnum];
}
if (Close[barnum] > Dir[barnum-1] AND (barnum - Current_running_pivot_Bar) >= Min_Bars)
{
if (direction == 0)
{
Direction = 1;
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = High[barnum];
}
else
{
Pivot_No++;
Direction = 1;
Prev_Bottom_Val = Last_Bottom_Val;
y1 = Last_Bottom_Val = Current_running_pivot_Val;
x1 = Last_Bottom_Bar = Current_running_pivot_Bar;
Current_running_pivot_Bar = barnum;
Current_running_pivot_Val = High[barnum];
y0 = Last_Top_Val;
x0 = Last_Top_Bar;
a = (y1 - y0) / (x1 - x0);
b = (y0 * x1 - y1 * x0) / (x1 - x0);
Trigger_Up[barnum] = 1;
if (barnum >= First_Visible_Bar AND barnum <= Last_Visible_Bar AND Annotate)
{
no_of_bars = x1 - x0;
delta_val = y1 - y0;
delta_pct = 100 * delta_val / y0;
PlotText(NumToStr(delta_val,1.2) + "\n" + NumToStr(delta_pct,1.1) + "%\n"+NumToStr(no_of_bars,1.0) +"b",x1,Bottom_start,Anno_color,bkcolor = colorDefault);
}
for (j = x0;j <= x1;j++)
{
Swing_Array[j]= a * j + b;
if (Pivot_No == 1)
Trend = -1;
else
if (Swing_Array[j] < Prev_Bottom_Val)
Trend = -1;
Trend_Array[j] = Trend;
}
}
}
}
}
if (Direction == 1 AND Current_running_pivot_Bar > Last_Bottom_Bar)
{
x0 = Last_Bottom_Bar;
y0 = Last_Bottom_Val;
x1 = Current_running_pivot_Bar;
y1 = Current_running_pivot_Val;
Prev_Top_Val = Last_Top_Val;
a = (y1 - y0) / (x1 - x0);
b = (y0 * x1 - y1 * x0) / (x1 - x0);
if (barnum >= First_Visible_Bar AND barnum <= Last_Visible_Bar AND Annotate)
{
no_of_bars = x1 - x0;
delta_val = y1 - y0;
delta_pct = 100 * delta_val / y0;
PlotText(NumToStr(delta_val,1.2) + "\n" + NumToStr(delta_pct,1.1) + "%\n"+NumToStr(no_of_bars,1.0) +"b",x1,Top_start,Anno_color,bkcolor = colorDefault);
}
for (j = x0;j <= x1;j++)
{
Swing_Array[j]= a * j + b;
if (Pivot_No == 0)
Trend = 1;
else
if (Swing_Array[j] > Prev_Top_Val)
Trend = 1;
Trend_Array[j] = Trend;
}
}
if (direction == -1 AND Current_running_pivot_Bar > Last_Top_Bar )
{
x0 = Last_Top_Bar;
y0 = Last_Top_Val;
x1 = Current_running_pivot_Bar;
y1 = Current_running_pivot_Val;
Prev_Bottom_Val = Last_Bottom_Val;
a = (y1 - y0) / (x1 - x0);
b = (y0 * x1 - y1 * x0) / (x1 - x0);
if (barnum >= First_Visible_Bar AND barnum <= Last_Visible_Bar AND Annotate)
{
no_of_bars = x1 - x0;
delta_val = y1 - y0;
delta_pct = 100 * delta_val / y0;
PlotText(NumToStr(delta_val,1.2) + "\n" + NumToStr(delta_pct,1.1) + "%\n"+NumToStr(no_of_bars,1.0) +"b",x1,Bottom_start,Anno_color,bkcolor = colorDefault);
}
for (j = x0;j <= x1;j++)
{
Swing_Array[j]= a * j + b;
if (Pivot_No == 0)
Trend = -1;
else
if (Swing_Array[j] < Prev_Bottom_Val)
Trend = -1;
Trend_Array[j] = Trend;
}
}
Color = IIf(Trend_Array == 1,Up_Color,Dn_Color);
Plot(Swing_Array,"",Color,Style);
if (Triggers)
{
PlotShapes(shapeSmallDownTriangle * Trigger_Dn,Trigger_Color,0,High);
PlotShapes(shapeSmallUpTriangle * Trigger_Up,Trigger_Color,0,Low);
}