he number of contracts traded in the last closed trade.
plot(strategy.closedtrades.size(strategy.closedtrades - 1), “Number of contracts traded”)
计算平仓交易的平均利润百分比
**例子**
```pine
strategy("`strategy.closedtrades.size` Example 2")
// Strategy calls to enter long trades every 15 bars and exit long trades every 20 bars.
if bar_index % 15 == 0
strategy.entry("Long", strategy.long)
if bar_index % 20 == 0
strategy.close("Long")
// Calculate profit for both closed trades.
profitPct = 0.0
for tradeNo = 0 to strategy.closedtrades - 1
entryP = strategy.closedtrades.entry_price(tradeNo)
exitP = strategy.closedtrades.exit_price(tradeNo)
profitPct += (exitP - entryP) / entryP * strategy.closedtrades.size(tradeNo) * 100
// Calculate average profit percent for both closed trades.
avgProfitPct = nz(profitPct / strategy.closedtrades)
plot(avgProfitPct)
参数
- trade_num
(series int) 已平仓交易的交易编号。第一笔交易的编号为零。
另见
### strategy.closedtrades.exit_time
返回已平仓交易退出的UNIX时间。
strategy.closedtrades.exit_time(trade_num)
**例子**
```pine
strategy("strategy.closedtrades.exit_time Example 1")
// Enter long trades on three rising bars; exit on two falling bars.
if ta.rising(close, 3)
strategy.entry("Long", strategy.long)
if ta.falling(close, 2)
strategy.close("Long")
// Calculate the average trade duration.
avgTradeDuration() =>
sumTradeDuration = 0
for i = 0 to strategy.closedtrades - 1
sumTradeDuration += strategy.closedtrades.exit_time(i) - strategy.closedtrades.entry_time(i)
result = nz(sumTradeDuration / strategy.closedtrades)
// Display average duration converted to seconds and formatted using 2 decimal points.
if barstate.islastconfirmedhistory
label.new(bar_index, high, str.tostring(avgTradeDuration() / 1000, "#.##") + " seconds")
X秒后重新打开已平仓交易
例子
strategy("strategy.closedtrades.exit_time Example 2")
// Strategy calls to emulate a single long trade at the first bar.
if bar_index == 0
strategy.entry("Long", strategy.long)
reopenPositionAfter(timeSec) =>
if strategy.closedtrades > 0
if time - strategy.closedtrades.exit_time(strategy.closedtrades - 1) >= timeSec * 1000
strategy.entry("Long", strategy.long)
// Reopen last closed position after 120 sec.
reopenPositionAfter(120)
if ta.change(strategy.opentrades)
strategy.exit("Long", stop = low * 0.9, profit = high * 2.5)
参数
- trade_num
(series int) 已平仓交易的交易编号。第一笔交易的编号为零。
另见
### strategy.risk.allow_entry_in
此函数可用于指定strategy.entry函数允许在哪个市场方向开仓。
strategy.risk.allow_entry_in(value)
**例子**
```pine
strategy("strategy.risk.allow_entry_in")
strategy.risk.allow_entry_in(strategy.direction.long)
strategy.entry("Long", strategy.long, when = open > close)
// Instead of opening a short position with 10 contracts, this command will close long entries.
strategy.entry("Short", strategy.short, when = open < close, qty = 10)
参数
- value
(simple string) 允许的方向。可能的值:strategy.direction.all
、strategy.direction.long
、strategy.direction.short
此规则的目的是确定市场头寸的最大值。 该规则影响以下功能:strategy.entry
。 “entry”数量可以减少(如果需要)到合同/股/手/单位数,所以头寸总值不超过’strategy.risk.max_position_size’中指定的值。如果最低数量仍然违反规则,则不会放置订单。
strategy.risk.max_position_size(contracts)
例子
strategy("risk.max_position_size Demo", default_qty_value = 100)
strategy.risk.max_position_size(10)
strategy.entry("buy", strategy.long, when = open > close)
plot(strategy.position_size) // max plot value will be 10
参数
- contracts
(simple int/float) 必要参数。仓位的合同/股/手/单位的最大数。
如果 number
>= 0,number
的绝对值为 number
,否则为 -number
。
math.abs(number)
返回值
number
的绝对值。
acos函数返回数字的反余弦(以弧度表示),如cos(acos(y)) = y 在 y 范围内 [-1, 1]。
math.acos(angle)
返回值 反余弦值。如果y超出范围[-1,1],返回角度在[0,Pi]或na的范围内。
返回伪随机值。该函数将为每个脚本执行生成不同的值序列。对可选的seed参数使用相同的值将产生可重复的序列。
math.random(min, max, seed)
返回值 一个随机值。
参数
- min
(series int/float) 随机值范围的下限。该值不包括在范围内。默认值为0。
- max
(series int/float) 随机值范围的上限。该值不包括在范围内。默认值为1。
- seed
(input int) 可选参数。当使用相同的seed时,允许连续调用该函数以产生一组可重复的值。
asin函数返回数字的反正弦(以弧度表示),正弦(asin(y)) = y 在 y 范围内[-1, 1]。
math.asin(angle)
返回值 反正弦值。如果y超出范围[-1,1],返回角度在[-Pi / 2,Pi / 2]或na的范围内。
atan函数返回数字的反正切(以弧度表示),tan(atan(y)) = 任何 y 中的 y。
math.atan(angle)
返回值 反正切值; 返回角度在[-Pi / 2,Pi / 2]的范围内。
向上取整函数返回大于或等于参数的最小(最接近负无穷)整数。
math.ceil(number)
返回值 小于或等于给定数字的最小整数
另见
### math.cos
cos函数返回角度的三角余弦。
math.cos(angle)
**返回值**
角的三角余弦。
**参数**
- ```angle``` (series int/float) 角度,以弧度
### math.exp
`number` 的 exp 函数是 e 的 `number` 次方,其中 e 是欧拉数。
math.exp(number)
**返回值**
一个表示 e 的值,它是 `number` 的幂。
**另见**
```math.pow```
### math.floor
math.floor(number)
**返回值**
小于或等于给定数字的最大整数。
**另见**
```math.ceil``` ```math.round```
### math.log
任何 `number` > 0 的自然对数是唯一的 y,使得 e^y = `number`。
math.log(number)
**返回值**
`number`的自然对数。
**另见**
```math.log10```
### math.log10
`number`的常用(或以10为底的)对数是必须将10提高到的幂才能获得`number`。10^y = `number`。
math.log10(number)
**返回值**
`number`的以10为底的对数。
**另见**
```math.log```
### math.pow
数学幂函数
math.pow(base, exponent)
**例子**
```pine
// math.pow
plot(math.pow(close, 2))
返回值
base
提高到exponent
的幂。如果base
是一个系列,它是按元素计算的。
参数
- base
(series int/float) 指定要使用的基础。
- exponent
(series int/float) 指定指数。
另见
### math.sign
如果“number”为零,则“number”的符号(signum)为零,如果“number”大于0,则为1.0,如果“number”小于0,则为-1.0。
math.sign(number)
**返回值**
参数的标志。
### math.sin
正弦函数返回一个角度的三角正弦。
math.sin(angle)
**返回值**
角的三角正弦。
**参数**
- ```angle``` (series int/float) 角度,以弧度
### math.sqrt
任何`number` >= 0的平方根是唯一的y >= 0使得y^2 = `number`。
math.sqrt(number)
**返回值**
`number`的平方根。
**另见**
```math.pow```
### math.tan
tan函数返回角度的三角正切。
math.tan(angle)
**返回值**
角的三角正切。
**参数**
- ```angle``` (series int/float) 角度,以弧度
### math.round
返回 `number` 的值,四舍五入到最接近的整数,并向上取整。如果使用了 `precision` 参数,则返回一个四舍五入到小数位数的浮点值。
math.round(number)
math.round(number, precision)
**返回值**
`number`的值四舍五入到最接近的整数,或根据精度。
**参数**
- ```number``` (series int/float) 要四舍五入的值。
- ```precision``` (series int) 可选参数。`number` 将被四舍五入的小数位数。当没有提供参数时,四舍五入到最接近的整数。
**备注**
请注意,对于'na'值,函数返回'na'。
**另见**
```math.ceil``` ```math.floor```
### math.max
返回多个值中最大的一个。
math.max(number0, number1, …)
**例子**
```pine
// math.max
plot(math.max(close, open))
plot(math.max(close, math.max(open, 42)))
返回值 多个给定值中最大的。
另见
### math.min
返回多个值中最小的一个。
math.min(number0, number1, …)
**例子**
```pine
// math.min
plot(math.min(close, open))
plot(math.min(close, math.min(open, 42)))
返回值 多个给定值中的最小值。
另见
### math.avg
计算所有系列的平均值(对应元素)。
math.avg(number0, number1, …)
**返回值**
平均
**另见**
```math.sum``` ```ta.cum``` ```ta.sma```
### math.round_to_mintick
返回四舍五入到商品的mintick的值,即可以除以syminfo.mintick的最接近的值,没有余数,并向上舍入。
math.round_to_mintick(number)
**返回值**
`number`四舍五入以精确到tick。
**参数**
- ```number``` (series int/float) 要四舍五入的值。
**另见**
```math.ceil``` ```math.floor```
### math.sum
sum函数返回x的最后y值的滑动综合。
math.sum(source, length)
**返回值**
`length`K线返回的`source`总和。
**参数**
- ```source``` (series int/float) 待执行的系列值。
- ```length``` (series int) K线数量(长度).
**另见**
```ta.cum``` ```for```
### math.todegrees
从以弧度为单位的角度,返回以度为单位的近似等效角度。
math.todegrees(radians)
**返回值**
以度为单位的角度值。
**参数**
- ```radians``` (series int/float) 以弧度为单位的角度。
### math.toradians
从以度为单位的角度,返回以弧度为单位的近似等效角度。
math.toradians(degrees)
**返回值**
以弧度为单位的角度值。
**参数**
- ```degrees``` (series int/float) 以度为单位的角度。
## others
### fixnan
对于给定的系列,将NaN值替换为先前的非NaN值。
fixnan(source)
**返回值**
无na间隙的系列。
**参数**
- ```source``` (series int/float/bool/color)
**另见**
```na``` ```nz```
### nz
以系列中的零(或指定数)替换NaN值。
nz(source, replacement)
nz(source)
**例子**
```pine
// nz
plot(nz(ta.sma(close, 100)))
返回值
source
的值,如果它不是na
。如果source
的值为na
,则返回0,如果使用1,则返回replacement
参数。
参数
- source
(series int/float/bool/color) 待执行的系列值。
- replacement
(series int/float/bool/color) 将替换“source”系列中的所有“na”值的值。
另见
### na
如为NaN,则测试价值。
na(x)
**返回值**
如果x非有效数字,则为true(x为NaN),否则为false。
**另见**
```fixnan``` ```nz```
### int
转换na或将float值截断为int。
int(x)
**返回值**
转换为int后的参数值。
**另见**
```float``` ```bool``` ```color``` ```string```
### float
将na设置为浮动。
float(x)
**返回值**
转换为float后的参数值。
**另见**
```int``` ```bool``` ```color``` ```string```
### alert
在实时K线期间调用时触发警报事件,并且之前通过“创建警报”对话框为指标或策略创建了基于警报功能事件的警报。
alert(message, freq)
**例子**
```pine
// alert() example
ma = ta.sma(close, 14)
xUp = ta.crossover(close, ma)
if xUp
// Trigger the alert the first time a cross occurs during the real-time bar.
alert("Price (" + str.tostring(close) + ") crossed over MA (" + str.tostring(ma) + ").", alert.freq_once_per_bar)
plot(ma)
plotchar(xUp, "xUp", "▲", location.top, size = size.tiny)
参数
- message
(series string) 警报触发时发送的消息。必填参数。
- freq
(input string) 触发频率。可能的值为:alert.freq_all(所有函数调用触发警报),alert.freq_once_per_bar(K线中的第一个函数调用触发警报),alert.freq_once_per_bar_close(函数调用仅在实时K线的最后一个脚本迭代期间发生时,在关闭时才触发警报)。默认值为alert.freq_once_per_bar。
备注 帮助中心介绍了如何创建此类警报。 与alertcondition相反,alert调用不算作额外的绘图。 函数调用可以位于全局和局部范围内。 函数调用在图表上不显示任何内容。 ‘freq‘参数仅影响使用此函数调用之处的触发频率。
另见
### alertcondition
创建警报条件,在创建警报话框中可用。 请注意,alertcondition不会创建警报,它只会在创建警报对话框中为您提供更多选项。 此外,alertcondition效果在图表上是看不见的。
alertcondition(condition, title, message)
**例子**
```pine
// alertcondition
alertcondition(close >= open, title='Alert on Green Bar', message='Green Bar!')
参数
- condition
(series bool) 用于警报的系列布尔值。 True值代表警报触发,false - 无警报。 必要参数。
- title
(const string) 警报条件的标题。 可选参数。
- message
(const string) 当警报触发时显示消息。可选参数。
备注 请注意,在Pine v4中,警报条件调用会生成一个额外的图。 当我们计算每个脚本的输出系列的数量时,会考虑所有这些调用。
另见
### indicator
为了兼容```Trading View```策略代码,实际不需要调用。
**另见**
```strategy```
### time
time函数返回指定时间范围和交易时段的当前K线的UNIX时间,如果时间点不在交易时段中,则返回NaN。注意:优宽不支持```session```参数。
~~```time(timeframe, session, timezone)```~~
~~```time(timeframe, session)```~~
time(timeframe)
**例子**
```pine
timeinrange(res, sess) => not na(time(res, sess, "America/New_York")) ? 1 : 0
plot(timeinrange("1", "1300-1400"), color=color.red)
// This plots 1.0 at every start of 10 minute bar on a 1 minute chart:
newbar(res) => ta.change(time(res)) == 0 ? 0 : 1
plot(newbar("10"))
当设置某个会话时,您不仅可以指定小时与分钟,也可以指定某一周内的日期。 如果没有指定日期,则认为交易时段设置为从星期日 (1) 到星期六 (7),即“1100-2000”与“1100-1200:1234567”相同。 您可以通过指定日期来更改它。例如,对于每周7天交易且24小时交易时段的商品,以下脚本不会为周六和周日着色:
例子
// Time
t1 = time(timeframe.period, "0000-0000:23456")
bgcolor(t1 ? color.new(color.blue, 90) : na)
一个session
参数可以包含多个不同的交易时段,以逗号分隔。例如,以下脚本将突出显示从10:00到11:00以及从14:00到15:00(仅限工作日)的K线图:
例子
// Time
t1 = time(timeframe.period, "1000-1100,1400-1500:23456")
bgcolor(t1 ? color.new(color.blue, 90) : na)
返回值 Unix时间。
参数
- timeframe
(simple string) 时间周期。空字符串被解释为图表的当前时间周期。
- (simple string) 交易时段规范。可选参数,默认情况下使用商品交易时段。空字符串被解释为商品的交易时段。优宽不支持。
- session
timezone
(simple string) session
参数的时区。只能在指定了“会话”时使用。可选。默认值为syminfo.timezone。可以用GMT表示法(例如“GMT-5”)或 IANA 时区数据库名称(例如“America/New_York”)指定。
备注 UNIX时间是自1970年1月1日UTC 00:00:00起已经过去的毫秒数。
year(time)
year(time, timezone)
返回值 提供UNIX时间的年份(交换时区)。
参数
- time
(series int) 以毫秒为单位的unix时间。
- timezone
(series string) 可选参数。时区。
备注 UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。 请注意,此函数根据K线的打开时间返回年份。对于隔夜交易时段(例如EURUSD周一交易时段从周日17:00 UTC-4开始),该值可以比交易日的年份低1。
另见
### month
month(time)
month(time, timezone)
**返回值**
提供UNIX时间的月份(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
请注意,此函数根据K线的打开时间返回月份。对于隔夜交易时段(例如,EURUSD周一交易时段从周日17:00 UTC-4开始),该值可以比交易日的月份低1。
**另见**
```month``` ```time``` ```year``` ```dayofmonth``` ```dayofweek``` ```hour``` ```minute``` ```second```
### hour
hour(time)
hour(time, timezone)
**返回值**
提供UNIX时间的小时(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
**另见**
```hour``` ```time``` ```year``` ```month``` ```dayofmonth``` ```dayofweek``` ```minute``` ```second```
### minute
minute(time)
minute(time, timezone)
**返回值**
提供UNIX时间的分钟(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
**另见**
```minute``` ```time``` ```year``` ```month``` ```dayofmonth``` ```dayofweek``` ```hour``` ```second```
### second
second(time)
second(time, timezone)
**返回值**
提供UNIX时间的秒数(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
**另见**
```second``` ```time``` ```year``` ```month``` ```dayofmonth``` ```dayofweek``` ```hour``` ```minute```
### weekofyear
weekofyear(time)
weekofyear(time, timezone)
**返回值**
提供UNIX时间的周期(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
请注意,该函数根据K线的打开时间返回周。对于隔夜交易时段(例如EURUSD,其周一交易时段从周日17:00开始),该值可以比交易日的周低1。该
**另见**
```weekofyear``` ```time``` ```year``` ```month``` ```dayofmonth``` ```dayofweek``` ```hour``` ```minute``` ```second```
### dayofweek
dayofweek(time)
dayofweek(time, timezone)
**返回值**
提供UNIX时间的每周日期(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
请注意,此函数根据K线的打开时间返回日期。对于隔夜交易时段(例如EURUSD周一交易时段从周日17:00开始),该值可以比交易日的日期低1。
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
**另见**
```time``` ```dayofmonth```
### dayofmonth
dayofmonth(time)
dayofmonth(time, timezone)
**返回值**
提供UNIX时间的每月日期(交换时区)。
**参数**
- ```time``` (series int) 以毫秒为单位的unix时间。
- ```timezone``` (series string) 可选参数。时区。
**备注**
UNIX时间是自1970年1月1日UTC 00:00:00以来的毫秒数。默认情况下,时区为syminfo.timezone。您可以使用timestamp检查可能的值。
请注意,此函数根据K线的打开时间返回日期。对于隔夜交易时段(例如EURUSD周一交易时段从周日17:00 UTC-4开始),该值可以比交易日的日期低1。
**另见**
```time``` ```dayofweek```
### timestamp
时间戳功能返回UNIX时间的指定日期和时间。
timestamp(dateString)
timestamp(year, month, day, hour, minute, second)
timestamp(timezone, year, month, day, hour, minute, second)
**例子**
```pine
// timestamp
plot(timestamp(2016, 01, 19, 09, 30), linewidth=3, color=color.green)
plot(timestamp(syminfo.timezone, 2016, 01, 19, 09, 30), color=color.blue)
plot(timestamp(2016, 01, 19, 09, 30), color=color.yellow)
plot(timestamp("GMT+6", 2016, 01, 19, 09, 30))
plot(timestamp(2019, 06, 19, 09, 30, 15), color=color.lime)
plot(timestamp("GMT+3", 2019, 06, 19, 09, 30, 15), color=color.fuchsia)
plot(timestamp("Feb 01 2020 22:10:05"))
plot(timestamp("2011-10-10T14:48:00"))
plot(timestamp("04 Dec 1995 00:12:00 GMT+5"))
返回值 Unix时间。
参数
- timezone
(series string) 时区。可选。默认值为syminfo.timezone。可以用GMT表示法(例如“GMT-5”)或 IANA 时区数据库名称(例如“America/New_York”)指定。
- year
(series int) 年。
- month
(series int) 月。
- day
(series int) 日。
- hour
(series int) (可选参数)小时。默认值为0。
- minute
(series int) (可选参数)分钟。默认值为0。
- second
(series int) (可选参数)Second。默认值为0。
- dateString
(const string) 一个字符串,其中包含日期以及可选的时间和时区。其格式必须符合IETF RFC 2822或ISO 8601标准(“DD MMM YYYY hh:mm:ss±hhmm”或“YYYY-MM-DDThh:mm:ss±hh:mm”,因此是“20 Feb 2020”或“2020-02-20”)。如果未提供时间,则使用“00:00”。如果未提供任何时区,则将使用GMT+0。请注意,这与函数通常的行为不同,后者返回交易所所在时区的时间。
备注 UNIX时间是自1970年1月1日UTC 00:00:00起已经过去的毫秒数。
另见
### fill
使用提供的颜色填充两个绘图或hline之间的背景。
fill(hline1, hline2, color, title, editable, fillgaps, display)
fill(plot1, plot2, color, title, editable, show_last, fillgaps, display)
**例子**
```pine
h1 = hline(20)
h2 = hline(10)
fill(h1, h2, color=color.new(color.blue, 90))
p1 = plot(open)
p2 = plot(close)
fill(p1, p2, color=color.new(color.green, 90))
参数
- hline1
(hline) 首个hline对象。 必要参数。
- hline2
(hline) 第二个hline对象。 必要参数。
- plot1
(plot) 首个绘图对象。 必需参数。
- plot2
(plot) 第二个绘图对象。 必要参数。
- color
(series color) 绘图的颜色。您可以使用如’color = red’或’color =#ff001a’的常量以及如 ‘color = close >= open ? green : red’的复杂表达式。 可选参数。
- title
(const string) 已创建填充对象的标题。 可选参数。
- editable
(const bool) 如果为true,则填充样式可在格式对话框中编辑。 默认值为true。
- show_last
(input int) 如已设置,则定义填充图表的k线数(从最后k线返回过去)。
- fillgaps
(const bool) 控制空隙的连续填充,即,当plot()调用之一返回na值时。设置为true时,最后的填充将继续填补空隙。默认为false。
- display
(plot_display) 控制填充的显示位置。可能的值为:display.none、display.all。默认为display.all。
另见
### hline
在给定的固定价格水平上呈现水平线。
hline(price, title, color, linestyle, linewidth, editable, display)
**例子**
```pine
// input.hline
hline(3.14, title='Pi', color=color.blue, linestyle=hline.style_dotted, linewidth=2)
// You may fill the background between any two hlines with a fill() function:
h1 = hline(20)
h2 = hline(10)
fill(h1, h2, color=color.new(color.green, 90))
返回值 可用于fill的hline对象。
参数
- price
(input int/float) 对象将呈现的价格值。必要参数。
- title
(const string) 对象的标题
- color
(input color) 渲染线的颜色。 必须是常量(非表达式)。 可选参数。
- linestyle
(hline_style) 渲染线的样式。 可能的值有:solid,dotted,dotted。 可选参数。
- linewidth
(input int) 渲染线的宽度。默认值为1。
- editable
(const bool) 如果为true,则hline样式可在格式对话框中编辑。 默认值为true。
- display
(plot_display) 控制线的显示位置。可能的值为:display.none、display.all。默认为display.all。
- overlay
(const bool) 优宽平台扩展的参数,用于设置当前函数在主图(设置true)或者副图(设置false)上画图显示,默认值为false。不指定该参数则按照strategy
或者indicator
中的overlay
参数设置,strategy
或者indicator
没有设置overlay
参数,则按照默认参数处理。
用指定颜色填充K线的背景。
bgcolor(color, offset, editable, show_last, title, display, overlay)
例子
// bgcolor example
bgcolor(close < open ? color.new(color.red,70) : color.new(color.green, 70))
参数
- color
(series color) 填充背景的颜色。 您可以使用如“red”或“#ff001a”的常量以及像 ‘close >= open ? green : red’的复杂表达式。必要参数。
- offset
(series int) 在k线特定数量上向左或向右移动颜色系列。 默认值为0。
- editable
(const bool) 如果为true,则bgcolor样式可在格式对话框中编辑。 默认值为true。
- show_last
(input int) 如已设置,则定义填充图表的k线数(从最后k线返回过去)。
- title
(const string) bgcolor的标题。 可选参数。
- display
(plot_display) 控制bgcolor的显示位置。可能的值为:display.none、display.all。默认为display.all。
- overlay
(const bool) 优宽平台扩展的参数,用于设置当前函数在主图(设置true)或者副图(设置false)上画图显示,默认值为false。不指定该参数则按照strategy
或者indicator
中的overlay
参数设置,strategy
或者indicator
没有设置overlay
参数,则按照默认参数处理。
另见
### barcolor
设置K线颜色。
barcolor(color, offset, editable, show_last, title, display)
**例子**
```pine
barcolor(close < open ? color.black : color.white)
参数
- color
(series color) K线颜色。您可以使用如“red”或“#ff001a”的常量,以及如 ‘close >= open ? green : red’的复杂表达式。必要参数。
- offset
(series int) 在k线特定数量上向左或向右移动颜色系列。 默认值为0。
- editable
(const bool) 如果为true,则barcolor样式可在格式对话框中编辑。 默认值为true。
- show_last
(input int) 如已设置,则定义填充图表的k线数(从最后k线返回过去)。
- title
(const string) Barcolor标题。可选参数。
- display
(plot_display) 控制K线颜色的显示位置。可能的值为:display.none、display.all。默认为display.all。
另见
### error
兼容PINE v4版本的```error```,功能与```runtime.error```一致。
# 内置变量
## order
### order.ascending
确定阵列从最小到最大的排序顺序。
**类型**
sort_order
**另见**
```array.new_float``` ```array.sort```
### order.descending
确定阵列从最大到最小的排序顺序。
**类型**
sort_order
**另见**
```array.new_float``` ```array.sort```
## timeframe
### timeframe.isdaily
如果当前分辨率是每日分辨率,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isdwm``` ```timeframe.isintraday``` ```timeframe.isminutes``` ```timeframe.isseconds``` ```timeframe.isweekly``` ```timeframe.ismonthly```
### timeframe.isdwm
如果当前分辨率是每日或每周或每月分辨率,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isintraday``` ```timeframe.isminutes``` ```timeframe.isseconds``` ```timeframe.isdaily``` ```timeframe.isweekly``` ```timeframe.ismonthly```
### timeframe.isintraday
如果当前周期是日内(分钟或秒)周期,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isminutes``` ```timeframe.isseconds``` ```timeframe.isdwm``` ```timeframe.isdaily``` ```timeframe.isweekly``` ```timeframe.ismonthly```
### timeframe.isminutes
如果当前周期是分钟周期,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isdwm``` ```timeframe.isintraday``` ```timeframe.isseconds``` ```timeframe.isdaily``` ```timeframe.isweekly``` ```timeframe.ismonthly```
### timeframe.ismonthly
如果当前分辨率是每月分辨率,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isdwm``` ```timeframe.isintraday``` ```timeframe.isminutes``` ```timeframe.isseconds``` ```timeframe.isdaily``` ```timeframe.isweekly```
### timeframe.isseconds
如果当前周期是秒,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isdwm``` ```timeframe.isintraday``` ```timeframe.isminutes``` ```timeframe.isdaily``` ```timeframe.isweekly``` ```timeframe.ismonthly```
### timeframe.isweekly
如果当前分辨率是每周分辨率,则返回true,否则返回false。
**类型**
simple bool
**另见**
```timeframe.isdwm``` ```timeframe.isintraday``` ```timeframe.isminutes``` ```timeframe.isseconds``` ```timeframe.isdaily``` ```timeframe.ismonthly```
### timeframe.multiplier
时间周期乘数,例如 '60' - 60,'D' - 1,'5D' - 5,'12M' - 12。
**类型**
simple int
**另见**
```syminfo.ticker``` ```syminfo.tickerid``` ```timeframe.period```
### timeframe.period
时间周期。比如 '60' - 60分钟, 'D' - 日, 'W' - 周, 'M' - 月, '5D' - 5 日, '12M' - 1年, '3M' - 1个季度。
**类型**
simple string
**另见**
```syminfo.ticker``` ```syminfo.tickerid``` ```timeframe.multiplier```
## display
### display.none
一个命名常数,指定绘图的显示位置。无处显示。在警报模板消息可用。
**类型**
plot_display
**另见**
```plot``` ```plotshape``` ```plotchar```
### display.all
一个命名常数,指定显示绘图的位置。显示任何位置。
**类型**
plot_display
**另见**
```plot``` ```plotshape``` ```plotchar``` ```plotarrow``` ```plotbar``` ```plotcandle```
## shape
### shape.xcross
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.cross
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.triangleup
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.triangledown
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.flag
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.circle
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.arrowup
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.arrowdown
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.labelup
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.labeldown
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.square
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
### shape.diamond
plotshape功能的形状样式。
**类型**
const string
**另见**
```plotshape```
## color
### color.aqua
是#00BCD4颜色的命名常量。
**类型**
const color
### color.black
是#363A45颜色的命名常量。
**类型**
const color
### color.blue
是#2962ff颜色的命名常量。
**类型**
const color
### color.fuchsia
是#E040FB颜色的命名常量。
**类型**
const color
### color.gray
是#787B86颜色的命名常量。
**类型**
const color
### color.green
是#4CAF50颜色的命名常量。
**类型**
const color
### color.lime
是#00E676颜色的命名常量。
**类型**
const color
### color.maroon
为#880E4F颜色的命名常量。
**类型**
const color
### color.navy
是#311B92颜色的命名常量。
**类型**
const color
### color.olive
是#808000颜色的命名常量。
**类型**
const color
### color.orange
是#FF9800颜色的命名常量。
**类型**
const color
### color.purple
是#9C27B0颜色的命名常量。
**类型**
const color
### color.red
是#FF5252颜色的命名常量。
**类型**
const color
### color.silver
为#B2B5BE颜色的命名常量。
**类型**
const color
### color.teal
color.teal
是#00897B颜色的命名常量。
**类型**
const color
### color.white
是#FFFFFF颜色的命名常量。
**类型**
const color
### color.yellow
是#FFEB3B颜色的命名常量。
**类型**
const color
## plot
### plot.style_line
'Line'样式的命名常量,用作plot函数中`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_linebr
'Line With Breaks'样式的命名常量,用作plot函数中`style`参数的参数。类似于plot.style_line,除了数据中的空白没有被填充。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_histogram
'Histogram'样式的命名常量,用作plot函数中`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_columns
'Columns' 样式的命名常量,用作plot函数中的`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_circles```
### plot.style_circles
'Circles' 样式的命名常量,用作plot函数中的`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns```
### plot.style_area
'Area'样式的命名常量,用作plot函数中`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_areabr``` ```plot.style_cross``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_areabr
'Area With Breaks'样式的命名常量,用作plot函数中`style`参数的参数。类似于plot.style_area,除了数据中的空白没有被填充。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_cross
'Cross' 样式的命名常量,用作plot函数中`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_stepline``` ```plot.style_stepline_diamond``` ```plot.style_histogram``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_stepline
'Step Line'样式的命名常量,用作plot函数中`style`参数的参数。
**类型**
plot_style
**另见**
```plot``` ```plot.style_stepline_diamond``` ```plot.style_linebr``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
### plot.style_stepline_diamond
'Step Line With Diamonds'样式的命名常量,用作plot函数中`style`参数的参数。类似于plot.style_stepline,除了数据变化也用菱形标记。
**类型**
plot_style
**另见**
```plot``` ```plot.style_line``` ```plot.style_linebr``` ```plot.style_histogram``` ```plot.style_cross``` ```plot.style_area``` ```plot.style_areabr``` ```plot.style_columns``` ```plot.style_circles```
## location
### location.abovebar
location.abovebar
plotshape,plotchar功能的位置值。 形状绘制在主系列k线上方。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```location.belowbar``` ```location.top``` ```location.bottom``` ```location.absolute```
### location.belowbar
plotshape,plotchar功能的位置值。 形状绘制在主要系列k线下方。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```location.abovebar``` ```location.top``` ```location.bottom``` ```location.absolute```
### location.top
plotshape,plotchar功能的位置值。 形状绘制在顶部图表边框附近。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```location.abovebar``` ```location.belowbar``` ```location.bottom``` ```location.absolute```
### location.bottom
plotshape,plotchar功能的位置值。 形状绘制在底部图表边框附近。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```location.abovebar``` ```location.belowbar``` ```location.top``` ```location.absolute```
### location.absolute
plotshape,plotchar功能的位置值。 形状在图表上绘制,使用指标值作为价格坐标。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```location.abovebar``` ```location.belowbar``` ```location.top``` ```location.bottom```
## size
### size.auto
size.auto
plotshape,plotchar功能的大小值。 形状的大小自动适应k线的大小。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.tiny``` ```size.small``` ```size.normal``` ```size.large``` ```size.huge```
### size.tiny
plotshape,plotchar功能的大小值。 形状的尺寸微小。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.auto``` ```size.small``` ```size.normal``` ```size.large``` ```size.huge```
### size.small
plotshape,plotchar功能的大小值。 形状的尺寸小。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.auto``` ```size.tiny``` ```size.normal``` ```size.large``` ```size.huge```
### size.normal
plotshape,plotchar功能的大小值。 形状的尺寸普通。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.auto``` ```size.tiny``` ```size.small``` ```size.large``` ```size.huge```
### size.large
plotshape,plotchar功能的大小值。 形状的尺寸大。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.auto``` ```size.tiny``` ```size.small``` ```size.normal``` ```size.huge```
### size.huge
plotshape,plotchar功能的大小值。 形状的尺寸巨大。
**类型**
const string
**另见**
```plotshape``` ```plotchar``` ```size.auto``` ```size.tiny``` ```size.small``` ```size.normal``` ```size.large```
## alert
### alert.freq_once_per_bar
与alert()函数的'freq'参数一起使用的命名常数。
K线中的第一个函数调用触发警报。
**类型**
const string
**另见**
```alert```
### alert.freq_all
与alert()函数的'freq'参数一起使用的命名常数。
所有函数调用触发警报。
**类型**
const string
**另见**
```alert```
### alert.freq_once_per_bar_close
与alert()函数的'freq'参数一起使用的命名常数。
该函数调用仅在实时K线的最后一个脚本迭代期间发生时,在关闭时触发警报。
**类型**
const string
**另见**
```alert```
## format
### format.inherit
是一个命名常量。
**类型**
const string
**另见**
```format.price``` ```format.volume```
### format.price
是一个命名常量。
**类型**
const string
**备注**
如果format是format.price,则设置默认精度值。您可以使用指标函数的precision参数来更改精度值。
**另见**
```format.inherit``` ```format.volume```
### format.volume
是一个命名常量。
**类型**
const string
**另见**
```format.inherit``` ```format.price```
## syminfo
### syminfo.ticker
无交易所前缀的商品代码,例如 'MSFT'。
**类型**
simple string
**另见**
```syminfo.tickerid``` ```timeframe.period``` ```timeframe.multiplier```
### syminfo.tickerid
带有交易所前缀的商品代码,例如 “BATS:MSFT”,“NASDAQ:MSFT”。
**类型**
simple string
**另见**
```syminfo.ticker``` ```timeframe.period``` ```timeframe.multiplier```
### syminfo.basecurrency
商品的基础货币。对于商品代码“ BTCUSD”,返回“ BTC”。
**类型**
simple string
**另见**
```syminfo.currency``` ```syminfo.ticker```
### syminfo.currency
当前商品的货币。返回货币代码:“ USD”,“ EUR”等。
**类型**
simple string
**另见**
```syminfo.basecurrency``` ```syminfo.ticker```
### syminfo.type
当前商品代码的类型。可能的值为stock, futures, index, forex, crypto, fund, dr。
**类型**
simple string
**另见**
```syminfo.ticker```
### syminfo.mintick
当前品种的最小刻度值。在优宽上,实盘/回测界面上「Pine语言交易类库」中的模板参数**定价货币精度**可以控制该值。**定价货币精度**设置2即交易时价格精确到小数点第二位,此时价格最小变动单位为0.01。syminfo.mintick的值即为0.01。
**类型**
simple float
**另见**
```syminfo.pointvalue```
### syminfo.pointvalue
当前商品的点值
**类型**
simple float
**另见**
```syminfo.mintick```
### syminfo.timezone
图表主要系列的交换时区。 可能的值见timestamp。
**类型**
simple string
**另见**
```timestamp```
## barstate
### barstate.islastconfirmedhistory
如果市场收盘时脚本在数据集的最后一根K线上执行,或者脚本正在实时K线之前的K线上执行,如果市场开盘,则返回true。否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.islast``` ```barstate.ishistory``` ```barstate.isrealtime``` ```barstate.isnew```
### barstate.isnew
如果脚本目前在新k线上计算着,则返回true,否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.islast``` ```barstate.ishistory``` ```barstate.isrealtime``` ```barstate.isconfirmed``` ```barstate.islastconfirmedhistory```
### barstate.isfirst
如果当前k线为k线组的第一条k线,则返回true,否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.islast``` ```barstate.ishistory``` ```barstate.isrealtime``` ```barstate.isnew``` ```barstate.isconfirmed``` ```barstate.islastconfirmedhistory```
### barstate.islast
如果当前k线为k线组的最后一条k线,则返回true,否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.ishistory``` ```barstate.isrealtime``` ```barstate.isnew``` ```barstate.isconfirmed``` ```barstate.islastconfirmedhistory```
### barstate.ishistory
如果当前k线为历史k线,则返回true,否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.islast``` ```barstate.isrealtime``` ```barstate.isnew``` ```barstate.isconfirmed``` ```barstate.islastconfirmedhistory```
### barstate.isconfirmed
如果脚本正在计算当前k线的最后(关闭)更新,则返回true。 下一个脚本将在新K线数据上计算。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
不建议在request.security表达式中使用barstate.isconfirmed。它从request.security请求的值是不可预测的。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.islast``` ```barstate.ishistory``` ```barstate.isrealtime``` ```barstate.isnew``` ```barstate.islastconfirmedhistory```
### barstate.isrealtime
如果当前k线为实时k线,则返回true,否则返回false。
**类型**
series bool
**备注**
使用此变量的PineScript代码可以对历史记录和实时数据进行不同的计算。
请注意,使用此变量/函数可能会导致指标重新绘制。
**另见**
```barstate.isfirst``` ```barstate.islast``` ```barstate.ishistory``` ```barstate.isnew``` ```barstate.isconfirmed``` ```barstate.islastconfirmedhistory```
### barstate.time
暂无
## ta
### ta.accdist
累积/分布指数
**类型**
series float
### ta.iii
盘中强度指数。
**类型**
series float
**例子**
```pine
// Intraday Intensity Index
plot(ta.iii, color=color.yellow)
// the same on pine
f_iii() =>
(2 * close - high - low) / ((high - low) * volume)
plot(f_iii())
负量指标。
类型 series float
例子
// Negative Volume Index
plot(ta.nvi, color=color.yellow)
// the same on pine
f_nvi() =>
float ta_nvi = 1.0
float prevNvi = (nz(ta_nvi[1], 0.0) == 0.0) ? 1.0: ta_nvi[1]
if nz(close, 0.0) == 0.0 or nz(close[1], 0.0) == 0.0
ta_nvi := prevNvi
else
ta_nvi := (volume < nz(volume[1], 0.0)) ? prevNvi + ((close - close[1]) / close[1]) * prevNvi : prevNvi
result = ta_nvi
plot(f_nvi())
正量指标。
类型 series float
例子
// Positive Volume Index
plot(ta.pvi, color=color.yellow)
// the same on pine
f_pvi() =>
float ta_pvi = 1.0
float prevPvi = (nz(ta_pvi[1], 0.0) == 0.0) ? 1.0: ta_pvi[1]
if nz(close, 0.0) == 0.0 or nz(close[1], 0.0) == 0.0
ta_pvi := prevPvi
else
ta_pvi := (volume > nz(volume[1], 0.0)) ? prevPvi + ((close - close[1]) / close[1]) * prevPvi : prevPvi
result = ta_pvi
plot(f_pvi())
能量潮指标。
类型 series float
例子
// On Balance Volume
plot(ta.obv, color=color.yellow)
// the same on pine
f_obv() =>
ta.cum(math.sign(ta.change(close)) * volume)
plot(f_obv())
价量趋势指标。
类型 series float
例子
// Price-Volume Trend
plot(ta.pvt, color=color.yellow)
// the same on pine
f_pvt() =>
ta.cum((ta.change(close) / close[1]) * volume)
plot(f_pvt())
威廉多空力度线。
类型 series float
例子
// Williams Accumulation/Distribution
plot(ta.wad, color=color.yellow)
// the same on pine
f_wad() =>
trueHigh = math.max(high, close[1])
trueLow = math.min(low, close[1])
mom = ta.change(close)
gain = (mom > 0) ? close - trueLow : (mom < 0) ? close - trueHigh : 0
ta.cum(gain)
plot(f_wad())
威廉变异离散量。
类型 series float
例子
// Williams Variable Accumulation/Distribution
plot(ta.wvad, color=color.yellow)
// the same on pine
f_wvad() =>
(close - open) / (high - low) * volume
plot(f_wvad())
是欧拉数的命名常数。它等于2.7182818284590452。
类型 const float
另见
math.phi
math.pi
math.rphi
是黄金分割的命名常数。等于1.6180339887498948。
类型 const float
另见
math.e
math.pi
math.rphi
是阿基米德常数的命名常数。它等于3.1415926535897932。
类型 const float
另见
math.e
math.phi
math.rphi
是黄金分割率的命名常数。它等于0.6180339887498948。
类型 const float
另见
math.e
math.pi
math.phi