ProRealCode Studio — extend ProRealTime Web from Chrome

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #266619 quote
    NicolasNicolas
    Keymaster
    Legend

    Hello everyone,

    ProRealCode Studio — Customize ProRealTime with thousands of codes — and create your own.

    ProRealCode Studio is a full development environment docked in the ProRealTime™ web platform. You log in with your own ProRealTime account as usual; Studio draws on top of that session.

    Studio runs ProRealCode Script, an extended language compatible with the ProBuilder syntax. Bring your existing codes as they are, then add great new possibilities with the extended instructions.

    Three steps. No setup.

    1. Add the extension — Install ProRealCode Studio from the Chrome Web Store.
    2. Open ProRealTime™ Web — Log in with your own account, as usual, and open a chart.
    3. Open Studio — Click ProRealCode Studio inside the platform, pick an example and press Ctrl + Enter.

    For Chrome — also works in Edge, Brave, Opera and other Chromium-based browsers.

    Templates, replay, alerts, backtests and more — on the same chart.

    • Code editor — A complete IDE, docked under your chart.
    • Fully compatible, and beyond — Your codes run as they are, on the candles or in their own panels.
    • Chart templates — A whole chart setup, saved and restored in one click.
    • Bar replay — Rewind the market, play it forward, trade it on paper.
    • Automated trading via webhook — Every strategy signal sent as JSON to the service you choose.
    • Alerts from your codes — alert() in a code: a notification on your desktop.
    • Backtests — A code with buy and sell instructions becomes a strategy.
    • Optimizer — Give each setting a range. The optimizer runs every combination, ranks them on the objective you choose and draws the surface.
    • Dashboard — Build your own board of cards — a chart, a table of several instruments, a KPI — each on the instrument and timeframe you choose, with its indicators.
    • The prorealcode.com library — Search the public library of indicators and strategies without leaving your chart.
    • Share codes with friends, privately — Give a code to the members you choose. They apply it to their own chart and adjust its settings, and never see its source: the code is computed on the ProRealCode server.

    Backtests, optimizations and replays are simulations on historical data. They do not predict future results and are not investment advice.

    Screenshots are in the attachments below.

    Find out more: the presentation page · the user guide.

    Questions, bug reports and feature ideas are welcome: reply in this topic, we read everything.

    JC_Bywan and GraHal thanked this post
    prorealcode-studio-en-prorealcode-studio-featured.jpg prorealcode-studio-en-prorealcode-studio-featured.jpg prorealcode-studio-en-stage-editor-1920.jpg prorealcode-studio-en-stage-editor-1920.jpg prorealcode-studio-en-stage-multi-1920.jpg prorealcode-studio-en-stage-multi-1920.jpg prorealcode-studio-en-dashboard-real.jpg prorealcode-studio-en-dashboard-real.jpg
    #266648 quote
    NicolasNicolas
    Keymaster
    Legend

    ProRealCode Script: all your codes, plus what you have been asking for

    First things first: ProRealCode Script is compatible with the ProBuilder syntax. The indicators and strategies you already have run in Studio as they are, with no rewrite.

    Studio also reads more than one syntax: the //@version scripts written for a well-known web charting platform are recognised on their own when you paste them into the editor, with no setting to change.

    Then it goes further. Studio adds instructions and features that the platform’s own language does not have, and many of them have been requested on this forum for years. Every one is documented in the user guide, with its syntax and an example: What ProRealCode Script adds.

    A non-exhaustive selection:

    • Alerts from the code — alert() fires from inside an indicator: a message in Studio, a sound, a desktop notification. Add a {key: value} block and the same alert leaves as JSON for a webhook.
    • External variables written in the code — input() declares a setting with its title, type and limits, and it shows up as a field of the Variables form.
    • Functions — FUNCTION … ENDFUNCTION: write a calculation once and call it as often as you like. Each call keeps its own history, and a function can return several values.
    • Function libraries — keep your functions in one saved code and pull them into any other with INCLUDE.
    • Colour gradients — GRADIENTCOLOR turns a value into a colour between two or three stops; COLORBETWEEN fills between two curves with a fade from one colour to another.
    • Draw in the oscillator and on the price at once — ONPRICE sends a drawing of a panel indicator onto the price chart: the oscillator keeps its own panel while its signals, zones or fills appear on the candles.
    • Many more array instructions — ARRAYSUM, ARRAYAVG, ARRAYMEDIAN, ARRAYPERCENTILE, ARRAYSTDDEV, ARRAYVARIANCE, ARRAYPUSH, ARRAYPOP, ARRAYINSERT, ARRAYSLICE, ARRAYINDEXOF and more, on arrays of up to a million values.
    • A console — console() prints the values you want to check while the code runs.
    • Other instruments in the same code — MarketData() reads another instrument or another timeframe.
    • Tables on the chart — DRAWTABLE and TABLECELL pin a table to a corner of the chart, filled from your code, with gauges, bars, heat cells and sparklines inside the cells.
    • Labels — DRAWLABEL puts a labelled shape on a bar: 14 shapes, 5 sizes, an optional tooltip.
    • Pivots that never repaint — PivotHigh and PivotLow, confirmed; ValueWhen() returns the value a series had the last time a condition was true.
    • Live bid and ask — BID, ASK and their sizes.
    • The trading session — SESSION() tells where a bar stands in the session.
    • ProRealTrend lines in your code — TrendLine() reads the support and resistance lines ProRealTrend draws on the chart.
    • Long and short at once — DEFPARAM Hedging = True holds long and short positions side by side in a strategy.

    One thing to keep in mind: a code that uses these additions runs in Studio, not on the platform’s own engine. The editor shows a Studio Only badge on such a code and names the instructions concerned. Everything the two share is documented as always on prorealcode.com/documentation.

    This is only the beginning. Anyone can ask for an improvement or a new instruction: reply in this topic, we will be glad to study it and deliver it as quickly as we can.

    In the next posts of this topic we will publish examples, one at a time, each with a capture taken on the platform.

    JC_Bywan and JS thanked this post
    #266656 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 1 — alerts from the code, with settings declared in the code

    An EMA crossover that warns you by itself. When the fast EMA crosses the slow one, the code draws a Buy or Sell label on the candle and calls alert(): you get a message in Studio, a sound and a desktop notification, and the same alert leaves as a JSON message if you send your signals to a webhook.

    What the code uses

    • input() — declares the two EMA lengths with a title and a type. They show up as fields of the Variables form, ready to change without touching the code.
    • alert(title, message, frequency, sound, {payload}) — #close# in the message is replaced by the closing price, onceperbar fires at most once per bar, and the {side, price} block is the JSON a webhook receives.
    • DRAWLABEL — the Buy and Sell labels, with a tooltip that reads the two lengths.
    • COLORBETWEEN — the ribbon between the two averages, green when the fast one is above, red when it is below.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter. Alerts fire on live updates while a ProRealTime tab is open; switch desktop notifications on in the Alerts panel. To forward the JSON, set your webhook URL in the Alert preferences.

    // ProRealCode Script example — alerts from the code
    // An EMA crossover that warns you by itself: a message in Studio, a sound,
    // a desktop notification, and a JSON message ready for a webhook.
    // input() turns the two lengths into fields of the Variables form.
    
    input(fastLen, 9, "Fast EMA", int)
    input(slowLen, 21, "Slow EMA", int)
    
    fastMa = ExponentialAverage[fastLen](close)
    slowMa = ExponentialAverage[slowLen](close)
    atr = AverageTrueRange[14](close)
    
    // The ribbon between the two averages, green above, red below
    IF fastMa >= slowMa THEN
      COLORBETWEEN(fastMa, slowMa, 60, 190, 120, 60)
    ELSE
      COLORBETWEEN(fastMa, slowMa, 230, 90, 90, 60)
    ENDIF
    
    IF fastMa CROSSES OVER slowMa THEN
      DRAWLABEL(barindex, low - 0.6 * atr, "Buy", arrowup, small, "EMA #fastLen# crossed over EMA #slowLen#") COLOURED(20, 22, 28) FILLCOLOR(120, 205, 150) BORDERCOLOR(30, 120, 70)
      alert("EMA cross", "Bullish cross at #close#", onceperbar, 1, {side: "long", price: close})
    ENDIF
    
    IF fastMa CROSSES UNDER slowMa THEN
      DRAWLABEL(barindex, high + 0.6 * atr, "Sell", arrowdown, small, "EMA #fastLen# crossed under EMA #slowLen#") COLOURED(235, 235, 235) FILLCOLOR(170, 60, 70) BORDERCOLOR(215, 95, 105)
      alert("EMA cross", "Bearish cross at #close#", onceperbar, 1, {side: "short", price: close})
    ENDIF
    
    RETURN fastMa AS "Fast EMA" COLOURED(90, 170, 255) STYLE(line, 2), slowMa AS "Slow EMA" COLOURED(240, 180, 60) STYLE(line, 2)

    The captures: the Mini Nasdaq 100 future in 1 minute with the code open in Studio, at the moment a live Sell signal fires. The alert shows up in Studio with its sound, and as a desktop notification.

    JC_Bywan thanked this post
    prorealcode-studio-example-1-alerts-live.jpg prorealcode-studio-example-1-alerts-live.jpg prorealcode-studio-example-1-desktop-notification.png prorealcode-studio-example-1-desktop-notification.png
    #266659 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 2 — functions

    Write a calculation once, call it as often as you like. This code defines a Hull moving average as a function and calls it twice, for a fast line and a slow one, plus a second function that returns both edges of an ATR channel in a single call.

    What the code uses

    • FUNCTION … ENDFUNCTION — hullMa(src, len) takes a series and a length; the value of its last line is what it returns.
    • One history per call — hullMa(close, fastLen) and hullMa(close, slowLen) each keep their own averages, so the two lines never mix.
    • Several values at once — atrChannel ends with RETURN mid + width, mid – width, and (upper, lower) = atrChannel(bandLen, bandMult) receives both.
    • input() — the two Hull lengths, the channel length and its width are fields of the Variables form.

    Good to know: a function is defined above the place where it is called, it cannot call itself, and it reads the code’s other variables without changing them.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter.

    // ProRealCode Script example — functions
    // FUNCTION ... ENDFUNCTION: write a calculation once, call it as often as you like.
    // Each call keeps its own history, and a function can return several values.
    
    input(fastLen, 16, "Fast Hull length", int)
    input(slowLen, 55, "Slow Hull length", int)
    input(bandLen, 20, "Channel length", int)
    input(bandMult, 2, "Channel width (ATR)", float)
    
    // A Hull moving average, written once
    FUNCTION hullMa(src, len)
      halfMa = WeightedAverage[MAX(1, ROUND(len / 2))](src)
      fullMa = WeightedAverage[len](src)
      WeightedAverage[MAX(1, ROUND(SQRT(len)))](2 * halfMa - fullMa)
    ENDFUNCTION
    
    // A channel that returns two values at once
    FUNCTION atrChannel(len, mult)
      mid = ExponentialAverage[len](close)
      width = mult * AverageTrueRange[len](close)
      RETURN mid + width, mid - width
    ENDFUNCTION
    
    fastHull = hullMa(close, fastLen)
    slowHull = hullMa(close, slowLen)
    (upper, lower) = atrChannel(bandLen, bandMult)
    
    COLORBETWEEN(upper, lower, 120, 140, 255, 25)
    
    IF fastHull >= slowHull THEN
      r = 60
      g = 190
      b = 120
    ELSE
      r = 230
      g = 90
      b = 90
    ENDIF
    
    RETURN fastHull AS "Hull fast" COLOURED(r, g, b) STYLE(line, 3), slowHull AS "Hull slow" COLOURED(200, 200, 210) STYLE(line, 1), upper AS "Channel top" COLOURED(120, 140, 255), lower AS "Channel bottom" COLOURED(120, 140, 255)

    The capture: the Mini Nasdaq 100 future in 1 minute with the code open in Studio. The fast Hull turns green above the slow one (grey) and red below it, inside the ATR channel (blue).

    JC_Bywan thanked this post
    prorealcode-studio-example-2-functions.jpg prorealcode-studio-example-2-functions.jpg
    #266661 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 3 — colour gradients, and one code drawing in its panel and on the price

    An RSI in its own panel that also paints the candles of the price chart in its colour: green when the RSI is low, grey around the middle, red when it is high.

    What the code uses

    • GRADIENTCOLOR(value, low, high, colour low, colour middle, colour high) — turns the RSI into a colour between three stops: green at 30 and below, grey at 50, red at 70 and above. It is used three times: on the RSI line, on the fill and on the candles.
    • COLORBETWEEN with a GRADIENTCOLOR — the fill between the RSI and the 50 line takes the RSI’s colour on every bar.
    • DEFPARAM overlay = false — the code gets its own panel under the chart.
    • ONPRICE — written at the end of the DRAWCANDLE line, it sends that one drawing onto the price chart while everything else stays in the panel. The values it draws are prices (open, high, low, close), because that drawing lands on the price scale.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter.

    // ProRealCode Script example — colour gradients and ONPRICE
    // An RSI in its own panel that also paints the candles of the price chart.
    // GRADIENTCOLOR turns a value into a colour; ONPRICE sends one drawing
    // of this panel indicator onto the price chart.
    
    DEFPARAM overlay = false
    
    input(len, 14, "RSI length", int)
    
    myRsi = RSI[len](close)
    
    // In the panel: the RSI line, coloured by its own value
    // and a fill that fades towards the 50 line
    COLORBETWEEN(myRsi, 50, GRADIENTCOLOR(myRsi, 30, 70, "#3fbf7f66", "#8fa4c022", "#e0556366"))
    
    // On the price chart: every candle takes the colour of the RSI
    DRAWCANDLE(open, high, low, close) GRADIENTCOLOR(myRsi, 30, 70, "#3fbf7f", "#8fa4c0", "#e05563") ONPRICE
    
    RETURN myRsi AS "RSI" GRADIENTCOLOR(myRsi, 30, 70, "#3fbf7f", "#8fa4c0", "#e05563") STYLE(line, 2), 70 AS "70" COLOURED(150, 150, 160), 50 AS "50" COLOURED(110, 110, 120), 30 AS "30" COLOURED(150, 150, 160)

    The capture: the Mini Nasdaq 100 future in 1 minute in the light theme, with the candles coloured by the RSI, the RSI and its fill in the panel below, and the code open in Studio.

    prorealcode-studio-example-3-gradients-onprice.jpg prorealcode-studio-example-3-gradients-onprice.jpg
    #266663 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 4 — arrays

    The last 50 closes go into an array, and the array gives what a single variable never could: their median, the band that holds 80% of them, their dispersion, and where the current close ranks among them. When the close becomes the highest or the lowest of its window, a flag marks it.

    What the code uses

    • ARRAYFROM($w, close, len) — fills the array with the last len closes in one line, the oldest in $w[0] and the current one in $w[len – 1].
    • ARRAYMEDIAN, ARRAYPERCENTILE, ARRAYSTDDEV — statistics on the whole array: the median (orange), the 10th and 90th percentiles (the blue band), and the standard deviation used to place the flags.
    • ARRAYCOPY, ARRAYSORT, ARRAYINDEXOF — a sorted copy of the window, then the position of the current close in it: 0 is the lowest of the window, len – 1 the highest.
    • DRAWLABEL with the flag shape — « High of 50 » and « Low of 50 » on the bars where the close takes the top or the bottom rank.

    Good to know: an array holds up to a million values and keeps them from one bar to the next. It has no history of its own, so a value you want to compare with the previous bar goes into an ordinary variable first, as rank does here with rank[1].

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter. The window length is a field of the Variables form.

    // ProRealCode Script example — arrays
    // The last closes go into an array, then the array gives their median,
    // a percentile band, their dispersion and the rank of the current close.
    
    input(len, 50, "Window (bars)", int)
    
    // $w[0] is the oldest close of the window, $w[len - 1] the current one
    ARRAYFROM($w, close, len)
    
    mid = ARRAYMEDIAN($w)
    low10 = ARRAYPERCENTILE($w, 10)
    high90 = ARRAYPERCENTILE($w, 90)
    dispersion = ARRAYSTDDEV($w)
    
    // The band that holds 80% of the window's closes
    COLORBETWEEN(low10, high90, 120, 140, 255, 30)
    
    // Rank of the current close among the window: 0 = lowest, len - 1 = highest
    ARRAYCOPY($ranked, $w)
    ARRAYSORT($ranked, ascend)
    rank = ARRAYINDEXOF($ranked, close)
    
    IF rank = len - 1 AND rank[1] < len - 1 THEN
      DRAWLABEL(barindex, high + dispersion * 0.3, "High of #len#", flag, tiny) COLOURED(20, 22, 28) FILLCOLOR(120, 205, 150)
    ENDIF
    IF rank = 0 AND rank[1] > 0 THEN
      DRAWLABEL(barindex, low - dispersion * 0.3, "Low of #len#", flag, tiny) COLOURED(235, 235, 235) FILLCOLOR(170, 60, 70)
    ENDIF
    
    RETURN mid AS "Median" COLOURED(240, 180, 60) STYLE(line, 2), low10 AS "10th percentile" COLOURED(120, 140, 255), high90 AS "90th percentile" COLOURED(120, 140, 255)

    The capture: the Mini Nasdaq 100 future in 1 minute, with the median, the 10–90 percentile band and the High / Low of 50 flags, and the code open in Studio.

    prorealcode-studio-example-4-arrays.jpg prorealcode-studio-example-4-arrays.jpg
    #266665 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 5 — a table on the chart

    A small dashboard pinned to the chart and filled entirely from the code: the RSI, the ATR as a percentage of the price, and the change over the last 14 bars, each with a gauge, a sparkline of its last 60 bars and a coloured cell.

    What the code uses

    • DRAWTABLE(rows, columns, position) — creates the table (4 rows, 3 columns, top right) with its background and border colours. The variable board is its handle for every line that follows.
    • TABLEMERGE and TABLECELL — the title row spans the three columns and holds a text that reads the length from the code (#len#).
    • TABLEGAUGE — a gauge between a minimum and a maximum, with its label. The RSI gauge takes its colour from GRADIENTCOLOR.
    • TABLESPARK — a sparkline of the last 60 values.
    • TABLEHEAT — a cell coloured by where the value sits between two bounds, red at the first one and green at the second, with the value written inside.
    • DEFPARAM DRAWONLASTBARONLY = True — the table is drawn once, on the last bar, which is all a dashboard needs.

    Good to know: the ranges of the ATR and change gauges are sized for the Nasdaq future in 1 minute; adjust them for another instrument or timeframe.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter.

    // ProRealCode Script example — a table on the chart
    // DRAWTABLE pins a table to a corner of the chart, and the cells are filled
    // from the code: text, gauges, bars, heat cells and sparklines.
    
    DEFPARAM DRAWONLASTBARONLY = True
    
    input(len, 14, "Length", int)
    
    myRsi = RSI[len](close)
    atrPct = AverageTrueRange[len](close) / close * 100
    change = (close - close[len]) / close[len] * 100
    
    rsiTxt = round(myRsi)
    atrTxt = round(atrPct * 100) / 100
    chgTxt = round(change * 100) / 100
    
    board = DRAWTABLE(4, 3, topright) FILLCOLOR(16, 20, 28, 235) BORDERCOLOR(48, 58, 76)
    
    // Title row across the three columns
    TABLEMERGE(board, 0, 0, 1, 3)
    TABLECELL(board, 0, 0, "Market board · #len# bars", bold, center, 12) COLOURED(214, 222, 235)
    
    // One column per measure: a gauge, a sparkline of its last 60 bars, a heat cell
    TABLEGAUGE(board, 1, 0, myRsi, 0, 100, "RSI #rsiTxt#") GRADIENTCOLOR(myRsi, 30, 70, "#3fbf7f", "#8fa4c0", "#e05563")
    TABLEGAUGE(board, 1, 1, atrPct, 0, 0.1, "ATR #atrTxt#%") COLOURED(120, 170, 255)
    TABLEGAUGE(board, 1, 2, change, 0 - 0.5, 0.5, "Change #chgTxt#%") COLOURED(190, 150, 255)
    
    TABLESPARK(board, 2, 0, myRsi, 60) COLOURED(150, 190, 235)
    TABLESPARK(board, 2, 1, atrPct, 60) COLOURED(120, 170, 255)
    TABLESPARK(board, 2, 2, change, 60) COLOURED(190, 150, 255)
    
    TABLEHEAT(board, 3, 0, myRsi, 70, 30, "#rsiTxt#")
    TABLEHEAT(board, 3, 1, atrPct, 0.1, 0, "#atrTxt#")
    TABLEHEAT(board, 3, 2, change, 0 - 0.5, 0.5, "#chgTxt#")
    
    RETURN

    The capture: the Mini Nasdaq 100 future in 1 minute in the light theme, with the table on the chart and the code open in Studio.

    prorealcode-studio-example-5-table.jpg prorealcode-studio-example-5-table.jpg
    #266667 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 6 — pivots that never repaint

    Swing highs and swing lows, marked on the bar where they happened and never moved afterwards, plus two lines that hold the last confirmed swing high and swing low as a resistance and a support.

    What the code uses

    • PivotHigh[left, right](high) and PivotLow[left, right](low) — a bar is a pivot when it is strictly higher (or lower) than the left bars before it and the right bars after it. The pivot is therefore known right bars later, on that bar, and never changes once it is known.
    • ValueWhen(condition, value, 0) — the value the series had the last time the condition was true. Here it carries the price of the last pivot forward, bar after bar, which draws the two step lines.
    • ISSET — on the bars that are not pivots, PivotHigh and PivotLow are undefined; ISSET tells the two apart.
    • DRAWLABEL — the price of each pivot, placed right bars back on the pivot bar itself, with a tooltip.

    Good to know: because a pivot is only confirmed right bars later, a strategy that reads it sees it on the bar of confirmation, never earlier. A backtest that uses it therefore does not read the future.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter. The two lengths are fields of the Variables form; raise them for fewer, larger swings.

    // ProRealCode Script example — pivots that never repaint
    // PivotHigh / PivotLow find swing points, confirmed a few bars later and never moved.
    // ValueWhen keeps the price of the last confirmed pivot as a support or a resistance.
    
    input(leftBars, 5, "Bars on the left", int)
    input(rightBars, 5, "Bars on the right", int)
    
    ph = PivotHigh[leftBars, rightBars](high)
    pl = PivotLow[leftBars, rightBars](low)
    
    // The last confirmed swing high and swing low, carried forward bar after bar
    resistance = ValueWhen(ISSET(ph), ph, 0)
    support = ValueWhen(ISSET(pl), pl, 0)
    
    // The pivots are marked on the bar where they happened, rightBars bars back
    IF ISSET(ph) THEN
      DRAWLABEL(barindex - rightBars, ph, "#ph#", arrowdown, small, "Swing high, confirmed #rightBars# bars later") COLOURED(20, 22, 28) FILLCOLOR(240, 190, 70) BORDERCOLOR(150, 105, 20)
    ENDIF
    IF ISSET(pl) THEN
      DRAWLABEL(barindex - rightBars, pl, "#pl#", arrowup, small, "Swing low, confirmed #rightBars# bars later") COLOURED(20, 22, 28) FILLCOLOR(120, 205, 150) BORDERCOLOR(30, 120, 70)
    ENDIF
    
    RETURN resistance AS "Last swing high" COLOURED(240, 150, 70) STYLE(line, 2), support AS "Last swing low" COLOURED(90, 190, 140) STYLE(line, 2)

    The capture: the Mini Nasdaq 100 future in 1 minute in the light theme, with the swing labels, the last swing high (orange) and the last swing low (green), and the code open in Studio.

    prorealcode-studio-example-6-pivots.jpg prorealcode-studio-example-6-pivots.jpg
    #266669 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 7 — an ICT toolkit

    The concepts ICT traders draw by hand, computed by one code and kept up to date on every bar: market structure with its breaks and changes of character, liquidity sweeps, fair value gaps that stay on the chart until price fills them, the premium and discount halves of the dealing range, the liquidity still resting above and below it, and a dashboard that sums it all up.

    What it draws

    • BOS and CHoCH — a close beyond the last confirmed swing breaks the structure. When it goes against the current trend it is a change of character (CHoCH, filled label); when it goes with it, a break of structure (BOS). A dashed line runs from the swing to the break.
    • Sweeps — a wick beyond the last swing that closes back inside: the liquidity resting there has been taken, but the structure has not been broken.
    • Fair value gaps — the three-candle imbalance, green for bullish and red for bearish. Each gap shrinks as price trades into it and disappears once it is completely filled; the 10 most recent open gaps are shown.
    • Premium and discount — the range between the last swing high and the last swing low, split at its equilibrium (EQ): above it is premium, below it discount.
    • BSL and SSL — the buy-side and sell-side liquidity still resting at the last swing high and swing low, as long as price has not taken it.
    • The dashboard — structure, where the close sits in the range, the number of open gaps each way, and the two liquidity levels.

    What the code uses

    • PivotHigh / PivotLow for confirmed swings that never repaint.
    • Four arrays ($gTop, $gBot, $gBar, $gDir) that hold every gap, updated on each bar and trimmed with ARRAYSHIFT.
    • DRAWLABEL, DRAWSEGMENT, DRAWRECTANGLE for the structure, the zones and the gaps; islastbarupdate so the zones, gaps and dashboard are drawn once, on the last bar.
    • DRAWTABLE with a TABLEBAR coloured by GRADIENTCOLOR, and a small FUNCTION that formats the prices.
    • alert() on every change of character, with a JSON block for a webhook.
    • input() for the swing length, the number of gaps shown and the smallest gap in ATR.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter. Raise the swing length for a calmer structure on short timeframes. This is an illustration of the concepts, not a trading signal.

    // ProRealCode Script example — an ICT toolkit
    // Market structure (BOS and CHoCH), liquidity sweeps, fair value gaps that stay
    // on the chart until price fills them, the premium and discount halves of the
    // dealing range, and a dashboard. Built with functions, arrays, labels,
    // gradients, a table and an alert.
    
    input(swingLen, 5, "Swing length (bars on each side)", int)
    input(maxGaps, 10, "Open fair value gaps shown", int)
    input(minGapAtr, 0.2, "Smallest gap (in ATR)", float)
    
    atr = AverageTrueRange[14](close)
    
    // A level written once, used for every price shown in the dashboard
    FUNCTION fmt(v)
      ROUND(v * 100) / 100
    ENDFUNCTION
    
    // ── 1. Swings: confirmed pivots, never repainted ─────────────────────────────
    ph = PivotHigh[swingLen, swingLen](high)
    pl = PivotLow[swingLen, swingLen](low)
    IF ISSET(ph) THEN
      swingHigh = ph
      swingHighBar = barindex - swingLen
      highTaken = 0
    ENDIF
    IF ISSET(pl) THEN
      swingLow = pl
      swingLowBar = barindex - swingLen
      lowTaken = 0
    ENDIF
    
    // ── 2. Liquidity sweeps and market structure ─────────────────────────────────
    // A wick above the last swing high that closes back below it takes the
    // buy-side liquidity; a close above it breaks the structure.
    IF swingHigh > 0 AND highTaken = 0 AND high > swingHigh THEN
      highTaken = 1
      IF close < swingHigh THEN
        DRAWLABEL(barindex, high + 0.4 * atr, "Sweep", arrowdown, tiny, "Buy-side liquidity taken above #swingHigh#") COLOURED(20, 22, 28) FILLCOLOR(240, 190, 70)
      ELSE
        DRAWSEGMENT(swingHighBar, swingHigh, barindex, swingHigh) COLOURED(60, 190, 120) STYLE(dashedline, 1)
        IF trend = -1 THEN
          DRAWLABEL(barindex, swingHigh + 0.3 * atr, "CHoCH", none, small, "Change of character: the bearish structure is broken") COLOURED(255, 255, 255) FILLCOLOR(40, 150, 95)
          alert("ICT", "Bullish change of character at #close#", onceperbar, 1, {event: "choch", side: "long", price: close})
        ELSE
          DRAWLABEL(barindex, swingHigh + 0.3 * atr, "BOS", none, tiny, "Break of structure, trend continues up") COLOURED(60, 190, 120)
        ENDIF
        trend = 1
      ENDIF
    ENDIF
    
    IF swingLow > 0 AND lowTaken = 0 AND low < swingLow THEN
      lowTaken = 1
      IF close > swingLow THEN
        DRAWLABEL(barindex, low - 0.4 * atr, "Sweep", arrowup, tiny, "Sell-side liquidity taken below #swingLow#") COLOURED(20, 22, 28) FILLCOLOR(240, 190, 70)
      ELSE
        DRAWSEGMENT(swingLowBar, swingLow, barindex, swingLow) COLOURED(230, 90, 90) STYLE(dashedline, 1)
        IF trend = 1 THEN
          DRAWLABEL(barindex, swingLow - 0.3 * atr, "CHoCH", none, small, "Change of character: the bullish structure is broken") COLOURED(255, 255, 255) FILLCOLOR(175, 55, 65)
          alert("ICT", "Bearish change of character at #close#", onceperbar, 1, {event: "choch", side: "short", price: close})
        ELSE
          DRAWLABEL(barindex, swingLow - 0.3 * atr, "BOS", none, tiny, "Break of structure, trend continues down") COLOURED(230, 90, 90)
        ENDIF
        trend = -1
      ENDIF
    ENDIF
    
    // ── 3. Fair value gaps, kept in arrays until price fills them ────────────────
    IF low > high[2] AND low - high[2] >= minGapAtr * atr THEN
      ARRAYPUSH($gTop, low)
      ARRAYPUSH($gBot, high[2])
      ARRAYPUSH($gBar, barindex - 1)
      ARRAYPUSH($gDir, 1)
    ENDIF
    IF high < low[2] AND low[2] - high >= minGapAtr * atr THEN
      ARRAYPUSH($gTop, low[2])
      ARRAYPUSH($gBot, high)
      ARRAYPUSH($gBar, barindex - 1)
      ARRAYPUSH($gDir, -1)
    ENDIF
    
    // Price eats into a gap: it shrinks, and disappears once filled
    FOR i = 0 TO ARRAYSIZE($gDir) - 1 DO
      IF $gDir[i] = 1 AND $gBar[i] < barindex - 1 THEN
        IF low <= $gBot[i] THEN
          $gDir[i] = 0
        ELSIF low < $gTop[i] THEN
          $gTop[i] = low
        ENDIF
      ELSIF $gDir[i] = -1 AND $gBar[i] < barindex - 1 THEN
        IF high >= $gTop[i] THEN
          $gDir[i] = 0
        ELSIF high > $gBot[i] THEN
          $gBot[i] = high
        ENDIF
      ENDIF
    NEXT
    
    // Keep the arrays short: the oldest entries leave from the front
    WHILE ARRAYSIZE($gDir) > 200 DO
      ARRAYSHIFT($gTop)
      ARRAYSHIFT($gBot)
      ARRAYSHIFT($gBar)
      ARRAYSHIFT($gDir)
    WEND
    
    // ── 4. On the last bar: zones, liquidity and the dashboard ───────────────────
    IF islastbarupdate AND swingHigh > 0 AND swingLow > 0 THEN
      rangeStart = MIN(swingHighBar, swingLowBar)
      rangeEnd = barindex + 12
      eq = (swingHigh + swingLow) / 2
    
      // Premium (upper half) and discount (lower half) of the dealing range
      DRAWRECTANGLE(rangeStart, swingHigh, rangeEnd, eq) FILLCOLOR(230, 90, 90, 22)
      DRAWRECTANGLE(rangeStart, eq, rangeEnd, swingLow) FILLCOLOR(60, 190, 120, 22)
      DRAWSEGMENT(rangeStart, eq, rangeEnd, eq) COLOURED(150, 150, 165) STYLE(dottedline, 1)
      DRAWLABEL(rangeEnd, eq, "EQ", none, tiny, right) COLOURED(150, 150, 165)
      DRAWLABEL(rangeEnd, swingHigh, "Premium", none, tiny, right) COLOURED(230, 90, 90)
      DRAWLABEL(rangeEnd, swingLow, "Discount", none, tiny, right) COLOURED(60, 190, 120)
    
      // Liquidity still resting above the last swing high and below the last swing low
      IF highTaken = 0 THEN
        DRAWSEGMENT(swingHighBar, swingHigh, rangeEnd, swingHigh) COLOURED(240, 190, 70) STYLE(dottedline, 2)
        DRAWLABEL(swingHighBar, swingHigh + 0.3 * atr, "BSL", none, tiny) COLOURED(240, 190, 70)
      ENDIF
      IF lowTaken = 0 THEN
        DRAWSEGMENT(swingLowBar, swingLow, rangeEnd, swingLow) COLOURED(240, 190, 70) STYLE(dottedline, 2)
        DRAWLABEL(swingLowBar, swingLow - 0.3 * atr, "SSL", none, tiny) COLOURED(240, 190, 70)
      ENDIF
    
      // The open fair value gaps, newest first
      shown = 0
      upGaps = 0
      downGaps = 0
      FOR i = ARRAYSIZE($gDir) - 1 DOWNTO 0 DO
        IF $gDir[i] = 1 THEN
          upGaps = upGaps + 1
        ELSIF $gDir[i] = -1 THEN
          downGaps = downGaps + 1
        ENDIF
        IF $gDir[i] <> 0 AND shown < maxGaps THEN
          shown = shown + 1
          IF $gDir[i] = 1 THEN
            DRAWRECTANGLE($gBar[i], $gTop[i], rangeEnd, $gBot[i]) FILLCOLOR(60, 190, 120, 70) COLOURED(60, 190, 120)
          ELSE
            DRAWRECTANGLE($gBar[i], $gTop[i], rangeEnd, $gBot[i]) FILLCOLOR(230, 90, 90, 70) COLOURED(230, 90, 90)
          ENDIF
        ENDIF
      NEXT
    
      // Where the close sits in the range: 0% = swing low, 100% = swing high
      pos = MAX(0, MIN(100, (close - swingLow) / (swingHigh - swingLow) * 100))
      posTxt = ROUND(pos)
      bsl = fmt(swingHigh)
      ssl = fmt(swingLow)
    
      board = DRAWTABLE(6, 2, topleft) FILLCOLOR(16, 20, 28, 235) BORDERCOLOR(48, 58, 76)
      TABLEMERGE(board, 0, 0, 1, 2)
      TABLECELL(board, 0, 0, "ICT dashboard", bold, center, 12) COLOURED(214, 222, 235)
      TABLECELL(board, 1, 0, "Structure") COLOURED(170, 180, 195)
      IF trend = 1 THEN
        TABLECELL(board, 1, 1, "Bullish", bold) COLOURED(255, 255, 255)
        TABLEBG(board, 1, 1, 40, 150, 95)
      ELSIF trend = -1 THEN
        TABLECELL(board, 1, 1, "Bearish", bold) COLOURED(255, 255, 255)
        TABLEBG(board, 1, 1, 175, 55, 65)
      ELSE
        TABLECELL(board, 1, 1, "Undecided") COLOURED(170, 180, 195)
      ENDIF
      TABLECELL(board, 2, 0, "Range position") COLOURED(170, 180, 195)
      TABLEBAR(board, 2, 1, pos, 0, 100, "#posTxt#%") GRADIENTCOLOR(pos, 0, 100, "#3fbf7f", "#8fa4c0", "#e05563")
      TABLECELL(board, 3, 0, "Open gaps up / down") COLOURED(170, 180, 195)
      TABLECELL(board, 3, 1, "#upGaps# / #downGaps#") COLOURED(214, 222, 235)
      TABLECELL(board, 4, 0, "Buy-side liquidity") COLOURED(170, 180, 195)
      TABLECELL(board, 4, 1, "#bsl#") COLOURED(240, 190, 70)
      TABLECELL(board, 5, 0, "Sell-side liquidity") COLOURED(170, 180, 195)
      TABLECELL(board, 5, 1, "#ssl#") COLOURED(240, 190, 70)
    ENDIF
    
    RETURN

    The captures: the Mini Nasdaq 100 future in 1 minute and in 15 minutes, the same code on both.

    prorealcode-studio-example-7-ict-1m.jpg prorealcode-studio-example-7-ict-1m.jpg prorealcode-studio-example-7-ict-15m.jpg prorealcode-studio-example-7-ict-15m.jpg
    #266672 quote
    NicolasNicolas
    Keymaster
    Legend

    Example 8 — reading ProRealTrend from the code

    ProRealTrend draws support and resistance lines on the chart. Six names let a code read those lines, whether ProRealTrend is shown or hidden, and work with them. This one tags every line with what it is, counts them in a dashboard, finds the nearest support and resistance, and raises an alert when the price comes near a major line.

    The six names

    • TrendLineCount — how many lines ProRealTrend has computed for this chart (12 on the captures).
    • TrendLine(i) — the level of line i on the current bar, for i from 1 to TrendLineCount. It reads undefined where the line is not drawn.
    • TrendLineType(i) — 1 for a support, -1 for a resistance.
    • TrendLineImportance(i) — ProRealTrend’s own rating: 3 major, 2 intermediate, 1 minor. Here the tags of stronger lines are more opaque.
    • TrendLineOblique(i) — 1 for an oblique line, 0 for a horizontal one.
    • TrendLinePeriod(i) — the timeframe the line was computed on, in seconds per bar. Compared with GetTimeframe, it picks out the lines that come from a higher timeframe.

    The code also joins strings with + to build each tag (« Resistance · major · monthly »), and uses DRAWLABEL, DRAWTABLE and alert() with a JSON block.

    Good to know: the lines are what ProRealTrend computes today. Read on a past bar, TrendLine(i) is where today’s line passes on that bar, not a line that was known at the time, so a backtest built on it would read the future. That is why this code reads them on the last bar only, with islastbarupdate. When ProRealTrend recomputes (a new bar, another instrument or timeframe), the code recalculates with the new set.

    How to use it: paste the code into a new Studio tab and press Ctrl + Enter. Show ProRealTrend if you want to see the lines under the tags; the code reads them either way.

    // ProRealCode Script example — reading ProRealTrend from the code
    // Six names read the support and resistance lines ProRealTrend computes for
    // this chart, shown or hidden: TrendLineCount, TrendLine(i), TrendLineType(i),
    // TrendLineImportance(i), TrendLineOblique(i) and TrendLinePeriod(i).
    // Read on a past bar, a line is where TODAY's line passes on that bar, so this
    // code reads them on the last bar only.
    
    input(minImportance, 1, "Lowest importance shown (1 minor, 2 intermediate, 3 major)", int)
    input(alertPct, 0.1, "Alert when the close is this near a major line (%)", float)
    
    IF islastbarupdate THEN
      supports = 0
      resistances = 0
      majors = 0
      obliques = 0
      higherTf = 0
      nearSup = undefined
      nearRes = undefined
    
      FOR i = 1 TO TrendLineCount DO
        lvl = TrendLine(i)
        imp = TrendLineImportance(i)
        IF ISSET(lvl) AND imp >= minImportance THEN
    
          // What the line is: support or resistance
          IF TrendLineType(i) = 1 THEN
            supports = supports + 1
            txt = "Support"
            r = 60
            g = 190
            b = 120
            IF lvl <= close AND (NOT ISSET(nearSup) OR lvl > nearSup) THEN
              nearSup = lvl
            ENDIF
          ELSE
            resistances = resistances + 1
            txt = "Resistance"
            r = 230
            g = 90
            b = 90
            IF lvl > close AND (NOT ISSET(nearRes) OR lvl < nearRes) THEN
              nearRes = lvl
            ENDIF
          ENDIF
    
          // How strong ProRealTrend rates it: 3 major, 2 intermediate, 1 minor
          IF imp = 3 THEN
            majors = majors + 1
            txt = txt + " · major"
            IF ABS(close - lvl) / lvl * 100 <= alertPct THEN
              alert("ProRealTrend", "Close #close# is near a major line at #lvl#", onceperbar, 1, {level: lvl, support: TrendLineType(i) = 1})
            ENDIF
          ELSIF imp = 2 THEN
            txt = txt + " · intermediate"
          ELSE
            txt = txt + " · minor"
          ENDIF
    
          // Horizontal or oblique
          IF TrendLineOblique(i) = 1 THEN
            obliques = obliques + 1
            txt = txt + " · oblique"
          ENDIF
    
          // The timeframe it was computed on, in seconds per bar
          p = TrendLinePeriod(i)
          IF p >= 2592000 THEN
            txt = txt + " · monthly"
          ELSIF p >= 604800 THEN
            txt = txt + " · weekly"
          ELSIF p >= 86400 THEN
            txt = txt + " · daily"
          ENDIF
          IF p > GetTimeframe THEN
            higherTf = higherTf + 1
          ENDIF
    
          // A tag at the right end of the line, stronger lines more opaque
          DRAWLABEL(barindex + 2, lvl, txt, arrowleft, small, left) COLOURED(255, 255, 255) FILLCOLOR(r, g, b, 80 + imp * 55)
        ENDIF
      NEXT
    
      // The dashboard
      board = DRAWTABLE(8, 2, topleft) FILLCOLOR(16, 20, 28, 235) BORDERCOLOR(48, 58, 76)
      TABLEMERGE(board, 0, 0, 1, 2)
      TABLECELL(board, 0, 0, "ProRealTrend, read by the code", bold, center, 12) COLOURED(214, 222, 235)
      TABLECELL(board, 1, 0, "Lines on the chart") COLOURED(170, 180, 195)
      TABLECELL(board, 1, 1, "#TrendLineCount#") COLOURED(214, 222, 235)
      TABLECELL(board, 2, 0, "Supports / resistances") COLOURED(170, 180, 195)
      TABLECELL(board, 2, 1, "#supports# / #resistances#") COLOURED(214, 222, 235)
      TABLECELL(board, 3, 0, "Major lines") COLOURED(170, 180, 195)
      TABLECELL(board, 3, 1, "#majors#") COLOURED(214, 222, 235)
      TABLECELL(board, 4, 0, "Oblique lines") COLOURED(170, 180, 195)
      TABLECELL(board, 4, 1, "#obliques#") COLOURED(214, 222, 235)
      TABLECELL(board, 5, 0, "From a higher timeframe") COLOURED(170, 180, 195)
      TABLECELL(board, 5, 1, "#higherTf#") COLOURED(214, 222, 235)
      TABLECELL(board, 6, 0, "Nearest support") COLOURED(170, 180, 195)
      IF ISSET(nearSup) THEN
        supLvl = ROUND(nearSup * 100) / 100
        supPct = ROUND((close - nearSup) / close * 10000) / 100
        TABLECELL(board, 6, 1, "#supLvl#  (-#supPct#%)") COLOURED(60, 190, 120)
      ELSE
        TABLECELL(board, 6, 1, "none below") COLOURED(170, 180, 195)
      ENDIF
      TABLECELL(board, 7, 0, "Nearest resistance") COLOURED(170, 180, 195)
      IF ISSET(nearRes) THEN
        resLvl = ROUND(nearRes * 100) / 100
        resPct = ROUND((nearRes - close) / close * 10000) / 100
        TABLECELL(board, 7, 1, "#resLvl#  (+#resPct#%)") COLOURED(230, 90, 90)
      ELSE
        TABLECELL(board, 7, 1, "none above") COLOURED(170, 180, 195)
      ENDIF
    ENDIF
    
    RETURN

    The captures: the Mini Nasdaq 100 future in 15 minutes, first with ProRealTrend shown, then hidden. The tags and the dashboard do not depend on the display: the code reads the lines in both cases.

    prorealcode-studio-example-8-prorealtrend-shown.jpg prorealcode-studio-example-8-prorealtrend-shown.jpg prorealcode-studio-example-8-prorealtrend-hidden.jpg prorealcode-studio-example-8-prorealtrend-hidden.jpg
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI
ProRealCode Studio New

Extend the power of ProRealTime™ Web

A complete code editor, chart templates, bar replay and alerts from your codes, right inside the platform.

Add to Chrome

Also works in Edge, Brave, Opera and other Chromium-based browsers.

Discover ProRealCode Studio

ProRealCode Studio — extend ProRealTime Web from Chrome


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Nicolas @nicolas Keymaster
Summary

This topic contains 9 replies,
has 1 voice, and was last updated by NicolasNicolas
1 hour, 52 minutes ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/01/2026
Status: Active
Attachments: 14 files
ProRealCode ProRealCode
Loading...