Support Resistance

Hi everyone.
Please could someone help me? I'm trying to modify some support and resistance code form the great @empottasch .
I would like to add a condition to the sup/res lines! Example : iif( displaylines - C < (a percentage), displaylines, null);
Basically I would like to only plot these lines when price is within the percentage range, of the support or resistance line? This is the clean up the chart and not have irrelevant lines everywhere. I have tried everything I can think of, but with no success. I even tried to modify, the "alternate" mode to display this in Line 153.
Anyway, I would love some help, much appreciated!

// AFL code by Edward Pottasch, Jan 2013
procedure alternate1_proc(pk,tr,px0,px1,px2,ph0,ph1,ph2,tx0,tx1,tx2,tl0,tl1,tl2)
{
	global pkh;
	global trh;
	
	pk=IIf((px0==px1 AND px2>tx1 AND ph1<ph2) 
	OR (tx0==tx1 AND px1<px0 AND px1>tx0 AND ph1<=ph0)
	OR (px0>px1 AND px1<tx0 AND px2>tx1 AND ph1<ph2)
	OR (px0>px1 AND px0<tx0 AND px1>tx1 AND ph1<ph0)
	OR (ph1==ph2 AND px2>tx1)
	OR (px1==tx1 AND px2>tx2),False,pk);

	tr=IIf((tx0==tx1 AND tx2>px1 AND tl1>tl2) 
	OR (px0==px1 AND tx1<tx0 AND tx1>px0 AND tl1>=tl0)
	OR (tx0>tx1 AND tx1<px0 AND tx2>px1 AND tl1>tl2)
	OR (tx0>tx1 AND tx0<px0 AND tx1>px1 AND tl1>tl0)
	OR (tl1==tl2 AND tx2>px1)
	OR (px1==tx1 AND tx2>px2),False,tr);
	
	pkh=pk;trh=tr;
}
//procedure calculatePivots(tfrm,PivotSymmetry,nbar,CleanPivots,x)
procedure calculatePivots(tfrm,nbar,x)

