繁体   English   中英

RSI 策略 - 策略问题。进入/退出

[英]RSI Strategy - Problem with Strategy.Entry/exit

因此,我即将在交易视图中创建我的第一个策略,到目前为止,它正确地绘制了对象,但是当我尝试使用进入/退出 function 时,它并没有按我的意愿工作。 在此处输入图像描述

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

所以我现在最大的问题是出口 function 不起作用,我不知道为什么它不起作用。

我的另一个问题是所有入口都被一根蜡烛抵消了,我不知道为什么。

到目前为止,当它触发一切错误时,我对策略测试器没有很好的抵抗力。

感谢我能得到的所有帮助

您没有解释所需进入/退出条件的详细信息,因此猜测。

  1. process_orders_on_close = true用于strategy()调用以尽可能在柱线收盘时执行订单。
  2. 使用default_qty_type = strategy.percent_of_equity, default_qty_value = 100 ,因为固定数量的合约会产生不切实际的结果,因为即使在所需资金不可用的情况下也可以进行交易。
  3. 在输入信号时,最大损失的退出订单也使用strategy.exit()输入。 损失金额可以通过Inputs更改。
  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)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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