编译脚本代码太长:噩梦

Compiled script code is too long: Nightmare

我认为很多很多开发人员一直都在遇到这个问题,我们的脚本很复杂而且是一体的。 TradingView 的团队没有真正的解决方案。我真的不确定如何优化更多。有什么帮助吗?

示例:

if hasToLongAllStableTriggers_highestEma
    b_long_isBullishHighestEmaBounce := true

这生成了 36 行代码!废话...

f_create_strategy(trigger, long_input, SL_long, id_str) =>
    if isActivateLongs and window() // Date time
        [b_close, long_comment] = f_triggerOption(long_input)
        [SL_b_close, SL_comment] = f_triggerOption(SL_long)
        SL_manipulation = rel_isYellowCross or A_isYellowCross
        
        if trigger and long_comment != None and b_close == false and SL_b_close == false and SL_manipulation == false
            strategy.entry(long=strategy.long, limit=close, id=id_str, comment=id_str)
        // Close
        if b_close
            strategy.close(id=id_str, comment=long_comment)
        // Stop Loss
        if SL_b_close
            strategy.close(id=id_str, comment=SL_comment)
        // Global manipulation
        if SL_by_manipulation and SL_manipulation
            strategy.close(id=id_str, comment="Manipulation Signal")
        //
        // Global - Take Profits - LONG
        //
        if enableTpForLongs and enableStopLossForLongs
            strategy.exit("TP/SL", id_str, limit=longTpPrice, stop=longStopPrice)
        
        else if enableTpForLongs
            strategy.exit("TP", id_str, limit=longTpPrice)
        //
        // Global - Stop Loss - LONG
        //
        else if enableStopLossForLongs
            strategy.exit("SL", id_str, stop=longStopPrice)
    

f_find_back(trigger, trigger_to_search, min_back, max_back) =>
    found = false
    if trigger
        for i = min_back to max_back
            if trigger_to_search[i]
                found := true
                break
    found
//
// 
//
isBullishDivHigherEma_long_run = rel_higher_ema1_BullDiv and floor(rel_higher_ema1)[3] < floor(rel_higher_ema1) and rel_higher_ema1 > osLevel2
isBullishDivHigherEma_ema8_low = rel_higher_ema1_BullDiv and rel_ema8 <= slArea
//isBullishDivHigherEma = rel_higher_ema1_BullDiv and rel_higher_ema1 > rel_higher_ema1_Low_prev 

isBullishDivHigherEma = f_find_back(rel_higher_ema1_Low_prev < rel_higher_ema1_Low_prev[1], rel_higher_ema1_BullDiv, 1, 10)

b_long_isBullishDivHigherEma = input(group = id_isBullishDivHigherEma, defval=true, title=str_input_long)
//if hasToLongAllStableTriggers_higherEma
//    b_long_isBullishDivHigherEma := true
    
long_isBullishDivHigherEma = input(group = id_isBullishDivHigherEma, defval=TPLevelCrossUpEma1, title=str_input_closeby, type=input.string, options=[None, TPLevelCrossUpEma1])
SL_isBullishDivHigherEma = input(group = id_isBullishDivHigherEma, defval=None, title=str_input_sl, type=input.string, options=[None, TPLevelCrossUpEma1])
if b_long_isBullishDivHigherEma
    f_create_strategy(isBullishDivHigherEma, long_isBullishDivHigherEma, SL_isBullishDivHigherEma, id_isBullishDivHigherEma)

生成超过 6464,至少,因为限制是 60k 猜测溢出是例如数量。

谢谢,有答案的祝福!

使用 for 循环在函数内部进行失控回测。

解决方案是从函数中删除 f_find_back 的逻辑,这样就可以节省成千上万行生成的无意义代码。

2 种方式:

result := false
if condition
    // Option 1
    for i = 1 to 10
        if what_you_want_to_back_test[i]
            result := true
            break
    // Option 2 - Transform series[bool] to a sum integer      
    float f_counter = 0.0 
    for i = 1 to 10
        f_counter += what_you_want_to_back_test[i] ? 1 : 0
        
    result := f_counter > 0.0

这两个选项消耗的生成代码行太少。 pine script最大的耻辱就是不能依靠任何类型的支持,即使你付了钱。