简体   繁体   中英

RSI Strategy - Problem with Strategy.Entry/exit

So I'm at the end of creating my first Strategy in tradingview and so far it plots the objects right but when i'm trying to use the entry/exit function it dosen't work as I want it to. 在此处输入图像描述

//@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)

So my biggest problem now is that the exit function dosen't work and I have no idé why it dosen't work.

My other problem is that all entrés are offset by one candle and I don't know why.

As of now I have no good reslust fro the strategy tester when It triggers everything wrong.

All the help I can get is appreciated

You didn't explain the detail of the required entry/exit conditions, so went with a guess.

  1. process_orders_on_close = true is used in the strategy() call to execute orders on bar close when possible.
  2. default_qty_type = strategy.percent_of_equity, default_qty_value = 100 is used because fixed number of contracts produce unrealistic results, as trade are entered even when the required funds are not available.
  3. Upon an entry signal, the exit order on max loss is also entered using strategy.exit() . The loss amount can be changed through Inputs .
  4. Entry signals in one direction close an open trade in the opposite direction.
  5. Upon exit signal, a trade exit is forced using strategy.close() .
  6. Entry/Exit condition markers were changed to show simultaneous conditions.
//@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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM