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

Pine Script指标多配置存储报错:参数类型不匹配问题

Pine Script多策略配置存储错误解决方法

问题现象

运行指标时触发两个类型不匹配错误:

  • 第108行:调用ta.ema时,参数length传入series int类型的ema_len1,预期为simple int。
  • 第109行:调用request.security时,参数timeframe传入series string类型的res,预期为simple string。

问题原因

自定义Strategy_settings类型的成员变量默认是序列类型(series),会随K线周期生成动态变化的序列值,但ta.ema的周期参数、request.security的时间框架参数要求是简单类型(simple)——固定不变的单值。直接从自定义类型读取值传递给这些函数时,就会触发类型不匹配错误。

解决方案

使用ta.valuewhen()函数将序列类型的配置值转换为简单类型,仅在策略配置发生变更时更新值,确保变量保持固定单值状态。

修改后的完整代码

//@version=5
indicator(title='EMAeinstellungen div Coins', shorttitle='EMAeinstellungen div Coins', overlay=true)

// 自定义策略配置类型
type Strategy_settings
    string res = "60"
    int ema_len1 = 0
    float ema_mult_1 = 0
    int ema_len2 = 0
    int ema_len3 = 0
    float ema_mult_3 = 0

// 策略配置选择器函数
selector(string strategy_name) =>
    strategy_settings = Strategy_settings.new()
    switch strategy_name
        "EIGENE EINSTELLUNG" =>
            strategy_settings.res := "60"
            strategy_settings.ema_len1 := 50
            strategy_settings.ema_mult_1 := 0.05
            strategy_settings.ema_len2 := 75
            strategy_settings.ema_len3 := 100
            strategy_settings.ema_mult_3 := 0.3

        "UNIVERSAL M15" => 
            strategy_settings.res := "60"
            strategy_settings.ema_len1 := 50
            strategy_settings.ema_mult_1 := 0.05
            strategy_settings.ema_len2 := 75
            strategy_settings.ema_len3 := 100
            strategy_settings.ema_mult_3 := 0.3
        "LINK M60" => 
            strategy_settings.res := "120"
            strategy_settings.ema_len1 := 34
            strategy_settings.ema_mult_1 := 0.1
            strategy_settings.ema_len2 := 110
            strategy_settings.ema_len3 := 195
            strategy_settings.ema_mult_3 := 1.28
        "DOT M30" => 
            strategy_settings.res := "15"
            strategy_settings.ema_len1 := 49
            strategy_settings.ema_mult_1 := 0.11
            strategy_settings.ema_len2 := 87
            strategy_settings.ema_len3 := 106
            strategy_settings.ema_mult_3 := 0.23
    strategy_settings

// 全局变量初始化
bool emaTrend_setzen = false
string res = "60"
int ema_len1 = 50
float ema_mult_1 = 0.00
int ema_len2 = 90
int ema_len3 = 150
float ema_mult_3 = 0.30

// 输入设置
// 策略选择
strategy_input = input.string(title = "STRATEGY", options = [
    "EIGENE EINSTELLUNG",
    ">>>bisher nur manuell!!!<<<",
    "UNIVERSAL M15",
    "LINK M60",
    "DOT M30",
    "===============",
    "xxxxx1",
    "xxxxx2",
    "xxxxx3",
    "xxxxx4"
    ], defval = "EIGENE EINSTELLUNG", tooltip = "EN:\nTo manually configure the strategy, select EIGENE EINSTELLUNG otherwise, changing the settings won't have any effect\n\nkeine fertigen strategien!!!!!!!!!")

// EMA趋势组设置
var emaTrend = "🐾 EMA Trend"
res_input = input.timeframe(title="/Timeframe", defval="60", group=emaTrend, inline = "htf ema 1")

// EMA1设置
ema_len1_input = input.int(title="EMA1 Kurzperiode", defval=53, group=emaTrend, tooltip='EMA-Cross gilt für EMA1 mit EMA3 / EMA-Folge gleich EMA1<>EMA2<>EMA3 / Close muss über EMA2 sein')
ema_mult_1_input = input.float(0.050, minval=0.000, step=0.001, title='EMA1 Standardabweichungen', group=emaTrend)

// EMA2设置
ema_len2_input = input.int(title="EMA2 Mittelperiode", defval=90, group=emaTrend)

// EMA3设置
ema_len3_input = input.int(title="EMA3 Langperiode", defval=175, group=emaTrend)
ema_mult_3_input = input.float(0.30, minval=0.000, step=0.001, title='EMA3 Standardabweichungen', group=emaTrend)

// 初始化策略配置实例
var Strategy_settings strategy_s = na
strategy_s := strategy_input == "EIGENE EINSTELLUNG" ? Strategy_settings.new(res_input, ema_len1_input, ema_mult_1_input, ema_len2_input, ema_len3_input, ema_mult_3_input) : selector(strategy_input)

// 关键修改:将序列类型转换为简单类型,仅在配置变更时更新值
res := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.res, 0)
ema_len1 := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.ema_len1, 0)
ema_mult_1 := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.ema_mult_1, 0)
ema_len2 := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.ema_len2, 0)
ema_len3 := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.ema_len3, 0)
ema_mult_3 := ta.valuewhen(strategy_s != strategy_s[1], strategy_s.ema_mult_3, 0)

// EMA计算部分
htf_ma1 = ta.ema(close, ema_len1)
out1 = request.security(syminfo.tickerid, res, htf_ma1)
ema_dev_1 = ta.stdev(close, ema_len1)
ema_upper_1 = out1 + ema_mult_1 * ema_dev_1
ema_lower_1 = out1 - ema_mult_1 * ema_dev_1

plot(out1, color=color.purple, title='EMA1', linewidth=1)
plot(ema_upper_1, color=color.rgb(155, 39, 176, 100), title='EMA1 Obere Standardabweichung')
plot(ema_lower_1, color=color.purple, title='EMA1 Untere Standardabweichung')

htf_ma2 = ta.ema(close, ema_len2)
out2 = request.security(syminfo.tickerid, res, htf_ma2)
ema_dev_2 = ta.stdev(close, ema_len2)

plot(out2, color=color.rgb(255, 153, 0, 50), title='EMA2', linewidth=1)

htf_ma3 = ta.ema(close, ema_len3)
out3 = request.security(syminfo.tickerid, res, htf_ma3)
ema_dev_3 = ta.stdev(close, ema_len2) // 注:原代码此处笔误为ema_len2,建议修正为ema_len3
ema_upper_3 = out3 + ema_mult_3 * ema_dev_3
ema_lower_3 = out3 - ema_mult_3 * ema_dev_3

plot(out3, color=color.fuchsia, title='EMA3', linewidth=1) // 修正原代码标题笔误
plot(ema_upper_3, color=color.rgb(223, 64, 251, 100), title='EMA3 Obere Standardabweichung') // 修正原代码标题笔误
plot(ema_lower_3, color=color.fuchsia, title='EMA3 Untere Standardabweichung') // 修正原代码标题笔误

关键修改说明

  1. 类型转换:通过ta.valuewhen(strategy_s != strategy_s[1], 配置值, 0)实现序列转简单类型,仅在策略配置变更时更新变量值,确保参数符合函数要求。
  2. 笔误修正:修复了原代码中EMA3的plot标题、标准差计算周期的两处错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 02:10:55