Rank: Advanced Member
Groups: Registered, Registered Users, Subscribers, Unverified Users Joined: 10/28/2004(UTC) Posts: 3,111 Location: Perth, Western Australia
Was thanked: 16 time(s) in 16 post(s)
|
Todd, The reason both codes match the built-in formula so closely is because they are essentially the same code! Hopefully the explanation will be clear enough to prove this, and show where the minor differences occur. Lets look at this code first. Code:
len1:=19;
len2:=30;
len3:=19;
A:=(H+L+2*C);
B:=Mov((HHV(H,2)-LLV(L,2)),len1,E);
BP:=V/Mov(V,len1,E) * ((A>=Ref(A,-1)) +(A<Ref(A,-1)) / Exp((0.375 * (A+Ref(A,-1)) /B ) *(Ref(A,-1)-A) / A));
SP := V/Mov(V,len1,E) * ((A<=Ref(A,-1)) + (A>Ref(A,-1)) / Exp((0.375 * (A+Ref(A,-1)) / B ) * (A-Ref(A,-1)) / Ref(A,-1)));
mabp:=Mov(BP,len3,E);
masp:=Mov(SP,len3,E);
divsor:=If(mabp>masp,mabp,masp);
divend:=If(mabp<masp,mabp,masp);
var2:=1-(divend/divsor);
var3:=If((masp>mabp),-var2,var2) ;
var4:=var3*100;
var5:=Mov(var4,len2,E);
var4
If we dont use the smoothing MA at the end, we can get rid of var5. We also notice that len1 and len3 are the same, so we will align these into len1. Notice in the BP and SP code, ((A>=Ref(A,-1)) +(A<Ref(A,-1)) which is essentially an If() statement. With whats left of the BP and SP code sections we will introduce two new variables to perform the common computations. So what we are left with is: Code:
len1:=19;
A:=(H+L+2*C);
B:=Mov((HHV(H,2)-LLV(L,2)),len1,E);
newVar1:=V/Mov(V,len1,E);
newVar2:=(A+Ref(A,-1))/B;
BP:=If(A>=Ref(A,-1),newVar1,1/Exp((newVar2)*(Ref(A,-1)-A)/A));
SP:=If(A<=Ref(A,-1),newVar1,1/Exp((newVar2)*(A-Ref(A,-1))/Ref(A,-1)));
mabp:=Mov(BP,len1,E);
masp:=Mov(SP,len1,E);
divsor:=If(mabp>masp,mabp,masp);
divend:=If(mabp<masp,mabp,masp);
var2:=1-(divend/divsor);
var3:=If((masp>mabp),-var2,var2) ;
var4:=var3*100;
var4;
Turning our attention now to: Code:
len:=Input("Length: ",1,1000,11)*2;
VolAvg1:=Ref(Mov(V,len,S),-1);
wCl:=(H+L+C+C)/4;
AvgTR:=Mov(HHV(H,2)-LLV(L,2),len,S);
VolAvg:=(VolAvg1*(len-1)+V)/len;
WtCRatio:=(wCl-Ref(wCl,-1)) / Min(wCl,Ref(wCl,-1));
VolRatio:=V/Max(VolAvg,0.0001);
Const:=((wCl*3)/AvgTR)*Abs(WtCRatio);
Const:=VolRatio/Exp(Min(Const,88));
BuyPr:=If(WtCRatio>0,VolRatio,Const);
SellPr:=If(WtCRatio>0,Const,VolRatio);
BuyPres:=Mov(BuyPr,len,E);
SellPres:=Mov(SellPr,len,E);
Sign:=If(SellPres>BuyPres,-1,1);
TempDI:=Sign*If(SellPres>BuyPres,BuyPres/Max(SellPres,0.00001),SellPres/Max(BuyPres,0.00001));
myDMI:=100*If(TempDI<0,-1-TempDI,1-TempDI);
myDMI;
Notice: VolAvg:=(Ref(Mov(V,len,S),-1)*(len-1)+V)/len; is VERY similar to Mov(V,len,S) so lets call it as the same. Const:=VolRatio/Exp(Min(Const,88)); Const will VERY rarely ever be greater than 88, so just use Const. So becomes: Code:
len:=Input("Length: ",1,1000,11)*2;
wCl:=(H+L+C+C)/4;
AvgTR:=Mov(HHV(H,2)-LLV(L,2),len,S);
VolRatio:=V/Mov(V,len,S);
WtCRatio:=(wCl-Ref(wCl,-1)) / Min(wCl,Ref(wCl,-1));
Const:=((wCl*3)/AvgTR)*Abs(WtCRatio);
Const:=VolRatio/Exp(Const);
BuyPr:=If(WtCRatio>0,VolRatio,Const);
SellPr:=If(WtCRatio<0,VolRatio,Const);
BuyPres:=Mov(BuyPr,len,E);
SellPres:=Mov(SellPr,len,E);
Sign:=If(SellPres>BuyPres,-1,1);
TempDI:=Sign*If(SellPres>BuyPres,BuyPres/Max(SellPres,0.00001),SellPres/Max(BuyPres,0.00001));
myDMI:=100*If(TempDI<0,-1-TempDI,1-TempDI);
myDMI;
So if we interleave these: Code:
len1:=19;
len:=Input("Length: ",1,1000,19); {make the lengths the same}
A:=(H+L+2*C);
wCl:=(H+L+C+C)/4;
B:=Mov((HHV(H,2)-LLV(L,2)),len1,E);
AvgTR:=Mov(HHV(H,2)-LLV(L,2),len,S); {a small difference here!}
newVar1:=V/Mov(V,len1,E);
VolRatio:=V/Mov(V,len,S); {a small difference here!}
newVar2:=(A+Ref(A,-1))/B;
WtCRatio:=(wCl-Ref(wCl,-1)) / Min(wCl,Ref(wCl,-1));
Const:=VolRatio/(Exp((wCl*3)/AvgTR)*Abs(WtCRatio));
BP:=If(A>=Ref(A,-1),newVar1,1/Exp((newVar2)*(Ref(A,-1)-A)/A));
BuyPr:=If(WtCRatio>=0,VolRatio,Const); {a small difference here!}
SP:=If(A<=Ref(A,-1),newVar1,1/Exp((newVar2)*(A-Ref(A,-1))/Ref(A,-1)));
SellPr:=If(WtCRatio<=0,VolRatio,Const); {a small difference here!}
mabp:=Mov(BP,len1,E);
BuyPres:=Mov(BuyPr,len,E);
masp:=Mov(SP,len1,E);
SellPres:=Mov(SellPr,len,E);
{format each plot}
divsor:=If(mabp>masp,mabp,masp);
divend:=If(mabp<masp,mabp,masp);
var2:=1-(divend/divsor);
var3:=If((masp>mabp),-var2,var2) ;
var4:=var3*100;
var4;
Sign:=If(SellPres>BuyPres,-1,1);
TempDI:=Sign*If(SellPres>BuyPres,BuyPres/Max(SellPres,0.00001),SellPres/Max(BuyPres,0.00001));
myDMI:=100*If(TempDI<0,-1-TempDI,1-TempDI);
myDMI;
So other than deciding to EMA vice SMA, the only real difference is between:
VolRatio/(Exp((wCl*3)/AvgTR)*Abs(WtCRatio));
and
1/Exp((newVar2)*(Ref(A,-1)-A)/A));
and if we notice (Ref(A,-1)-A)/A) is the same as Abs(WtCRatio) in the different scenarios, we can see the difference is further isolated, specifically to the difference between: VolRatio/(Exp((wCl*3)/AvgTR));
and
1/Exp(newVar2);
So essentially, they are the same codes with one minor differernce in their computation, however, James Sibbet's Demand Index Indicator programmed by David Fenstemaker still tracks the MS builtin function more closely with a period set at 19 periods, that is: Code:
{James Sibbet's Demand Index Indicator}
{Programmed by David Fenstemaker}
{Omega Code downloaded from}
{http://www.purebytes.com/archives/omega/2002/msg04345.html}
{converted to MS by wabbit :D}
{19 April 2006}
{edited 25 September 2006}
len:=19;
wCl:=(H+L+C+C)/4;
AvgTR:=Mov(HHV(H,2)-LLV(L,2),len,S);
VolRatio:=V/Mov(V,len,S);
WtCRatio:=(wCl-Ref(wCl,-1)) / Min(wCl,Ref(wCl,-1));
Const:=((wCl*3)/AvgTR)*Abs(WtCRatio);
BuyPr:=If(WtCRatio>=0,VolRatio,VolRatio/Exp(Const));
SellPr:=If(WtCRatio<=0,VolRatio,VolRatio/Exp(Const));
BuyPres:=Mov(BuyPr,len,E);
SellPres:=Mov(SellPr,len,E);
Sign:=If(SellPres>BuyPres,-1,1);
TempDI:=Sign*If(SellPres>BuyPres,BuyPres/Max(SellPres,0.00001),SellPres/Max(BuyPres,0.00001));
myDMI:=100*If(TempDI<0,-1-TempDI,1-TempDI);
myDMI;
wabbit [:D]
|