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

为何图表内收盘价可正常计算MRS,request.security()获取的却不行?

问题:Mansfield相对强度(MRS)脚本在使用request.security()获取收盘价时计算错误

我编写了一个计算Mansfield相对强度(MRS)的脚本,单独运行效果正常,但在构建复合指标时发现异常:

  • 图表加载AAPL,使用本地收盘价(OnScreen)时,MRS结果显示正常
  • 改用request.security()获取AAPL收盘价(BehindScenes)时,MRS结果完全错误
  • 对比两种方式获取的收盘价,数值完全一致,尝试多种修复仍无效,怀疑是操作失误或Pine Script固有特性导致

原代码如下:

//@version=5
// Use this indicator to compare how security is performing in compare with prefered index (SPX by default). 
// > 0 outperforming,   < 0 underperforming
// Works best for weekly, but can be applied to monthly and daily charts. It will be rather useless to use it in smaller timeframes
// Apply it to SPX, industry index, sector index or other security in similar sector

indicator('Mansfield - Why wrong')
OnScreen = close
BehindScenes = request.security(symbol = "AAPL", timeframe = timeframe.period, expression = close)

index = input.symbol(title='Index', defval='SPY')

len_daily = input(title='MA length for Daily', defval=150)
len = len_daily

ma_func(val3, length) =>
    ta.sma(val3, length)

calc_mrs_func(ind, length, symbol) =>
    ( symbol / request.security(ind, timeframe.period, symbol) / ma_func((symbol / request.security(ind, timeframe.period, symbol)), length) - 1) * 10

mrs_index = calc_mrs_func(index, len, OnScreen)
c = math.sign(mrs_index)

mrs_index2 = calc_mrs_func(index, len, BehindScenes)
c2 = math.sign(mrs_index2)


color_3 = color.new(color.green, 70)
color_4 = color.new(color.red, 70)
color_7 = color.new(color.gray, 90)
bgcolor(c == 1 ? color_3 : c == -1 ? color_4 : color_7)
plot(mrs_index, linewidth=3, title='MRS index')
// Comment out lines above and uncomment lines below
//bgcolor(c2 == 1 ? color_3 : c2 == -1 ? color_4 : color_7)
//plot(mrs_index2, linewidth=3, title='MRS index')
hline(price=0, linestyle=hline.style_dashed, title='Zero baseline')

plot(OnScreen, color=color.green, linewidth = 8)
plot(BehindScenes, color=color.red, linewidth = 3)

问题根源

错误出在嵌套的request.security()调用和Pine Script的序列对齐逻辑上:

  1. 在calc_mrs_func函数中,你两次调用request.security(ind, timeframe.period, symbol),当symbol是通过request.security()获取的跨品种序列时,会触发嵌套的安全函数调用
  2. 本地收盘价(OnScreen)是当前图表的原生序列,与指数的request.security()返回序列时间对齐正常;但跨品种获取的BehindScenes序列,在嵌套调用时会破坏时间轴对齐关系,导致比值和均线计算完全错误

修复方案

核心思路是提前计算标的与指数的比值,避免嵌套的request.security()调用,将比值计算与均线处理分离,确保所有序列时间对齐一致。

修正后的代码:

//@version=5
indicator('Mansfield - Fixed')
OnScreen = close
BehindScenes = request.security(symbol = "AAPL", timeframe = timeframe.period, expression = close)

index = input.symbol(title='指数', defval='SPY')
len_daily = input(title='日线MA长度', defval=150)
len = len_daily

ma_func(val3, length) =>
    ta.sma(val3, length)

// 提前计算标的与指数的比值,避免嵌套request.security调用
calc_ratio(symbol_price, index_symbol) =>
    index_price = request.security(index_symbol, timeframe.period, close)
    symbol_price / index_price

// 基于预计算的比值计算MRS
calc_mrs_func(ratio, length) =>
    (ratio / ma_func(ratio, length) - 1) * 10

// OnScreen版本MRS计算
ratio_on_screen = calc_ratio(OnScreen, index)
mrs_index = calc_mrs_func(ratio_on_screen, len)
c = math.sign(mrs_index)

// BehindScenes版本MRS计算
ratio_behind = calc_ratio(BehindScenes, index)
mrs_index2 = calc_mrs_func(ratio_behind, len)
c2 = math.sign(mrs_index2)

color_3 = color.new(color.green, 70)
color_4 = color.new(color.red, 70)
color_7 = color.new(color.gray, 90)

// 可切换查看两种版本结果
bgcolor(c == 1 ? color_3 : c == -1 ? color_4 : color_7)
plot(mrs_index, linewidth=3, title='MRS (OnScreen)')
// bgcolor(c2 == 1 ? color_3 : c2 == -1 ? color_4 : color_7)
// plot(mrs_index2, linewidth=3, title='MRS (BehindScenes)')

hline(price=0, linestyle=hline.style_dashed, title='基准线')

plot(OnScreen, color=color.green, linewidth = 8)
plot(BehindScenes, color=color.red, linewidth = 3)

关键修改点:

  • 新增calc_ratio函数,单独获取指数收盘价并计算标的/指数比值,彻底避免嵌套的request.security()调用
  • 将MRS计算拆分为比值计算和均线处理两步,确保所有序列的时间轴完全对齐
  • 本地收盘价与跨品种收盘价使用完全一致的计算逻辑,保证结果一致性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 12:35:28