PineScript 语法与绘图
Tradingview 是一个价格图表和分析软件,提供免费和付费选项,由一群交易员和软件开发商在 2011 年 9 月推出。投资者可以通过 Tradingview 查看各种不同金融市场和资产类别的价格图表,包括股票、货币对、债券、期货以及加密货币。除此之外,投资者还可以通过该平台查看多个交易品种,比如股指期货、欧美货币对、黄金、原油、比特币等等。
Pine 编程
脚本模式
模式 | 说明 |
---|---|
indicator | 指标 |
strategy | 策略(可以回测) |
library | 库的声明 |
价格调用
如果要调用前面第3跟K线,那么就要用下标标注如:
close[3]
价格模式 | 说明 |
---|---|
close | 收盘价 |
open | 开盘价 |
high | 最高价 |
low | 最低价 |
atr = ta.atr(14) // 如果要调用往前数第 10 跟蜡烛图的 ATR 值
plot(art[10], color=color.white) // 根据往前数第10跟蜡烛图的atr值划线,白色
数据类型
类型 | 写法 |
---|---|
整数 | integer |
浮点小数 | float |
布尔 | boolean |
颜色 | color |
字符 | string |
用户输入设置
inputvalue=input.bool(title="On/Off", defval=true, tooltip="提示语", group="设置分区1", inline="x", confirm=true)
关键词 | 说明 |
---|---|
title | 设置名称 |
defval | 默认值:minival-最小值, maxval-最大值,step-输入值每次最小增减值 |
options | 限制用户只能在列表中选择输入值 option=[1,2,3,4] |
tooltip | 提示语 |
group | 设置分区 |
inline | 当需要将参数在同一行现实时试用 |
confirm | 定义为 true 则在调用程序前强制用户设置参数 |
在设置不同类型的输入值时,其参数也会不同,下面的示例展示了很多可能性
//@version=5
indicator("array.new_line example")
inputvalue=input.bool(title="On/Off", defval=true, tooltip="Turn on/off this setting", group="General Setting", inline="x", confirm=true)
inputColor = input.color(title="Color", defval=color.black)
inputInteger = input.int(title="Whole Number", defval=5, options=[1, 2, 3, 4, 5])
inputFloat = input.float(title="Decimal Number", defval=-0.5, minval=-3.14, maxval=3.14, step=0.01)
inputSymbol = input.symbol(title="Symbol", defval="SPY")
inputPrice = input.price(title="Price", defval=0.0)
inputSource = input.source(title="Source", defval=close)
inputString = input.string(title="String", defval="A", options=["A", "B", "C"], tooltip="Select an option")
// --- GET TIME INPUTS --- //
var G_TIME = "Time Settings"
inputTime = input.time(title="Time", defval=timestamp("01 Jan 2000 13:30 +0000"), group=G_TIME)
inputResolution = input.timeframe(title="Timeframe", defval="D", group=G_TIME)
inputSession = input.session(title="Session", defval="0300-1300", group=G_TIME)
plot(close)

工具箱
类型 | 快捷输入 |
---|---|
技术分析 Technical Analysis | ta.xxx |
文本处理 String / Text | str.xxx |
财务信息调用 | request.xxx |
自定义工具类 | Libraries |
绘图选择
绘图模式 | 说明 |
---|---|
默认 | 添加到下方 |
indicator(name, overlay=true) | 添加在图标中 |
绘制均线
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rum7126
//@version=5
indicator("我的脚本", (overlay = true));
// technical analysis 技术分析库
ma20 = ta.sma(close, 20); // simple moving average : 简单 ma 均线
plot(ma20, (color = color.red), (linewidth = 2)); // 加粗 + 变色
变色均线
当前均线值 > 上一个均线, 改变颜色
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rum7126
//@version=5
indicator("我的脚本", overlay=true)
// technical analysis 技术分析库
ma14 = ta.sma(close, 14)
_color = if ma14 > ma14[1] // 当前均线值 > 上一个均线 ma14[1]
color.green
else
color.red
plot(ma14, color=_color, linewidth=3)

