Edge Script documentation

Edge Script is Bulledge's own scripting language: you write your own indicator in a few lines, it runs on our servers and is drawn on the Edge Chart. It reads like Pine Script with less ceremony, and most Pine indicators run unchanged.

Your first script

  • Open the Edge Chart and press the </> icon on the right edge: the Edge Script panel opens.
  • Press New script. The panel opens with a working example.
  • Press Save and draw (or Ctrl+Enter). The script runs on the server over the chart's candles and its lines land on the chart.
  • The eye in the list turns a script on or off the chart. Several scripts can be drawn at once.
Two averages and their crossings
indicator("EMA crossover", overlay = true)

fast = ema(close, 12)
slow = ema(close, 26)
plot(fast, "Fast", color = orange)
plot(slow, "Slow", color = blue)

mark(crossover(fast, slow), "BUY", position = "below", color = green)
mark(crossunder(fast, slow), "SELL", position = "above", color = red)

As you type, the editor checks the code with the server's own compiler: the faulty line is underlined, and the line underneath says what is wrong, with its line and column, in your language. A misspelt name gets the nearest right one suggested.

How it runs

The script runs once per bar, top to bottom, from the oldest bar to the newest. On any line, close is the close of the bar being run. The chart shows the newest 1,000 bars; the script runs over a longer history, so a long average is already warm on the first bar on screen.

Every value is a series: it has a value on every bar, and its history is read with []. close[1] is the previous bar's close, and ema(close, 20)[3] is the average three bars back.

An unknown value is na: before an average has filled, before the start of history, or from a division by zero. na turns a sum into na and is not drawn; nz(x) turns it into zero and fixnan(x) into the last known value.

The chart is read again every minute and the script runs again with the new bars. It also sees the newest, still-forming bar; a crossing on that bar can change until it closes.

Values and types

TypeWritten asNote
number14, 2.5, 1e-3No separate integer and float.
booleantrue, falseConditions and mark take these.
text"Rising", 'BUY'Labels, titles, alert messages.
colourred, #26a69a, #26a69a80The last two digits are transparency; rgb() and color.new() give colours too.
nanaThe unknown value.

Named colours: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, yellow — also with the color. prefix.

Variables and history

A variable is made by giving a name a value with =; no type needs declaring. It is computed afresh on every bar. Assign it again with = or :=, and add to it with +=, -=, *=, /=.

A variable declared with var is set only on the first bar and carries its value from bar to bar: a counter, the price of the last signal, or a state.

var count = 0
var lastSignal = na

if crossover(close, sma(close, 50))
    count += 1
    lastSignal := close

change = (close - lastSignal) / lastSignal * 100
plot(change, "% since the last signal", pane = "Signal")

Every variable's history is read with [], up to 5,000 bars back.

Operators

OperatorMeaning
+ - * / %Arithmetic; + joins two texts.
== != < > <= >=Comparison.
and or notLogic.
a ? b : cb when a is true, c otherwise.
x[n]x as it was n bars ago.

Conditions and loops

Blocks are written by indentation (four spaces or a tab): there are no braces and no end.

r = rsi(close, 14)
state = 0
if r > 70
    state := 1
else if r < 30
    state := -1
else
    state := 0

// how many of the last 10 bars closed up
up = 0
for i = 0 to 9
    if close[i] > open[i]
        up += 1
plot(up, "Up bars", pane = "Count", style = "histogram")
  • for i = a to b counts from a to b inclusive; by step sets the step, and it counts down when b is below a.
  • while condition loops while the condition holds.
  • break leaves the loop, continue goes to the next turn.
  • A loop turns at most 10,000 times on one bar.
  • Functions that keep a history (ema, crossover, …) cannot be called inside a loop: compute them before it.

Your own functions

A function is defined with name(parameters) =>: one expression on the same line, or an indented block whose last line is its value. Several values are returned as [a, b] and taken with [x, y] = f().

band(src, len, mult) =>
    mid = sma(src, len)
    dev = stdev(src, len)
    [mid + mult * dev, mid - mult * dev]

[upper, lower] = band(close, 20, 2)
plot(upper, "Upper", color = teal)
plot(lower, "Lower", color = teal)

