CODESCRIPT

System

14 entries. What each one takes, what it returns, and a working example.

chartBgColor()

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.

CODESCRIPT
if barstate.islast {
  label(close, chartBgColor(), barIndex)
}
plot(close)

Writes the background color onto the last bar as a label.

chartFgColor()

Chart foreground color. Result: #e8eaed

The chart foreground (text) color (#rrggbb).

Note

For keeping label/text color consistent with the theme.

CODESCRIPT
if barstate.islast {
  label(close, chartFgColor(), barIndex)
}
plot(close)

Writes the foreground color onto the last bar as a label.

chartIsStandard()

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.

CODESCRIPT
plot(chartIsStandard() ? 1 : 0, "is it standard")

Plots a constant 1 on a standard chart.

chartLeftVisibleBarTime()

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.

CODESCRIPT
plot(chartLeftVisibleBarTime())

Plots the first bar's timestamp as a flat line.

chartRightVisibleBarTime()

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).

CODESCRIPT
plot(chartRightVisibleBarTime())

Plots the last bar's timestamp as a flat line.

logError(message)

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).

message
string
The error text to write to the log.

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.

CODESCRIPT
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.

logInfo(message)

Writes an info log.

Does not return a value; appends the message to the run log as info.

message
string
The information text to write to the log.

Note

Does not stop the code. For tracing intermediate values or which branch ran — the output collects in a separate log list.

CODESCRIPT
if barstate.islast {
  logInfo("scan complete")
}
plot(close)

Adds an info line to the log on the last bar; the plot is unaffected.

logWarning(message)

Writes a warning log.

Does not return a value; appends the message to the run log as a warning.

message
string
The warning text to write to the log.

Note

Does not stop the code; flags a non-fatal situation that deserves a touch more attention than info.

CODESCRIPT
if rsi(close, 14) > 70 {
  logWarning("overbought zone")
}
plot(close)

Adds a warning to the log on bars where RSI rises above 70.

runtimeError(message)

Halts execution with the given message (error).

Does not return a value; stops the code and reports the given message as an error.

message
string
The error message to show the user.

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.

CODESCRIPT
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.

time_close(timeframe)

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.

timeframe
string
Must be an empty string ("") or the current chart's timeframe.

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.

CODESCRIPT
plot(time_close(""))

Plots each bar's close time (epoch ms).

timeframeChange(tf)

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.

tf
string
Timeframe (with a unit; e.g. "1D", "1W", "1h").

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.

CODESCRIPT
plotshape(timeframeChange("1D"), "New day")

Places a mark on the first bar of each new day.

CODESCRIPT
plot(timeframeChange("1W") ? high : na, "Start of week")

Plots the high on the first bar of a new week, empty (na) otherwise.

timeframeFromSeconds(seconds)

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.

seconds
number
Length in seconds (e.g. 3600).

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.

CODESCRIPT
label(close, timeframeFromSeconds(3600))

Labels "1h" (3600 seconds = 1 hour).

CODESCRIPT
label(close, timeframeFromSeconds(86400))

Labels "1D" (86400 seconds = 1 day).

timeframeInSeconds(tf)

Converts a timeframe to seconds. Result: 14400

Returns the timeframe's length in seconds (e.g. "1h" → 3600). na for an unrecognized timeframe.

tf
string
Timeframe (with a unit; e.g. "15m", "1h", "1D").

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.

CODESCRIPT
plot(timeframeInSeconds("1h"), "Second")

Plots a constant 3600 (1 hour = 3600 seconds).

CODESCRIPT
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.

year
number
Year (e.g. 2024).
month
number
Month (1-12).
day
number
Day (1-31).
hour
number
Optional, defaults to 0.
minute
number
Optional, defaults to 0.
second
number
Optional, defaults to 0.

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.

CODESCRIPT
plot(timestamp(2024, 1, 1, 0, 0, 0))

Plots the epoch millisecond value of 2024-01-01 00:00:00 UTC.

inSession(session)

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.

session
string
A session-time string in "HHMM-HHMM" form (e.g. "1000-1800"); comma-separated multiple ranges and a colon-separated day mask are both supported (e.g. "0930-1730:23456", 1=Sunday..7=Saturday).

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.

CODESCRIPT
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.

title
string
The indicator name shown on the chart and in the indicator list.
overlay
bool
Optional. When true the indicator draws on the PRICE pane; otherwise in a separate pane.
max_boxes_count
number
Optional. Maximum number of boxes kept on the chart at once. Defaults to 50; code that draws more silently loses its OLDEST boxes. Up to 500.
max_labels_count
number
Optional. Maximum number of labels kept at once. Defaults to 50, up to 500.
max_lines_count
number
Optional. Maximum number of lines kept at once. Defaults to 50, up to 500.
max_polylines_count
number
Optional. Maximum number of polylines kept at once. Defaults to 50, up to 500.

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.

CODESCRIPT
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.

title
string
The strategy name.
overlay
bool
Optional. When true it draws on the price pane.
initial_capital
number
Optional. Starting capital (default 10000).
pyramiding
number
Optional. How many same-direction entries may stack (default 1).
slippage
number
Optional. Default slippage percentage per order.
commission_value
number
Optional. Commission percentage per trade.
max_boxes_count
number
Optional. Maximum number of boxes kept on the chart at once. Defaults to 50; code that draws more silently loses its OLDEST boxes. Up to 500.
max_labels_count
number
Optional. Maximum number of labels kept at once. Defaults to 50, up to 500.
max_lines_count
number
Optional. Maximum number of lines kept at once. Defaults to 50, up to 500.
max_polylines_count
number
Optional. Maximum number of polylines kept at once. Defaults to 50, up to 500.

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.

CODESCRIPT
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.

lang

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.

CODESCRIPT
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.