Стратегия RSI - проблема со стратегией. Вход / выход - PullRequest
0 голосов
/ 08 мая 2020

Итак, я заканчиваю создание своей первой стратегии в tradingview, и пока она правильно отображает объекты, но когда я пытаюсь использовать функцию входа / выхода, она работает не так, как я хочу. enter image description here

//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mattehalen


strategy("Mathias & Christer Timeframe RSI", shorttitle="M&C_RSI",overlay=true)
len = input(7, title="Length", type=input.integer)
src = input(close, title="Source", type=input.source)
show4h = input(true, title="show 4h", type=input.bool)

rsiCurrent = rsi(src, len)
rsi4h = security(syminfo.ticker, "240", rsi(src, len))

//--------------------------------------------------
//MA
trendMA = ema(close,200)
shortMA = ema(close,50)
longMA  = ema(close,20)
plot(trendMA, color=color.black, linewidth=5)
plot(shortMA, color=color.red, linewidth=2)
plot(longMA, color=color.green, linewidth=2)
bgcolor(crossunder(longMA,shortMA) ? color.black : na, transp=10)


//--------------------------------------------------
//RSI
BuySignalBarssince = barssince(rsi4h[1]<rsi4h[0] and rsi4h[1]<20)
BuySignal       = (rsi4h[1]<rsi4h[0] and rsi4h[1]<20 and BuySignalBarssince[1]>10)
BuySignalOut   = crossunder(shortMA,longMA)
bgcolor(BuySignal ? color.green : na, transp=70)
bgcolor(BuySignalOut ? color.green : na, transp=10)



SellSignalBarssince = barssince(rsi4h[1]>rsi4h[0] and rsi4h[1]>80)
SellSignal      = (rsi4h[1]>rsi4h[0] and rsi4h[1]>80 and SellSignalBarssince[1]>10)
SellSignalOut   = crossunder(longMA,shortMA)
bgcolor(SellSignal ? color.red : na, transp=70)
bgcolor(SellSignalOut ? color.red : na, transp=10)

strategy.entry("short", false, 10000, when = SellSignal)
strategy.exit("exit", "short", when = SellSignalOut, loss = 5000)
strategy.entry("long", true, 10000, when = BuySignal)
strategy.exit("exit", "long", when = BuySignalOut, loss = 5000)

Итак, моя самая большая проблема сейчас в том, что функция выхода не работает, и я не знаю, почему она не работает.

Другая моя проблема в том, что все входы компенсируются одной свечой, и я не знаю почему.

На данный момент у меня нет хорошего отклика на тестер стратегий, когда он все запускает неправильно. Я благодарен за любую помощь, которую я могу получить

1 Ответ

1 голос
/ 08 мая 2020

Вы не объяснили детали требуемых условий входа / выхода, поэтому сделали предположение.

  1. process_orders_on_close = true используется в вызове strategy() для исполнения ордеров при закрытии бара когда возможно.
  2. default_qty_type = strategy.percent_of_equity, default_qty_value = 100 используется, потому что фиксированное количество контрактов дает нереалистичные c результаты, так как сделки открываются, даже когда требуемые средства недоступны.
  3. После сигнала входа, ордер на выход по максимальному убытку также вводится с использованием strategy.exit(). Сумма убытка может быть изменена через Входы .
  4. Сигналы входа в одном направлении закрытие открытой сделки в противоположном направлении.
  5. По сигналу выхода принудительный выход из сделки с использованием strategy.close().
  6. Маркеры условий входа / выхода были изменены для отображения одновременных условий.
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mattehalen

strategy("Mathias & Christer Timeframe RSI", shorttitle="M&C_RSI",overlay=true, process_orders_on_close = true, default_qty_type =  strategy.percent_of_equity, default_qty_value = 100)
len = input(7, title="Length", type=input.integer)
src = input(close, title="Source", type=input.source)
show4h = input(true, title="show 4h", type=input.bool)
maxLoss = input(2000)

rsiCurrent = rsi(src, len)
rsi4h = security(syminfo.ticker, "240", rsi(src, len))

//--------------------------------------------------
//MA
trendMA = ema(close,200)
shortMA = ema(close,50)
longMA  = ema(close,20)
plot(trendMA, color=color.black, linewidth=5)
plot(shortMA, color=color.red, linewidth=2)
plot(longMA, color=color.green, linewidth=2)
bgcolor(crossunder(longMA,shortMA) ? color.silver : na, transp=10)

//--------------------------------------------------
//RSI
BuySignalBarssince = barssince(rsi4h[1]<rsi4h[0] and rsi4h[1]<20)
BuySignal       = (rsi4h[1]<rsi4h[0] and rsi4h[1]<20 and BuySignalBarssince[1]>10)
BuySignalOut   = crossunder(shortMA,longMA)

SellSignalBarssince = barssince(rsi4h[1]>rsi4h[0] and rsi4h[1]>80)
SellSignal      = (rsi4h[1]>rsi4h[0] and rsi4h[1]>80 and SellSignalBarssince[1]>10)
SellSignalOut   = crossunder(longMA,shortMA)

if BuySignal
    strategy.close("short", comment = "Exit short")
    strategy.entry("long", true)
    strategy.exit("Max Loss", "long", loss = maxLoss)
if BuySignalOut
    strategy.close("long", comment = "Exit Long")

if SellSignal
    // Enter trade and issue exit order on max loss.
    strategy.close("long", comment = "Exit Long")
    strategy.entry("short", false)
    strategy.exit("Max Loss", "short", loss = maxLoss)
if SellSignalOut
    // Force trade exit.
    strategy.close("short", comment = "Exit short")

plotchar(BuySignal, "BuySignal", "►", location.top, color.lime)
plotchar(BuySignalOut, "BuySignalOut", "◄", location.top, color.lime)
plotchar(SellSignal, "SellSignal", "►", location.bottom, color.red)
plotchar(SellSignalOut, "SellSignalOut", "◄", location.bottom, color.red)

enter image description here

...