解构多行函数

问题描述

我正在尝试将用户“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)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...