RSI 策略 - 策略问题。Entry/exit
RSI Strategy - Problem with Strategy.Entry/exit
所以我在 tradingview 中创建了我的第一个策略,到目前为止它绘制了正确的对象但是当我尝试使用 entry/exit 函数时它没有像我想要的那样工作它到。
//@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)
所以我现在最大的问题是退出功能不起作用,我不知道为什么它不起作用。
我的另一个问题是所有主菜都被一根蜡烛抵消了,我不知道为什么。
到目前为止,当策略测试器触发所有错误时,我对策略测试器没有很好的抵抗力。
感谢我能得到的所有帮助
您没有详细说明 entry/exit 条件,所以只能猜测。
process_orders_on_close = true
用于 strategy()
调用以在可能的情况下在收盘时执行订单。
使用 default_qty_type = strategy.percent_of_equity, default_qty_value = 100
是因为固定数量的合约会产生不切实际的结果,因为即使在所需资金不可用的情况下也会进行交易。
- 根据进入信号,最大损失的退出订单也使用
strategy.exit()
输入。可以通过 Inputs. 更改损失金额
- 一个方向的入市信号关闭相反方向的未平仓交易。
- 根据退出信号,使用
strategy.close()
. 强制退出交易
- Entry/Exit 条件标记已更改为显示同时发生的条件。
//@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)
所以我在 tradingview 中创建了我的第一个策略,到目前为止它绘制了正确的对象但是当我尝试使用 entry/exit 函数时它没有像我想要的那样工作它到。
//@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)
所以我现在最大的问题是退出功能不起作用,我不知道为什么它不起作用。
我的另一个问题是所有主菜都被一根蜡烛抵消了,我不知道为什么。
到目前为止,当策略测试器触发所有错误时,我对策略测试器没有很好的抵抗力。
感谢我能得到的所有帮助
您没有详细说明 entry/exit 条件,所以只能猜测。
process_orders_on_close = true
用于strategy()
调用以在可能的情况下在收盘时执行订单。
使用 default_qty_type = strategy.percent_of_equity, default_qty_value = 100
是因为固定数量的合约会产生不切实际的结果,因为即使在所需资金不可用的情况下也会进行交易。- 根据进入信号,最大损失的退出订单也使用
strategy.exit()
输入。可以通过 Inputs. 更改损失金额
- 一个方向的入市信号关闭相反方向的未平仓交易。
- 根据退出信号,使用
strategy.close()
. 强制退出交易
- Entry/Exit 条件标记已更改为显示同时发生的条件。
//@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)