(Pine Script) Как упростить повторную безопасность ()? Является ли это возможным? - PullRequest
0 голосов
/ 09 июля 2020

Я обнаружил, что скрипт сосны предоставляет только 40 ценных бумаг в скрипте. И я пытаюсь упростить повторяющееся «security ()», а также «len (n) = input». Ниже приведен случайный код.

//@version=4
study("How to make it simple?", overlay=false)

len1 = input(10)
sma_H1 = security(syminfo.tickerid, "5", sma(high, len1))
sma_L1 = security(syminfo.tickerid, "5", sma(low, len1))

UP1 = sma_H1 and sma_L1 > close
DN1 = sma_H1 and sma_L1 < close

plotshape(UP1, style= shape.xcross, location= location.top, color=color.green)
plotshape(DN1, style= shape.xcross, location= location.top, color=color.red)

Я хочу добавить больше таймфреймов с другим "len (n) = input". Ниже, как вы можете видеть, повторяется много кодов. Можно ли сохранить только 2 security () и добавить больше таймфреймов, используя «цикл for» или «оператор if»?

Я пробовал много методов, но скрипт говорит: «Невозможно использовать изменяемую переменную. в качестве аргумента функции безопасности »в сценарии или« Недопустимое значение аргумента «разрешение» (i) в функции «безопасность» »на диаграмме.

Любая ссылка также будет очень полезна или может кто-нибудь сделает приведенные ниже коды как можно проще? Пожалуйста, кто-нибудь, помогите мне с этим ..

//@version=4
study("How to make it simple?", overlay=false)

len1 = input(10)
sma_H1 = security(syminfo.tickerid, "5", sma(high, len1))
sma_L1 = security(syminfo.tickerid, "5", sma(low, len1))
//------------------------------------------------------------------------------
len2 = input(20)
sma_H2 = security(syminfo.tickerid, "15", sma(high, len2))
sma_L2 = security(syminfo.tickerid, "15", sma(low, len2))
//------------------------------------------------------------------------------
len3 = input(30)
sma_H3 = security(syminfo.tickerid, "30", sma(high, len3))
sma_L3 = security(syminfo.tickerid, "30", sma(low, len3))
//------------------------------------------------------------------------------
len4 = input(40)
sma_H4 = security(syminfo.tickerid, "45", sma(high, len4))
sma_L4 = security(syminfo.tickerid, "45", sma(low, len4))

//////////////////////////////////////////////////////////////////////////////////////////////
UP1 = sma_H1 and sma_L1 > close
DN1 = sma_H1 and sma_L1 < close
//------------------------------------------------------------------------------
UP2 = sma_H2 and sma_L2 > close
DN2 = sma_H2 and sma_L2 < close
//------------------------------------------------------------------------------
UP3 = sma_H3 and sma_L3 > close
DN3 = sma_H3 and sma_L3 < close
//------------------------------------------------------------------------------
UP4 = sma_H4 and sma_L4 > close
DN4 = sma_H4 and sma_L4 < close

//////////////////////////////////////////////////////////////////////////////////////////////
UP = UP1 and UP2 and UP3 and UP4
DN = DN1 and DN2 and DN3 and DN4
plotshape(UP, style= shape.xcross, location= location.top, color=color.green)
plotshape(DN, style= shape.xcross, location= location.top, color=color.red)

1 Ответ

2 голосов
/ 10 июля 2020

Вы можете создать функцию, которая возвращает кортеж, это уменьшит некоторый код.

//@version=4
study("How to make it simple?", overlay=false)

f_security(_tf, _len) => [_high, _low] = security(syminfo.tickerid, _tf, [sma(high, _len), sma(low, _len)])

//------------------------------------------------------------------------------
len1 = input(10)
[sma_H1, sma_L1] = f_security("5", len1)
//------------------------------------------------------------------------------
len2 = input(20)
[sma_H2, sma_L2] = f_security("15", len2)
//------------------------------------------------------------------------------
len3 = input(30)
[sma_H3, sma_L3] = f_security("30", len3)
//------------------------------------------------------------------------------
len4 = input(40)
[sma_H4, sma_L4] = f_security("45", len4)
...