======================================================= Null (N/A) bar removal, with and without PREV functions. ======================================================= Problem: ------- Indicators and conditions generally introduce leading and (sometimes) trailing Null (N/A) bars on a plot. These Null bars can stop any other signals being plotted during this Null period. For example, a 21-bar SMA will not allow any signals associated with it to be plotted on any of the first 20 bars. No other related code can possibly plot anything within this Null zone. Example: ------- (note 32/31 bar leading/trailing null zone) ---8<--------------------------------------- { Three month centered Simple Moving Average } pds:=Input("SMA periods",1,2520,63); Ref(Mov(C,pds,S),LastValue(pds/2)) ---8<--------------------------------------- Solution 1: ---------- (compare plot to above) ======================= Null bar removal - PREV ======================= ---8<--------------------------------------- { Removes leading/trailing Null (N/A) bars. Centered SMA example uses hindsight - do not trade. To avoid the PREVx2 processing overhead, try the Xtend.dll custom DLL, available for US$60 through MetaStockTools.com. Also available for free at Trader's Consortium forum: http://tradersconsortium.com ©Copyright 2003-2007 Jose Silva. For personal use only: no resale or repackaging allowed. All code remains the property of Jose Silva. http://www.metastocktools.com } { User input } pds:=Input("SMA periods",1,2520,63); { Centered SMA } indic:=Ref(Mov(C,pds,S),LastValue(pds/2)); { 1st valid plot bar } defined:=IsDefined(indic); bar1:=Cum(defined)=1; { Remove leading/trailing Null bars } NoNull:=If(Cum(defined)>0, LastValue(indic+PREV-PREV), LastValue(ValueWhen(1,bar1,indic))); { Plot on chart } NoNull ---8<--------------------------------------- The solution above introduces PREV function related problems, mainly a major slowdown of processing speed (not suitable for real-time data), and unreliability issues. Solution 2: ---------- Same null-less plot as above, with the much simpler and more reliable Xtend.dll, and without the PREVx2 processing overhead. ====================== Null bar removal - DLL ====================== ---8<--------------------------------------- { Removes leading/trailing Null (N/A) bars. Centered SMA example uses hindsight - do not trade. Xtend.dll must be in ...\MetaStock\External Function DLLs\ folder. Xtend.dll custom DLL, available for US$60 through MetaStockTools.com. Also available for free at Trader's Consortium forum: http://tradersconsortium.com ©Copyright 2003-2007 Jose Silva. For personal use only: no resale or repackaging allowed. All code remains the property of Jose Silva. http://www.metastocktools.com } { User input } pds:=Input("SMA periods",1,2520,63); { Centered SMA } indic:=Ref(Mov(C,pds,S),LastValue(pds/2)); { Plot on chart } ExtFml("Xtend.Xtend",indic) ---8<--------------------------------------- Xtend.dll sample application 1: ------------------------------ ================== MA - centered (DLL) ================== ---8<--------------------------------------- { Centered Moving Average v6.1 Uses forward-referencing to center Mov Avg and project future direction. Uses hindsight - do not trade! Xtend.dll must be in ...\MetaStock\External Function DLLs\ folder. Xtend.dll custom DLL, available for US$60 through MetaStockTools.com. Also available for free at Trader's Consortium forum: http://tradersconsortium.com Copyright © 2003~2007 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("Mov Avg periods",1,2600,21); fwPds:=Input("Forward-referencing periods [automatic: -1]",-1,2600,-1); proj:=Input("Project last known MA: [1]Direction, [2]Value",1,2,1); projCl:=Input("Project back last Close? [1]Yes, [0]No",0,1,0); channel:=Input("Channel +/- boundary %", 0,1000,4)/100; type:=Input("[1]EMA [2]SMA [3]TmSr [4]Tri [5]Var [6]Vol [7]Wght",1,7,2); { Choose MovAvg type: 1 - Exponential MA 2 - Simple MA 3 - Time Series MA 4 - Triangular MA 5 - Variable MA 6 - Volume adjusted MA 7 - Weighted MA } ma:= If(type=1,Mov(C,pds,E), If(type=2,Mov(C,pds,S), If(type=3,Mov(C,pds,T), If(type=4,Mov(C,pds,TRI), If(type=5,Mov(C,pds,VAR), If(type=6,Mov(C,pds,VOL), Mov(C,pds,W))))))); { Automatic period-centering } center:=LastValue(If(fwPds<0,Int(pds/2),fwPds)); { Forward-referenced MovAvg } fwd:=Ref(ma,center); { Last valid MovAvg plot point } valid:=Cum(IsDefined(fwd)) =LastValue(Cum(IsDefined(fwd))); valid:=valid AND Alert(valid=0,2); { Extend MovAvg plot into Null zones } movAvg:=ExtFml("Xtend.Xtend",fwd); { Last MA known direction & future projection } init:=Cum(IsDefined(fwd))=1; direction:=movAvg+ If(IsUndefined(fwd), ValueWhen(1,init OR valid,movAvg)- ValueWhen(1,init OR valid,Ref(movAvg,-1)),0) *BarsSince(init OR valid); { Choose Centered MovAvg type } CMA:=If(proj=1,direction,movAvg); { Projection channels } restrict:=If(BarsSince(valid),CMA,CMA); UpChannel:=restrict*(1+channel); DwChannel:=restrict*(1-channel); { Last Close back-projection } x:=LastValue(C); x:=If(projCl,If(BarsSince(valid),x,x),CMA); { Plot on price chart } UpChannel;DwChannel;x;CMA ---8<--------------------------------------- Xtend.dll sample application 2 ------------------------------ Problem: ------- ---8<--------------------------------------- entry:=Cross(C,Mov(C,5,S)); exit:=Cross(Mov(C,21,S),C); entry-exit ---8<--------------------------------------- Note how the undefined exit signals in the formula above prevent any entry signals from plotting in the first 20 bars. Why would we want to mix entry & exit signals into a single plot? Compare above & below plots: ---8<--------------------------------------- { Entry/Exit signals } entry:=Cross(C,Mov(C,5,S)); exit:=Cross(Mov(C,21,S),C); { Remove redundant signals } init:=Cum(IsDefined(entry+exit))=1; bin:=ValueWhen(1,entry-exit<>0 OR init,entry); long:=bin*(Alert(bin=0,2) OR entry*Cum(entry)=1); short:=(bin=0)*(Alert(bin,2) OR exit*Cum(exit)=1); { Plot in own window } long-short ---8<--------------------------------------- Now compare the following to the almost identical version below, where the Null zone is removed from the initial 20 bars of the chart: ================= Null zone removed ================= ---8<--------------------------------------- { Allows the plotting of entry/exit signals during undefined signal periods. Sample only - do not trade! Xtend.dll must be in ...\MetaStock\External Function DLLs\ folder. Xtend.dll custom DLL, available for US$60 through MetaStockTools.com. Also available for free at Trader's Consortium forum: http://tradersconsortium.com Copyright © 2003~2007 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 } { Entry/Exit signals } entry:=Cross(C,Mov(C,5,S)); exit:=Cross(Mov(C,21,S),C); { Remove Null plot bars } entry:=IsDefined(entry)*ExtFml("Xtend.Xtend",entry); exit:=IsDefined(exit)*ExtFml("Xtend.Xtend",exit); { Remove redundant signals } init:=Cum(IsDefined(entry+exit))=1; bin:=ValueWhen(1,entry-exit<>0 OR init,entry); long:=bin*(Alert(bin=0,2) OR entry*Cum(entry)=1); short:=(bin=0)*(Alert(bin,2) OR exit*Cum(exit)=1); { Plot in own window } long-short ---8<--------------------------------------- The Xtend.dll custom DLL is a simple time-saving tool, useful for whenever a plot needs to be extended from the last known defined value through and into the Null (N/A) bar zone. The Xtend function will extend the first definable value backwards to the first bar, and the last definable value forward to the last bar. It does this with minimal processing overheads. Xtend.dll is a small and efficient MetaStock DLL, and is compatible with all versions of MetaStock v7.0 and later. The Xtend.dll custom DLL is available for US$60 through MetaStockTools.com. Also available for free at the download section of Trader's Consortium forum: http://tradersconsortium.com Once downloaded, Xtend.dll can be installed easily: Close MetaStock (v8.0 or later), extract the contents of Xtend.zip and run Xtend.exe, which will place Xtend.dll in the ...\MetaStock\External Function DLLs\ folder, and also import "Null bar removal - DLL" & "MA - centered (DLL)" sample indicators. For MetaStock versions earlier than 8.0, please copy Xtend.dll manually to the ...\MetaStock\External Function DLLs\ folder, and re-start MetaStock. jose '-) http://www.metastocktools.com