{
TimeFrameSet(tfrm);

global tr;global pk;global px0;global px1;global px2;global ph0;global ph1;global ph2;
global trl;global pkh;global tx0;global tx1;global tx2;global tl0;global tl1;global tl2;
global fact;
//if(PivotSymmetry)
{
	fc=1;
	pk=H>Ref(HHV(H,nbar*fc),-1) AND Ref(HHV(H,nbar),nbar)<=H;
	tr=L<Ref(LLV(L,nbar*fc),-1) AND Ref(LLV(L,nbar),nbar)>=L;
}
//else
{
	fc=2;
	pk=H>Ref(HHV(H,nbar*fc),-1) AND Ref(HHV(H,nbar),nbar)<=H;
	tr=L<Ref(LLV(L,nbar*fc),-1) AND Ref(LLV(L,nbar),nbar)>=L;
}

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

//if(CleanPivots)
{
	alternate1_proc(pk,tr,px0,px1,px2,ph0,ph1,ph2,tx0,tx1,tx2,tl0,tl1,tl2);
	pk=pkh;tr=trh;
	
	px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
	px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
	px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
	ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
	ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
	ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
	
	alternate1_proc(pk,tr,px0,px1,px2,ph0,ph1,ph2,tx0,tx1,tx2,tl0,tl1,tl2);
	pk=pkh;tr=trh;
	
	px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
	px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
	px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
	ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
	ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
	ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
}

pkh=IIf(pk,H,Null);
trl=IIf(tr,L,Null);
TimeFrameRestore();
fact=Nz(Max(tfrm/60,Interval()/60)/(Interval()/60));
if(fact==0)fact=1;
Lkbk=Nz(tfrm/Interval());
if(Lkbk>1)
{
	pk=TimeFrameExpand(pk,tfrm,expandFirst);
	pkh=TimeFrameExpand(pkh,tfrm,expandFirst);
	pkhs=IIf(!IsEmpty(pkh),1,0);pkhs=pkhs-Ref(pkhs,-1);
	pk=pk AND H==pkh;
	cond1=Sum(pk,BarsSince(pkhs==1)+1)==1 AND pk;
	pk=pk AND cond1;
	
	tr=TimeFrameExpand(tr,tfrm,expandFirst);	
	trl=TimeFrameExpand(trl,tfrm,expandFirst);
	trls=IIf(!IsEmpty(trl),1,0);trls=trls-Ref(trls,-1);
	tr=tr AND L==trl;
	cond1=Sum(tr,BarsSince(trls==1)+1)==1 AND tr;
	tr=tr AND cond1;
	
	px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
	px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
	px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
	px3=ValueWhen(pk,x,3); tx3=ValueWhen(tr,x,3);
	ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
	ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
	ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
	ph3=ValueWhen(pk,H,3); tl3=ValueWhen(tr,L,3);
}
}
function GetVisibleBarCount() 
{
	lvb=Status("lastvisiblebar");
	fvb=Status("firstvisiblebar");
	return Min(lvb-fvb,BarCount-fvb);
} 
function GfxConvertBarToPixelX(bar) 
{ 
	lvb=Status("lastvisiblebar"); 
	fvb=Status("firstvisiblebar"); 
	pxchartleft=Status("pxchartleft"); 
	pxchartwidth=Status("pxchartwidth"); 
	return Nz(pxchartleft+bar*pxchartwidth/(lvb-fvb+1)); 
} 
function GfxConvertValueToPixelY(value) 
{
	local Miny,Maxy,pxchartbottom,pxchartheight; 
	Miny=Status("axisminy");
	Maxy=Status("axismaxy");
	pxchartbottom=Status("pxchartbottom");
	pxchartheight=Status("pxchartheight");
	return Nz(pxchartbottom-floor(0.5+(Value-Miny)*pxchartheight/(Maxy-Miny)));
} 
function GfxConvertValueToPixelYlog(Value)
{
	local Miny,Maxy,pxchartbottom,pxchartheight;
	pxchartheight=Status("pxchartheight");
	pxchartbottom=Status("pxchartbottom");
	Miny=Status("axisminy");
	Maxy=Status("axismaxy");
	x=log(Value/Miny)/log(Maxy/Miny);
	return Nz(pxchartbottom-round(pxchartheight*x));
}
function displayLines(pk,tr,bkColorRes,bkColorSup,lnColorRes,lnColorSup,lnWidthRes,lnWidthSup,displayMode,logscale)
{
AllVisibleBars=GetVisibleBarCount();
fvb=Status("firstvisiblebar");
for(i=0;i<AllVisibleBars;i++) 
{
	DisplayRes=1;
	DisplaySup=1;
	lvix=i+fvb;
	if(pk[lvix])
	{
		if(displayMode)
		{
			hval=H[lvix];
			for(j=i;j<AllVisibleBars;j++)
			{
				lvjx=j+fvb;
				if(H[lvjx]>hval)
				{
					DisplayRes=0;
					break;
				}
			}
		}
		if(DisplayRes)
		{
			if(logscale) vh=GfxConvertValueToPixelYLog(H[lvix]); 
			else vh=GfxConvertValueToPixelY(H[lvix]);
			GfxSelectPen(lnColorRes,lnWidthRes,0);
			GfxMoveTo(GfxConvertBarToPixelX(i),vh);
			GfxLineTo(GfxConvertBarToPixelX(AllVisibleBars-1),vh);
			GfxSelectFont("Tahoma",8,650);GfxSetBkColor(bkColorRes);GfxSetTextColor(colorWhite);
			GfxTextOut(""+H[lvix],GfxConvertBarToPixelX(AllVisibleBars+1),vh);			
		}
	}
	if(tr[lvix])
	{
		if(displayMode)
		{
			lval=L[lvix];
			for(j=i;j<AllVisibleBars;j++)
			{
				lvjx=j+fvb;
				if(L[lvjx]<lval)
				{
					DisplaySup=0;
					break;
				}
			}
		}	
		if(DisplaySup)
		{	
			if(logscale) vl=GfxConvertValueToPixelYLog(L[lvix]); 
			else vl=GfxConvertValueToPixelY(L[lvix]);		
			GfxSelectPen(lnColorSup,lnWidthSup,0);
			GfxMoveTo(GfxConvertBarToPixelX(i),vl);
			GfxLineTo(GfxConvertBarToPixelX(AllVisibleBars-1),vl);
			GfxSelectFont("Tahoma",8,650);GfxSetBkColor(bkColorSup);GfxSetTextColor(colorWhite);
			GfxTextOut(""+L[lvix],GfxConvertBarToPixelX(AllVisibleBars+1),vl);
		}
	}	
}
}
function plotPivots(pkh,trl,pk,tr,Lx,nbar,fact,x)
{
	Plot(pkh,"",colorRed,styleThick,0,0,0,-1);
	Plot(trl,"",colorBrightGreen,styleThick,0,0,0,-1);
	PlotShapes(shapeSmallCircle*tr,IIf(Lx-ValueWhen(tr,x)>nbar*fact,colorBrightGreen,colorWhite),0,L,-10);
	PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>nbar*fact,colorRed,colorWhite),0,H,10);
}

_SECTION_BEGIN("Background_Setting");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),
ParamColor("BgBottom", colorDarkGrey),ParamColor("TitleBack",colorGrey40)); 
SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
SetChartOptions(0,chartShowArrows|chartShowDates);
_SECTION_END();

