MetaStock -> Tools -> Indicator Builder -> New -> Copy and paste complete formulae between "---8<---" lines. ============================== EMA - Breakout-sensitive (BEMA) ============================== ---8<------------------------------------- { [B]reakout-sensitive [E]xp [M]ov [A]vg v1.0 } { BEMA based on bars since price breakouts. Options: [1] Composite BEMA: (Upper+Lower)/2; [2] Upper BEMA band based on upside breaks; Lower BEMA band based on downside breaks; [3] BEMA shifts to Upper/Lower on crossovers} { ©Copyright 2005 Jose Silva. The grant of this license is for personal use only - no resale or repackaging allowed. All code remains the property of Jose Silva. http://www.metastocktools.com } { User inputs } pds:=Input("BEMA and HHV/LLV lookback periods", 1,2520,21); plot:=Input("BEMA: [1]Composite, [2]Upper+Lower, [3]Long/Short",1,4,1); spread:=Input("Upper/Lower BEMA bands shift %", 0,100,2)/200; x:=Input("Breakouts, use: [1]Close, [2]High/Low",1,2,1); { Upside breakouts } xup:=If(x=1,C,H); up:=xup=HHV(xup,pds); { Downside breakouts } xdw:=If(x=1,C,L); dw:=xdw=LLV(xdw,pds); { Remove ghost breakout signals } init:=Cum(IsDefined(up+dw))=1; bin:=ValueWhen(1,up-dw<>0 OR init,up); up:=bin AND (Alert(bin=0,2) OR init); dw:=bin=0 AND (Alert(bin,2) OR init); NullZone:=Cum(up)>0 AND Cum(dw)<1 OR Cum(up)<1 AND Cum(dw)>0; { Periods since breakouts } upPds:=BarsSince(up OR Cum(1)=pds)+2; upPds:=If(upPds>pds,pds,upPds); dwPds:=BarsSince(dw OR Cum(1)=pds)+2; dwPds:=If(dwPds>pds,pds,dwPds); { Upside breakout-based EMA } upEma:=xup*2/(upPds+1)+PREV*(1-2/(upPds+1)); upEma:=If(NullZone,Mov(xup,pds,E),upEma); upEma:=upEma*(1+spread); { Downside breakout-based EMA } dwEma:=xdw*2/(dwPds+1)+PREV*(1-2/(dwPds+1)); dwEma:=If(NullZone,Mov(xdw,pds,E),dwEma); dwEma:=dwEma*(1-spread); { Composite BEMA } BEMA:=(upEma+dwEma)/2; LngSht:=If(C>=BEMA,dwEma,upEma); { Plot on price chart } If(plot=1,BEMA,If(plot=2,dwEma, If(plot=3,LngSht,up))); If(plot=1,BEMA,If(plot=2,upEma, If(plot=3,LngSht,-dw))) ---8<------------------------------------- ========================= EMA - (BEMA), dll version ========================= ---8<------------------------------------- { [B]reakout-sensitive [E]xp [M]ov [A]vg v.dll - Plots much faster than PREV-based version.} { BEMA based on bars since price breakouts. Options: [1] Composite BEMA: (Upper+Lower)/2; [2] Upper BEMA band based on upside breaks; Lower BEMA band based on downside breaks; [3] BEMA shifts to Upper/Lower on crossovers} { Forum.dll from http://forum.equis.com or ASI.dll from http://www.thedml.com must be in: ...\MetaStock\External Function DLLs\ folder.} { ©Copyright 2005 Jose Silva. The grant of this license is for personal use only - no resale or repackaging allowed. All code remains the property of Jose Silva. http://www.metastocktools.com } { User inputs } pds:=Input("BEMA and HHV/LLV lookback periods", 1,2520,21); plot:=Input("BEMA: [1]Composite, [2]Upper+Lower, [3]Long/Short",1,4,1); spread:=Input("Upper/Lower BEMA bands shift %", 0,100,2)/200; x:=Input("Breakouts, use: [1]Close, [2]High/Low",1,2,1); { Upside breakouts } xup:=If(x=1,C,H); up:=xup=HHV(xup,pds); { Downside breakouts } xdw:=If(x=1,C,L); dw:=xdw=LLV(xdw,pds); { Remove ghost breakout signals } init:=Cum(IsDefined(up+dw))=1; bin:=ValueWhen(1,up-dw<>0 OR init,up); up:=bin AND (Alert(bin=0,2) OR init); dw:=bin=0 AND (Alert(bin,2) OR init); NullZone:=Cum(up)>0 AND Cum(dw)<1 OR Cum(up)<1 AND Cum(dw)>0; { Periods since breakouts } upPds:=BarsSince(up OR Cum(1)=pds)+2; upPds:=If(upPds>pds,pds,upPds); dwPds:=BarsSince(dw OR Cum(1)=pds)+2; dwPds:=If(dwPds>pds,pds,dwPds); { Upside breakout-based EMA } {upEma:=ExtFml("Forum.VarMOV",xup,upPds,e);} upEma:=ExtFml("ASI.EMA",xup,upPds); upEma:=If(NullZone,Mov(xup,pds,E),upEma); upEma:=upEma*(1+spread); { Downside breakout-based EMA } {dwEma:=ExtFml("Forum.VarMOV",xdw,dwPds,e);} dwEma:=ExtFml("ASI.EMA",xdw,dwPds); dwEma:=If(NullZone,Mov(xdw,pds,E),dwEma); dwEma:=dwEma*(1-spread); { Composite BEMA } BEMA:=(upEma+dwEma)/2; LngSht:=If(C>=BEMA,dwEma,upEma); { Plot on price chart } If(plot=1,BEMA,If(plot=2,dwEma, If(plot=3,LngSht,up))); If(plot=1,BEMA,If(plot=2,upEma, If(plot=3,LngSht,-dw))) ---8<------------------------------------- http://www.metastocktools.com