Divergence LRS indicator in MetaStock
 
This indicator is plotted in the article's Figure 3 for FMXI.


D1:=Input("DAYS FOR DIVERGENCE " ,2 ,200 ,120 );
VFI:=Fml("VFI");
P1:=VFI+Abs(Lowest(VFI))+10;
LRSI:=LinRegSlope(P1,D1)/Abs(Ref(P1,-D1))*100;
LRS:=LinRegSlope(C,D1)/Abs(Ref(C,-D1))*100;
DIVERG:=(LRSI-LRS);
Mov(DIVERG,3,E)*100;

To create this formula in MetaStock, click on the indicator
builder (fx), click on New, type Divergence LRS Indicator in the
Name box and the code in the formula box.

This formula can only be applied to indicators that do not take
zero or near-zero values. In order to use it with VFI or other
indicators that cross the zero line, the indicator must be
modified to take only positive values.



Test 1. VFI >0

Enter Long

PERIOD:=26;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1); VFI:=Sum(If(MF>CUTOFF, +VC, If(MF <-CUTOFF, -VC,0)),
PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
MOV(VFI,7,E)>0

Sell Order

PERIOD:=26;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1)); VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF, +VC, If(MF <-CUTOFF, -VC,0)),
PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
MOV(VFI,7,E)<0

To create this test, click on enhanced system tester, then new
system (type the buy and sell order code). To run the test, click
on new simulation. Next, add securities (select GLW), select
periodicity (weekly), click on dates, (fill in the date and use
data from 4/15/94), click on next, type in initial equity (10000),
select default size (% available equity 10000), select only long
trades, and check close all positions on the last bar.

Click on more, fill in interest rate (3%), and fill in 10 points
per transaction for the commissions. Click on trade execution,
uncheck realistic market prices, and select buy price and sell
price at open.



Test 2. Divergence

Enter Long

PERIOD:=130;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF,VC,If(MF<-CUTOFF,-VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
P1:=VFI+Abs(Lowest(VFI))+10;
D1:=OPT2 {DAYS FOR DIVERG};
LRSI:=LinRegSlope(P1,D1)/Abs(Ref(P1,-D1))*100;
LRS:=LinRegSlope(C,D1)/Abs(Ref(C,-D1))*100;
DIVERG:=LRSI-LRS;
DIVERG:=Mov(DIVERG,3,E)*100;

REF(diverg,-1)>100 AND DIVERG< REF(diverg,-1) and LRSI>0

Sell Order

PERIOD:=130;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF,+VC,If(MF<-CUTOFF,-VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
P1:=VFI+Abs(Lowest(VFI))+10;
D1:=OPT2 {DAYS FOR DIVERG};
LRSI:=LinRegSlope(P1,D1)/Abs(Ref(P1,-D1))*100;
LRS:=LinRegSlope(C,D1)/Abs(Ref(C,-D1))*100;
DIVERG:=LRSI-LRS;
DIVERG:=Mov(DIVERG,3,E)*100;

diverg<0 OR LRSI<0



Test 3. Break from base

VFI
Enter Long

PERIOD:=130; VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF,+VC,If(MF<-CUTOFF,-VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);

VFI>-2 AND LinRegSlope(VFI,20)>.1 AND VFI>Mov(VFI,40,E) AND
 LinRegSlope(C,30)< Ref(C,-30) *.6/100 AND LinRegSlope(C,30)>0

Sell Order

PERIOD:=130; VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF, +VC, If(MF <-CUTOFF, -VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);

VFI<-2 OR LinRegSlope(VFI,20)<-.8
 
OBV
Enter Long

LinRegSlope(OBV(),20)>.1
AND OBV()>Mov(OBV(),40,E) AND
LinRegSlope(C,30)< Ref(C,-30)*.6/100 AND LinRegSlope(C,30)>0

Sell Order

LinRegSlope(OBV(),20)<-.8



Test 4. MA crossover

Enter Long

PERIOD:=130;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF, +VC, If(MF <-CUTOFF, -VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
SMA:=MOV(VFI,OPT1,E);
LMA:=MOV(VFI,OPT2,E);
CROSS(SMA,LMA)

Sell Order

PERIOD:=130;
VCOEF:=2.5;
INTER:=Log(TYPICAL())-Log(Ref(TYPICAL(),-1));
VINTER:=Stdev(INTER,30);
CUTOFF:=.2*VINTER*C;
VAVE:=Ref(Mov(V,PERIOD,S),-1);
VMAX:=VAVE*VCOEF;
VC:=If(V< VMAX,V,VMAX);
MF:=TYPICAL()-Ref(TYPICAL(),-1);
VFI:=Sum(If(MF>CUTOFF, +VC, If(MF <-CUTOFF, -VC,0)), PERIOD)/VAVE;
VFI:=Mov(VFI,3,E);
SMA:=MOV(VFI,OPT1,E);
LMA:=MOV(VFI,OPT2,E);
CROSS(LMA,SMA)

Optimized values for SMA and LMA were 11 and 25, respectively.

        - Markos Katsanos