在pine script的编码上对于有 一个函数或者一个字符串的多行拼写的时候,一定要记得,多行的开头空格不能是四个,以后我形成习惯都是2个空格吧。这样就不至于老是报错了。
label.new(
bar_index, low, “交叉发生!\nClose=” + str.tostring(close, “#.##”) + “\nMA=” + str.tostring(ma, “#.##”), style=label.style_label_up, color=color.green,
textcolor=color.white,
tooltip = “交叉发生!\nClose=” + str.tostring(close, “#.##”) + “\nMA=” + str.tostring(ma, “#.##”),
size=size.tiny
)// 标签出现在当前 K 线上方
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thanklife
//@version=6
strategy("交叉追踪策略回测", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// === 用户输入 ===
length = input.int(14, "MA 周期")
takeProfitPct = input.float(3.0, "止盈 %", step=0.1)
stopLossPct = input.float(1.5, "止损 %", step=0.1)
// === 指标计算 ===
ma = ta.sma(close, length)
xUp = ta.crossover(close, ma)
// === 计算止盈止损价格 ===
longTakeProfit = close * (1 + takeProfitPct / 100)
longStopLoss = close * (1 - stopLossPct / 100)
// === 策略执行 ===
// 1. 进场条件:上穿均线
if xUp
strategy.entry("多头进场", strategy.long)
// 2. 离场条件:止盈或止损(系统会自动触发)
strategy.exit("出场", from_entry="多头进场",
limit=longTakeProfit,
stop=longStopLoss)
// === 可视化辅助 ===
// 标记交叉点
plot(ma, title="SMA", color=color.orange)
plotshape(xUp, title="上穿", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
// 标签显示:显示止盈止损价位
if xUp
label.new(bar_index, low,
"进场价: " + str.tostring(close, "#.##") + "\n止盈: " + str.tostring(longTakeProfit, "#.##") + "\n止损: " + str.tostring(longStopLoss, "#.##"),
style=label.style_label_down, color=color.green, textcolor=color.white, size=size.small)