{ Downloaded From https://www.WiseStockTrader.com }
{ William Blau's Ergodic indicator v2.0 }
{ Incorporating auto overbought/sold levels,
  and Ergodic/Signal line crossover signals }
{ With thanks to William Schamp at:
   ProfLogic@earthlink.net }

{ ©Copyright 2004~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 }
choose:=Input("Version:  [1]Basic,  [2]Proflogic's Ensign",1,2,2);
pds1:=Input("Proflogic's Ensign orig: EMApds 28, SmoothPds 84",0,0,0);
pds1:=Input("EMA periods",1,252,21);
pds2:=Input("Smoothing periods",1,252,5);
pds3:=Input("Signal periods",1,252,10);
plot:=Input("[1]Ergodic,  [2]Histogram,  [3]Crossover Signals",1,3,1);

{ Ergodic }
x:=Mov(Mov(H-L,pds1,E),pds2,E);
x:=If(x>0,x,.000001);
ergo1:=(Mov(Mov(C-O,pds1,E),pds2,E)/x)*100;

{ Proflogic's Ensign Ergodic version:
   EMA periods = 28, Smoothing periods = 84 }
ergo2:=
 Mov(Mov(Mov(C-Ref(C,-1),pds1,W),pds2,W),pds1,W)
/Mov(Mov(Mov(Abs(C-Ref(C,-1)),pds1,W),pds2,W),
 pds1,W)*100;

{ Selected Ergodic version }
ergodic:=If(choose=1,ergo1,ergo2);

{ Ergodic signal & histogram }
signal:=Mov(ergodic,pds3,E);
ergoPlot:=If(plot=1,ergodic,ergodic-signal);

{ Ergodic/Signal crossovers }
long:=Cross(ergodic,signal);
short:=Cross(signal,ergodic);

{ Average (zero-line) }
ergoPlotAvg:=
 Cum(ergoPlot)/Cum(IsDefined(ergoPlot));

{ Auto overbought/sold boundaries }
Pk:=Ref(ergoPlot,-1)=HHV(ergoPlot,3)
 AND Ref(ergoPlot,-1)>ergoPlotAvg;
PkVal:=ValueWhen(1,Pk,Ref(ergoPlot,-1));
Obought:=Cum(PkVal)/Cum(IsDefined(PkVal));
Tr:=Ref(ergoPlot,-1)=LLV(ergoPlot,3)
 AND Ref(ergoPlot,-1)<ergoPlotAvg;
TrVal:=ValueWhen(1,Tr,Ref(ergoPlot,-1));
Osold:=Cum(TrVal)/Cum(IsDefined(TrVal)); 

{ Plot in own window }
If(plot=1,signal,If(plot=2,ergoPlotAvg,0));
If(plot=3,long-short,Obought);
If(plot=3,long-short,Osold);
If(plot=3,long-short,ergoPlot)