微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解构多行函数

如何解决解构多行函数

我正在尝试将用户“layzybear”指标之一实施到策略中。该指标是 Elder Impuls System "EIS_lb",我已将其编码为 v4。 目标是设置(绿色背景 = 可以购买)和(红色背景 = 可以出售)和(蓝色背景 = 可以交易)。但我无法理解如何解构多行功能来设置这些规则。 通常我只是将情节条件设置为买卖规则,但这在这里不起作用。

我将非常感谢您的任何帮助或想法。

useCustomresolution=input(true,type=input.bool)
customresolution=input("D",type=input.resolution)
source = security(syminfo.ticker,useCustomresolution ? customresolution : timeframe.period,close,barmerge.gaps_off,barmerge.lookahead_on)
lengthEMA = input(13)
fastLength = input(12,minval=1),slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

calc_hist(source,fastLength,slowLength) =>
    fastMA = ema(source,fastLength)
    slowMA = ema(source,slowLength)
    macd = fastMA - slowMA
    signal = sma(macd,signalLength)
    macd - signal

get_color(emaSeries,macdHist) =>
    g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
    r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
    g_f ? color.green : r_f ? color.red : color.blue
    
b_color = get_color(ema(source,lengthEMA),calc_hist(source,slowLength))
bgcolor(b_color,transp=50)

解决方法

首先要注意 - 使用 security 函数和 lookahead_on 参数而不使用历史引用运算符 [] 将导致您的脚本“重新绘制”。如果您仍想使用它,我将历史引用 1 添加到关闭源代码并添加了带有重绘值的第二条注释行。

//@version=4
strategy("My Strategy",overlay=true)

useCustomResolution=input(true,type=input.bool)
customResolution=input("D",type=input.resolution)
source = security(syminfo.ticker,useCustomResolution ? customResolution : timeframe.period,close[1],barmerge.gaps_off,barmerge.lookahead_on)
// source = security(syminfo.ticker,close,barmerge.lookahead_on) // Repaint
lengthEMA = input(13)
fastLength = input(12,minval=1),slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

calc_hist(source,fastLength,slowLength) =>
    fastMA = ema(source,fastLength)
    slowMA = ema(source,slowLength)
    macd = fastMA - slowMA
    signal = sma(macd,signalLength)
    macd - signal

// get_color(emaSeries,macdHist) =>
//     g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
//     r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
//     g_f ? color.green : r_f ? color.red : color.blue

get_signal(emaSeries,macdHist) =>
    g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
    r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
    g_f ? true : r_f ? false : na

signal = get_signal(ema(source,lengthEMA),calc_hist(source,slowLength))

bgcolor(signal == true ? color.green : signal == false ? color.red : color.blue,transp=50)


if signal == true
    strategy.entry("My Long Entry Id",strategy.long)

if signal == false
    strategy.entry("My Short Entry Id",strategy.short)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。