_SECTION_BEGIN("Candle color");

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"Close",IIf(C > Ref(C,-1),ParamColor("Wick UP Color", colorDarkGreen),IIf(C < Ref(C,-1),ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

grid_year = IIf(Year()!=Ref(Year(),-1),1,0); 
Plot(grid_year,"",colorDarkGrey,styleHistogram|styleDots|styleNoLabel|styleOwnScale);
_SECTION_END();

// MAIN code
RequestTimedRefresh(0.1,True); 
xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",5,2,50,1); 
CleanPivots=ParamToggle("Use Clean Pivots","Off|On",1);
PivotSymmetry=ParamToggle("Use Symmetric Pivots","Off|On",0);
displayMode=ParamToggle("Display Mode","Classic|Alternative",1);
logscale=ParamToggle("Axis type","Linear|Logarithmic",0);
weeklyTF=ParamToggle("WEEKLY timeframe","Off|On",1);
weeklyLineColorRes=ParamColor("WEEKLY Line Color (resistance)",ColorRGB(150,0,0)); 
weeklyLineColorSup=ParamColor("WEEKLY Line Color (support)",ColorRGB(0,0,150));
weeklyBackColorRes=ParamColor("WEEKLY Back Color (resistance)",ColorRGB(250,0,0)); 
weeklyBackColorSup=ParamColor("WEEKLY Back Color (support)",ColorRGB(0,0,250));
weeklyLineWidth=Param("WEEKLY Line Width",6,1,10,1); 

dailyTF=ParamToggle("DAILY timeframe","Off|On",1);
dailyLineColorRes=ParamColor("DAILY Line Color (resistance)",ColorRGB(50,0,0)); 
dailyLineColorSup=ParamColor("DAILY Line Color (support)",ColorRGB(0,0,50));
dailyBackColorRes=ParamColor("DAILY Back Color (resistance)",ColorRGB(250,0,0)); 
dailyBackColorSup=ParamColor("DAILY Back Color (support)",ColorRGB(0,0,250));
dailyLineWidth=Param("DAILY Line Width",2,1,10,1); 

if(logscale) SetChartOptions(2,chartLogarithmic,chartGridMiddle|chartGridMargins); 
else SetChartOptions(3,chartLogarithmic,!(chartGridMiddle|chartGridMargins));  

GraphXSpace=5;

// setup for weekly
if(weeklyTF)
{
calculatePivots(inWeekly,nbar,x);
displayLines(pk,tr,weeklyBackColorRes,weeklyBackColorSup,weeklyLineColorRes,weeklyLineColorSup,weeklyLineWidth,weeklyLineWidth,displayMode,logscale);
plotPivots(pkh,trl,pk,tr,Lx,nbar,fact,x);
}
// setup for daily
if(dailyTF)
{
calculatePivots(inDaily,nbar,x);
displayLines(pk,tr,dailyBackColorRes,dailyBackColorSup,dailyLineColorRes,dailyLineColorSup,dailyLineWidth,dailyLineWidth,displayMode,logscale);
plotPivots(pkh,trl,pk,tr,Lx,nbar,fact,x);
}

Title = Name() +  "  -  " + Date() + "  -  Close = " + WriteVal(C,0.2);

yikes :astonished: Kevin that is old code I like to forget about :smiley:

we talked last year and you have the ATR pivot code. Also if you want to use the fractal pivots maybe look here:

1 Like

Hahaha. Thanks Ed, I know its old, but its still great!!! I'll look into the fractal pivots. Thanks for the reply.

what you want is pretty easy to do. I will make an example in the weekend

hi Kevin,

here is an example of what I think you are looking for. In the param window you can change the ATR multiple that makes the zone broader or more narrow. I used ATR instead of percentage because ATR can be used in all timeframes with the same settings.

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
lineswitch = ParamToggle( "Show Line", "Off|On", 1 );
per = Param( "ATR Period", 50, 1, 200, 1 );
mult = Param( "ATR Multiple", 0.25, 0.1, 5, 0.05 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
atrarray = ATR( per ) * mult;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), ColorRGB( 0, 255, 0 ), 0, L, -10 );

if( lineswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        lasth = C[lvb];
        lastl = C[lvb];

        if( pk[i] AND( H[i] < lasth + atrarray[ lvb ] AND  H[i] > lasth - atrarray[ lvb ] ) )
        {
            line = LineArray( i, H[i], lvb, H[i] );
            Plot( line, "", colorRed, styleLine, Null, Null, 0 , 0, 1 );
            y1 = IIf( bi >= i && bi <= lvb, H[i] + atrarray[ lvb ], Null );
            y0 = IIf( bi >= i && bi <= lvb, H[i] - atrarray[ lvb ], Null );
            PlotOHLC( y1, y1, y0, y0, "", ColorRGB( 30, 0, 0 ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, 0, -1, 1 );
        }

        if( tr[i] AND( L[i] < lastl + atrarray[ lvb ] AND  L[i] > lastl - atrarray[ lvb ] ) )
        {
            line = LineArray( i, L[i], lvb, L[i] );
            Plot( line, "", colorBrightGreen, styleLine, Null, Null, 0 , 0, 1 );
            y1 = IIf( bi >= i && bi <= lvb, L[i] + atrarray[ lvb ], Null );
            y0 = IIf( bi >= i && bi <= lvb, L[i] - atrarray[ lvb ], Null );
            PlotOHLC( y1, y1, y0, y0, "", ColorRGB( 0, 30, 0 ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, 0, -1, 1 );
        }
    }
}
3 Likes

Hi Ed,
Thanks very much for your effort there, really appreciated!!! This is what was trying to achieve with your support resistance code. I was trying to add a conditional statement to the "displaylines". I like this code as it shows the strength of the lines in "weekly" "daily" etc etc.
Here is an example of some code i was able to do it, but your code is much better, as it will show all lines using the "AllVisibleBars" method.
Pic 1 . All lines
all lines
pics 2 . Only shows where price is with 0.2 (20 pips) from a previous support/resistance line
supp res
Here is that code.

SRswitch = ParamToggle("Sup / Res Lines","On,Off");
CHLswitch = ParamToggle("Hi Low / Close","Hi Low,Close");
NoLines = Param("No of Lines",5,1,1000,1);
Sen = Param("Sensitivity",5,1,100,1);
SUPRES = Param("SUP|RES vs PX",0.2,0.00,2,0.01);

y=0;
x=0;
for( i = 1; i < NoLines+1 ; i++ )
{
Y[i]=LastValue(Peak(IIf(CHLswitch,C,H),Sen,i));
x[i]=BarCount - 1 - LastValue(PeakBars(IIf(CHLswitch,C,H),Sen,i));
Line1 = LineArray( x[i], y[i], Null, y[i], 1 );

Peakss1    = IIf(Line1-Avg <SUPRES AND Line1-Avg > -SUPRES,Line1,Null);

Y[i]=LastValue(Trough(IIf(CHLswitch,C,L),Sen,i));
x[i]=BarCount - 1 - LastValue(TroughBars(IIf(CHLswitch,C,L),Sen,i));
Line2 = LineArray( x[i], y[i], Null, y[i], 1 );

troughss1= IIf(Line2-Avg <SUPRES AND Line2-Avg > -SUPRES,Line2,Null);

if ( ParamToggle( "SUP|RES", "OFF|ON", 0 ) )
{
Plot( IIf(SRswitch,Null,Line2), "", colorYellow,0,0,0,0,0,3  );
Plot( IIf(SRswitch,Null,Line1), "", colorYellow,0,0,0,0,0,3 );
Plot(troughss1,"",colorDarkRed,styleLine,0,0,0,2,5);
Plot(Peakss1,"",colorDarkRed,styleLine,0,0,0,2,5);
}
}

As I said, I was just trying to do the same with your code on the "displaylines" ?

ok good. If you use fractal pivots I would not bother with trying to make them "alternate" or "clean'. There are other methods to do that like ATR pivots, Gann pivots, or just the Percentage pivots that is built inside Amibroker.

Looks like Gold and Silver are at resistance now. I hope they push higher now finally :smiley:
gc
si

Is there a way I can modify your "alternate" mode in your "old" support resistance code, to only plot when price is near a previous support resistance line? I have tried everything.
Where am I going wrong here? from line 153.... I have tried to add this condition, but cannot crack this one....

function displayLines(pk,tr,bkColorRes,bkColorSup,lnColorRes,lnColorSup,lnWidthRes,lnWidthSup,displayMode,logscale)
{
AllVisibleBars=GetVisibleBarCount();
fvb=Status("firstvisiblebar");
for(i=0;i<AllVisibleBars;i++) 
{
	DisplayRes=1;
	DisplaySup=1;
	lvix=i+fvb;
	if(pk[lvix])
	{
		if(displayMode)
		{
			hval=vh=GfxConvertValueToPixelY(H[lvix]);//H[lvix];
			for(j=i;j<AllVisibleBars;j++)
			{
				lvjx=j+fvb;
				if(hval-Avg[lvix] < 0.1 AND hval-Avg[lvix] > - 0.1)
				//if(H[lvjx]>hval)
				{
					DisplayRes=0;
					break;
				}
			}
		}

i do not understand. The code I posted does not do what you want? That pivot code you use is ancient. Not only because I wrote that like a decade ago but Amibroker now does not need that complicated code any more since it has additional functions like Setcoordsmode().

So I suggest you simplify your code and try again

Ahh I see. Okay no problem. I'll look into the gfx formulas. Thanks Ed.

Apologies Ed, I see what your code is doing now. Its fantastic actually! Thanks very much!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.