prelude
TIP
Prelude 会自动加载到每个脚本中。它的类型和函数都应直接用名称引用——不要添加命名空间前缀(例如写 close,而不是 prelude.close)。
类型
| 名称 | 说明 |
|---|---|
AlertFreq | alert() 的触发频率常量。 |
Array<T> | 类型为 T 的有序、可索引的元素集合。 |
BarmergeGaps | request.*() 函数的间隙模式。 |
BarmergeLookahead | request.*() 函数的前瞻模式。 |
Box | 方框图的句柄。 |
CommissionType | strategy() 的佣金计算方式。 |
Currency | 货币代码(ISO 4217)。 |
DayOfWeek | 星期常量。 |
DefaultQtyType | strategy() 的默认数量模式。 |
Direction | 策略订单方向。 |
DividendsField | request.dividends() 的字段选择器。 |
EarningsField | request.earnings() 的字段选择器。 |
Extend | 线条延伸方向。 |
FontFamily | 文字绘图的字体系列。 |
Format | 图表和标签的数值格式。 |
Hline | 由 hline 创建的水平线的句柄。 |
HlineStyle | 水平线绘图的样式。 |
Label | 标签绘图的句柄。 |
LabelStyle | 标签绘图的样式。 |
Line | 画线句柄。 |
LineStyle | 线条绘图的样式。 |
Linefill | 两行之间的行填充句柄。 |
Location | plot_shape() 和 plot_char() 的垂直位置。 |
Map<K, V> | 将 K 类型的键映射到 V 类型值的键值存储。 |
Matrix<T> | 以行优先顺序存储的 T 类型元素的二维表。 |
OcaType | 策略订单的 OCA(一单取消全部)行为。 |
Plot | 由 plot 创建的绘图的句柄。 |
PlotDisplay | 显示绘图选项(例如全部、无、仅价格)。 |
PlotSimpleDisplay | 控制绘图显示的价格比例和图表。 |
PlotStyle | 图表系列的绘图样式。 |
Polyline | 多段线绘图的句柄。 |
Position | 表格和标签在图表面板上的位置。 |
ScaleType | indicator() 和 strategy() 的价格坐标轴挂载模式。 |
Session | 交易时段类型。 |
Shape | plot_shape() 绘制的图形。 |
Size | 绘图和标签的尺寸常量。 |
SortOrder | 数组和矩阵操作的排序方向。 |
SplitsField | request.splits() 的字段选择器。 |
Table | 表格绘图的句柄。 |
TextFormat | 文字格式标志(粗体、斜体)。可使用 + 组合多个值。 |
TextHAlign | 文字水平对齐方式。 |
TextVAlign | 文字垂直对齐方式。 |
TextWrap | 文字换行模式。 |
Xloc | 绘图对象的 X 轴坐标模式。 |
Yloc | 标签的 Y 轴坐标模式。 |
属性
ask
类型: series float
当前 K 线的最优卖价(ask)。仅在 1T(tick)周期可用,其他周期返回 na。
bar_index
类型: series int
当前 bar 的索引。编号从零开始,因此图表上第一个(最旧的)bar 的索引为 0。每个后续 bar 的值都会增加 1。使用 bar_index[1] 引用前一个 bar 的索引。
bid
类型: series float
当前 K 线的最优买价(bid)。仅在 1T(tick)周期可用,其他周期返回 na。
close
类型: series float
当前 bar 的收盘价。在历史 bars 上,这是 bar 收盘时的最后交易价格。在实时 bars 上,这是当前的最后交易价格,并将发生变化,直到 bar 收盘。
day_of_month
类型: int
返回交易品种时区中当前 bar 开市时间的月份 (1-31) 中的某一天。
day_of_week
类型: DayOfWeek
返回当前 bar 的开放时间是星期几。
high
类型: series float
当前 bar 期间达到的最高价格。在历史 bars 上,这是最高交易价格。在实时 bars 上,该值可能会随着新高的出现而增加。
hl2
类型: series float
高价和低价的中点:(high + low) / 2。也称为中位价格。可用于简单估算 bar 的中心价格。
hlc3
类型: series float
典型价格:(high + low + close) / 3。一种常见的价格代理,对收盘价和高低范围的权重相等。常用于体积加权计算。
hlcc4
类型: series float
加权收盘价:(high + low + close + close) / 4。对收盘价给予额外权重,占总权重的 50%。
hour
类型: int
返回符号时区中当前 bar 开放时间的小时 (0-23)。
last_bar_index
类型: series int
图表上最后一个(最新)bar 的索引。与 bar_index 结合,这可用于确定从当前 bar 到图表末尾的距离,例如,last_bar_index - bar_index 给出剩余的 bars。
last_bar_time
类型: series int
图表上最后一个 bar 的时间戳,采用 UNIX 格式(自 1970-01-01 00:00:00 UTC 以来的毫秒数)。对于确定图表的时间范围或相对于图表结束的基于时间的计算很有用。
low
类型: series float
当前 bar 期间达到的最低价格。在历史 bars 上,这是最低交易价格。在实时 bars 上,该值可能会随着新低的出现而减小。
minute
类型: int
返回交易品种时区中当前 bar 开市时间的分钟 (0-59)。
month
类型: int
返回交易品种时区中当前 bar 开放时间的月份 (1-12)。
ohlc4
类型: series float
平均价格:(open + high + low + close) / 4。所有四个 OHLC 价格的算术平均值,代表 bar 的整体价格活动。
open
类型: series float
当前 bar 的开盘价。这是 bar 开盘时的第一个交易价格。在实时 bars 上,一旦 bar 打开,该值就固定。
second
类型: int
返回交易品种时区中当前 bar 开放时间的秒数 (0-59)。
time
类型: series int
bar 左边缘的 UNIX 时间戳。该值以 Unix 纪元的毫秒数表示,并且在当前实时 bar 仍在形成时不会漂移。
time_close
类型: series int
bar 右边缘的 UNIX 时间戳。在基于时间的图表上,这是活跃 bar 的预定收盘价。
time_now
类型: series int
UNIX 格式的当前实际时间(自 1970-01-01 00:00:00 UTC 以来的毫秒数)。与返回 bar 时间的 time 不同,time_now 返回实际的当前时间并在实时 bars 上持续更新。
time_tradingday
类型: series int
返回当前 bar 所属交换会话的 UTC 午夜标记。对于共享同一交易时段的所有 bars,结果保持相同,这在交易跨日历边界时非常方便。
volume
类型: series float
当前 bar 的交易量。代表交易的股票或合约总数。如果交易品种的成交量数据不可用,则可能为 na。
week_of_year
类型: int
返回符号时区中当前 bar 开放时间的一年中的第几周 (1-53)。
year
类型: int
返回符号时区中当前 bar 开放时间的年份。
函数
alert
alert(message: series string, freq: input AlertFreq = AlertFreq.OncePerBar)使用给定的消息和频率创建警报。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
message | series string | ||
freq | input AlertFreq | AlertFreq.OncePerBar |
alert_condition
alert_condition(
condition: series bool,
title: const string,
message: const string
)创建可用于根据布尔条件触发警报的警报条件。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
condition | series bool | ||
title | const string | ||
message | const string |
bar_color
bar_color(
color: series color,
offset: simple int = 0,
editable: input bool = true,
show_last: input int = na,
title: const string = na,
display: input PlotSimpleDisplay
)设置图表上价格 bars 的颜色。每个 bar 可以有自己的颜色,允许根据指示器条件进行动态着色。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
color | series color | 应用于 bar 的颜色。使用 na 保留默认颜色。 | |
offset | simple int | 0 | 将颜色向左(负)或右(正)移动指定数量的 bars。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑该设置。 |
show_last | input int | na | 如果设置,则仅对最后 N 个 bars 着色。 |
title | const string | na | 标题显示在样式对话框中。 |
display | input PlotSimpleDisplay | 控制颜色可见的位置。 |
bg_color
bg_color(
color: series color,
offset: simple int = 0,
editable: input bool = true,
show_last: input int = na,
title: const string = na,
display: input PlotSimpleDisplay = display.ALL,
force_overlay: const bool = na
)用指定的颜色填充图表 bars 的背景。用于突出显示图表上的特定条件或时间段。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
color | series color | 背景颜色。使用 na 表示透明。 | |
offset | simple int | 0 | 将背景颜色向左(负)或右(正)移动指定数量的 bars。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑该设置。 |
show_last | input int | na | 如果设置,则仅填充最后 N 个 bars。 |
title | const string | na | 标题显示在样式对话框中。 |
display | input PlotSimpleDisplay | display.ALL | 控制背景可见的位置。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
bool
将 x 值转换为 bool 值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | int |
返回: bool — 如果 x 是 na、false 或等于 0 的 int/float 值,则为 false。 true 对于所有其他值。
box
box(x: Box): Box将 na 转换为盒子类型。对于初始化稍后将分配的框变量很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | Box |
返回: Box
color
color(x: color): color将 na 投射为颜色
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | color |
返回: color
day_of_month
day_of_month(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取月份中的日期 (1-31)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
day_of_week
day_of_week(timestamp: int, timezone: string = syminfo.timezone): DayOfWeek从指定时区的 UNIX 时间戳中提取星期几。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: DayOfWeek — 1(周日)至 7(周六)。
fill
用纯色填充两条水平线之间的区域。对于创建视觉区域(例如超买/超卖区域)很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
hline1 | Hline | 第一个水平线句柄(来自 hline)。 | |
hline2 | Hline | 第二个水平线句柄(来自 hline)。 | |
color | series color | color.BLUE | 填充颜色。 |
title | const string | na | 标题显示在样式对话框中。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑填充。 |
fill_gaps | const bool | false | 如果为真,则填补数据中的空白。 |
display | input PlotSimpleDisplay | display.ALL | 控制填充的可见位置。 |
fixnan
fixnan(value: series T): series T将 na 值替换为系列中最后一个非 na 值。延续最新的有效值,有效“填补”数据空白。如果尚未看到有效值,则返回 na。对于保持偶尔缺失值的系列的连续性很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | series T | 要处理的系列值。 |
返回: series T — 当前值(如果不是 na),否则为最新的非 na 值,或者 na(如果尚未看到有效值)。
float
将 na 强制转换为浮动
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | float |
返回: float
hline
hline(
price: input float,
title: const string = na,
color: input color = color.BLUE,
linestyle: input HlineStyle = HlineStyle.Dashed,
linewidth: input int = 1,
editable: input bool = true,
display: input PlotSimpleDisplay = display.ALL
): Hline在整个图表上以固定价格水平绘制一条水平线。与 plot 不同,价格必须是常量或传入值,而不是序列。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
price | input float | 绘制线的价格水平。 | |
title | const string | na | 标题显示在样式对话框中。 |
color | input color | color.BLUE | 线条颜色。 |
linestyle | input HlineStyle | HlineStyle.Dashed | 线条样式:hLineStyle.Solid、hLineStyle.Dashed、hLineStyle.Dotted。 |
linewidth | input int | 1 | 线条的宽度(以像素 (1-4) 为单位)。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑该线。 |
display | input PlotSimpleDisplay | display.ALL | 控制线的可见位置。 |
返回: Hline — 可与 fill 一起使用的 hline 句柄。
hour
hour(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取小时 (0-23)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
indicator
indicator(
title: const string,
shorttitle: const string = na,
overlay: const bool = false,
format: const Format = Format.Inherit,
precision: const int = na,
scale: const ScaleType = na,
max_bars_back: const int = na,
timeframe: const string = na,
timeframe_gaps: const bool = true,
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,
max_polylines_count: const int = 50,
dynamic_requests: const bool = true,
behind_chart: const bool = true
)将脚本指定为指标并设置与指标相关的属性。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
title | const string | 脚本的标题。当不使用 shorttitle 参数时,它会显示在图表上,并在发布脚本时成为出版物的默认标题。 | |
shorttitle | const string | na | 脚本在图表上的显示名称。如果指定,它将替换大多数图表相关窗口中的 title 参数。 |
overlay | const bool | false | 如果为 true,则如果用户将脚本直接添加到图表,则脚本的视觉效果将显示在主图表窗格上;如果用户将脚本应用到该脚本,则脚本的视觉效果将显示在另一个脚本的窗格中。如果为 false,则脚本的视觉效果将显示在单独的窗格中。 |
format | const Format | Format.Inherit | 指定脚本显示值的格式。可能的值:Format.Inherit、Format.Price、Format.Volume、Format.Percent。选修的。 |
precision | const int | na | 指定脚本显示值的浮点后的位数。必须是不大于 16 的非负整数。如果 format 设置为 Format.Inherit 并指定 precision,则格式将设置为 Format.Price。当函数的 format 参数使用 Format.Volume 时,precision 参数不会影响结果,因为 Format.Volume 定义的小数精度规则会取代其他精度设置。选修的。默认值继承自图表符号的精度。 |
scale | const ScaleType | na | 使用的价格等级。可能的值:scale.right、scale.left、scale.none。 scale.none 值只能与 overlay = true 结合使用。选修的。默认情况下,脚本使用与图表相同的比例。使用 scale_type 类型。 |
max_bars_back | const int | na | 脚本为每个变量和函数保留的历史缓冲区的长度,它确定可以使用 [] 历史引用运算符引用多少个过去的值。 |
timeframe | const string | na | 向简单脚本添加多时间范围功能。 |
timeframe_gaps | const bool | true | 指定当时间范围高于图表的时间范围时指标值如何在图表 bars 上显示。如果是 true,则仅当较高的 timeframe 值可用时,值才会出现在图表 bar 上,否则返回 na (因此会出现“间隙”)。使用 false 时,原本存在的间隙将被返回的最新已知值填充,从而避免使用 na 值。选修的。默认为 true。 |
explicit_plot_zorder | const bool | false | 指定脚本的绘图、填充和线条的渲染顺序。如果是 true,则按照脚本代码中出现的顺序绘制绘图,每个较新的绘图都绘制在之前的绘图上方。这仅适用于 plot*() 函数、fill 和 hline。 |
max_lines_count | const int | 50 | 最后显示的 line 绘图的数量。可能的值:1-500。计数为近似值;可能会显示多于指定数量的绘图。 |
max_labels_count | const int | 50 | 最后显示的 label 绘图的数量。可能的值:1-500。计数为近似值;可能会显示多于指定数量的绘图。 |
max_boxes_count | const int | 50 | 最后显示的 box 绘图的数量。可能的值:1-500。计数为近似值;可能会显示多于指定数量的绘图。 |
calc_bars_count | const int | na | 将脚本的初始计算限制为最后指定的 bars 数量。默认值为 na,在这种情况下,脚本将在所有可用的 bars 上执行。 |
max_polylines_count | const int | 50 | 最后显示的 polyline 绘图的数量。可能的值:1-100。计数为近似值;可能会显示多于指定数量的绘图。 |
dynamic_requests | const bool | true | 指定脚本是否可以动态调用 request.*() 命名空间中的函数。条件结构(例如 if)、循环(例如 for)和导出函数的本地范围内允许动态 request.*() 调用。此外,此类调用允许其许多参数使用“系列”参数。 |
behind_chart | const bool | true | 控制所有绘图和绘图是显示在图表显示后面(如果 true)还是显示在图表显示前面(如果 false)。该参数仅在overlay参数为true时生效。 |
示例
// RSI indicator in a separate pane
indicator("My RSI", shorttitle: "RSI", overlay: false);
plot(ta.rsi(close, 14), title: "RSI");// Overlay indicator on the price chart
indicator("EMA Cross", overlay: true);
plot(ta.ema(close, 9), color: color.GREEN);
plot(ta.ema(close, 21), color: color.RED);input
创建在脚本设置 UI 中公开的传入值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
defval | const int | ||
title | const string | na | |
tooltip | const string | na | |
inline | const string | na | |
group | const string | na | |
display | const PlotDisplay | display.ALL | |
active | input bool | true |
返回: input int
int
将 na 转换为 int
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | int |
返回: int
label
label(x: Label): Label将 na 转换为标签类型。对于初始化稍后分配的标签变量很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | Label |
返回: Label
library
library(
title: const string,
overlay: const bool = false,
dynamic_requests: const bool = true
)将脚本标识为库的声明语句。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
title | const string | 图书馆的标题及其标识符。它不能包含空格、特殊字符或以数字开头。它用作出版物的默认标题,并在另一个脚本使用它时在 import 语句中唯一标识该库。它也用作图表上的脚本名称。 | |
overlay | const bool | false | 如果为 true,则库导出函数生成的任何视觉效果都将绘制在主图表窗格上。如果是 false,它们将出现在单独的窗格中。选修的。默认为 false。 |
dynamic_requests | const bool | true | 指定库的导出函数是否可以包含动态 request.*() 调用 - 即放置在条件结构或循环内的调用,或者接受通常需要更简单限定符的参数的“系列”参数的调用。选修的。默认为 true。 |
line
line(x: Line): Line将 na 转换为线型。对于初始化稍后将分配的行变量很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | Line |
返回: Line
linefill
linefill(x: Linefill): Linefill将 na 转换为行填充类型。对于初始化稍后分配的行填充变量很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | Linefill |
返回: Linefill
max_bars_back
max_bars_back(variable: variableref, num: const int)通过 [] 历史引用运算符设置特定变量或内置系列可用的历史 bars 的最大数量。如果 bars 的实际数量超过最大值,则只有最新的 bars 可用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
variable | variableref | ||
num | const int | 历史 bars 的最大数量。 |
minute
minute(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取分钟 (0-59)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
month
month(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取月份 (1-12)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
na
na(value: any): bool测试 value 是否为 na(不可用)。对于处理数据间隙、系列缺失值或未初始化变量至关重要。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | any | 要测试的值。可以是任何类型。 |
返回: bool — 如果值未定义或缺失,则为 true,否则为 false。
nz
将 na 值替换为指定的替换值。如果 source 不是 na,则返回 source 不变。如果 source 是 na,则返回 replacement。对于确保计算不会传播 na 值至关重要。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
source | T | 要检查的值。 | |
replacement | T | 如果 source 是 na,则返回值。 |
返回: T
plot
plot(
series: series float,
title: const string = na,
color: series color = color.BLUE,
linewidth: input int = 1,
style: input PlotStyle = PlotStyle.Line,
track_price: input bool = false,
hist_base: input float = 0,
offset: simple int = 0,
join: input bool = false,
editable: input bool = true,
show_last: input int = na,
display: input PlotDisplay = display.ALL,
format: input Format = na,
precision: input int = na,
force_overlay: const bool = na,
linestyle: input PlotStyle = PlotStyle.Line
): Plot在图表上绘制一系列数据作为可视化表示。支持线条、柱状图、面积、柱状图等多种样式。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
series | series float | 要绘制的一系列值。 | |
title | const string | na | 绘图的标题,显示在样式对话框中并作为数据窗口工具提示。 |
color | series color | color.BLUE | 绘制元素的颜色。 |
linewidth | input int | 1 | 绘制线的宽度,以像素为单位 (1-4)。 |
style | input PlotStyle | PlotStyle.Line | 绘图样式:plot.style_line、plot.style_histogram、plot.style_area、plot.style_columns 等。 |
track_price | input bool | false | 如果为 true,则会在最后一个值处显示一条水平价格线。 |
hist_base | input float | 0 | 直方图/列样式的基值。条形图从此水平延伸至系列值。 |
offset | simple int | 0 | 将绘图向左(负)或右(正)移动指定数量的 bars。 |
join | input bool | false | 如果为真,则用线连接系列中的间隙。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑打印样式。 |
show_last | input int | na | 如果设置,则仅显示最后 N 个 bars。 |
display | input PlotDisplay | display.ALL | 控制绘图的显示位置。使用 display.all、display.none、display.pane、display.price_scale 等。 |
format | input Format | na | 覆盖数据窗口中该图的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,则强制绘图显示在主图表窗格上。 |
linestyle | input PlotStyle | PlotStyle.Line | 线条样式:PlotStyle.Line、PlotStyle.Line、PlotStyle.Line。 |
返回: Plot — 可与 fill 一起使用的绘图句柄来填充绘图之间的区域。
示例
// Plot a simple moving average
let smaLine = plot(ta.sma(close, 20), title: "SMA 20", color: color.BLUE);// Plot two EMAs and fill between them
let p1 = plot(ta.ema(close, 9), title: "EMA 9", color: color.GREEN); let p2 =
plot(ta.ema(close, 21), title: "EMA 21", color: color.RED); fill(p1, p2,
color: color.new(color.GRAY, 80));plot_arrow
plot_arrow(
series: series float,
title: const string = na,
color_up: series color = color.BLUE,
color_down: series color = color.BLACK,
offset: simple int = 0,
min_height: input int = 5,
max_height: input int = 100,
editable: input bool = true,
show_last: input int = na,
display: input PlotDisplay = display.ALL,
format: input Format = na,
precision: input int = na,
force_overlay: const bool = na
)根据系列的符号在图表上绘制向上和向下箭头。当值为正时绘制向上箭头,当值为负时绘制向下箭头。箭头高度与绝对值成正比,在 minheight 和 maxheight 之间缩放。对于可视化动量或方向变化很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
series | series float | 数据系列。正值绘制向上箭头,负值绘制向下箭头。 na 或零值不会绘制任何内容。 | |
title | const string | na | 标题显示在样式对话框和数据窗口中。 |
color_up | series color | color.BLUE | 向上箭头的颜色。 |
color_down | series color | color.BLACK | 向下箭头的颜色。 |
offset | simple int | 0 | 将箭头向左(负)或右(正)移动指定的 bars。 |
min_height | input int | 5 | 最小箭头高度(以像素为单位)。 |
max_height | input int | 100 | 最大箭头高度(以像素为单位)。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑绘图。 |
show_last | input int | na | 如果设置,则仅显示最后 N 个 bars 上的箭头。 |
display | input PlotDisplay | display.ALL | 控制箭头的显示位置。 |
format | input Format | na | 覆盖数据窗口中的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
plot_bar
plot_bar(
open: series float,
high: series float,
low: series float,
close: series float,
title: const string = na,
color: series color = color.BLUE,
editable: input bool = true,
show_last: input int = na,
display: input PlotDisplay = display.ALL,
format: input Format = na,
precision: input int = na,
force_overlay: const bool = na
)在图表上绘制 OHLC bars。每个 bar 将开盘价、最高价、最低价和收盘价显示为传统的 bar 图表元素。对于覆盖自定义 OHLC 数据或显示转换后的价格数据非常有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
open | series float | 每个 bar 的开盘价。 | |
high | series float | 每个 bar 的价格很高。 | |
low | series float | 每个 bar 的低价。 | |
close | series float | 每个 bar 的收盘价。 | |
title | const string | na | 标题显示在样式对话框和数据窗口中。 |
color | series color | color.BLUE | 条形颜色。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑绘图。 |
show_last | input int | na | 如果设置,则仅显示最后 N 个 bars。 |
display | input PlotDisplay | display.ALL | 控制 bars 的显示位置。 |
format | input Format | na | 覆盖数据窗口中的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
plot_candle
plot_candle(
open: series float,
high: series float,
low: series float,
close: series float,
title: const string = na,
color: series color = color.BLUE,
wick_color: series color = color.BLUE,
editable: input bool = true,
show_last: input int = na,
border_color: series color = color.BLACK,
display: input PlotDisplay = display.ALL,
format: input Format = na,
precision: input int = na,
force_overlay: const bool = na
)在图表上绘制烛台。每根蜡烛均显示开盘价、最高价、最低价和收盘价,并带有实体和影线。对于覆盖自定义烛台数据(例如 Heikin-Ashi)或显示转换后的价格系列非常有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
open | series float | 每根蜡烛的开盘价。 | |
high | series float | 每根蜡烛的价格很高。 | |
low | series float | 每支蜡烛的低价。 | |
close | series float | 每根蜡烛的收盘价。 | |
title | const string | na | 标题显示在样式对话框和数据窗口中。 |
color | series color | color.BLUE | 蜡烛体颜色。 |
wick_color | series color | color.BLUE | 烛芯颜色。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑绘图。 |
show_last | input int | na | 如果设置,则仅显示最后 N 根蜡烛。 |
border_color | series color | color.BLACK | 蜡烛体边框颜色。 |
display | input PlotDisplay | display.ALL | 控制蜡烛的显示位置。 |
format | input Format | na | 覆盖数据窗口中的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
plot_char
在系列具有非 na 值的每个 bar 处绘制一个字符。该字符基于 location 相对于 bar 定位。用于在图表上标记特定事件或条件。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
series | series float | 数据系列。当值不是 na 时,将绘制字符。 | |
title | const string | na | 标题显示在样式对话框和数据窗口中。 |
char | input string | "⭐" | 要显示的字符(支持 Unicode)。 |
location | input Location | Location.AboveBar | 垂直位置:location.abovebar、location.belowbar、location.top、location.bottom、location.absolute。 |
color | series color | color.BLUE | 人物颜色。 |
offset | simple int | 0 | 将字符向左(负)或右(正)移动 bars。 |
text | const string | na | 显示在角色附近的可选文本。 |
text_color | series color | color.BLUE | 可选文本的颜色。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑绘图。 |
size | const Size | Size.Auto | 字符大小:Size.Auto、Size.Tiny、Size.Small、Size.Normal、Size.Large、Size.Huge。 |
show_last | input int | na | 如果设置,则仅显示最后 N 个 bars 上的字符。 |
display | input PlotDisplay | display.ALL | 控制字符的显示位置。 |
format | input Format | na | 覆盖数据窗口中的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
plot_shape
在每个 bar 处绘制一个形状,其中该系列具有非 na 值。提供各种形状样式(箭头、圆形、十字形等)。该形状基于 location 相对于 bar 定位。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
series | series float | 数据系列。当值不是 na 时,将绘制形状。 | |
title | const string | na | 标题显示在样式对话框和数据窗口中。 |
style | input Shape | Shape.XCross | 形状样式: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 Location | Location.AboveBar | 垂直位置:location.abovebar、location.belowbar、location.top、location.bottom、location.absolute。 |
color | series color | color.BLUE | 形状颜色。 |
offset | simple int | 0 | 将形状向左(负)或右(正)移动 bars。 |
text | const string | na | 形状附近显示的可选文本。 |
text_color | series color | color.BLUE | 可选文本的颜色。 |
editable | input bool | true | 如果为 true,则可以在样式对话框中编辑绘图。 |
size | const Size | Size.Auto | 形状大小:Size.Auto、Size.Tiny、Size.Small、Size.Normal、Size.Large、Size.Huge。 |
show_last | input int | na | 如果设置,则仅显示最后 N 个 bars 上的形状。 |
display | input PlotDisplay | display.ALL | 控制形状的显示位置。 |
format | input Format | na | 覆盖数据窗口中的默认数字格式。 |
precision | input int | na | 显示的小数位数。 |
force_overlay | const bool | na | 如果为 true,力将显示在主图表窗格上。 |
second
second(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取秒数 (0-59)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
strategy
strategy(
title: const string,
shorttitle: const string = na,
overlay: const bool = false,
format: const Format = Format.Inherit,
precision: const int = na,
scale: const ScaleType = 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 DefaultQtyType = DefaultQtyType.Fixed,
default_qty_value: const float = 1,
initial_capital: const float = 1000000,
currency: simple string = syminfo.currency,
slippage: const int = 0,
commission_type: const CommissionType = CommissionType.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
)将脚本指定为策略并设置与策略相关的属性。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
title | const string | 脚本的标题。未设置 shorttitle 时显示在图表上,并成为发布时的默认标题。 | |
shorttitle | const string | na | 图表和大多数图表相关窗口中显示的脚本的缩写显示名称。选修的。 |
overlay | const bool | false | 如果是 true,则策略的视觉效果将绘制在主图表窗格上。如果是 false,它们将出现在单独的窗格中。选修的。默认为 false。 |
format | const Format | Format.Inherit | 指定策略显示值的格式。可能的值:Format.Inherit、Format.Price、Format.Volume、Format.Percent。选修的。 |
precision | const int | na | 策略显示值的小数位数。必须是不大于16的非负整数。当使用Format.Volume时,此设置无效。选修的。默认值继承自图表符号的精度。 |
scale | const ScaleType | na | 使用的价格范围。可能的值:scale.right、scale.left、scale.none。仅当 overlay = true 时才允许 scale.none。选修的。 |
pyramiding | const int | 0 | 同一时间允许在同一方向上进入的最大数量。选修的。默认值为 0,表示每个方向仅允许一个条目。 |
calc_on_order_fills | const bool | false | 如果是 true,则只要在 bar 期间执行订单,策略就会重新计算其逻辑,而不仅仅是在 bar 收盘时。选修的。默认为 false。 |
calc_on_every_tick | const bool | false | 如果是 true,策略会在每次传入的实时价格更新(变动点)时重新计算。如果是 false,则计算仅在 bar 关闭时发生。选修的。默认为 false。 |
max_bars_back | const int | na | 为所有变量和函数保留的历史缓冲区的深度,控制可以使用 [] 运算符读取多少个过去的值。选修的。 |
backtest_fill_limits_assumption | const int | 0 | 在假设执行限价单之前,市场必须移动超出限价的价格变动数。选修的。默认为 0。 |
default_qty_type | const DefaultQtyType | DefaultQtyType.Fixed | 确定如何解释 default_qty_value。可能的值:strategy.fixed、strategy.percent_of_equity、strategy.cash。选修的。默认值为 strategy.fixed。 |
default_qty_value | const float | 1 | 在入场调用中未指定数量时使用的默认订单大小。它的含义取决于default_qty_type。选修的。默认为 1.0。 |
initial_capital | const float | 1000000 | 回测开始时策略可用的启动资金,以 currency 表示。选修的。默认为 1000000.0。 |
currency | simple string | syminfo.currency | 策略跟踪资本、利润和损失所用的基础货币。选修的。默认为图表符号的货币。 |
slippage | const int | 0 | 添加到每个订单执行价格以模拟执行滑点的价格变动数量。选修的。默认为 0。 |
commission_type | const CommissionType | CommissionType.Percent | 指定如何计算交易佣金。可能的值:strategy.commission.percent、strategy.commission.cash_per_contract、strategy.commission.cash_per_order。选修的。默认为 strategy.commission.percent。 |
commission_value | const float | 0 | 佣金金额,根据 commission_type 解释。选修的。默认为 0.0。 |
process_orders_on_close | const bool | false | 如果是 true,则在 bar 期间生成的订单将按该 bar 的收盘价处理,而不是在下一个 bar 的开盘价处理。选修的。默认为 false。 |
close_entries_rule | const string | "FIFO" | 关闭多个打开的条目时应用的排序规则。 \"FIFO 首先关闭最旧的条目; ANY 允许关闭调用通过 ID 定位特定条目。选修的。默认为 FIFO\"。 |
margin_long | const float | 100 | 多头头寸的保证金要求,以头寸价值的百分比表示。值 100 表示没有杠杆。选修的。默认为 100.0。 |
margin_short | const float | 100 | 空头头寸的保证金要求,以头寸价值的百分比表示。值 100 表示没有杠杆。选修的。默认为 100.0。 |
explicit_plot_zorder | const bool | false | 如果 true,绘图、填充和线条将按照它们在脚本代码中出现的顺序呈现,后面的声明绘制在前面的声明之上。选修的。默认为 false。 |
max_lines_count | const int | 50 | 图表上保存的最新 line 绘图的最大数量。可能的值:1–500。选修的。默认为 50。 |
max_labels_count | const int | 50 | 图表上保存的最新 label 绘图的最大数量。可能的值:1–500。选修的。默认为 50。 |
max_boxes_count | const int | 50 | 图表上保存的最新 box 绘图的最大数量。可能的值:1–500。选修的。默认为 50。 |
calc_bars_count | const int | na | 将回测限制为指定的最新 bars 数量。当 na 时,策略在所有可用历史记录上运行。选修的。默认为 na。 |
risk_free_rate | const float | 2 | 计算夏普比率和索尔蒂诺比率时使用的年度无风险利率(百分比)。选修的。默认为 2.0。 |
use_bar_magnifier | const bool | false | 如果是 true,引擎会使用更高分辨率的柱内数据来确定每个 bar 内更准确的订单执行价格。选修的。默认为 false。 |
fill_orders_on_standard_ohlc | const bool | false | 如果 true,限价单和止损单将根据 bar 的标准 OHLC 价格执行,忽略任何柱内价格路径。选修的。默认为 false。 |
max_polylines_count | const int | 50 | 图表上保存的最新 polyline 绘图的最大数量。可能的值:1–100。选修的。默认为 50。 |
dynamic_requests | const bool | true | 指定策略是否可以在条件结构或循环内包含动态 request.*() 调用,或者接受通常为非系列参数的“系列”参数的调用。选修的。默认为 true。 |
behind_chart | const bool | true | 如果是 true,则所有图表和绘图都将呈现在图表的烛台或 bars 后面。仅当 overlay = true 时生效。选修的。默认为 true。 |
string
string(x: string): string将 na 转换为字符串
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | string |
返回: string
table
table(x: Table): Table将 na 转换为表类型。对于初始化稍后分配的表变量很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | Table |
返回: Table
time
解析由 timeframe 和 session 选择的 bar 存储桶的打开时间戳。当您希望在特定时区解释目标会话时,此重载非常方便。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timeframe | series string | 时间范围代码,例如 \"D、60 或 W\"。 | |
session | series string | na | 可选的会话窗口。省略时,将使用交易品种的常规会话规则。 |
timezone | series string | syminfo.timezone | 评估会话字符串时使用的时区。 |
返回: series int — 匹配 bar 开始的毫秒时间戳,或当前 bar 不属于请求的会话时的 na。
time_close
解析由 timeframe 和 session 选择的 bar 存储桶的关闭时间戳。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timeframe | series string | 时间范围代码,例如 \"D、60 或 W\"。 | |
session | series string | na | 查找过程中使用的可选会话窗口。 |
timezone | series string | syminfo.timezone | 解释会话字符串时使用的时区。 |
返回: series int — 匹配 bar 关闭的毫秒时间戳,或当当前 bar 位于请求的会话之外时的 na。
timestamp
解析日期/时间字符串并返回相应的 UNIX 时间戳(以毫秒为单位)。该字符串必须采用 ISO 8601 格式(例如“2024-01-15”或“2024-01-15T09:30:00Z”)或 IETF RFC 2822 格式。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
date_string | string | 要解析的日期/时间字符串。 |
返回: int
week_of_year
week_of_year(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取一年中的第几周 (1-53)。第 1 周是包含 1 月 4 日的那一周 (ISO 8601)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int
year
year(timestamp: int, timezone: string = syminfo.timezone): int从指定时区的 UNIX 时间戳中提取年份。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timestamp | int | UNIX 时间戳(以毫秒为单位)。 | |
timezone | string | syminfo.timezone | 用于解释的时区。默认为符号的时区。 |
返回: int