均线填充
给 ema 区块进行颜色填充
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rum7126
//@version=5
indicator("金叉死叉均线区域填充", overlay=true)
// technical analysis 技术分析库
// 取均线
maA = ta.ema(close, 32)
maB = ta.ema(close, 64)
// 画均线
line1 = plot(maA, color=color.red)
line2 = plot(maB, color=color.blue)
// 获取金叉死叉
gold = ta.crossover(maA, maB) // 金叉
dead = ta.crossunder(maA, maB) // 死叉
// 金叉死叉信号绘制
plotchar(gold, char="买", location=location.belowbar, color=color.green, size=size.tiny) // location: 位置上方
plotchar(gold, char="卖", location=location.abovebar, color=color.red, size=size.tiny)
// 填充背景 fill 填充
_color = maA>maB ? color.new(#ff0000, 80) : color.new(#00ff00, 80) // 三元运算,颜色变化, 80 的透明度
fill(line1, line2, color=_color)

警报通知
- alert 用于触发报警事件,可以在图标上创建报警对话框中调用,可以在if中嵌套,并且可以引用动态数值
- alertcondition 创建警报条件,在创建警报话框中可用。 请注意,alertcondition不会创建警报,它只会在创建警报对话框中为您提供更多选项。 此外,alertcondition效果在图表上是看不见的。
//@version=5
indicator("example")
hc=close>high[1] // hc=high close
lc=close<low[1] // lc=low close
if hc
alert("当前蜡烛比前一个高: " + str.tostring(close), alert.freq_once_per_bar_close)
if lc
alert("当前蜡烛比前一个底: " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(hc,"测试:提示", "出发:当前蜡烛比前一个高") //禁止调用动态的信息
plot(close)

绘图
Plot 标准绘图
plot(close, title="title of plot element",color=color.purple, linewidth=5, sytle=columns, trackprice=true, histbase=0, offset=10,join=true,show_last=10)
close
以蜡烛图结束的价格绘图title
后期在调整图表参数时显示的名字color
绘图的颜色,如果需要透明显示可以用color.new(color.purple, 100)
100代表透明度为100%linewidth
绘图线条宽度style
图表类型:line, stepline, histogram, cross, area, columns, circles
, 默认值是line线型图trackprice
如果=true, 则根据最新的值显示一条水平虚线histbase
类似于MACD的显示方式,比如histbase=0, 则显示以0为中界上下显示offset
某些特殊情况,需要把指标显示提前或推后显示,offset的值对应了移动的蜡烛图个数join
true则意味着显示的图形会被连起来,比如用circles的时候,每个圆点中间会有一条线连接show_last
只显示最后 xx个蜡烛图对应的指标
Fill 填充模式
h=plot(high)
l=plot(low)
fill(h, l, color=color.new(color.red, 20), title="Fill area")
color 颜色控制
color = color.blue 用于设置常见颜色
color.new(color.red, 50) 用于设置透明度
color.rgb(xxx,xxx,xxx) 设置RGB颜色,选择更精准
color=#fffff 设置HEX颜色
bgcolor 背景颜色

inside=high<high[1] and low>low[1]
bg(inside ? color.blue:na)
// 如果inside是true,则颜色为蓝色,否则没有颜色
Bar color 蜡烛颜色
inside=high<high[1] and low>low[1]
barcolor(inside ? color.purple : na ) // 如果inside的值是true,bar的颜色改为紫色,否则颜色不变
// 公式局限,只能更改蜡烛图中间部分的颜色,无法改上引线和下引线的颜色,日后我们会介绍其他公式更改这部分的颜色
Bars & Candles 绘制蜡烛
c=close>open ? color.green : color.red // 如果收盘价>开盘价,颜色为绿色,否则为红色
plotcandle (open, high,low,close, color=c, title="Cancle", wickcolor=c, bordercolor=color.pine) //绘制蜡烛图, wickcolor就是上下引线的颜色, bordercolor是蜡烛图外边框的颜色
plotshape 绘制图形

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rum7126
//@version=5
indicator("Test", overlay=true) //overlay=true 绘制的图形将在蜡烛图上直接显示,就像移动平均线一样
higherclose=close>high[1]
lowerclose=close<low[1]
plotshape(
higherclose,
title="Higher Close",
style=shape.triangleup,
location=location.belowbar,
color=color.green,
text="hc",
textcolor=color.white,
size=size.auto
) //当higherclose为true的时候绘制向上的三角形,位于蜡烛图下方,绿色,尺寸大小自动调整
plotshape(
lowerclose,
title="Lower Close",
style=shape.arrowdown,
color=color.red,
text="lc",
textcolor=color.white,
size=size.auto
)
plot character 绘制符号

//@version=5
indicator("plotchar example", overlay=true)
data = close >= open
plotchar(data, char='❄')
plot arrow 绘制箭头

indicator("plotarrow example", overlay=true)
codiff = close - open
plotarrow(codiff, colorup=color.new(color.teal,40), colordown=color.new(color.orange, 40))
Lines 绘制直线

该功能功能非常多,可以在官方文档详细阅读
//@version=5
indicator("plotarrow example", overlay=true)
// 绘制直线需要起点和终点的坐标,x1, y1 就是起点坐标,x2,y2是终点坐标
myline=line.new(x1=bar_index[10],y1=low[10], x2=bar_index,y2=high)
line.set_color(myline, color.orange)
//现在的问题是,每一根蜡烛图结束后都会画一条线,所以我们需要删除无用的线,通过下面的代码每次画新线的同时也会删除之前的线
line.delete(myline[1])
plot(na)
labels 绘制标签
indicator("Labels", overlay=true)
mylabel=barstate.islast ? label.new(x=bar_index, y=low, text="close " + str.tostring(close) ) : na
//只在最后一个bar上显示标签, str.tostring(xx)将数字转换成文本
label.delete(mylabel[1]) //也可以用这个功能删除前一个标签
mylabel=label.new(
x=bar.index, //bar.index是当前蜡烛图在X轴的位置
y=close,
text="Close: " + str.tostring(close) + "\nHigh: " + str.tostring(high),
//这是显示在标签上的文字,由于是文本类型数据,需要把价格通过str.tostring转换为文本, \n 是换行的意思
yloc=yloc.price //标签相对蜡烛图的具体问之,现在选择的是当前价格的高度
color=close>open ? color.green : color.red, //如果收盘价大于开盘价 显示绿色,否则红色
sytle=label.style_label_down //标签的式样,箭头向下
textcolor=color.white //标签内文字颜色
tooltip="Current ATR: " + str.tostring(atr) //当鼠标悬浮在标签上时显示的内容
)
if syminfo.type=="crypto" //如果当前交易品种是加密货币
label.set_color(mylabel, low<low[1] ? color.purple : high>high[1] ? color.blue : na)
// 如果最低价低于上一根蜡烛图的最低价,颜色为紫色,如果最高价大于上一根蜡烛图的最高价,颜色为蓝色,其他显示默认色
table 表格绘制

//@version=5
indicator("table.new example")
var testTable = table.new(position = position.top_right, columns = 2, rows = 1, bgcolor = color.yellow, border_width = 1)
if barstate.islast
table.cell(table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
table.cell(table_id = testTable, column = 1, row = 0, text = "Close is " + str.tostring(close), bgcolor=color.teal)
box 箱体

//@version=5
indicator("box.new")
var b = box.new(time, open, time + 60 * 60 * 24, close, xloc=xloc.bar_time, border_style=line.style_dashed)
box.set_lefttop(b, time, 100)
box.set_rightbottom(b, time + 60 * 60 * 24, 500)
box.set_bgcolor(b, color.green)
常见指标开发
- 震荡指标
- 趋势指标
MACD
//@version=5
indicator("MACD")
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
plot(macdLine, color=color.blue) // macd 线
plot(signalLine, color=color.orange) // 信号线
plot(histLine, color=color.red, style=plot.style_histogram) // 柱子

上面是调用 ta 中现成的 macd 函数,如果我们自己写的话如下
//@version=5
indicator("MACD")
macd(src, fastLen, slowLen, signaLen) => // 定义函数
fastMA = ta.ema(src, fastLen) // 快线
slowMA = ta.ema(src, slowLen) // 慢线
macd = fastMA - slowMA // macd 线(蓝线)
signal = ta.ema(macd, signaLen) // 橙线 信号线
hist = macd - signal // 柱子
[macd, signal, hist] // 返回三个参数
[macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
plot(macdLine, color=color.blue) // macd 线
plot(signalLine, color=color.orange) // 信号线
// 变色上涨绿色,下跌红色
plot(histLine, color=histLine>histLine[1]?color.green:color.red, style=plot.style_histogram)

趋势指标
- 均线
- MACD线
- DMI趋向系统
- OBV能量潮
震荡指标
- MACD 柱子
- RSI
- CCI
- KD
- KDJ
- William
- %R
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!