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

Pine Script报错:request.security类型不匹配及期权价格获取问题

问题解决:Pine Script获取期权数据报错及实现需求方案

错误原因分析

你遇到的无法调用该运算符,传入了'series string'类型参数,但需要'simple string'类型报错,核心问题有两个:

  1. 合约代码格式错误:NSE的BANKNIFTY期权代码使用月份缩写(如JUL、AUG)而非数字(07、08),直接拼接数字会导致无法匹配到有效合约。
  2. 动态字符串兼容性:你生成的strikep依赖随K线变化的close值,属于序列字符串,旧版Pine Script的request.security仅支持静态字符串;不过Pine Script v5已支持动态合约符号,但需要配合正确的写法。

修正后代码(实现需求)

以下代码可获取当前价格上下2个行权价的看涨/看跌期权数据,通过表格展示并在图表上标记收盘价:

//@version=5
indicator(title='Breakout Stock', overlay=true, max_labels_count=500)

// 基础配置参数
strike_interval = 100       // 行权价间隔
expiry_year = "24"          // 到期年份后两位
expiry_month = "JUL"        // 到期月份缩写(需对应NSE规范:JUL/AUG/SEP等)
expiry_day = "31"           // 到期日
underlying_code = "NSE:BANKNIFTY"

// 计算ATM(平值)行权价
atm_strike = int(math.round(close / strike_interval) * strike_interval)

// 生成需监控的行权价列表(当前价格上下2个区间)
target_strikes = [
    atm_strike - 2*strike_interval,
    atm_strike - strike_interval,
    atm_strike,
    atm_strike + strike_interval,
    atm_strike + 2*strike_interval
]

// 生成期权合约符号的工具函数
get_option_symbol(strike, option_type) =>
    str.format("{0}{1}{2}{3}{4}{5}", 
        underlying_code, expiry_year, expiry_month, expiry_day, option_type, str.tostring(strike))

// 初始化期权数据展示表格
var option_table = table.new(position.top_right, 3, 6, title="期权报价表", border_width=1)

// 仅在最后一根K线更新数据
if barstate.islast
    // 填充表格表头
    table.cell(option_table, 0, 0, "行权价")
    table.cell(option_table, 1, 0, "看涨收盘价")
    table.cell(option_table, 2, 0, "看跌收盘价")
    
    // 遍历所有目标行权价,获取并展示数据
    for i = 0 to 4
        current_strike = target_strikes[i]
        // 获取看涨期权收盘价
        call_symbol = get_option_symbol(current_strike, "C")
        call_close = request.security(call_symbol, timeframe.period, close)
        // 获取看跌期权收盘价
        put_symbol = get_option_symbol(current_strike, "P")
        put_close = request.security(put_symbol, timeframe.period, close)
        
        // 填充表格内容
        table.cell(option_table, 0, i+1, str.tostring(current_strike))
        table.cell(option_table, 1, i+1, str.tostring(call_close))
        table.cell(option_table, 2, i+1, str.tostring(put_close))
        
        // 图表上标记期权价格(可选)
        label.new(bar_index, close + strike_interval*0.3, 
            str.format("C{0}: {1}", current_strike, call_close), 
            color=color.new(color.green, 0), textcolor=color.white)
        label.new(bar_index, close - strike_interval*0.3, 
            str.format("P{0}: {1}", current_strike, put_close), 
            color=color.new(color.red, 0), textcolor=color.white)

关键修改说明

  • 合约格式修正:使用NSE规范的月份缩写生成合约符号,确保能匹配到有效期权合约。
  • 动态合约支持:Pine Script v5原生支持传入序列字符串作为request.security的合约参数,解决原报错问题。
  • 批量行权价处理:生成覆盖当前价格上下2个区间的行权价列表,满足你对多期权数据的需求。
  • 可视化展示:通过固定表格集中展示期权数据,同时可选择在图表上标记对应价格,兼顾数据查看的便捷性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 19:13:18