Simple Coding Help - No Promise.

Hello Josh

Search for the Support - Resistance AFL code by Edward Pottasch, you will get what you are looking for or maybe better :)

Happy :)
Here Goes . . .

Had to re-look for it, as have deleted all that stuff from my workstation :D

Happy :)

Code:
// AFL code by Edward Pottasch, Jan 2013

// 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)",in15Minute/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",1);

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));
		}
	}	
}
 
i would like to have the AFL for moving average envelope(not with the SMA but EMA).the same kind thats there in the ZT/nest trader.

the problem with nest is lack of sufficient backfill.i've got a lot of backdata in amibroker,so backtesting will become easy and i can use realtime nest charts along with non-realtime amibroker charts to do what i want

am hoping the afl code wont be that complicated
 
Can anyone help?

I currently use this AFL to color my bars.

Code:
OB = H > Ref( H, -1 ) AND L < Ref( L, -1 ) ; //outsidebar blue
IB = H < Ref( H, -1 ) AND L > Ref( L, -1 ) ; //insidebar grey  
UB = H > Ref( H, -1 ) AND L > Ref( L, -1 ) ; //upbar lime
DB = L < Ref( L, -1 ) AND H < Ref( H, -1 ) ; //downbar red
//NB = L == Ref( L, -1 ) OR H == Ref( H, -1 ) ; //neutralbar gold
NBL = int(L) == Ref( int(L), -1 ) ; //neutralbar gold
NBH = int(H) == Ref( int(H), -1 ) ; //neutralbar gold

Plot( C, "", 
	IIf( OB , colorBlue, 
	IIf( IB, colorGrey40, 
	IIf( UB , colorGreen, 
	IIf( DB, colorOrange, 
	IIf( NBL, colorLightOrange, 
	IIf( NBH, colorPaleGreen, colorBlueGrey ) ) ) ) ) ), 
		ParamStyle("Chart Style", styleCandle, maskPrice) | styleThick, Null, Null, 0, 1, 2 ); // last param is line width as percent of bar
Symptom: Whats happens is that the above code only checks the previous bar to mark an inside/outside bar.

Problem: I would like to mark all inside/outside bars the same color UPTO the bar that breaks out of the range that started with the first inside/outside bar.

Concern: the inside bar range starts from PREVIOUS bar while outside bar range starts from CURRENT bar.

Please help.
 

amitrandive

Well-Known Member
i would like to have the AFL for moving average envelope(not with the SMA but EMA).the same kind thats there in the ZT/nest trader.

the problem with nest is lack of sufficient backfill.i've got a lot of backdata in amibroker,so backtesting will become easy and i can use realtime nest charts along with non-realtime amibroker charts to do what i want

am hoping the afl code wont be that complicated

Try This


Code:
_SECTION_BEGIN("MA Percentage Band");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", colorBlack, styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
P = ParamField("Price field",-1);
Periods = Param("Periods", 200, 2, 300, 1, 10 );
Percent = Param("Percentage %", 25, 1,100, 1, 10 );
SelectedIndicator = ParamList( "Show", "SMA,EMA,WMA,DEMA,TEMA", 1 );
 
if ( SelectedIndicator == "SMA" )
{
Positive_Percent_Band = MA( P, Periods )+ (MA( P, Periods )*percent/100);
Negative_Percent_Band = MA( P, Periods )- (MA( P, Periods )*percent/100);
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Positive_Percent_Band, "Positive %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Negative_Percent_Band, " Negative %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
}
else
if ( SelectedIndicator == "EMA" )
{
Positive_Percent_Band = EMA( P, Periods )+ (EMA( P, Periods )*percent/100);
Negative_Percent_Band = EMA( P, Periods )- (EMA( P, Periods )*percent/100);
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Positive_Percent_Band, "Positive %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Negative_Percent_Band, " Negative %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
}
else
if ( SelectedIndicator == "WMA" )
{
Positive_Percent_Band = WMA( P, Periods )+ (WMA( P, Periods )*percent/100);
Negative_Percent_Band = WMA( P, Periods )- (WMA( P, Periods )*percent/100);
Plot( WMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Positive_Percent_Band, "Positive %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Negative_Percent_Band, " Negative %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
}
else
if ( SelectedIndicator == "DEMA" )
{
Positive_Percent_Band = DEMA( P, Periods )+ (DEMA( P, Periods )*percent/100);
Negative_Percent_Band = DEMA( P, Periods )- (DEMA( P, Periods )*percent/100);
Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Positive_Percent_Band, "Positive %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Negative_Percent_Band, " Negative %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
}
else
if ( SelectedIndicator == "TEMA" )
{
Positive_Percent_Band = TEMA( P, Periods )+ (TEMA( P, Periods )*percent/100);
Negative_Percent_Band = TEMA( P, Periods )- (TEMA( P, Periods )*percent/100);
Plot( TEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Positive_Percent_Band, "Positive %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
Plot( Negative_Percent_Band, " Negative %", ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
}
_SECTION_END();
 
Hi,
Is there any way to fix the number of bars on chart ?
One, which i tried is "no of bars" in database setting. But it does not work as with zoom out number of bars increases.
Kindly help.
Regards,
S
 

Similar threads