prelude
TIP
The prelude is loaded automatically into every script. Its types and functions are referenced directly by name — do not use a namespace prefix (e.g. write close, not prelude.close).
Types
box
Handle for a box drawing.
hline
Handle for a horizontal line created by hline().
hline_style
Style of a horizontal line (e.g. solid, dashed, dotted).
label
Handle for a label drawing.
line
Handle for a line drawing.
linefill
Handle for a linefill between two lines.
plot
Handle for a plot created by plot().
plot_display
Display options for plots (e.g. all, none, price_only).
plot_line_style
A named constant for use with the plot() function's linestyle parameter, which modifies the appearance of plotted lines.
If the style argument of the function call specifies a plot style that displays a line, using this constant as the linestyle argument specifies that the plotted line is solid.
plot_simple_display
Controls which price scale and chart the plot is displayed on.
plot_style
Named constants for the style of a plot (e.g. line, histogram, area).
polyline
Handle for a polyline drawing.
scale_type
Scale value for indicator() function.
sort_order
Determines the sort order of array operations (ascending or descending).
strategy_direction
table
Handle for a table drawing.
text_format
Format string for text (e.g. in labels).
Properties
bar_index
Type: series int
Index of the current bar.
Numbering is zero-based, so the first (oldest) bar on the chart has index 0. The value increases by 1 for each subsequent bar. Use bar_index[1] to reference the previous bar's index.
close
Type: series float
Closing price of the current bar.
On historical bars, this is the last traded price when the bar closed. On real-time bars, this is the current last traded price and will change until the bar closes.
dayofmonth
Type: int
Returns the day of month (1-31) of the current bar's opening time in the symbol's timezone.
dayofweek
Type: int
Returns the day of week of the current bar's opening time.
high
Type: series float
Highest price reached during the current bar.
On historical bars, this is the maximum traded price. On real-time bars, this value can increase as new highs are made.
hl2
Type: series float
The midpoint of high and low prices: (high + low) / 2.
Also known as the median price. Useful as a simple estimate of the bar's central price.
hlc3
Type: series float
The typical price: (high + low + close) / 3.
A common price proxy that weights the close equally with the high-low range. Often used in volume weighted calculations.
hlcc4
Type: series float
Weighted close price: (high + low + close + close) / 4.
Places extra weight on the closing price, giving it 50% of the total weight.
hour
Type: int
Returns the hour (0-23) of the current bar's opening time in the symbol's timezone.
last_bar_index
Type: series int
Index of the last (newest) bar on the chart.
Combined with bar_index, this can be used to determine the distance from the current bar to the chart's end, e.g., last_bar_index - bar_index gives bars remaining.
last_bar_time
Type: series int
Timestamp of the last bar on the chart, in UNIX format (milliseconds since 1970-01-01 00:00:00 UTC).
Useful for determining the chart's time range or for time-based calculations relative to the chart end.
low
Type: series float
Lowest price reached during the current bar.
On historical bars, this is the minimum traded price. On real-time bars, this value can decrease as new lows are made.
minute
Type: int
Returns the minute (0-59) of the current bar's opening time in the symbol's timezone.
month
Type: int
Returns the month (1-12) of the current bar's opening time in the symbol's timezone.
ohlc4
Type: series float
Average price: (open + high + low + close) / 4.
The arithmetic mean of all four OHLC prices, representing the bar's overall price activity.
open
Type: series float
Opening price of the current bar.
This is the first traded price when the bar opened. On real-time bars, this value is fixed once the bar opens.
second
Type: int
Returns the second (0-59) of the current bar's opening time in the symbol's timezone.
time
Type: series int
Opening time of the current bar in UNIX format (milliseconds since 1970-01-01 00:00:00 UTC).
On real-time bars, this is fixed when the bar opens. Use with time functions like year(), month(), hour() to extract components.
time_close
Type: series int
Closing time of the current bar in UNIX format (milliseconds since 1970-01-01 00:00:00 UTC).
This is when the bar will close based on the chart's timeframe.
timenow
Type: series int
Current real-world time in UNIX format (milliseconds since 1970-01-01 00:00:00 UTC).
Unlike time, which returns the bar's time, timenow returns the actual current time and updates continuously on real-time bars.
volume
Type: series float
Trading volume of the current bar.
Represents the total number of shares or contracts traded. May be na if volume data is unavailable for the symbol.
weekofyear
Type: int
Returns the week of year (1-53) of the current bar's opening time in the symbol's timezone.
year
Type: int
Returns the year of the current bar's opening time in the symbol's timezone.
Functions
alert
alert(series string message, input string freq = alert.freq_once_per_bar)Creates an alert with the given message and frequency.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
message | series string | ||
freq | input string | alert.freq_once_per_bar |
alertcondition
alertcondition(series bool condition, const string title, const string message)Creates an alert condition that can be used to trigger alerts based on a boolean condition.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
condition | series bool | ||
title | const string | ||
message | const string |
barcolor
barcolor(
series color color,
simple int offset = 0,
input bool editable = true,
input int show_last = na,
const string title = na,
input plot_simple_display display
)Sets the color of price bars on the chart.
Each bar can have its own color, allowing for dynamic coloring based on indicator conditions.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
color | series color | The color to apply to the bar. Use na to keep the default color. | |
offset | simple int | 0 | Shifts the coloring left (negative) or right (positive) by the specified number of bars. |
editable | input bool | true | If true, the setting can be edited in the style dialog. |
show_last | input int | na | If set, only colors the last N bars. |
title | const string | na | Title shown in the style dialog. |
display | input plot_simple_display | Controls where the coloring is visible. |
bgcolor
bgcolor(
series color color,
simple int offset = 0,
input bool editable = true,
input int show_last = na,
const string title = na,
input plot_simple_display display = display.all,
const bool force_overlay = na
)Fills the background of chart bars with a specified color.
Useful for highlighting specific conditions or time periods on the chart.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
color | series color | The background color. Use na for transparent. | |
offset | simple int | 0 | Shifts the background coloring left (negative) or right (positive) by the specified number of bars. |
editable | input bool | true | If true, the setting can be edited in the style dialog. |
show_last | input int | na | If set, only fills the last N bars. |
title | const string | na | Title shown in the style dialog. |
display | input plot_simple_display | display.all | Controls where the background is visible. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
bool
Converts the x value to a bool value.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | int |
Returns: bool — false if x is na, false, or an int/float value equal to 0. true for all other values.
box
box(box x) → boxCasts na to box type.
Useful for initializing box variables that will be assigned later.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | box |
Returns: box
color
color(color x) → colorCasts na to color
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | color |
Returns: color
dayofmonth
dayofmonth(int timestamp, string timezone = syminfo.timezone) → intExtracts the day of month (1-31) from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
dayofweek
dayofweek(int timestamp, string timezone = syminfo.timezone) → intExtracts the day of week from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int — 1 (Sunday) through 7 (Saturday).
fill
Fills the area between two horizontal lines with a solid color.
Useful for creating visual zones (e.g., overbought/oversold regions).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
hline1 | hline | The first horizontal line handle (from hline()). | |
hline2 | hline | The second horizontal line handle (from hline()). | |
color | series color | color.blue | The fill color. |
title | const string | na | Title shown in the style dialog. |
editable | input bool | true | If true, the fill can be edited in the style dialog. |
fillgaps | const bool | false | If true, fills gaps in the data. |
display | input plot_simple_display | display.all | Controls where the fill is visible. |
fixnan
fixnan(series T value) → series TReplaces na values with the last non-na value in the series.
Carries forward the most recent valid value, effectively "filling" gaps in the data. If no valid value has been seen yet, returns na. Useful for maintaining continuity in series that have occasional missing values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | series T | The series value to process. |
Returns: series T — The current value if it is not na, otherwise the most recent non-na value, or na if no valid value has been seen yet.
float
Casts na to float
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | float |
Returns: float
hline
hline(
input float price,
const string title = na,
input color color = color.blue,
input hline_style linestyle = hline.style_dashed,
input int linewidth = 1,
input bool editable = true,
input plot_simple_display display = display.all
) → hlineDraws a horizontal line at a fixed price level across the entire chart.
Unlike plot(), the price must be a constant or input value, not a series.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
price | input float | The price level where the line is drawn. | |
title | const string | na | Title shown in the style dialog. |
color | input color | color.blue | Line color. |
linestyle | input hline_style | hline.style_dashed | Line style: hline.style_solid, hline.style_dashed, hline.style_dotted. |
linewidth | input int | 1 | Width of the line in pixels (1-4). |
editable | input bool | true | If true, the line can be edited in the style dialog. |
display | input plot_simple_display | display.all | Controls where the line is visible. |
Returns: hline — An hline handle that can be used with fill().
hour
hour(int timestamp, string timezone = syminfo.timezone) → intExtracts the hour (0-23) from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
indicator
indicator(
const string title,
const string shorttitle = na,
const bool overlay = false,
const string format = format.inherit,
const int precision = na,
const scale_type scale = na,
const int max_bars_back = na,
const string timeframe = na,
const bool timeframe_gaps = true,
const bool explicit_plot_zorder = false,
const int max_lines_count = 50,
const int max_labels_count = 50,
const int max_boxes_count = 50,
const int calc_bars_count = na,
const int max_polylines_count = 50,
const bool dynamic_requests = true,
const bool behind_chart = true
)Designates the script as an indicator and sets indicator-related properties.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
title | const string | The title of the script. It is displayed on the chart when no shorttitle argument is used, and becomes the publication's default title when publishing the script. | |
shorttitle | const string | na | The script's display name on charts. If specified, it will replace the title argument in most chart-related windows. |
overlay | const bool | false | If true, the script's visuals appear on the main chart pane if the user adds it to the chart directly, or in another script's pane if the user applies it to that script. If false, the script's visuals appear in a separate pane. |
format | const string | format.inherit | Specifies the formatting of the script's displayed values. Possible values: format.inherit, format.price, format.volume, format.percent. Optional. |
precision | const int | na | Specifies the number of digits after the floating point of the script's displayed values. Must be a non-negative integer no greater than 16. If format is set to format.inherit and precision is specified, the format will instead be set to format.price. When the function's format parameter uses format.volume, the precision parameter will not affect the result, as the decimal precision rules defined by format.volume supersede other precision settings. Optional. The default is inherited from the precision of the chart's symbol. |
scale | const scale_type | na | The price scale used. Possible values: scale.right, scale.left, scale.none. The scale.none value can only be applied in combination with overlay = true. Optional. By default, the script uses the same scale as the chart. Uses the scale_type type. |
max_bars_back | const int | na | The length of the historical buffer the script keeps for every variable and function, which determines how many past values can be referenced using the [] history-referencing operator. |
timeframe | const string | na | Adds multi-timeframe functionality to simple scripts. |
timeframe_gaps | const bool | true | Specifies how the indicator's values are displayed on chart bars when the timeframe is higher than the chart's. If true, a value only appears on a chart bar when the higher timeframe value becomes available, otherwise na is returned (thus a "gap" occurs). With false, what would otherwise be gaps are filled with the latest known value returned, avoiding na values. Optional. The default is true. |
explicit_plot_zorder | const bool | false | Specifies the order in which the script's plots, fills, and hlines are rendered. If true, plots are drawn in the order in which they appear in the script's code, each newer plot being drawn above the previous ones. This only applies to plot*() functions, fill(), and hline(). |
max_lines_count | const int | 50 | The number of last line drawings displayed. Possible values: 1-500. The count is approximate; more drawings than the specified count may be displayed. |
max_labels_count | const int | 50 | The number of last label drawings displayed. Possible values: 1-500. The count is approximate; more drawings than the specified count may be displayed. |
max_boxes_count | const int | 50 | The number of last box drawings displayed. Possible values: 1-500. The count is approximate; more drawings than the specified count may be displayed. |
calc_bars_count | const int | na | Limits the initial calculation of a script to the last number of bars specified. The default is na, in which case the script executes on all available bars. |
max_polylines_count | const int | 50 | The number of last polyline drawings displayed. Possible values: 1-100. The count is approximate; more drawings than the specified count may be displayed. |
dynamic_requests | const bool | true | Specifies whether the script can dynamically call functions from the request.*() namespace. Dynamic request.*() calls are allowed within the local scopes of conditional structures (e.g., if), loops (e.g., for), and exported functions. Additionally, such calls allow "series" arguments for many of their parameters. |
behind_chart | const bool | true | Controls whether all plots and drawings appear behind the chart display (if true) or in front of it (if false). This parameter only takes effect when the overlay parameter is true. |
input
Add an input to the script settings.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
defval | const int | ||
title | const string | na | |
tooltip | const string | na | |
inline | const string | na | |
group | const string | na | |
display | const plot_display | display.all | |
active | input bool | true |
Returns: input int
int
Casts na to int
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | int |
Returns: int
label
label(label x) → labelCasts na to label type.
Useful for initializing label variables that will be assigned later.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | label |
Returns: label
library
library(
const string title,
const bool overlay = false,
const bool dynamic_requests = true
)Declaration statement identifying a script as a library.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
title | const string | The title of the library and its identifier. It cannot contain spaces, special characters or begin with a digit. It is used as the publication's default title, and to uniquely identify the library in the import statement, when another script uses it. It is also used as the script's name on the chart. | |
overlay | const bool | false | |
dynamic_requests | const bool | true |
line
line(line x) → labelCasts na to line type.
Useful for initializing line variables that will be assigned later.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | line |
Returns: label
linefill
linefill(linefill x) → linefillCasts na to linefill type.
Useful for initializing linefill variables that will be assigned later.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | linefill |
Returns: linefill
minute
minute(int timestamp, string timezone = syminfo.timezone) → intExtracts the minute (0-59) from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
month
month(int timestamp, string timezone = syminfo.timezone) → intExtracts the month (1-12) from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
na
na(any value) → boolTests if value is na (not available).
Essential for handling gaps in data, missing values in series, or uninitialized variables.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | any | The value to test. Can be any type. |
Returns: bool — true if the value is undefined or missing, false otherwise.
nz
Replaces na values with a specified replacement.
If source is not na, returns source unchanged. If source is na, returns replacement. Essential for ensuring calculations don't propagate na values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
source | T | The value to check. | |
replacement | T | The value to return if source is na. |
Returns: T
plot
plot(
series float series,
const string title = na,
series color color = color.blue,
input int linewidth = 1,
input plot_style style = plot.style_line,
input bool trackprice = false,
input float histbase = 0,
simple int offset = 0,
input bool join = false,
input bool editable = true,
input int show_last = na,
input plot_display display = display.all,
input string format = na,
input int precision = na,
const bool force_overlay = na,
input plot_line_style linestyle = plot.linestyle_solid
) → plotPlots a series of data on the chart as a visual representation.
Supports various styles including line, histogram, area, columns, and more.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
series | series float | The series of values to plot. | |
title | const string | na | Title of the plot, shown in the style dialog and as a data window tooltip. |
color | series color | color.blue | Color of the plotted element. |
linewidth | input int | 1 | Width of the plotted line, in pixels (1-4). |
style | input plot_style | plot.style_line | Plot style: plot.style_line, plot.style_histogram, plot.style_area, plot.style_columns, etc. |
trackprice | input bool | false | If true, a horizontal price line is shown at the last value. |
histbase | input float | 0 | The base value for histogram/columns style. Bars extend from this level to the series value. |
offset | simple int | 0 | Shifts the plot left (negative) or right (positive) by the specified number of bars. |
join | input bool | false | If true, connects gaps in the series with a line. |
editable | input bool | true | If true, the plot style can be edited in the style dialog. |
show_last | input int | na | If set, only the last N bars are displayed. |
display | input plot_display | display.all | Controls where the plot is displayed. Use display.all, display.none, display.pane, display.price_scale, etc. |
format | input string | na | Overrides the default number format for this plot in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces the plot to display on the main chart pane. |
linestyle | input plot_line_style | plot.linestyle_solid | Line style: plot.linestyle_solid, plot.linestyle_dashed, plot.linestyle_dotted. |
Returns: plot — A plot handle that can be used with fill() to fill areas between plots.
plotarrow
plotarrow(
series float series,
const string title = na,
series color colorup = color.blue,
series color colordown = color.black,
simple int offset = 0,
input int minheight = 5,
input int maxheight = 100,
input bool editable = true,
input int show_last = na,
input plot_display display = display.all,
input string format = na,
input int precision = na,
const bool force_overlay = na
)Plots up and down arrows on the chart based on the sign of the series.
An up arrow is drawn when the value is positive, a down arrow when negative. The arrow height is proportional to the absolute value, scaled between minheight and maxheight. Useful for visualizing momentum or direction changes.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
series | series float | The data series. Positive values draw up arrows, negative draw down arrows. na or zero values draw nothing. | |
title | const string | na | Title shown in the style dialog and data window. |
colorup | series color | color.blue | Color of up arrows. |
colordown | series color | color.black | Color of down arrows. |
offset | simple int | 0 | Shifts arrows left (negative) or right (positive) by the specified bars. |
minheight | input int | 5 | Minimum arrow height in pixels. |
maxheight | input int | 100 | Maximum arrow height in pixels. |
editable | input bool | true | If true, the plot can be edited in the style dialog. |
show_last | input int | na | If set, only shows arrows on the last N bars. |
display | input plot_display | display.all | Controls where arrows are displayed. |
format | input string | na | Overrides the default number format in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
plotbar
plotbar(
series float open,
series float high,
series float low,
series float close,
const string title = na,
series color color = color.blue,
input bool editable = true,
input int show_last = na,
input plot_display display = display.all,
input string format = na,
input int precision = na,
const bool force_overlay = na
)Plots OHLC bars on the chart.
Each bar shows open, high, low, and close prices as a traditional bar chart element. Useful for overlaying custom OHLC data or displaying transformed price data.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
open | series float | The open price for each bar. | |
high | series float | The high price for each bar. | |
low | series float | The low price for each bar. | |
close | series float | The close price for each bar. | |
title | const string | na | Title shown in the style dialog and data window. |
color | series color | color.blue | Bar color. |
editable | input bool | true | If true, the plot can be edited in the style dialog. |
show_last | input int | na | If set, only shows the last N bars. |
display | input plot_display | display.all | Controls where bars are displayed. |
format | input string | na | Overrides the default number format in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
plotcandle
plotcandle(
series float open,
series float high,
series float low,
series float close,
const string title = na,
series color color = color.blue,
series color wickcolor = color.blue,
input bool editable = true,
input int show_last = na,
series color bordercolor = color.black,
input plot_display display = display.all,
input string format = na,
input int precision = na,
const bool force_overlay = na
)Plots candlesticks on the chart.
Each candle shows open, high, low, and close prices with a body and wicks. Useful for overlaying custom candlestick data (e.g., Heikin-Ashi) or displaying transformed price series.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
open | series float | The open price for each candle. | |
high | series float | The high price for each candle. | |
low | series float | The low price for each candle. | |
close | series float | The close price for each candle. | |
title | const string | na | Title shown in the style dialog and data window. |
color | series color | color.blue | Candle body color. |
wickcolor | series color | color.blue | Candle wick color. |
editable | input bool | true | If true, the plot can be edited in the style dialog. |
show_last | input int | na | If set, only shows the last N candles. |
bordercolor | series color | color.black | Candle body border color. |
display | input plot_display | display.all | Controls where candles are displayed. |
format | input string | na | Overrides the default number format in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
plotchar
Plots a character at each bar where the series has a non-na value.
The character is positioned relative to the bar based on location. Useful for marking specific events or conditions on the chart.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
series | series float | The data series. A character is drawn when the value is not na. | |
title | const string | na | Title shown in the style dialog and data window. |
char | input string | "⭐" | The character to display (Unicode supported). |
location | input string | location.abovebar | Vertical position: location.abovebar, location.belowbar, location.top, location.bottom, location.absolute. |
color | series color | color.blue | Character color. |
offset | simple int | 0 | Shifts the character left (negative) or right (positive) by bars. |
text | const string | na | Optional text displayed near the character. |
textcolor | series color | color.blue | Color of the optional text. |
editable | input bool | true | If true, the plot can be edited in the style dialog. |
size | const string | size.auto | Character size: size.auto, size.tiny, size.small, size.normal, size.large, size.huge. |
show_last | input int | na | If set, only shows characters on the last N bars. |
display | input plot_display | display.all | Controls where characters are displayed. |
format | input string | na | Overrides the default number format in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
plotshape
Plots a shape at each bar where the series has a non-na value.
Various shape styles are available (arrows, circles, crosses, etc.). The shape is positioned relative to the bar based on location.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
series | series float | The data series. A shape is drawn when the value is not na. | |
title | const string | na | Title shown in the style dialog and data window. |
style | input string | shape.xcross | Shape style: shape.xcross, shape.cross, shape.triangleup, shape.triangledown, shape.flag, shape.circle, shape.arrowup, shape.arrowdown, shape.labelup, shape.labeldown, shape.square, shape.diamond. |
location | input string | location.abovebar | Vertical position: location.abovebar, location.belowbar, location.top, location.bottom, location.absolute. |
color | series color | color.blue | Shape color. |
offset | simple int | 0 | Shifts the shape left (negative) or right (positive) by bars. |
text | const string | na | Optional text displayed near the shape. |
textcolor | series color | color.blue | Color of the optional text. |
editable | input bool | true | If true, the plot can be edited in the style dialog. |
size | const string | size.auto | Shape size: size.auto, size.tiny, size.small, size.normal, size.large, size.huge. |
show_last | input int | na | If set, only shows shapes on the last N bars. |
display | input plot_display | display.all | Controls where shapes are displayed. |
format | input string | na | Overrides the default number format in the data window. |
precision | input int | na | Number of decimal places for display. |
force_overlay | const bool | na | If true, forces display on the main chart pane. |
second
second(int timestamp, string timezone = syminfo.timezone) → intExtracts the second (0-59) from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
strategy
strategy(
const string title,
const string shorttitle = na,
const bool overlay = false,
const string format = format.inherit,
const int precision = na,
const scale_type scale = na,
const int pyramiding = 0,
const bool calc_on_order_fills = false,
const bool calc_on_every_tick = false,
const int max_bars_back = na,
const int backtest_fill_limits_assumption = 0,
const string default_qty_type = strategy.fixed,
const float default_qty_value = 1,
const float initial_capital = 1000000,
simple string currency = syminfo.currency,
const int slippage = 0,
const string commission_type = strategy.commission.percent,
const float commission_value = 0,
const bool process_orders_on_close = false,
const string close_entries_rule = "FIFO",
const float margin_long = 100,
const float margin_short = 100,
const bool explicit_plot_zorder = false,
const int max_lines_count = 50,
const int max_labels_count = 50,
const int max_boxes_count = 50,
const int calc_bars_count = na,
const float risk_free_rate = 2,
const bool use_bar_magnifier = false,
const bool fill_orders_on_standard_ohlc = false,
const int max_polylines_count = 50,
const bool dynamic_requests = true,
const bool behind_chart = true
)Designates the script as a strategy and sets strategy-related properties.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
title | const string | ||
shorttitle | const string | na | |
overlay | const bool | false | |
format | const string | format.inherit | |
precision | const int | na | |
scale | const scale_type | na | |
pyramiding | const int | 0 | |
calc_on_order_fills | const bool | false | |
calc_on_every_tick | const bool | false | |
max_bars_back | const int | na | |
backtest_fill_limits_assumption | const int | 0 | |
default_qty_type | const string | strategy.fixed | |
default_qty_value | const float | 1 | |
initial_capital | const float | 1000000 | |
currency | simple string | syminfo.currency | |
slippage | const int | 0 | |
commission_type | const string | strategy.commission.percent | |
commission_value | const float | 0 | |
process_orders_on_close | const bool | false | |
close_entries_rule | const string | "FIFO" | |
margin_long | const float | 100 | |
margin_short | const float | 100 | |
explicit_plot_zorder | const bool | false | |
max_lines_count | const int | 50 | |
max_labels_count | const int | 50 | |
max_boxes_count | const int | 50 | |
calc_bars_count | const int | na | |
risk_free_rate | const float | 2 | |
use_bar_magnifier | const bool | false | |
fill_orders_on_standard_ohlc | const bool | false | |
max_polylines_count | const int | 50 | |
dynamic_requests | const bool | true | |
behind_chart | const bool | true |
string
string(string x) → stringCasts na to string
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | string |
Returns: string
time
time(
series string timeframe,
series string session = na,
series string timezone = syminfo.timezone
) → series intReturns the bar's opening time for a specific timeframe and session.
Useful for multi-timeframe analysis or session-aware calculations.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timeframe | series string | The timeframe string (e.g., "D", "60", "W"). | |
session | series string | na | Optional session specification (e.g., "0930-1600"). If na, no session filter is applied. |
timezone | series string | syminfo.timezone | Timezone for session interpretation. Defaults to symbol's timezone. |
Returns: series int — UNIX timestamp in milliseconds, or na if the bar falls outside the specified session.
time_close
time_close(
series string timeframe,
series string session = na,
series string timezone = syminfo.timezone
) → series intReturns the bar's closing time for a specific timeframe and session.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timeframe | series string | The timeframe string (e.g., "D", "60", "W"). | |
session | series string | na | Optional session specification. If na, no session filter is applied. |
timezone | series string | syminfo.timezone | Timezone for session interpretation. Defaults to symbol's timezone. |
Returns: series int — UNIX timestamp in milliseconds, or na if the bar falls outside the specified session.
timestamp
Parses a date/time string and returns the corresponding UNIX timestamp in milliseconds.
The string must be in ISO 8601 format (e.g., "2024-01-15" or "2024-01-15T09:30:00Z") or IETF RFC 2822 format.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
date_string | string | The date/time string to parse. |
Returns: int
weekofyear
weekofyear(int timestamp, string timezone = syminfo.timezone) → intExtracts the week of year (1-53) from a UNIX timestamp in the specified timezone.
Week 1 is the week containing January 4th (ISO 8601).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int
year
year(int timestamp, string timezone = syminfo.timezone) → intExtracts the year from a UNIX timestamp in the specified timezone.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
timestamp | int | The UNIX timestamp in milliseconds. | |
timezone | string | syminfo.timezone | The timezone for interpretation. Defaults to symbol's timezone. |
Returns: int