# NEED Elder auto envelope afl code

#### ford7k

##### Banned
hi friends
here is metastock code for Elder auto envelope

Can somebody please convert it into amibroker formula!
Actually we have the afl code by Guno van Engel -but with errors to be fixed.
can somebody help?

HTML:
``````MetaStock FORMULA

EMA:=Input("Base EMA",1,100,22);
Factor:=Input("Factor",1,50,27);
Avg:=Mov(C,EMA,E);
csize:= StDev(2*Max(abs(H-Avg) ,abs(L-Avg)) / Avg,100)*Factor/10;
{Use 100 days for stable channel size - default is 2.7 std}
Csize:= ValueWhen(1,
DayOfWeek()<Ref(DayOfWeek() ,-1) OR (
DayOfWeek()=Ref(DayOfWeek(),-1) AND DayOfMonth() <> Ref(DayOfMonth(),-1))
,Ref(csize,-1));
{ This pegs the StDev to last bar of week  AND only changes once per week}
csize:=LastValue(csize);
{fix to constant using last value}
channel:=csize*Avg;
Avg+channel/2;
Avg-channel/2;
Avg;``````

it seems to a bband for periods 100 of ema of close.
how the auto comes into the name-is curious thing.
HTML:
``````//====================
//Elder's AutoEnvelope
//====================
//---8<---------------------------

//{Dr A. Elder's AutoEnvelope interpretation v1.0}
//{ www.elder.com/MetaStock/AutoEnvelope.htm }
//{ http://www.metastocktools.com }
//Modified for AmiBroker by Guno van Engel

/*
AutoEnvelope
Envelope channels are set parallel to the moving average (parallel to the slow MA if you use two MAs).
The two channel lines must contain approximately 90-95% of all prices for the past two OR three months between them, with only the extremes protruding Outside.
Envelope channels provide attractive profit targets - Sell longs near the upper channel line AND Cover shorts near the lower channel line.
The AutoEnvelope is a custom indicator - an original tool that automatically sizes channels by calculating a standard deviation for the last 100 bars.
It is designed to change value at most once a week, making it suitable even for intra-Day data.
A change from Elder-disk Version 1.0 is that we Now Plot the EMA in addition to the AutoEnvelope.
This combination allows you to change the EMA AND have the envelope follow it without adjusting two separate indicators.
if you do NOT want to see the moving average, go into the formula AND delete the last line which reads "avg."
*/

//Fibonacci: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

// sesje podzielone na dni INTRADAY
grid_month = IIf(Month()!=Ref(Month(),-1),1,0);//pierwszy dzieñ transakcji w miesi¹cu. Dla ostatniego zmieñ REF na (..,1)
Plot(grid_month,"",colorRed,styleHistogram|styleDashed|styleNoLabel|styleOwnScale);
grid_day = IIf(Day()!=Ref(Day(),-1),1,0);
Plot(grid_day,"",colorBlue,styleHistogram|styleDashed|styleNoLabel|styleOwnScale);

//{ User inputs }
//pds=Input("EMA periods",1,252,21);
pds = Param("EMA periods", 1, 252, 21);

//pdsBak=Input("lookback periods",1,252,42);
pdsBak=Param("lookback periods",1,252,42);

//x=Input("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,5,4);
x=Param("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,5,4);

//Plot1=Input("AutoEnvelope, Long signals, All  signals",1,3,1);
Plot1=Param("AutoEnvelope, Long signals, All  signals",1,3,1);

//delay=Input("Entry/Exit signals delay",0,5,0);
delay=Param("Entry/Exit signals delay",0,5,0);

//{ Price field }
WC = TimeFrameGetPrice( "C", inWeekly, -1 ); // gives you previous week Open price
x=IIf(x==1,O,IIf(x==2,H,IIf(x==3,L,IIf(x==5,WC,C))));

//{ Envelope bands }
AvgX=EMA(x,pds);
hiAvg=HHV(H,pdsBak);
loAvg=LLV(L,pdsBak);
shift= EMA(IIf(hiAvg > AvgX, hiAvg - AvgX, AvgX - loAvg),pds);
UpperBand=AvgX+shift;
LowerBand=AvgX-shift;

//{ Envelope signals }
In=Cross(x,LowerBand);
Out=Cross(x,UpperBand);
Init=Cum(In + Out > - 1)==1;
InInit=Cum(In)==1;
flag=BarsSince(Init OR In) < BarsSince(Init OR Out)+InInit;
signals=Ref((InInit AND Hold(InInit=0,2) OR flag AND Hold (flag=0,2)) -(flag=0 AND Hold(flag,2)),-delay);

//{ Plot envelope on price chart }
UpBand = IIf(PPlot=1,UpperBand,IIf(Plot1=2,signals,In-Out));
MidBand = IIf(PPlot=1,AvgX,IIf(Plot1=2,0,0));
LowBand = IIf(PPlot=1,LowerBand,IIf(Plot1=2,signals,In-Out));

Plot(UpBand ,"UpperBand",colorRed,styleLine ); //| styleThick
Plot(MidBand , "MidleBand", colorYellow, styleLine ); //| styleThick
Plot(LowBand, "LowerBand", colorGreen, styleLine ); //| styleThick
Plot(C , "Price", colorBlack, styleCandle ); //styleThick

/*
pds = Param("EMA periods",21,2,252,2);
pdsBak = Param("lookback periods",42,2,252,2);
x = Param("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,1,5,4);
Plot1 = Param("AutoEnvelope, Long signals, All signals",1,1,3,1);
delay = Param("Entry/Exit signals delay",0,0,5,1);
*/

AddTextColumn(FullName(),"Name");
AddTextColumn(SectorID(1),"Sector");
AddTextColumn(IndustryID(1),"Industry");
AddColumn(O, "O", 1.2, colorDefault);
AddColumn(L, "L", 1.2, colorDefault);
AddColumn(H, "H", 1.2, colorDefault);
AddColumn(C, "C", 1.2, colorDefault);
AddColumn(V, "V", 1.0, colorDefault);
AddColumn(LowBand, "LowBand", 1.2, colorDefault);
AddColumn(MidBand, "MidBand", 1.2, colorDefault);
AddColumn(UpBand, "UpBand", 1.2, colorDefault);
war1 = H > UpBand;
war2 = C > UpBand;
war3 = O > UpBand;
war4 = L > UpBand;
AddColumn(war3, "O > UpBand", 1.0, IIf(war3 >= 1, colorRed, colorWhite));
AddColumn(war4, "L > UpBand", 1.0, IIf(war4 >= 1, colorRed, colorWhite));
AddColumn(war1, "H > UpBand", 1.0, IIf(war1 >= 1, colorRed, colorWhite));
AddColumn(war2, "C > UpBand", 1.0, IIf(war2 >= 1, colorRed, colorWhite));
war5 = O < LowBand;
war6 = L < LowBand;
war7 = H < LowBand;
war8 = C < LowBand;
AddColumn(war5, "O < LowBand", 1.0, IIf(war5 >= 1, colorGreen, colorWhite));
AddColumn(war6, "L < LowBand", 1.0, IIf(war6 >= 1, colorGreen, colorWhite));
AddColumn(war7, "H < LowBand", 1.0, IIf(war7 >= 1, colorGreen, colorWhite));
AddColumn(war8, "C < LowBand", 1.0, IIf(war8 >= 1, colorGreen, colorWhite));

//Komentarz: William's % R-all.afl
"Interpretacja: " + FullName() + " (" + Name() + ")" + " z dnia " + Date();

//"Current Statistics\n";
"O: " + WriteVal(O, format = 1.2) + ", " + "H: " + WriteVal(H, format = 1.2) + ", " + "L: " + WriteVal(L, format = 1.2) + ", " + "C: " + WriteVal(C, format = 1.2) + ", \nVolume: " + Volume ;
"ATR [œredni zasiêg]: " + NumToStr(ATR(1), 4.2) + " ( " +
NumToStr((C - Ref(C, -1)), 4.2) + " PLN / " +
NumToStr((((C - Ref(C, -1)) / Ref(C, -1)) * 100), 2.1) + 	"% ).";

"\nDane spó³ki w bazie od " + WriteVal(BeginValue(DateTime()), formatDateTime) + " do " + WriteVal(EndValue(DateTime()), formatDateTime);;

"";
"LowBand = " + WriteVal ( LowBand , 1.2);
"MidBand = " + WriteVal ( MidBand , 1.2);
"UpBand = " + WriteVal ( UpBand , 1.2);
//+ WriteIf(( PercentR1( pds1 ) <= 5), "  --> OS 1", "") + WriteIf(( PercentR1( pds1 ) >= 95), "  --> OB 1", "");

Filter = 1;``````
cheers
ford

Last edited:

#### doorway2heaven

##### New Member
Deleting-- since it was duplicate

Last edited:

#### arunpanda

##### New Member
hi friends
here is metastock code for Elder auto envelope

Can somebody please convert it into amibroker formula!
Actually we have the afl code by Guno van Engel -but with errors to be fixed.
can somebody help?

HTML:
``````MetaStock FORMULA

EMA:=Input("Base EMA",1,100,22);
Factor:=Input("Factor",1,50,27);
Avg:=Mov(C,EMA,E);
csize:= StDev(2*Max(abs(H-Avg) ,abs(L-Avg)) / Avg,100)*Factor/10;
{Use 100 days for stable channel size - default is 2.7 std}
Csize:= ValueWhen(1,
DayOfWeek()<Ref(DayOfWeek() ,-1) OR (
DayOfWeek()=Ref(DayOfWeek(),-1) AND DayOfMonth() <> Ref(DayOfMonth(),-1))
,Ref(csize,-1));
{ This pegs the StDev to last bar of week  AND only changes once per week}
csize:=LastValue(csize);
{fix to constant using last value}
channel:=csize*Avg;
Avg+channel/2;
Avg-channel/2;
Avg;``````

it seems to a bband for periods 100 of ema of close.
how the auto comes into the name-is curious thing.
HTML:
``````//====================
//Elder's AutoEnvelope
//====================
//---8<---------------------------

//{Dr A. Elder's AutoEnvelope interpretation v1.0}
//{ www.elder.com/MetaStock/AutoEnvelope.htm }
//{ http://www.metastocktools.com }
//Modified for AmiBroker by Guno van Engel

/*
AutoEnvelope
Envelope channels are set parallel to the moving average (parallel to the slow MA if you use two MAs).
The two channel lines must contain approximately 90-95% of all prices for the past two OR three months between them, with only the extremes protruding Outside.
Envelope channels provide attractive profit targets - Sell longs near the upper channel line AND Cover shorts near the lower channel line.
The AutoEnvelope is a custom indicator - an original tool that automatically sizes channels by calculating a standard deviation for the last 100 bars.
It is designed to change value at most once a week, making it suitable even for intra-Day data.
A change from Elder-disk Version 1.0 is that we Now Plot the EMA in addition to the AutoEnvelope.
This combination allows you to change the EMA AND have the envelope follow it without adjusting two separate indicators.
if you do NOT want to see the moving average, go into the formula AND delete the last line which reads "avg."
*/

//Fibonacci: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

// sesje podzielone na dni INTRADAY
grid_month = IIf(Month()!=Ref(Month(),-1),1,0);//pierwszy dzieñ transakcji w miesi¹cu. Dla ostatniego zmieñ REF na (..,1)
Plot(grid_month,"",colorRed,styleHistogram|styleDashed|styleNoLabel|styleOwnScale);
grid_day = IIf(Day()!=Ref(Day(),-1),1,0);
Plot(grid_day,"",colorBlue,styleHistogram|styleDashed|styleNoLabel|styleOwnScale);

//{ User inputs }
//pds=Input("EMA periods",1,252,21);
pds = Param("EMA periods", 1, 252, 21);

//pdsBak=Input("lookback periods",1,252,42);
pdsBak=Param("lookback periods",1,252,42);

//x=Input("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,5,4);
x=Param("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,5,4);

//Plot1=Input("AutoEnvelope, Long signals, All  signals",1,3,1);
Plot1=Param("AutoEnvelope, Long signals, All  signals",1,3,1);

//delay=Input("Entry/Exit signals delay",0,5,0);
delay=Param("Entry/Exit signals delay",0,5,0);

//{ Price field }
WC = TimeFrameGetPrice( "C", inWeekly, -1 ); // gives you previous week Open price
x=IIf(x==1,O,IIf(x==2,H,IIf(x==3,L,IIf(x==5,WC,C))));

//{ Envelope bands }
AvgX=EMA(x,pds);
hiAvg=HHV(H,pdsBak);
loAvg=LLV(L,pdsBak);
shift= EMA(IIf(hiAvg > AvgX, hiAvg - AvgX, AvgX - loAvg),pds);
UpperBand=AvgX+shift;
LowerBand=AvgX-shift;

//{ Envelope signals }
In=Cross(x,LowerBand);
Out=Cross(x,UpperBand);
Init=Cum(In + Out > - 1)==1;
InInit=Cum(In)==1;
flag=BarsSince(Init OR In) < BarsSince(Init OR Out)+InInit;
signals=Ref((InInit AND Hold(InInit=0,2) OR flag AND Hold (flag=0,2)) -(flag=0 AND Hold(flag,2)),-delay);

//{ Plot envelope on price chart }
UpBand = IIf(PPlot=1,UpperBand,IIf(Plot1=2,signals,In-Out));
MidBand = IIf(PPlot=1,AvgX,IIf(Plot1=2,0,0));
LowBand = IIf(PPlot=1,LowerBand,IIf(Plot1=2,signals,In-Out));

Plot(UpBand ,"UpperBand",colorRed,styleLine ); //| styleThick
Plot(MidBand , "MidleBand", colorYellow, styleLine ); //| styleThick
Plot(LowBand, "LowerBand", colorGreen, styleLine ); //| styleThick
Plot(C , "Price", colorBlack, styleCandle ); //styleThick

/*
pds = Param("EMA periods",21,2,252,2);
pdsBak = Param("lookback periods",42,2,252,2);
x = Param("use: Open=1, High=2, Low=3, Close=4, WClose=5",1,1,5,4);
Plot1 = Param("AutoEnvelope, Long signals, All signals",1,1,3,1);
delay = Param("Entry/Exit signals delay",0,0,5,1);
*/

AddTextColumn(FullName(),"Name");
AddTextColumn(SectorID(1),"Sector");
AddTextColumn(IndustryID(1),"Industry");
AddColumn(O, "O", 1.2, colorDefault);
AddColumn(L, "L", 1.2, colorDefault);
AddColumn(H, "H", 1.2, colorDefault);
AddColumn(C, "C", 1.2, colorDefault);
AddColumn(V, "V", 1.0, colorDefault);
AddColumn(LowBand, "LowBand", 1.2, colorDefault);
AddColumn(MidBand, "MidBand", 1.2, colorDefault);
AddColumn(UpBand, "UpBand", 1.2, colorDefault);
war1 = H > UpBand;
war2 = C > UpBand;
war3 = O > UpBand;
war4 = L > UpBand;
AddColumn(war3, "O > UpBand", 1.0, IIf(war3 >= 1, colorRed, colorWhite));
AddColumn(war4, "L > UpBand", 1.0, IIf(war4 >= 1, colorRed, colorWhite));
AddColumn(war1, "H > UpBand", 1.0, IIf(war1 >= 1, colorRed, colorWhite));
AddColumn(war2, "C > UpBand", 1.0, IIf(war2 >= 1, colorRed, colorWhite));
war5 = O < LowBand;
war6 = L < LowBand;
war7 = H < LowBand;
war8 = C < LowBand;
AddColumn(war5, "O < LowBand", 1.0, IIf(war5 >= 1, colorGreen, colorWhite));
AddColumn(war6, "L < LowBand", 1.0, IIf(war6 >= 1, colorGreen, colorWhite));
AddColumn(war7, "H < LowBand", 1.0, IIf(war7 >= 1, colorGreen, colorWhite));
AddColumn(war8, "C < LowBand", 1.0, IIf(war8 >= 1, colorGreen, colorWhite));

//Komentarz: William's % R-all.afl
"Interpretacja: " + FullName() + " (" + Name() + ")" + " z dnia " + Date();

//"Current Statistics\n";
"O: " + WriteVal(O, format = 1.2) + ", " + "H: " + WriteVal(H, format = 1.2) + ", " + "L: " + WriteVal(L, format = 1.2) + ", " + "C: " + WriteVal(C, format = 1.2) + ", \nVolume: " + Volume ;
"ATR [œredni zasiêg]: " + NumToStr(ATR(1), 4.2) + " ( " +
NumToStr((C - Ref(C, -1)), 4.2) + " PLN / " +
NumToStr((((C - Ref(C, -1)) / Ref(C, -1)) * 100), 2.1) + 	"% ).";

"\nDane spó³ki w bazie od " + WriteVal(BeginValue(DateTime()), formatDateTime) + " do " + WriteVal(EndValue(DateTime()), formatDateTime);;

"";
"LowBand = " + WriteVal ( LowBand , 1.2);
"MidBand = " + WriteVal ( MidBand , 1.2);
"UpBand = " + WriteVal ( UpBand , 1.2);
//+ WriteIf(( PercentR1( pds1 ) <= 5), "  --> OS 1", "") + WriteIf(( PercentR1( pds1 ) >= 95), "  --> OB 1", "");

Filter = 1;``````
cheers
ford

Hi,

I am a novice at writing AFL code. Reproducing a code that I have written converting the Metastock code. Please correct in case of any mistakes.

Auto Envelope

_SECTION_BEGIN("Auto Envelope");
BEMA = Param( "Base EMA", 22, 1, 200, 1 );
Factor = Param( "Factor", 27, 1, 50, 1 );

Avg= EMA(C,BEMA);
csize= StDev(2*Max(abs(H-Avg), abs(L-Avg))/Avg,100)*Factor/10;
csize= ValueWhen(DayOfWeek()<Ref(DayOfWeek() ,-1) OR (
DayOfWeek()=Ref(DayOfWeek(),-1) AND DayOfMonth() <> Ref(DayOfMonth(),-1)),Ref(csize,-1),1);

csize=LastValue(csize);
channel=csize*Avg;
Plot((Avg+channel/2),"Upper Line",colorViolet,styleDashed|styleThick);
Plot((Avg-channel/2),"Lower Line",colorOrange,styleDashed|styleThick);
Plot(Avg,"EMA",colorBlack,styleLine|styleThick));
_SECTION_END();

#### arunpanda

##### New Member
Hi,

I am a novice at writing AFL code. Reproducing a code that I have written converting the Metastock code. Please correct in case of any mistakes.

Auto Envelope

_SECTION_BEGIN("Auto Envelope");
BEMA = Param( "Base EMA", 22, 1, 200, 1 );
Factor = Param( "Factor", 27, 1, 50, 1 );

Avg= EMA(C,BEMA);
csize= StDev(2*Max(abs(H-Avg), abs(L-Avg))/Avg,100)*Factor/10;
csize= ValueWhen(DayOfWeek()<Ref(DayOfWeek() ,-1) OR (
DayOfWeek()=Ref(DayOfWeek(),-1) AND DayOfMonth() <> Ref(DayOfMonth(),-1)),Ref(csize,-1),1);

csize=LastValue(csize);
channel=csize*Avg;
Plot((Avg+channel/2),"Upper Line",colorViolet,styleDashed|styleThick);
Plot((Avg-channel/2),"Lower Line",colorOrange,styleDashed|styleThick);
Plot(Avg,"EMA",colorBlack,styleLine|styleThick));
_SECTION_END();

