System
14 entries. What each one takes, what it returns, and a working example.
Chart background color. Result: #0a0b0e
The chart background color (#rrggbb).
Note
For drawing in keeping with the theme — so your added layers color consistently with the background.
if barstate.islast { label(close, chartBgColor(), barIndex) } plot(close)
Writes the background color onto the last bar as a label.
Chart foreground color. Result: #e8eaed
The chart foreground (text) color (#rrggbb).
Note
For keeping label/text color consistent with the theme.
if barstate.islast { label(close, chartFgColor(), barIndex) } plot(close)
Writes the foreground color onto the last bar as a label.
true if a standard candle chart.
Returns whether the chart uses standard candles — currently always true.
Note
A check for logic that branches on bar type; the value is constant because CodeScript produces standard bars.
When to use
To condition logic that is only meaningful on the standard bar type (a bar-type check).
Limits
For now it always returns true; it does not detect alternative bar types (heikin-ashi/renko, etc.) because those types are not produced here.
plot(chartIsStandard() ? 1 : 0, "is it standard")
Plots a constant 1 on a standard chart.
Time of the first visible bar. Result: 1700000000
The timestamp of the first bar in the data range.
Note
For anchoring on the left edge of the visible window; as a reference when positioning along the time axis.
plot(chartLeftVisibleBarTime())
Plots the first bar's timestamp as a flat line.
Time of the last visible bar. Result: 1700212400
The timestamp of the last bar in the data range.
Note
For anchoring on the right edge of the visible window (the most recent bar).
plot(chartRightVisibleBarTime())
Plots the last bar's timestamp as a flat line.
Writes an error log (does not halt execution).
Does not return a value; appends the message to the run log as an error (does NOT stop the code).
Note
Does not halt execution — use runtimeError if you want to stop. For leaving a durable trace of an unexpected condition in the log.
Not for
Not for actually stopping execution; use runtimeError for that.
if close <= 0 { logError("invalid price") } plot(close)
Had price been zero/negative an error line would be logged; on normal data it is not triggered.
Writes an info log.
Does not return a value; appends the message to the run log as info.
Note
Does not stop the code. For tracing intermediate values or which branch ran — the output collects in a separate log list.
if barstate.islast { logInfo("scan complete") } plot(close)
Adds an info line to the log on the last bar; the plot is unaffected.
Writes a warning log.
Does not return a value; appends the message to the run log as a warning.
Note
Does not stop the code; flags a non-fatal situation that deserves a touch more attention than info.
if rsi(close, 14) > 70 { logWarning("overbought zone") } plot(close)
Adds a warning to the log on bars where RSI rises above 70.
Halts execution with the given message (error).
Does not return a value; stops the code and reports the given message as an error.
Note
Halts execution the moment it is called. To report a broken assumption (an invalid parameter, an unexpected state) early and clearly — guard it behind a condition so it only fires on the bad case.
if close < 0 { runtimeError("price cannot be negative") } plot(close)
Had price been negative the code would stop; on normal data the condition is not met and price is plotted.
The CLOSE time (milliseconds) of the current bar. time() gives the bar's open time, time_close() the close (open + bar duration). The timeframe must be empty or the current chart TF.
The CLOSE time (epoch milliseconds) of the current bar.
Note
`time()` gives the bar's OPEN time, `time_close()` gives its close (open + bar duration). The result does not depend on exchange timezone — it's based purely on UTC bar boundaries.
Limits
Passing a different timeframe (e.g. a higher TF) raises a clear error — only the current chart's TF is supported.
plot(time_close(""))
Plots each bar's close time (epoch ms).
true at the timeframe boundary (new period). Result: False
A boolean series: true on the bar that OPENS a new bucket of the given timeframe, false otherwise.
Note
The first bar is always false (there is nothing before it). For an unrecognized timeframe the whole series is false.
When to use
To catch day/week/session boundaries — a new-day marker, once-per-day logic, reset-on-first-bar, and so on.
Limits
The timeframe must carry a unit (m/h/d/w) such as "1D"; a unit-less number is not recognized. Boundary detection is based on bar timestamps; the weekly bucket starts on Monday.
Not for
Not for reading a higher-timeframe value (that is htf/security's job); it only marks the bucket boundary.
plotshape(timeframeChange("1D"), "New day")
Places a mark on the first bar of each new day.
plot(timeframeChange("1W") ? high : na, "Start of week")
Plots the high on the first bar of a new week, empty (na) otherwise.
Converts seconds to a timeframe string. Result: 4h
Converts seconds into a compact timeframe string (e.g. 3600 → "1h", 86400 → "1D"). Zero/negative or non-number → an empty string.
Note
The inverse of timeframeInSeconds; it picks the largest evenly-dividing unit (week > day > hour > minute).
When to use
To turn a numeric length back into a readable timeframe label (e.g. to display a computed interval).
Limits
Picks the largest exactly-dividing unit: 604800→"1W", 86400→"1D", 3600→"1h", otherwise minutes ("Nm"). Non-positive or non-number input returns an empty string.
label(close, timeframeFromSeconds(3600))
Labels "1h" (3600 seconds = 1 hour).
label(close, timeframeFromSeconds(86400))
Labels "1D" (86400 seconds = 1 day).
Converts a timeframe to seconds. Result: 14400
Returns the timeframe's length in seconds (e.g. "1h" → 3600). na for an unrecognized timeframe.
Note
Converts a timeframe string into a numeric length; useful in scaling or comparisons.
When to use
To turn a timeframe into a number to ratio against another timeframe or to scale a threshold by bar length.
Limits
The timeframe must carry a unit (m/h/d/w); a unit-less number (e.g. "60") returns na — use "60m" or "1h". Minute=60, hour=3600, day=86400, week=604800.
plot(timeframeInSeconds("1h"), "Second")
Plots a constant 3600 (1 hour = 3600 seconds).
plot(timeframeInSeconds("1D") / 3600, "hours per day")
Plots a constant 24 (hours in a day).
timestamp(year, month, day, hour, minute, second)
Builds an epoch timestamp (milliseconds, UTC) from calendar parts (year, month, day[, hour, minute, second]). Missing hour/minute/second default to 0; arguments may be constants or series (e.g. year, month).
The epoch timestamp (milliseconds, UTC) built from the calendar parts.
Note
The conversion is plain UTC — exchange timezone/DST is not involved (that only applies to READ variables like `hour`/`minute`/`year`). Arguments may be constants or series (e.g. `year`, `month`); if a series is given, the result is also computed bar-by-bar.
plot(timestamp(2024, 1, 1, 0, 0, 0))
Plots the epoch millisecond value of 2024-01-01 00:00:00 UTC.
Whether the bar falls inside the given session time range — "1000-1800" (optional day-mask "1000-1800:23456", 1=Sunday..7=Saturday). The single-call equivalent of the indirect `not na(time(timeframe.period, session))` pattern; it reads the chart's own bar time and takes no separate timeframe argument.
A boolean series: true when the bar's time falls inside the given session range, false otherwise.
Note
The same result can be written with the indirect `not na(time(timeframe.period, session))` pattern; inSession() gives it in a single call. The start is INCLUSIVE, the end EXCLUSIVE ("1000-1800" — the 18:00 bar is outside). An overnight range (start>end, e.g. "2200-0300") is supported.
When to use
To run a strategy only during certain hours — e.g. only entering trades between 10:00-18:00 on BIST, filtering out a lunch break, or excluding an overnight session.
Limits
If the symbol's timezone cannot be resolved (no bridge metadata or symbol classification, e.g. US stocks) it raises a clear error — a silent +0 offset is never fabricated. It takes no timeframe argument; it always looks at the chart's own bar time.
enterLong(close > sma(close, 20) and inSession("1000-1800"))
Opens a long position when price is above its 20-bar average and the bar falls between 10:00 and 18:00; it does not enter outside those hours.
indicator(title, overlay?, max_boxes_count?, max_labels_count?, max_lines_count?, max_polylines_count?)
Written at the top of the code to declare indicator settings. The four most useful ones are the DRAWING LIMITS: how many boxes, labels, lines and polylines are kept on the chart at once. Default is 50 each — code that draws more silently loses its OLDEST objects. If your code draws 200 boxes, write max_boxes_count = 200 (500 max).
Returns nothing; it declares the code's settings.
Note
Drawing limits are real behaviour, not decoration: code that draws a box or label on every bar will exceed the limit on a long chart and lose its oldest objects. If part of your drawings seem missing, look here first.
indicator("Zones", overlay = true, max_boxes_count = 200)
Declares the code as an indicator, draws it over the price chart and allows up to 200 boxes.
strategy(title, overlay?, initial_capital?, pyramiding?, slippage?, commission_value?, max_boxes_count?, max_labels_count?, max_lines_count?, max_polylines_count?)
Written at the top of the code to declare strategy settings: initial capital, how many same-direction entries may stack (pyramiding), slippage and commission. Drawing limits work here too, same meaning as in indicator.
Returns nothing; it declares the strategy's settings.
Note
Drawing limits are real behaviour, not decoration: code that draws a box or label on every bar will exceed the limit on a long chart and lose its oldest objects. If part of your drawings seem missing, look here first.
strategy("Breakout", overlay = true, initial_capital = 10000, pyramiding = 1)
Declares the code as a strategy: it runs over the price chart with 10,000 units of starting capital and allows more than one entry in the same direction.
The interface language in use: "tr" or "en". Use it to pick the texts your code displays.
The interface language in use: "tr" or "en".
Note
Use it to pick the texts your code displays. It is plain text and does not change bar to bar.
ad = lang == "en" ? "Length" : "Length" x = input(20, 1, 100, 1, ad) plot(sma(close, x), ad)
Picks the settings box label to match the interface language: "Length" in English, "Uzunluk" in Turkish.