# Implement choppiness index function in EA

I am willing to write a choppiness index function inside my expert to use it in the following way :

```if(chopIndex <= 40)
{
//Do the following
}```

Using Pinescript on TradingView, I was able to simply do the following:

```length = input(14, minval=1)
//Choppiness index formula
ci = 100 * log10(sum(atr(1), length) / (highest(length) - lowest(length))) / log10(length)```

I found an MT4 indicator but didn't know how to extract the function :

https://forex-station.com/app.php/attach/file/3316129
Board index
• forex-station.com
When it comes to the MetaTrader Platform, Forex Station is the best forex forum for sourcing Non Repainting MT4/MT5 Indicators, Trading Systems & EA's.

```//+------------------------------------------------------------------+
/*| Choppiness Index (CHOP) - Tradingview Wiki                       |
//+------------------------------------------------------------------+
100 * LOG10( SUM(ATR(1), n) / ( MaxHi(n) - MinLo(n) ) ) / LOG10(n)

n = User defined period length.
LOG10(n) = base-10 LOG of n
ATR(1) = Average True Range (Period of 1)
SUM(ATR(1), n) = Sum of the Average True Range over past n buffers
MaxHi(n) = The highest high over past n  buf[]                             */
double   Chop(COUNT n, INDEX s=0){  return Chop(_Symbol, PERIOD_CURRENT, n, s);}
double   Chop(ENUM_TIMEFRAMES tf,
COUNT n, INDEX s=0){  return Chop(_Symbol, tf,             n, s);}
double   Chop(SYMBOL sym, ENUM_TIMEFRAMES tf, COUNT n, INDEX s=0){
PRICE    HH    = iHigh(sym, tf, iHighest(sym, tf, MODE_HIGH, n, s) );
PRICE    LL    =  iLow(sym, tf,  iLowest(sym, tf, MODE_LOW,  n, s) );
CHANGE   Eatr  = 0.0; for(COUNT i=n; i>0;--i) Eatr += iATR(sym, tf, 1, s++);
return 100 * log10(Eatr / (HH-LL)) / log10(n);
}
```

William Roeder:
Thanks for your suggested solution; however, it didn't work as it's throwing some errors, have you had a chance to test it?

It worked fine when I wrote the indicator five years ago.

William Roeder:
It worked fine when I wrote the indicator five years ago.
I had to make some changes to make it work. Thanks a lot anyway!
Reason: