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

如何从Pine Script的strategy()函数中获取配置参数数据?

在Pine Script v5中获取策略保证金参数的可行方法

你想获取strategy()函数中设置的margin_long和margin_short参数值用于后续计算或可视化,这完全可行,以下是正确的实现方案:

正确代码示例

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

// 正确获取策略保证金参数
var float marginLong = strategy.margin_long
var float marginShort = strategy.margin_short

// 计算杠杆值
float leverageLong = 100 / marginLong
float leverageShort = 100 / marginShort

// 示例:在图表上绘制杠杆数值
plot(leverageLong, color=color.green, title="Long Leverage", linewidth=2)
plot(leverageShort, color=color.red, title="Short Leverage", linewidth=2)

关键说明

  • 必须使用strategy.margin_long和strategy.margin_short这两个内置全局变量来访问策略的保证金设置,直接使用margin_long会报错——它只是strategy()函数的入参,并非全局可访问变量。
  • 用var声明变量可以确保仅在脚本初始化时赋值一次,避免重复计算,提升效率。
  • 获取到的保证金/杠杆值可自由用于绘图、表格展示或其他逻辑计算,完全满足你的需求。

拓展:用表格展示实时数据

如果需要更直观地在图表上查看参数值,可以添加表格组件:

if barstate.islast
    var table paramTable = table.new(position.top_right, 2, 4)
    // 填充表格内容
    table.cell(paramTable, 0, 0, "参数类型", text_color=color.white, bg_color=color.blue)
    table.cell(paramTable, 1, 0, "数值", text_color=color.white, bg_color=color.blue)
    table.cell(paramTable, 0, 1, "Long Margin (%)", bg_color=color.green)
    table.cell(paramTable, 1, 1, str.tostring(marginLong), bg_color=color.green)
    table.cell(paramTable, 0, 2, "Short Margin (%)", bg_color=color.red)
    table.cell(paramTable, 1, 2, str.tostring(marginShort), bg_color=color.red)
    table.cell(paramTable, 0, 3, "Long Leverage", bg_color=color.green)
    table.cell(paramTable, 1, 3, str.tostring(leverageLong), bg_color=color.green)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 06:05:31