标准库
OpenPine 附带了使用 Pine Script 编写的标准库。这些模块在每个脚本中自动可用。
模块概览
| 模块 | 描述 |
|---|---|
ta | 技术分析 — SMA、EMA、RSI、MACD、布林带等 |
math | 数学函数 — abs、round、max、min、pow、log 等 |
str | 字符串操作 — tostring、contains、replace、split 等 |
array | 动态数组操作 — push、pop、get、set、sort 等 |
map | 键值映射操作 — put、get、contains、keys、values 等 |
matrix | 二维矩阵操作 — get、set、rows、columns、transpose 等 |
color | 颜色函数 — new、r、g、b、from_gradient |
input | 用户输入 — int、float、bool、string、source、color、timeframe |
label | 图表标签对象 — new、set_text、set_xy、delete |
line | 图表线条对象 — new、set_xy1、set_xy2、delete |
box | 图表框对象 — new、set_lefttop、set_rightbottom、delete |
table | 图表表格对象 — new、cell、merge_cells、delete |
strategy | 策略函数 — entry、close、exit、order |
runtime | 运行时信息 — error |
log | 日志 — info、warning、error |
timeframe | 时间周期工具 |
syminfo | 标的信息 |
barstate | K 线状态 — isfirst、islast、isconfirmed、isrealtime |
技术分析(ta)
pine
// 移动平均线
sma20 = ta.sma(close, 20)
ema12 = ta.ema(close, 12)
wma10 = ta.wma(close, 10)
vwma20 = ta.vwma(close, 20)
// 振荡器
rsi = ta.rsi(close, 14)
[macdLine, signalLine, histogram] = ta.macd(close, 12, 26, 9)
[k, d] = ta.stoch(close, high, low, 14)
// 波动率
atr = ta.atr(14)
[middle, upper, lower] = ta.bb(close, 20, 2.0)
// 趋势
[diPlus, diMinus, adx] = ta.dmi(14, 14)
supertrend = ta.supertrend(3.0, 10)
// 价格行为
highest = ta.highest(high, 20)
lowest = ta.lowest(low, 20)
change = ta.change(close)
crossover = ta.crossover(ema12, sma20)
crossunder = ta.crossunder(ema12, sma20)数学(math)
pine
rounded = math.round(3.14159, 2) // 3.14
absolute = math.abs(-42) // 42
maximum = math.max(open, close) // 较大的值
power = math.pow(2, 10) // 1024
squareRoot = math.sqrt(144) // 12
logarithm = math.log(100) // 自然对数字符串(str)
pine
text = str.tostring(close, "#.##")
combined = str.format("{0}: {1}", "Price", close)
hasWord = str.contains("hello world", "world") // true
upper = str.upper("hello") // "HELLO"
replaced = str.replace("foo bar", "foo", "baz") // "baz bar"
length = str.length("hello") // 5数组
pine
var prices = array.new<float>(0)
array.push(prices, close)
size = array.size(prices)
first = array.get(prices, 0)
last = array.get(prices, size - 1)
avg = array.avg(prices)
maxPrice = array.max(prices)
minPrice = array.min(prices)
// 从值创建
var levels = array.from(100.0, 110.0, 120.0)输入
pine
length = input.int(14, "Length", minval = 1, maxval = 200)
factor = input.float(2.0, "Factor", step = 0.1)
useEMA = input.bool(true, "Use EMA")
src = input.source(close, "Source")
maColor = input.color(color.blue, "MA Color")
tf = input.timeframe("D", "Timeframe")