语法速查
新(v0.3):request.security("ETHUSDT", "D", close) 读别的币 / 周期;label.new line.new box.new table.new 在图上放标签、线、方框、表格;var label x = na 这类类型写法;barstate.islast syminfo.ticker timeframe.period;hour dayofweek 等时间字段。打字时会自动提示(Ctrl + 空格 手动打开)。
基本:indicator("名字", overlay=true) 主图叠加;不写 overlay 就开副图。每根 K 线从上到下跑一遍。
变量:x = close - open 声明;x := x + 1 改值;var n = 0 跨 K 线保留;close[1] 上一根。
流程:if 条件 / else if / else、for i = 0 to 9、while,代码块缩进 4 格。a ? b : c 三元。and or not。
函数:f(x, n) => ta.sma(x, n) * 2;多行函数换行后缩进,最后一行是返回值;[a, b] = ... 接收多个返回值。
参数:input.int input.float input.bool input.string input.color input.source,会在编辑器下方变成可调的框。
画图:plot(style=plot.style_line / style_columns / style_circles)、plotshape、hline、fill、bgcolor。颜色 color.red、#ff9900、color.new(c, 透明度)、color.rgb。
指标函数 ta.*:sma ema rma wma hma vwma stdev variance dev highest lowest change mom roc rising falling cum rsi atr macd bb stoch cci mfi dmi supertrend vwap linreg crossover crossunder cross barssince valuewhen pivothigh pivotlow;变量 ta.tr ta.obv。
数学 math.*:abs sqrt log log10 exp pow floor ceil round sign max min avg sum sin cos tan;nz na fixnan。
内置数据:open high low close volume hl2 hlc3 ohlc4 hlcc4 time bar_index last_bar_index。
提醒:alertcondition(条件, "标题", "内容"),内容里可写 {{ticker}} {{interval}} {{close}} {{open}} {{high}} {{low}} {{volume}} {{time}};到「🔔 提醒」页打开开关。
回测:第一行写 strategy("名字", initial_capital=10000, commission_value=0.1);strategy.entry("多", strategy.long) 开仓、strategy.close("多") 平仓、strategy.close_all()、strategy.exit("出", "多", stop=止损价, limit=止盈价);
可读 strategy.position_size strategy.position_avg_price strategy.equity。下单方式 default_qty_type=strategy.percent_of_equity / strategy.cash / strategy.fixed。
规则:收盘出信号、下一根开盘成交;同根同时碰到止损止盈先算止损;不加仓、不加杠杆。
脚本在你的浏览器里运行,引擎不执行任何网页代码,碰不到账号和网络。