Indicator Help - The Chop Index

 

Hopefully I can get someone's input on what I'm doing wrong here.

I've created a very simple indicator that looks at the number of inflections of a Moving Average (ie 200bar MA) over a certain period of time (ie. 1000 bars). So every time the 200MA changes direction, the indicator adds 1. And looking backward in that window, when an inflection drops out of the 1000bar window, the indicator subtracts 1. The net result is a general indicator for how choppy the market has been lately (lower values indicator nice prolonged moves).

I've tried two approaches with coding this and am completely stuck.

Method 1: Upon the creation of a new bar, look backward over ALL 1000 bars and add up the inflections. PROBLEM: This is a huge drain on the system and takes forever when using it in an EA for optimization because it is recalculating as it steps through every new bar.

Method 2:Use a global variable as a buffer...adding +1 when a new inflection is encountered....subtracting -1 when looking only at the 1000th previous bar and seeing an inflection thus ignoring all the bars in between. PROBLEM: The indicator plots fine however when referencing it in an EA it is constantly spitting out a value of 0. I am pulling it into my EA with an iCustom command which results in a value lof 0. I'm guessing my problem has something with the way I'm using iCustom.

Below is the code for the indicator. Can anyone tell me why this doesn't work when referencing in an EA? Is there a better method than I'm using?

//---------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_minimum 0
#property indicator_maximum 20


extern double Range=500;
extern double MovingAvg=200;


double Buf_0[];

//--------------------------------------------------------------------
int init()
{
SetIndexBuffer(0,Buf_0);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,5);
return;
}
//--------------------------------------------------------------------
int start()
{
int i, n, Counted_bars;
int LookBack=1;
double BearTwist;
string BearTwistGV="BearTwistGV";
//--------------------------------------------------------------------
Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;
while(i>=0)
{
for(n=i;n<=i+LookBack-1;n++)
{
double BearTwistGet=GlobalVariableGet(BearTwistGV);
double MARecent1=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n);
double MARecent2=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n+1);
double MARecent3=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n+2);
if(MARecent1<MARecent2 && MARecent2>MARecent3)
{
BearTwist++;
BearTwistGet=GlobalVariableSet(BearTwistGV,BearTwist);
}
double MADist1=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n+Range);
double MADist2=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n+Range+1);
double MADist3=iMA(NULL,0,MovingAvg,0,MODE_SMA,PRICE_CLOSE,n+Range+2);
if(MADist1<MADist2 && MADist2>MADist3)
{
BearTwist--;
BearTwistGet=GlobalVariableSet(BearTwistGV,BearTwist);
}
}

BearTwistGet=GlobalVariableGet(BearTwistGV);
Buf_0[i]=BearTwistGet;

i--;
}
//--------------------------------------------------------------------
return;
}

Reason: