You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Pine Script中如何获取当前价格?能否替换策略中的SMA9?

Pine Script 当前价格引用说明

Pine Script原生支持直接引用当前K线的各类价格参数,无需额外计算即可直接使用,常用的价格内置变量包括:

  • close:当前K线收盘价,是回测场景下最常用的「当前价格」指代变量
  • open:当前K线开盘价
  • high:当前K线最高价
  • low:当前K线最低价
  • hlc3:当前K线最高价、最低价、收盘价的加权平均值,可作为当前价格的中性参考

修改后的完整代码

你只需要删除原代码中SMA9的计算、绘制逻辑,将买卖条件中的sma9替换为close即可,修改后代码如下:

//@version=4
strategy ("Moving Average Cross", overlay=true, initial_capital=4000)

start = timestamp(2021,8,18,0,0)
end = timestamp(2021,10,18,0,0)

sma200 = sma(close,200)
plot(sma200, title="200", color=#0000ff, linewidth=3)

// Get user input
rsiSource = input(title="RSI Source", type=input.source, defval=close)
rsiLength = input(title="RSI Length", type=input.integer, defval=14)//defval is how many candles
rsiOverbought = input(title="RSI Overbought Level", type=input.integer, defval=58)
rsiOversold = input(title="RSI Oversold Level", type=input.integer, defval=35)
// Get RSI value
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold
// Plot signals to chart
plotshape(isRsiOB, title="Overbought", location=location.abovebar, color=color.red, transp=0, style=shape.triangledown, text="OB")
plotshape(isRsiOS, title="Oversold", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="OS")

long = isRsiOS and (close > sma200)
short = isRsiOB or (close < sma200)

if time >= start and time <= end
    strategy.entry ("long", strategy.long, 100.0, when=long)
    strategy.close ("long", when=short)

修改点说明

  1. 删除了原代码中sma9的计算行、sma9的绘制行
  2. 将做多条件isRsiOS and (sma9 > sma200)替换为isRsiOS and (close > sma200)
  3. 将平仓/做空条件isRsiOB or (sma9 < sma200)替换为isRsiOB or (close < sma200)

如果需要用其他价格作为当前价格参考,直接把close替换成对应内置变量即可。

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 21:36:06