ProRealCode Studio is on its way. As soon as it is published, this button will take you straight to its Chrome Web Store page.
ProRealCode Script is an extended language compatible with the ProBuilder syntax. Everything the two share, from IF to RSI to DRAWTEXT, is documented once, on prorealcode.com. This chapter lists only what the language adds, family by family.
Studio Only badge The form to write, with optional parts in [ ]. Words in capitals or lower case are the same word.
A short code that runs as it is. Paste it into an empty tab and click “Run”.
Where the result appears: a curve, a row in ⚙ Settings, a line in the console, a drawing.
Code using only the instructions shared with the ProBuilder syntax is in ProBuilder Compatibility Mode. Nothing declares it: the editor marks the opposite case with a Studio Only badge.
input and alert.An unlit badge does not mean the ProRealTime editor will accept the code. The check knows instruction names, and some differences are not names: two RETURN lines run in Studio and do not compile on the platform.
Copy any part of a code out of the editor, three characters or the whole tab, and the clipboard receives one comment line on top. It says which version of Studio wrote the code. The platform's editor reads it as a comment and skips it. Studio does not add it twice, and you never type it yourself.
//@ProRealCodeScript@version=x.y.z@ input(myLen, 14, "Length", int) RETURN RSI[myLen](close) AS "RSI"
FUNCTION name(param1 [= default1], param2 [= default2], …) … lines … lastExpression ENDFUNCTION
FUNCTION zScore(src, len = 20) mean = Average[len](src) sd = STD[len](src) (src - mean) / sd ENDFUNCTION RETURN zScore(close) AS "Z-score 20", zScore(close, 50) AS "Z-score 50"
FUNCTION channel(src, len) mid = Average[len](src) w = STD[len](src) RETURN mid + 2 * w, mid - 2 * w ENDFUNCTION (upper, lower) = channel(close, 20) RETURN upper AS "Upper", lower AS "Lower"
| Rule | What it means for you |
|---|---|
| The last line is the value | The function returns its last expression. RETURN x leaves early with one value, RETURN a, b with several, read with (x, y) = f(…). |
| Defaults | A parameter written len = 20 may be left out of the call. |
| Above the call | Define a function before the line that uses it. A function may call the functions written above it. |
| One history per call | Each written call keeps its own past values: zScore(close) and zScore(close, 50) are two separate series. |
| Your variables, read-only | A function reads the variables assigned before it. A variable it assigns stays inside it. |
| Not allowed | A function calling itself, a function inside another, two functions with the same name. |
DEFPARAM FunctionLibrary = true FUNCTION zScore(src, len = 20) mean = Average[len](src) sd = STD[len](src) (src - mean) / sd ENDFUNCTION
INCLUDE "my toolbox" RETURN zScore(close, 100) AS "Z-score"
RETURN, drawings, settings and other lines are ignored.IF, a loop or a function. A one-word name may go without quotes: INCLUDE toolbox.| Message in the Problems panel | What to do |
|---|---|
include 'x' not found in your codes | Save a code under that name, or fix the name. |
'x' is not a function library | Add DEFPARAM FunctionLibrary = true at the top of the library. |
CALL, shared with the ProBuilder syntaxCALL runs another saved code and takes what it returns. In Studio the values in brackets go to that code's input() settings, in the order they are declared. Only your own saved codes can be called, and a called code draws nothing.
fast = CALL "my ema"[10] slow = CALL "my ema"[50] RETURN fast AS "fast", slow AS "slow"
input(name, defaultValue [, "Title"] [, type] { , attribute = value })
| Type | Control in ⚙ Settings |
|---|---|
int | whole number |
float | number (the default type) |
bool | switch, 1 or 0 |
matype | list of average types, 0 to 8 |
string | text field |
color | colour picker, "#RRGGBB" |
timeframe | list of timeframes |
instrument | list of instruments |
| Attribute | Effect |
|---|---|
options | [a, b]: a list, nothing else |
minval maxval | the value is kept inside |
step | the field's increment, display only |
group | a section heading in ⚙ Settings |
inline | several controls on one row |
tooltip | the row's help text |
input(len, 20, "Length", int, minval = 5, maxval = 200, group = "Average") input(kind, "EMA", "Type", string, options = ["SMA", "EMA"], group = "Average") input(tint, "#ff8800", "Colour", color, group = "Look") IF kind = "EMA" THEN ma = ExponentialAverage[len](close) ELSE ma = Average[len](close) ENDIF RETURN ma AS "MA" COLOURED(tint)
What you see: ⚙ Settings of this code shows an “Average” section with “Length” and “Type”, then “Look” with a colour (chapter 08).
console(arg1, arg2, …)r = RSI[14](close) console("RSI #r#", close) RETURN r AS "RSI"
What you see: one line per bar in the >_ Console under the editor, here RSI 41.2 close = 1.1702. A text prints as written, #name# is replaced by the value, a bare name prints as name = value. The console keeps the newest 1,000 lines. PRINT(…) does exactly the same.
alert([title,] message, freq, sound)r = RSI[14](close) IF r CROSSES UNDER 30 THEN alert("RSI below 30", "RSI #r# at #close#", onceperbarclose, 1) ENDIF RETURN r AS "RSI"
What you see: a popup on the chart with the title and the message, a beep when sound is 1, and a desktop notification if you switched them on in the Alerts window (chapter 16).
freq | The alert fires |
|---|---|
freqall | on every price update of the live bar |
onceperbar | at most once per bar (the default) |
onceperbarclose | only when the live bar closes |
Add {key: value, …} to an alert, and each time it fires Studio also builds a JSON message: the instrument, the timeframe, the bar, the title, the message and your keys, valued at the firing bar. The Alert tab of ⚙ Preferences sends it to your webhook (chapter 17).
alert("Golden cross", "MA50 over MA200 at #close#", onceperbarclose, 1, {side: "long", price: close, rsi: RSI[14](close)})
onceperbarclose alert can skip a bar.| Form | Returns |
|---|---|
PivotHigh[L,R](price) | The price of the bar R bars back when it is strictly higher than the L bars before it and the R bars after it. Undefined otherwise. |
PivotLow[L,R](price) | The same for a low. |
PivotHigh[N] | Short form: L = R = N, source high (low for PivotLow). |
ValueWhen(cond, source, n) | The value of source on the bar where cond was true for the n-th most recent time. n = 0 is the latest, and may be left out. source defaults to close. |
ForceIndex[p](price) | The force index smoothed by an exponential average of p bars. Without the bracket, ForceIndex(close) is the shared instruction. |
ph = PivotHigh[5,5](high) IF ISSET(ph) THEN DRAWTEXT("#ph#", barindex - 5, ph) COLOURED(200, 0, 0) ENDIF crossUp = close CROSSES OVER ExponentialAverage[20](close) lastEntry = ValueWhen(crossUp, close) prevEntry = ValueWhen(crossUp, close, 1) RETURN lastEntry AS "Last cross", prevEntry AS "Cross before"
What you see: the price of each swing high written above it, and two flat steps that move to the close of each new upward cross of the average.
PivotHigh[5,5] knows a top 5 bars later. The value appears there, carrying the top's price.
Both read only bars that are already closed. What you saw yesterday is still there today, unlike ZigZag.
Two equal highs report nothing. Before enough bars, the value is undefined: a gap.
IF close > open THEN side = "up" ELSE side = "down" ENDIF myLabel = "Bar " + side console(myLabel) RETURN close
+ joins two texts. =, <, > compare them. myLabel[1] is the text of the bar before.
DRAWTEXT(myLabel, …), DRAWLABEL, TABLECELL, console(). Not into RETURN, which draws numbers, and not as a condition: write IF side = "up" THEN. Both mistakes are reported before the code runs.
DRAWTEXT takes one text or one variable, not a join: write "Bar #side#" there.
A name that starts with $ is an array: cells $a[0] to $a[999999]. A cell never written reads 0. An array is not a history: $a[2] is cell 2, not two bars ago, and the array keeps its content from bar to bar, so clear it with ARRAYCLEAR($a) when needed.
ARRAYFROM($w, close, 20) mid = ARRAYMEDIAN($w) spread = ARRAYPERCENTILE($w, 75) - ARRAYPERCENTILE($w, 25) RETURN mid AS "Median 20", spread AS "Spread 25-75"
| Family | Instructions |
|---|---|
| Size and order | ARRAYSIZE ARRAYCLEAR ARRAYPUSH ARRAYPOP ARRAYSHIFT ARRAYUNSHIFT ARRAYINSERT ARRAYREMOVE ARRAYSET ARRAYGET |
| Statistics | ARRAYSUM ARRAYAVG ARRAYMEDIAN ARRAYMODE ARRAYVARIANCE ARRAYSTDDEV ARRAYMIN ARRAYMAX ARRAYRANGE ARRAYPERCENTILE |
| Search | ARRAYINDEXOF ARRAYLASTINDEXOF ARRAYINCLUDES ARRAYCOUNT |
| Build | ARRAYSORT ARRAYREVERSE ARRAYFILL ARRAYNEW ARRAYFROM ARRAYCOPY ARRAYSLICE ARRAYCONCAT |
MarketData("TICKER", timeframe, source, mode)
| Part | What to write |
|---|---|
| Instrument | "EURUSD", or "" for the chart's own instrument, or an input() of type instrument. |
| Timeframe | 1 hour, 15 minutes, daily… or default for the chart's own, or an input() of type timeframe. |
| Source | open high low close volume range typicalprice medianprice weightedclose totalprice. |
| Mode | default: the candle as it stands. updateonclose: closed candles only, for a backtest. |
eu = MarketData("EURUSD", 1 hour, close, updateonclose) RETURN Average[20](eu) AS "EUR/USD hourly MA"
MarketData: cannot resolve '…' and the value stays undefined. It is never an error: the chart keeps drawing.rebuilt locally from the chart timeframe. Daily and above can differ by a candle.TIMEFRAME, chosen from ⚙ SettingsTIMEFRAME(…) is shared with the ProBuilder syntax. In Studio the timeframe can be a setting, declared first. A timeframe finer than the chart's is refused.
input(mytf, "1 hour", "Timeframe", timeframe) TIMEFRAME(mytf) h = RSI[14](close) TIMEFRAME(default) RETURN h AS "RSI, higher timeframe"
SESSION("mode")"isopen" | 1 when the bar opens inside the trading hours |
"ispremarket" | 1 between pre-open and open |
"isbreak" | 1 during the lunch break |
"isfirstbar" | 1 on a session's first bar |
"opentime" | open time, HHMMSS |
inHours = SESSION("isopen") and not SESSION("isbreak") IF SESSION("isfirstbar") THEN BACKGROUNDCOLOR(0, 120, 255, 30) ENDIF RETURN inHours AS "Market open"
What you see: a 1 / 0 curve, each session's first bar tinted blue. "isopen" alone counts the break as open.
BID and ASKBID BESTBID | best bid price |
ASK BESTASK | best ask price |
BIDSIZE ASKSIZE | volume offered at that price |
BIDVOLUME ASKVOLUME | volume carried by that quote |
IF ISSET(bid) AND ISSET(ask) THEN spreadTicks = (ask - bid) / ticksize ELSE spreadTicks = undefined ENDIF RETURN spreadTicks AS "Spread (ticks)"
ISSET.Six names read the support and resistance lines ProRealTrend computes for the chart, whether it is shown or hidden. With no line, the code still runs. All six light the Studio Only badge.
TrendLineCount | how many lines, shown or hidden; 0 when there are none |
TrendLine(i) | level of line i on the current candle; undefined before its start, after its end, or for an i outside 1 to TrendLineCount: test with IsSet |
TrendLineType(i) | 1 support, -1 resistance |
TrendLineImportance(i) | 3 major, 2 intermediate, 1 minor, as ProRealTrend ranks the line |
TrendLineOblique(i) | 1 oblique, 0 horizontal; an oblique runs straight from candle to candle |
TrendLinePeriod(i) | the line's own timeframe in seconds, like GetTimeframe: day 86400, week 604800, month 2592000. On a daily chart ProRealTrend mixes all three |
nearSup = undefined nearRes = undefined FOR i = 1 TO TrendLineCount DO lvl = TrendLine(i) IF IsSet(lvl) THEN IF lvl <= close AND (NOT IsSet(nearSup) OR lvl > nearSup) THEN nearSup = lvl ENDIF IF lvl > close AND (NOT IsSet(nearRes) OR lvl < nearRes) THEN nearRes = lvl ENDIF ENDIF NEXT RETURN nearSup AS "Nearest support", nearRes AS "Nearest resistance"
Two curves that follow the closest line under the price and the closest line over it. When ProRealTrend recomputes its lines, the indicator recalculates.
A major horizontal resistance breaks: in the same loop, keep the lines where TrendLineType(i) = -1 AND TrendLineImportance(i) = 3 AND TrendLineOblique(i) = 0, then test close > lvl AND close[1] <= lvl.
Distance to a higher-timeframe level: keep the lines where TrendLinePeriod(i) > GetTimeframe, then compute (close - lvl) / lvl * 100.
TrendLine(i) is where today's line passes, not one known then. Decide on the last candles; do not backtest on these values.t = DRAWTABLE(rows, cols, position [, width [, height]]) TABLECELL(t, row, col, "text" [, font] [, style] [, size] [, align] [, "tooltip"]) TABLEMERGE(t, row, col, rowSpan, colSpan) TABLEBG(t, row, col, r, g, b [, alpha])
DEFPARAM DrawOnLastBarOnly = true r = RSI[14](close) t = DRAWTABLE(2, 2, topright) FILLCOLOR(20, 24, 32) TABLECELL(t, 0, 0, "RSI 14") COLOURED(200, 200, 200) TABLECELL(t, 0, 1, "#r#") COLOURED(255, 255, 255) TABLEMERGE(t, 1, 0, 1, 2) TABLECELL(t, 1, 0, "close #close#") RETURN
| RSI 14 | 41.25 |
| close 1.17021 | |
| Rule | What it means for you |
|---|---|
DRAWTABLE is assigned | It returns a handle, t here, that the other instructions take first. |
| Corners | topleft, topright (default), bottomleft, bottomright, center. The table stays clear of the price scale. |
| Size | Leave out width and height and each column fits its text. Writing past the declared rows or columns makes the table grow. |
| Colours | COLOURED is the text, FILLCOLOR the background, BORDERCOLOR the lines. TABLEBG changes one cell's background only. |
| One table per line | A table is drawn once for the whole chart: a cell written on every bar shows the last bar's value. DEFPARAM DrawOnLastBarOnly = true is the cheap way. |
| Limits | Up to 4,000 cells per code. Tooltips are kept but cannot be hovered: the chart layer takes no mouse. |
TABLEGAUGE(t, row, col, value, min, max [, "label"]) TABLEBAR (t, row, col, value, min, max [, "label"]) TABLEHEAT (t, row, col, value, min, max [, "text"]) TABLESPARK(t, row, col, series, period)
DEFPARAM DrawOnLastBarOnly = true r = RSI[14](close) t = DRAWTABLE(2, 2, bottomright) TABLEGAUGE(t, 0, 0, r, 0, 100, "RSI") COLOURED(90, 170, 255) TABLESPARK(t, 0, 1, close, 30) TABLEBAR(t, 1, 0, r, 0, 100, "RSI") TABLEHEAT(t, 1, 1, r, 100, 0, "#r#") RETURN
| Widget | Shows |
|---|---|
TABLEGAUGE | a level: a half dial with a needle |
TABLEBAR | a size: a bar filled from min to value |
TABLESPARK | a shape: the last period values, newest on the right, up to 200 |
TABLEHEAT | a zone: the cell coloured from low to high. min above max reverses it |
DRAWLABEL example on Spot EUR/USD. Captured with the platform in dark mode.DRAWLABELDRAWLABEL(x, y, "text" [, shape] [, size] [, align] [, "tooltip"])
ph = PivotHigh[5,5](high) IF ISSET(ph) THEN DRAWLABEL(barindex - 5, ph, "#ph#", arrowdown, small) FILLCOLOR(255, 200, 0, 200) ENDIF RETURN
GRADIENTCOLORGRADIENTCOLOR(value, low, high, colourLow [, colourMid], colourHigh)
RETURN close AS "Price" GRADIENTCOLOR(RSI[14](close), 30, 70, "#4040ff", "white", "#ff4040")
COLORBETWEEN in two coloursCOLORBETWEEN(v1, v2, topColour, bottomColour [, topValue, bottomValue])
ma = Average[20](close) COLORBETWEEN(ma, close, "#00e5b019", "#00e5b0cc") RETURN ma AS "MA"
What you see: the band between the average and the price, almost clear along the average and denser towards the price, inside every bar.
DEFPARAM overlay and ONPRICEDEFPARAM overlay = false puts the whole code in its own panel under the chart; true, the default, puts it on the price. ONPRICE, written after one drawing, sends that single drawing to the price chart while the rest stays in the panel.
DEFPARAM overlay = false myrsi = RSI[14](close) IF myrsi CROSSES OVER 30 THEN DRAWARROWUP(barindex, low) COLOURED(0, 200, 0) ONPRICE ENDIF RETURN myrsi AS "RSI", 70 AS "70", 30 AS "30"
What you see: the RSI and its two levels in a panel, and a green arrow under the candle each time the RSI crosses back over 30.
DEFPARAM Hedging = True DEFPARAM CumulateOrders = False IF close CROSSES OVER Average[50](close) THEN BUY 1 SHARES AT MARKET ENDIF IF RSI[14](close) > 70 THEN SELLSHORT 1 SHARES AT MARKET ENDIF IF RSI[14](close) < 50 THEN EXITSHORT AT MARKET ENDIF SET STOP %LOSS 2
With Hedging = True a short order no longer closes the long one: the two positions live side by side. SELL closes only the long, EXITSHORT only the short. The default is False.
CumulateOrders and the stops apply to each side on its own. Declare it before the first order; changing it while a position or an order is open is an error.
| You want to | Write |
|---|---|
| reuse a calculation | FUNCTION … ENDFUNCTION, INCLUDE "library" |
| let the reader tune it | input(name, default, "Title", type) |
| see a value, be warned | console(), alert(…, {key: value}) |
| mark swing points | PivotHigh, PivotLow, ValueWhen |
| read other data | MarketData, SESSION, BID / ASK, TrendLine(i) |
| draw and colour | DRAWTABLE, DRAWLABEL, GRADIENTCOLOR, ONPRICE |
Chapter 24 · All preferences