Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
Find Out More Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

S/R type code for Amibroker (AFL)

Rating:
3 / 5 (Votes 6)
Tags:
amibroker, support, resistance

code written while answering a question on Amibroker Yahoo board. Posted here mainly to exchange AFL coding skills and not sure if it will make you any money. Code has 2 modes of displaying S/R levels based on fractal pivots. Further multiple time frames can be used, meaning when you display 1 hour data you can set the time frame in the parameter window to e.g. 120 min so the pivots are calculated using the 2 hour time frame. Play with the parameter window a bit to see what it does.

Screenshots

Similar Indicators / Formulas

Intraday Trades
Submitted by vishalsbharati almost 6 years ago
price support resistance
Submitted by icemanc over 6 years ago
Support & Resistance
Submitted by shyam242 over 6 years ago
Automatic Support and Resistance with Channel
Submitted by kaiji almost 8 years ago
Support/Resistance Modified
Submitted by dkeleher almost 7 years ago

Indicator / Formula

Copy & Paste Friendly
// AFL code by Edward Pottasch, Jan 2013
// Oz weekly pivots: http://i1145.photobucket.com/albums/o515/Austtrade/SupRes_zpsbebc3ab9.png
// Added toggle to log scale for Y-axis

RequestTimedRefresh(0.1,True); 
xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",5,2,50,1); 
tf=Param("Time Frame (min)",inWeekly/60,1,inWeekly/60,1);tfrm=in1Minute*tf;
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);

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

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;
}

TimeFrameSet(tfrm); 
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);
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);

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);
	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);	
	
	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);
	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);	
}
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);
}

GraphXSpace=5;SetChartBkColor(ColorRGB(0,0,0));
SetBarFillColor(IIf(C>O,colorGreen,IIf(C<=O,colorRed,colorLightGrey)));
Plot(C,"Price",IIf(C>O,colorDarkGreen,IIf(C<=O,colorDarkRed,colorLightGrey)),64,0,0,0,0);
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,ColorRGB(0,100,0),colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>nbar*fact,ColorRGB(255,0,0),colorWhite),0,H,10);

qq=Interval()/60;
if(qq < 60){tf=" min";tt=qq;}
else if(qq >= 60 AND qq < 1440){tf=" hrs";tt=qq/60;}
else if(qq >= 1440){tf=" days";tt=(qq/60)/24;}
qq=Max(tfrm/60,Interval()/60);
if(qq < 60){tfa=" min";tta=qq;}
else if(qq >= 60 AND qq < 1440){tfa=" hrs";tta=qq/60;}
else if(qq >= 1440){tfa=" days";tta=(qq/60)/24;}

Title = Name() + 
"\nNbar: " + nbar + 
"\nChart TF: " + tt + tf + 
"\nTrend TF: " + tta + tfa;

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,logscale) 
{
	local Miny,Maxy,pxchartbottom,pxchartheight; 
	Miny=Status("axisminy");
	Maxy=Status("axismaxy");
	pxchartbottom=Status("pxchartbottom");
	pxchartheight=Status("pxchartheight");
	if(logscale) rv=Nz(pxchartbottom-floor(0.5+(log(value)-log(Miny))*pxchartheight/(log(maxy)-log(miny))));
	else rv=Nz(pxchartbottom-floor(0.5+(Value-Miny)*pxchartheight/(Maxy-Miny)));
	return rv;
} 
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)
		{
			GfxSelectPen(ColorRGB(100,0,0),3,0);
			GfxMoveTo(GfxConvertBarToPixelX(i),GfxConvertValueToPixelY(H[lvix],logscale));
			GfxLineTo(GfxConvertBarToPixelX(AllVisibleBars-1),GfxConvertValueToPixelY(H[lvix],logscale));
			GfxSelectFont("Tahoma",8,650);GfxSetBkColor(ColorRGB(200,0,0));GfxSetTextColor(colorWhite);
			GfxTextOut(""+H[lvix],GfxConvertBarToPixelX(AllVisibleBars+1),GfxConvertValueToPixelY(H[lvix],logscale));			
		}
	}
	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)
		{	
			GfxSelectPen(ColorRGB(0,0,100),3,0);
			GfxMoveTo(GfxConvertBarToPixelX(i),GfxConvertValueToPixelY(L[lvix],logscale));
			GfxLineTo(GfxConvertBarToPixelX(AllVisibleBars-1),GfxConvertValueToPixelY(L[lvix],logscale));
			GfxSelectFont("Tahoma",8,650);GfxSetBkColor(ColorRGB(0,0,200));GfxSetTextColor(colorWhite);
			GfxTextOut(""+L[lvix],GfxConvertBarToPixelX(AllVisibleBars+1),GfxConvertValueToPixelY(L[lvix],logscale));
		}
	}	
}

1 comments

1. lemefr

Great S/R code! Thank you very much… trying to use it in Daily Time Frame…I think it’s ok…

Leave Comment

Please login here to leave a comment.

Back