Every call site keeps its own history: band(close, 20, 2) and band(high, 50, 3) never mix their averages. A function cannot call itself, and is defined only at the top level.

Inputs

input() declares a setting of the script; its kind comes from its default: a number, true/false, a text, or a source such as close. minval and maxval bound a number. The editor shows the script's inputs and their values underneath.

length = input(20, "Length", minval = 1)
source = input(close, "Source")
show = input(true, "Show the bands")

Drawing, marks and alerts

  • indicator("Name", overlay = true) names the script. overlay = true (the default) draws over the price, false in its own pane.
  • plot() draws a line; style = "histogram", "area" or "circles" are the other shapes. pane = "Name" puts it in a named pane under the price; plots naming the same pane share it.
  • color may change bar by bar: color = close > open ? green : red.
  • hline() draws a horizontal line at a fixed price — RSI's 70/30 in a pane, say.
  • mark() puts an arrow, circle or square on the bars where its condition is true, with a short text if you like.
  • alert() records the bars where its condition is true; the editor lists this chart's latest.
MACD in its own pane
indicator("MACD", overlay = false)
[m, signal, hist] = macd(close, 12, 26, 9)
plot(hist, "Histogram", color = hist >= 0 ? green : red, style = "histogram")
plot(m, "MACD", color = blue)
plot(signal, "Signal", color = orange)
hline(0, "Zero", color = gray)

Built-in values

NameValue
open high low close volumeThe bar's open, high, low, close and volume.
hl2 hlc3 ohlc4 hlcc4The bar's average prices.
tr obvTrue range and on-balance volume (ta.tr, ta.obv).
timeThe bar's opening instant, in Unix seconds.
bar_index last_bar_indexThe bar's position (from 0) and the last one's.
barstate.isfirst barstate.islasttrue on the first or the last bar.
syminfo.ticker syminfo.tickerid timeframe.periodThe symbol and the timeframe, as text.
math.pi math.eConstants.

Function reference

Parameters marked ? are optional. Any parameter can be given by name: plot(x, color = red). The ta. and math. prefixes are optional: ta.ema and ema are the same. The indicators follow TradingView's own definitions.

Indicators

sma(source, length)
Simple moving average. na until the window is full, and while any value in it is na.
ema(source, length)
Exponential moving average, seeded with the simple average of its first length values — TradingView's own seeding.
rma(source, length)
Wilder's moving average (the one RSI and ATR use), seeded like ema.
wma(source, length)
Weighted moving average: the newest value weighs most.
vwma(source, length)
Volume-weighted moving average.
hma(source, length)
Hull moving average.
rsi(source, length)
Relative strength index, Wilder's (0–100). Both averages at zero read 50.
macd(source, fastlen?, slowlen?, siglen?)
Gives three values: [macd, signal, histogram]. Defaults 12, 26, 9.
bb(source, length, mult?)
Bollinger bands. Gives three values: [middle, upper, lower]; population deviation, mult 2 by default.
stoch(source, high, low, length)
Raw stochastic %K (0–100). Smooth it with sma for %D.
atr(length)
Average true range, Wilder's.
cci(source, length)
Commodity channel index.
mfi(source, length)
Money flow index (0–100), from the source and the bar's volume.
supertrend(factor, atrPeriod)
Gives two values: [line, direction]. Direction is −1 in an uptrend and 1 in a downtrend, as in Pine.
stdev(source, length, biased?)
Standard deviation over the window; biased (population) by default.
variance(source, length, biased?)
Variance over the window.
highest(source, length?)
The highest value of the last length bars. With one argument it is a length over high.
lowest(source, length?)
The lowest value of the last length bars. With one argument it is a length over low.
highestbars(source, length?)
How many bars back the highest value was (0 or negative, as in Pine).
lowestbars(source, length?)
How many bars back the lowest value was.
sum(source, length)
Sum of the last length values.
cum(source)
Running total since the first bar.
change(source, length?)
source − source[length] (length 1 by default).
mom(source, length?)
Momentum: the same as change.
roc(source, length)
Rate of change, in percent.
percentrank(source, length)
Percent of the previous length values at or below the current one.
pivothigh(source, leftbars, rightbars?)
A swing high, reported rightbars bars after it (it needs them to be sure); na elsewhere. pivothigh(left, right) reads high.
pivotlow(source, leftbars, rightbars?)
A swing low, reported rightbars bars after it. pivotlow(left, right) reads low.