Amended code working on Amibroker

_SECTION_BEGIN("Auto Envelope");
BEMA = Param( "Base EMA", 22, 1, 200, 1 );
Factor = Param( "Factor", 27, 1, 50, 1 );

Avg= EMA(C,BEMA);
csize= StDev(2*Max(abs(H-Avg), abs(L-Avg))/Avg,100)*Factor/10;
csize= ValueWhen(DayOfWeek()<Ref(DayOfWeek() ,-1) OR (
DayOfWeek()==Ref(DayOfWeek(),-1) AND Day() != Ref(Day(),-1)),Ref(csize,-1),1);

csize=LastValue(csize);
channel=csize*Avg;
Plot((Avg+channel/2),"Upper Line",colorViolet,styleDashed|styleThick);
Plot((Avg-channel/2),"Lower Line",colorOrange,styleDashed|styleThick);
Plot(Avg,"EMA",colorBlack,styleLine|styleThick);
_SECTION_END();

## Zerodha – Open Paperless Account

Open online account with Zerodha. Free delivery trading and Max Rs 20 for Intraday, F&O, Currency and Commodity Trading. Intraday High leverage with MIS, CO and BO.

 Name: Phone: Email: City: State: Select StateAndaman and Nicobar IslandsAndhra PradeshArunachal PradeshAssamBiharChhattisgarhDadra and Nagar HaveliDaman and DiuDelhiGoaGujaratHaryanaHimachal PradeshJammu and KashmirJharkhandKarnatakaKeralaLakshadweepMadhya PradeshMaharashtraManipurMeghalayaMizoramNagalandOdishaPuducherryPunjabRajasthanSikkimTamil NaduTelanganaTripuraUttarakhandUttar PradeshWest Bengal Are you a day trader?