RSI Trend Reversal

joseph_lemery · strategy · 81 行 · 点赞 2,369 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
strategy("RSI Trend Reversal", overlay=true, max_bars_back = 4999, calc_on_every_tick = false)
 
 
//INPUTS
rsi_length = input.int(title = "Lenght", defval = 8)
rsi_mult = input.float(title = "Multiplier", defval = 1.5, step = .05)
lookback = input.int(title = "Delay to prevent idealization", defval = 1)
sltp = input.float(title = "Minimum Difference", defval = 10)
src = input.source(title = "Source Input", defval = close)
 
//PARAMETERS INITILIZATION
hclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, src)
//FUNCTION INITILIZATION
highest_custom(src, length) =>
    x = src
    for i = 0 to length
        if src[i] > x
            x := src[i]
    x
lowest_custom(src, length) => 
    x = src
    for i = 0 to length
        if src[i] < x
            x := src[i]
    x
rsilev(src, length, mult, sltp) =>
    sl = (100 - sltp) / 100
    tp = (100 + sltp) / 100
    var bool crossup = na
    var bool crossdown = na
    var float dir = na
    dir_change = ta.change(dir)
    var float BearGuy = 0
    BullGuy = ta.barssince(crossup or crossdown)
    if na(BullGuy)
        BearGuy += 1
    else
        BearGuy := BullGuy
    var float upper = na
    var float lower = na
    rsilower = ta.rsi(src, length)
    rsiupper = math.abs(ta.rsi(src, length) - 100)
    atr = ta.atr(length) / src
    lower := highest_custom(math.max(highest_custom(highest_custom(src, BearGuy) * (1 - (atr + ((1 / (rsilower) * mult)))), BearGuy), src * sl), BearGuy)
    upper := lowest_custom(math.min(lowest_custom(lowest_custom(src, BearGuy) * (1 + (atr + ((1 / (rsiupper) * mult)))), BearGuy), src * tp), BearGuy)
    var float thresh = na
    if na(thresh)
        thresh := lower
    if na(dir)
        dir := 1
    if crossdown
        dir := -1
    if crossup
        dir := 1
    if dir == 1
        thresh := lower
    if dir == -1
        thresh := upper
    crossup := ta.crossover(src, thresh)
    crossdown := ta.crossunder(src, thresh)
    thresh
 
rsiclose = rsilev(hclose, rsi_length, rsi_mult, sltp)
 
//PLOTTING
var color col = color.lime
if hclose > rsiclose
    col := color.lime
if hclose < rsiclose
    col := color.red
plot(rsiclose, linewidth = 2, color = col)
 
//STRATEGY
buy = ta.crossover(hclose, rsiclose)
sell = ta.crossunder(hclose, rsiclose)
 
if buy[lookback]
    strategy.entry("long", strategy.long)
if sell[lookback]
    strategy.entry("Short", strategy.short)

← 返回列表