iCCI in Pine Script: ta.cci with the applied price as source
Short answer
Use ta.cci(source, length) with the same source: MetaTrader's default PRICE_TYPICAL is Pine's hlc3. Both use the 0.015 constant over the same window, so the lines are identical.
iCCI returns a handle to MetaTrader's Commodity Channel Index; the Pine Script equivalent is ta.cci(source, length). Both compute the distance of the price from its simple moving average divided by 0.015 times the mean absolute deviation over the same window, so the two lines are identical once you pass the same source: MetaTrader's default PRICE_TYPICAL is Pine's hlc3.
What iCCI computes
The signature is iCCI(symbol, timeframe, ma_period, applied_price). For each bar i with at least n bars of history the stock indicator (Indicators\Examples\CCI.mq5) computes the simple average m of the applied price over the last n bars, the mean absolute deviation D = Σ|p − m| / n over the same bars, and the line CCI = (p − m) / (0.015 · D). If D is zero the value is 0. The 0.015 constant is Lambert's original scaling, chosen so that about three quarters of readings fall between −100 and +100. Cells before the first full window hold 0.0 and are hidden by PLOT_DRAW_BEGIN. Unlike many MetaTrader oscillators there is no recursion here: every value depends only on the last n prices, so there is no seed effect and no dependence on where the loaded history starts.
The Pine Script equivalent
ta.cci(source, length) implements the same three steps with the same constant. The applied price becomes the source argument; because the MetaTrader default is the typical price, a port that keeps the defaults should use hlc3, not close.
//@version=6
indicator("iCCI port", overlay = false)
len = input.int(14, "Period", minval = 1)
src = input.source(hlc3, "Applied price") // PRICE_TYPICAL is MetaTrader's default
cci = ta.cci(src, len)
plot(cci, "CCI", color.rgb(30, 144, 255))
hline(100, "Upper", color.gray)
hline(-100, "Lower", color.gray)The MQL5 indicator this replaces, which reads the built-in CCI through its handle and draws the ±100 levels with #property lines:
#property indicator_separate_window
#property indicator_level1 -100
#property indicator_level2 100
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 14; // ma_period
input ENUM_APPLIED_PRICE InpPrice = PRICE_TYPICAL; // applied_price
double CciBuffer[];
int cciHandle;
int OnInit()
{
SetIndexBuffer(0, CciBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod - 1);
cciHandle = iCCI(_Symbol, _Period, InpPeriod, InpPrice);
if(cciHandle == INVALID_HANDLE)
return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
if(BarsCalculated(cciHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(cciHandle, 0, 0, to_copy, CciBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iCCI | Pine ta.cci |
|---|---|---|
| Formula | (p − SMA) / (0.015 · mean absolute deviation) | Identical |
| Default source | PRICE_TYPICAL = (high + low + close) / 3 | Whatever you pass; use hlc3 to match |
| Zero deviation | 0.0 | na (division by zero); happens only on a window of identical prices |
| Warm-up | 0.0 for the first n−1 bars | na for the first n−1 bars |
| History start | Irrelevant: the window is finite, so both platforms agree from bar n−1 on | |
| Levels | #property indicator_level1 / level2 | hline(100) / hline(-100) |
| Source flexibility | Seven applied-price enums | Any series, including another indicator's output |
What the Stratlate converter emits
The MQL5 → Pine engine maps iCCI directly to ta.cci(<applied price as a Pine series>, int(period)) (PineCatalog, entry iCCI) and replaces the handle variable and its CopyBuffer with an assignment of that series to the buffer. Our probe with PRICE_TYPICAL converted with no warnings and emitted B0 := ta.cci(hlc3, int(14)), so the applied price is carried across rather than defaulted to close. The mapping is treated as exact; the differential verifier compares the output against the original on the same bars, and you should still compile and compare the result yourself.
Pitfalls
- Porting
iCCI(…, PRICE_TYPICAL)asta.cci(close, n). The lines differ on every bar; the fix ishlc3. - Rewriting the deviation with
ta.stdev. CCI uses the mean absolute deviation, not the standard deviation; the standard-deviation version is roughly 20% smaller in magnitude on typical data and all threshold crossings move. - Reading MetaTrader's early 0.0 cells as "CCI at zero". They are unfilled warm-up values, shown as
nain Pine. - Expecting a
na-safe result on flat bars. A window of n identical prices gives 0.0 in MetaTrader andnain Pine; wrap withnz()if the series feeds a comparison. - Reading
CciBuffer[0]as the current bar in MT5. WithoutArraySetAsSeriesthat is the oldest bar; the current value isCciBuffer[rates_total − 1].
FAQ
Are iCCI and ta.cci exactly the same?
Yes, for the same source and period. Both are a finite-window calculation with the 0.015 constant and mean absolute deviation, so they agree from the first complete window onwards with no seed effect.
Which Pine source matches each applied price?
PRICE_CLOSE → close, PRICE_OPEN → open, PRICE_HIGH → high, PRICE_LOW → low, PRICE_MEDIAN → hl2, PRICE_TYPICAL → hlc3, PRICE_WEIGHTED → hlcc4.
Can I smooth the CCI the way some MT5 templates do?
Yes: pass the CCI into any moving average, for example ta.sma(ta.cci(hlc3, 14), 5). In MQL5 the same thing needs an iMA on a custom indicator or a second buffer computed by hand.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter