iCustom in Pine Script: no equivalent, so inline the logic

Short answer

Pine Script has no equivalent: a script cannot load or run another script. Put the called indicator's calculation into your script, import it from a Pine library, or let the user feed the other indicator's plot in with input.source.

iCustom loads another compiled indicator (.ex5) by name, passes it input values and returns a handle whose buffers you read with CopyBuffer. Pine Script has no equivalent: a script cannot load or run another script. The three ways to port it are to put the called indicator's calculation into your script, to import that calculation from a Pine library, or to let the user feed another indicator's plot into your script with input.source.

What iCustom computes

The signature is iCustom(symbol, timeframe, name, …). name is a path relative to MQL5\Indicators (for example "Examples\\Custom Moving Average"), and the remaining arguments are that indicator's input parameters in declaration order; omitted trailing inputs take their defaults. The terminal loads the .ex5, runs its OnCalculate on the requested symbol and timeframe in the background, and exposes every buffer it registered with SetIndexBuffer by index. The called indicator's arrays are oldest-first like any other, and its values become available only once BarsCalculated(handle) reaches the bar count.

The Pine Script equivalent

There is no function to call. The usual port is to read the called indicator's source and write its calculation inline; for the stock "Custom Moving Average" used below with MODE_SMMA, that is one line of ta.rma. When the logic is shared by several scripts, publish it as a Pine library and import it. When the user runs the other indicator on TradingView anyway, input.source lets them pick one of its plots in your script's settings; the value then arrives as an ordinary series. request.security does not help here: it evaluates an expression of your own script on another symbol or timeframe, it cannot run a different script.

//@version=6
indicator("iCustom replacement", overlay = true)
// Option 1: inline the custom indicator's logic. Here: MetaTrader's
// "Custom Moving Average" example called with period 13, shift 0, MODE_SMMA.
len   = input.int(13, "MA period", minval = 1)
shift = input.int(0, "MA shift")
smma  = ta.rma(close, len)
plot(smma, "Inlined custom MA", color.rgb(30, 144, 255), offset = shift)

// Option 2: take a plot of another indicator already on the chart.
// The user picks it in the settings, e.g. "Custom MA: Plot".
ext = input.source(close, "Value from another indicator")
plot(ext, "External source", color.gray)

The MQL5 indicator this replaces, calling the stock example with period 13, shift 0 and MODE_SMMA:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue

input int InpPeriod = 13; // passed to the custom indicator

double OutBuffer[];
int    custHandle;

int OnInit()
  {
   SetIndexBuffer(0, OutBuffer, INDICATOR_DATA);
   // Path is relative to MQL5\Indicators; the extra arguments are that indicator's inputs, in order
   custHandle = iCustom(_Symbol, _Period, "Examples\\Custom Moving Average", InpPeriod, 0, MODE_SMMA);
   if(custHandle == 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(custHandle) < rates_total)
      return(0);
   int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
   if(CopyBuffer(custHandle, 0, 0, to_copy, OutBuffer) <= 0)
      return(0);
   return(rates_total);
  }

Where the two differ

TopicMQL5 iCustomPine Script
Calling another indicatorBy file name, at run timeNot possible
Sharing logicCompiled .ex5 or #includePublished library with import
InputsPositional arguments after the nameFunction parameters of the inlined or library code
OutputsBuffers read by indexInlined code returns whatever series you need
Other indicator on chartNot required; runs invisiblyinput.source requires it to be on the same chart and selected by the user
Different timeframeTimeframe argumentWrap the inlined calculation in request.security

What the Stratlate converter emits

The MQL5 → Pine engine reports iCustom as unsupported: the catalog entry is Unsupported("Pine has no MQL-custom-indicator dispatch"), which raises warning MQLW0500 and lowers the call to na. In our probe of the indicator above, the handle and the plot buffer both became float(na), and because that buffer was the only thing plotted the engine also raised error MQLE0522, saying the converted script would draw nothing and that the Pine is shown for reference only. To convert such a program, convert the called indicator's source as well, or inline its calculation, and replace the iCustom reads with that series.

Pitfalls

  • Porting only the calling program. Without the called indicator's source there is nothing to compute, and the converted script plots nothing.
  • Getting the input order wrong when inlining. iCustom passes inputs positionally; check them against the called file's input declarations in order, and remember that omitted trailing inputs take the called file's defaults.
  • Reading the wrong buffer. CopyBuffer indices follow SetIndexBuffer order in the called file, which is not always the order of its plots.
  • Expecting request.security to call a published script. It only evaluates expressions from your own script.
  • Relying on input.source in an automated setting. The link to the other indicator exists only on that chart and must be selected by hand.

FAQ

Is there any Pine function like iCustom?

No. Pine scripts cannot load other scripts. The nearest mechanisms are libraries, which share functions but not running indicators, and input.source, which reads a plot of another indicator on the same chart.

Can I use a TradingView indicator whose source is hidden?

Only through input.source on a chart where it is applied, and only its visible plots. There is no programmatic access to protected or invite-only code.

What about MQL5 resources (#resource) bundling the custom indicator?

The same answer applies: the bundled indicator still has to be ported as code. Pine has no notion of embedding another compiled script.

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

Related