PineScript 策略开发教程和汇总

策略基础模板

//@version=5
// pyramiding = 1 同一方向允许的最大条目数。如果值为0,则只能开同一个方向的挂单,拒绝追加挂单。此设置也可以在策略的“设置/属性”标签页中更改。可选。默认值为0。
// initial_capital =10000 最初可用于策略交易的资金量,以currency为单位。可选。默认值为1000000。
strategy("止盈止损模板", overlay = true, pyramiding = 1, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills = false, slippage = 0, commission_type = strategy.commission.percent, commission_value = 0.04)


// 结尾
// 策略
Long = malong and rsiLong
Short = maShort and rsiShort
// 信号
if Long
    strategy.entry("L", strategy.long, alert_message = "xxxx")
if Short 
    strategy.entry("S", strategy.short, alert_message = "xxxx")

per(pcnt) =>
    strategy.position_size != 0 ? math.round(pcnt /100 * strategy.position_avg_price/syminfo.mintick): float(na)
stoploss = input.float(title="stop loss", defval = 9, minval=0.01)
los = per(stoploss)
q = input.int(title = "qty percent", defval = 100, minval=1)
tp = input.float(title = "Take profit", defval=1.7, minval=0.01)

strategy.exit("tp", qty_percent = q, profit = per(tp), loss=los, alert_message = "xxxx")

简单示例

//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)

// 黄金交叉
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

警报通知

- 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)

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!