Conditions and history

crossover(a, b)
true on the bar a moves above b (it was at or below on the bar before).
crossunder(a, b)
true on the bar a moves below b.
cross(a, b)
true on either crossing.
rising(source, length)
true when source rose on each of the last length bars.
falling(source, length)
true when source fell on each of the last length bars.
barssince(condition)
Bars since condition was last true; na until it has been.
valuewhen(condition, source, occurrence?)
The value source had when condition was true — the latest time, or the occurrence-th before it.
fixnan(x)
The last value of x that was not na.
nz(x, replacement?)
x, or replacement (0 by default) when x is na.
na(x)
true when x is na. The bare word na is the value itself.

Maths

abs(x)
Absolute value.
sqrt(x)
Square root.
pow(base, exponent)
base to the power exponent.
exp(x)
e to the power x.
log(x)
Natural logarithm.
log10(x)
Base-10 logarithm.
round(x, precision?)
Rounded to precision decimals (0 by default).
floor(x)
Rounded down.
ceil(x)
Rounded up.
sign(x)
−1, 0 or 1.
max(a, b, …)
The largest of up to ten values.
min(a, b, …)
The smallest of up to ten values.
avg(a, b, …)
The average of up to ten values.
sin(x)
Sine, in radians.
cos(x)
Cosine, in radians.
tan(x)
Tangent, in radians.

Text and colour

str(x, decimals?)
A number as text, for a mark or an alert. Also str.tostring.
rgb(red, green, blue, transp?)
A colour from 0–255 channels; transp 0–100. Also color.rgb.
color.new(color, transp?)
The same colour at a transparency from 0 (solid) to 100.

Inputs

input(defval, title?, minval?, maxval?, step?)
A setting of the script, its kind taken from defval: a number, true/false, a text or a source such as close. input.int, input.float, input.bool, input.string and input.source all work.

Drawing and alerts

indicator(title?, overlay?)
Names the script and says where it draws: over the price (overlay = true, the default) or in its own pane.
plot(series, title?, color?, linewidth?, style?, pane?)
Draws a line. style: "line", "histogram", "area" or "circles" (plot.style_* too). color may change bar by bar. pane names a pane under the price.
hline(price, title?, color?, linestyle?, pane?)
A horizontal line at a fixed price: "solid", "dashed" (default) or "dotted".
mark(condition, text?, position?, color?, shape?)
A marker on each bar where condition is true: position "above" or "below", shape "arrowUp", "arrowDown", "circle" or "square".
plotshape(series, title?, style?, location?, color?, text?)
Pine's spelling of mark; location.abovebar / location.belowbar pick the side.
alert(condition, message?)
Records an alert on each bar where condition is true; the editor lists this chart's. alert("message") inside an if works too.
alertcondition(condition, title?, message?)
Pine's spelling of alert.

Coming from Pine Script

Most Pine indicators run as they are: the //@version line, the ta. and math. prefixes, input.int and friends, typed declarations such as float x = 1, :=, plotshape and alertcondition are all accepted; Pine arguments that mean nothing here (tooltip, group, offset…) are quietly ignored.

Not there yet:

  • request.security and data from another symbol or timeframe.
  • Arrays, matrices, maps and switch.
  • strategy.* — trade simulation.
  • label, line, box and tables; plot, hline and mark are the ways to draw.
  • Recursive functions.

Limits

A script runs on our servers, and every run is bounded so everybody's chart stays fast. A script that meets a limit stops with an error that says which.

LimitValue
Script length32 KB
Run time2 seconds
Total steps4,000,000
Loop turns on one bar10,000
History (x[n]) and length5,000
plot / hline24 / 24
mark calls / markers24 / 2,000
Alerts500
Inputs32
Saved scripts50

Edge Script is an analysis tool; what it draws is computed from past prices and is not investment advice.