Не могу понять, почему сделки не выполняются - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь выполнить длинную запись, когда выполняются 3 условия (redCandleDirection, isRsiLow, isUnderBollinger), но это не сработает. Я попытался вручную установить для них все значение true, но записи по-прежнему не выполняются. Благодаря этому я вполне уверен, что в моем logi c есть ошибка, но я не могу ее найти. Любая помощь будет принята с благодарностью.

Условия и время их проверки:

redCandleDirection = ((close[0] < open[0]) and (close[1] < open[1]) and (close[2] < open[2]) and (close[3] < open[3]) and (close[4] < open[4]))
isRsiLow = (d <= 10)
isUnderBollinger = (crossover(src, lower))
entryLong = false


isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger) =>
    if (redCandleDirection)
        if (isRsiLow)
            if (isUnderBollinger)
                entryLong = true
            else
                entryLong = false
        else
            entryLong = false
    else
        entryLong = false
    entryLong



if isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger)
    strategy.entry(id="Entry Long", long=true)
    strategy.exit(id="Exit Long", from_entry="Entry Long", qty_percent=100, limit=sma, stop=close[1])

Полный код, если он вам нужен:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PatrickML
// @ ChuckBanger

//@version=4
strategy(title="Bollinger Bands w/ Limit and Trailing Stop", shorttitle="BB w/ L + TS", 
   overlay=true,  pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=50, initial_capital=1000.0)


length = input(title="Length", type=input.integer, defval=20, minval=1)
regular = input(title="Regular BB Or CBMA?", type=input.bool, defval=true)
src = input(title="Source", type=input.source, defval=close)
mult = input(title="Multipler", type=input.float, defval=2.3, minval=.001, maxval=50, step=.1)
emaLen = input(title="EMA Length", type=input.integer, defval=20, minval=1)
emaGL = input(title="EMA Gain Limit", type=input.integer, defval=50, minval=1)
highlight = input(title="Highlight On/Off", type=input.bool, defval=true)

smaLen = input(title="SMA Length", type=input.integer, defval=9, minval=1)
sma = sma(close, smaLen)

direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)

strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))

//strategy.risk.max_drawdown(50, strategy.percent_of_equity)


///STOCH.RSI///
smoothK = input(3, minval=1, title="Stochastic %K Smoothing")
smoothD = input(3, minval=1, title="Stochastic %K Moving Average")
lengthRSI = input(14, minval=1, title="RSI Length")
lengthStoch = input(14, minval=1, title="Stochastic Length")
RSIprice = close
rsi1 = rsi(RSIprice, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)


calc_hma(src, length) =>
    hullma = wma(2*wma(src, length/2)-wma(src, length), round(sqrt(length)))
    hullma

calc_cbma(price, length, emaLength, emaGainLimit) =>
    alpha = 2 / (emaLength + 1)
    ema = ema(price, emaLength)
    int leastError = 1000000

    float ec = 0
    float bestGain = 0

    for i = emaGainLimit to emaGainLimit
        gain = i / 10
        ec := alpha * ( ema + gain * (price - nz(ec[1])) ) + (1 - alpha) * nz(ec[1])
        error = price - ec
        if (abs(error) < leastError)
            leastError = abs(error)
            bestGain = gain

    ec := alpha * ( ema + bestGain * (price - nz(ec[1])) ) + (1 - alpha) * nz(ec[1])
    hull = calc_hma(price, length)

    cbma = (ec + hull) / 2
    cbma

cbma = calc_cbma(src, length, emaLen, emaGL)
basis = regular ? sma(src, length) : cbma
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
cbmaColor = fixnan(highlight and not regular ? cbma > high ? color.purple : cbma < low ? color.aqua : na : color.red)
plot(basis, color=cbmaColor)
plot(sma, color=color.orange)
p1 = plot(upper, color=color.blue)
p2 = plot(lower, color=color.blue)
fill(p1, p2)

redCandleDirection = ((close[0] < open[0]) and (close[1] < open[1]) and (close[2] < open[2]) and (close[3] < open[3]) and (close[4] < open[4]))
isRsiLow = (d <= 10)
isUnderBollinger = (crossover(src, lower))
entryLong = false


isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger) =>
    if (redCandleDirection)
        if (isRsiLow)
            if (isUnderBollinger)
                entryLong = true
            else
                entryLong = false
        else
            entryLong = false
    else
        entryLong = false
    entryLong



if isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger)
    strategy.entry(id="Entry Long", long=true)
    strategy.exit(id="Exit Long", from_entry="Entry Long", qty_percent=100, limit=sma, stop=close[1])

1 Ответ

0 голосов
/ 28 мая 2020

Это должно помочь вам начать работу. Вы не можете использовать оператор = в локальном блоке для переназначения значения переменной, в противном случае вы создаете новый локальный экземпляр этой переменной. Вместо этого необходимо использовать :=.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PatrickML
// @ ChuckBanger

//@version=4
strategy(title="Bollinger Bands w/ Limit and Trailing Stop", shorttitle="BB w/ L + TS", 
   overlay=true,  pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=50, initial_capital=1000.0)


length = input(title="Length", type=input.integer, defval=20, minval=1)
regular = input(title="Regular BB Or CBMA?", type=input.bool, defval=true)
src = input(title="Source", type=input.source, defval=close)
mult = input(title="Multipler", type=input.float, defval=2.3, minval=.001, maxval=50, step=.1)
emaLen = input(title="EMA Length", type=input.integer, defval=20, minval=1)
emaGL = input(title="EMA Gain Limit", type=input.integer, defval=50, minval=1)
highlight = input(title="Highlight On/Off", type=input.bool, defval=true)

smaLen = input(title="SMA Length", type=input.integer, defval=9, minval=1)
sma = sma(close, smaLen)

direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)

strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))

//strategy.risk.max_drawdown(50, strategy.percent_of_equity)


///STOCH.RSI///
smoothK = input(3, minval=1, title="Stochastic %K Smoothing")
smoothD = input(3, minval=1, title="Stochastic %K Moving Average")
lengthRSI = input(14, minval=1, title="RSI Length")
lengthStoch = input(14, minval=1, title="Stochastic Length")
RSIprice = close
rsi1 = rsi(RSIprice, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)


calc_hma(src, length) =>
    hullma = wma(2*wma(src, length/2)-wma(src, length), round(sqrt(length)))
    hullma

calc_cbma(price, length, emaLength, emaGainLimit) =>
    alpha = 2 / (emaLength + 1)
    ema = ema(price, emaLength)
    int leastError = 1000000

    float ec = 0
    float bestGain = 0

    for i = emaGainLimit to emaGainLimit
        gain = i / 10
        ec := alpha * ( ema + gain * (price - nz(ec[1])) ) + (1 - alpha) * nz(ec[1])
        error = price - ec
        if (abs(error) < leastError)
            leastError := int(abs(error))
            bestGain := gain

    ec := alpha * ( ema + bestGain * (price - nz(ec[1])) ) + (1 - alpha) * nz(ec[1])
    hull = calc_hma(price, length)

    cbma = (ec + hull) / 2
    cbma

cbma = calc_cbma(src, length, emaLen, emaGL)
basis = regular ? sma(src, length) : cbma
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
cbmaColor = fixnan(highlight and not regular ? cbma > high ? color.purple : cbma < low ? color.aqua : na : color.red)
plot(basis, color=cbmaColor)
plot(sma, color=color.orange)
p1 = plot(upper, color=color.blue)
p2 = plot(lower, color=color.blue)
fill(p1, p2)

redCandleDirection = ((close[0] < open[0]) and (close[1] < open[1]) and (close[2] < open[2]) and (close[3] < open[3]) and (close[4] < open[4]))
isRsiLow = (d <= 10)
isUnderBollinger = (crossover(src, lower))


isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger) =>
    _entryLong = false
    if (redCandleDirection)
        if (isRsiLow)
            if (isUnderBollinger)
                _entryLong := true
            else
                _entryLong := false
        else
            _entryLong := false
    else
        _entryLong := false
    _entryLong



if isLongEntry(redCandleDirection, isRsiLow, isUnderBollinger)
    strategy.entry(id="Entry Long", long=true)
    strategy.exit(id="Exit Long", from_entry="Entry Long", qty_percent=100, limit=sma, stop=close[1])
...