解构多行函数

De-construct multiline function

我正在尝试将用户“layzybear”指标之一实施到策略中。指标是 Elder Impuls System“EIS_lb”,我已将其编码为 v4。 目标是设置(绿色背景 = OK to Buy)和(红色背景 = OK to Sell)和(蓝色背景 = NOK to trade)。但我就是不明白如何解构多行函数来设置这些规则。 通常我只是将情节条件设置为买卖规则,但这在这里行不通。

如果有任何帮助或想法,我将不胜感激。

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, fastLength, slowLength))
bgcolor(b_color, transp=50)

首先要注意的是 - 使用带有 lookahead_on 参数的 security 函数而不使用历史引用运算符 [] 会将您的脚本引导至 'repaint'。我将历史参考 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, useCustomResolution ? customResolution : timeframe.period, close, barmerge.gaps_off, 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, fastLength, 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)