首页 > 解决方案 > 带有止损买单的 RSI 指标

问题描述

我了解当价格高于当前市场价格时会出现买入止损。

我需要了解为什么止损单会出现在这里。我不太明白 RSI 信号指标如何影响买入止损?

这是代码

//if above 70 , overbought , if below 30, then oversold
strategy(title="Stop Order RSI Strategy", shorttitle="Stop Order RSI", format=format.price, precision=2)

//Inputs
i_oversold = input(30 , title="Oversold")
i_overbought = input(70 , title="Overbought")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)

//Calculations
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//Plotting
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

goLong = rsi < i_oversold
goShort = rsi > i_overbought
strategy.entry("Buy Stop" , stop=high , when = goLong , long = true)
strategy.entry("Sell Stop" , stop=low , when = goShort , long = strategy.short)

在此处输入图像描述

感谢有人能花时间解释这里发生了什么,因为我很迷茫。

标签: pine-scriptalgorithmic-tradingtrading

解决方案


您的脚本行为正确。
这是我对您的代码的再现,添加//@version=4为第一行,最后添加了一个调试部分。

//@version=4
//if above 70 , overbought , if below 30, then oversold
strategy(title="Stop Order RSI Strategy", shorttitle="Stop Order RSI", format=format.price, precision=2)

//Inputs
i_oversold = input(30 , title="Oversold")
i_overbought = input(70 , title="Overbought")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)

//Calculations
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//Plotting
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

goLong = rsi < i_oversold
goShort = rsi > i_overbought
strategy.entry("Buy Stop" , stop=high , when = goLong , long = true)
strategy.entry("Sell Stop" , stop=low , when = goShort , long = strategy.short)

// === DEBUG ===
plotchar(rsi, "rsi", "")
plotchar(i_oversold, "i_oversold", "")
plotchar(i_overbought, "i_overbought", "")
plotchar(goLong, "goLong", "")
plotchar(goShort, "goShort", "")

当您将鼠标悬停在某个栏上时,调试部分会在数据窗口中显示该栏的变量值,如下所示: 数据窗口

11 Mar '20你看那goLong0

12 Mar '20你看goLong就变成1了。
这意味着 Pine 将在下high一个柱上做多,在当前柱的位置设置止损单。
因此,它希望在 高位继续13 Mar '2016.642745212 Mar '20
在 上13 Mar '20,永远不会达到这个止损价,所以它不会做多。

就那一样13 Mar '20,你看那goLong还是1
重复相同的过程。Pine 将在下high一个柱上做多,在当前柱的位置设置止损单。因此,它希望在 高位继续14 Mar '2012.371869913 Mar '20
同样,在 上14 Mar '20,这个止损价格永远不会达到,所以它不会做多。

14 Mar '20了,你看那goLong还是1
同样的过程再次重复。Pine 将在下high一个柱上做多,在当前柱的位置设置止损单。因此,它希望在 高位继续15 Mar '2011.439280214 Mar '20
这一次,在 上15 Mar '20,达到此止损价,系统进入多头头寸。


推荐阅读