UT Bot Stochastic RSI

Rajjariorr · study · 121 行 · 点赞 1,763 · TradingView 原页

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

Pine Script

//@version=5
indicator(title='UT Bot Stochastic RSI', overlay=true)
 
atrSensitivity = input(2, title='Atr sensitivity') 
atrPeriod = input(3, title='ATR Period')
isHeikin = input(false, title='Signals from Heikin Ashi Candles')
emaFastPeriod = input(9, title='Fast EMA Period')
emaSlowPeriod = input(20, title='Slow EMA Period')
showBacktest = input(true, title='Show backtest result')
backtestDateStart = input.time(title="Backtest Start Date", defval=timestamp("2000-01-01"))
backtestDateEnd = input.time(title="Backtest End Date", defval=timestamp("2050-01-01"))
showBuySell = input(true, title='Show Buy/Sell signal')
useRSI = input(true, title='Use RSI')
useStochastic = input(true, title='Use Stochastic')
 
 
calculateATRTrailingStop(src, atrSensitivity, atrPeriod) =>
    xATR = ta.atr(atrPeriod)
    nLoss = atrSensitivity * xATR
 
    xATRTrailingStop = 0.0
    iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
    iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
    xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
    xATRTrailingStop
 
getStochasticBuySignal(src, periodK, smoothK, periodD) =>
    var globalKCrossD = false
    // Create stochastic
    // k = green or up, d = red or down
    k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
    d = ta.sma(k, periodD)
 
    // k crosses d below 20
    if ta.crossover(k, d) and d <= 20
        globalKCrossD := true
 
    // If k crosses d below then set globalKCrossD = false
    if ta.crossover(d, k) and d>40
        globalKCrossD := false
    
    // stochasticBuySignal = k>d and d<60 and globalKCrossD
    stochasticBuySignal = k>d and d<60
 
getRsiBuySignal(src, rsiPeriod, emaPeriod) =>
    rsi = ta.rsi(src, rsiPeriod)
    emaRSI = ta.ema(rsi, emaPeriod)
 
    srcWeekly = request.security(syminfo.tickerid, "W", close)
    srcMonthly = request.security(syminfo.tickerid, "M", close)
    rsiWeekly = ta.rsi(srcWeekly, rsiPeriod)
    rsiMonthly = ta.rsi(srcMonthly, rsiPeriod)
    
    // rsiBuySignal = rsi>=50 and rsiWeekly>50 and rsiMonthly>50 and rsi>emaRSI and emaRSI > 40
    rsiBuySignal = rsi>=50 and rsi>emaRSI and emaRSI > 40
 
 
calculateSignals(src, atrSensitivity, atrPeriod, xATRTrailingStop, useRSI, useStochastic) =>
    ema = ta.ema(src, 1)
    buySignal = ta.crossover(ema, xATRTrailingStop)
    sellSignal = ta.crossover(xATRTrailingStop, ema)
 
    if useRSI and useStochastic
        log.info("useRSI" + str.tostring(useRSI))
        log.info("useStochastic" + str.tostring(useStochastic))
        buySignal := getRsiBuySignal(src, 14, 14) and getStochasticBuySignal(src, 14, 3, 3)
        log.info("BuySignal - inside double check: "+str.tostring(buySignal))
 
    else if useRSI
        buySignal := getRsiBuySignal(src, 14, 14)
    
    else if useStochastic        
        buySignal := getStochasticBuySignal(src, 14, 3, 3)
 
    log.info("BuySignal: final return: "+str.tostring(buySignal))
    buy = src > xATRTrailingStop and buySignal
    sell = src < xATRTrailingStop and sellSignal
    [buy, sell]
 
 
src = isHeikin ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close
xATRTrailingStop = calculateATRTrailingStop(src, atrSensitivity, atrPeriod)
plot(xATRTrailingStop, color = src>xATRTrailingStop ? color.green : color.red)
 
var buyCanBeSet = true
var sellCanBeSet = true
[buy, sell] = calculateSignals(src, atrSensitivity, atrPeriod, xATRTrailingStop, useRSI, useStochastic)
 
// It plots buy sell signal
plotshape(series=buy and buyCanBeSet and showBuySell, location=location.belowbar, color=color.green, text="buy", textcolor=color.white, style=shape.labelup)
plotshape(series=sell and sellCanBeSet and showBuySell, location=location.abovebar, color=color.red, text="exit", textcolor=color.white, style=shape.labeldown)
 
// Plot value of rsi
plot(ta.rsi(src, 14), title='Rsi(14)')
 
if buy and buyCanBeSet
    buyCanBeSet := false
    sellCanBeSet := true
 
if sell and sellCanBeSet
    sellCanBeSet := false
    buyCanBeSet := true
 
// Add fast ema
emaFast = ta.ema(src, emaFastPeriod)
plot(emaFast, color=color.purple, title="EMA Fast")
 
// Add slow ema
emaSlow = ta.ema(src, emaSlowPeriod)
plot(emaSlow, color=color.blue, title="EMA Slow")
 
enclosedArea = src > xATRTrailingStop ? (src < emaFast ? true : false) : (src > emaFast ? true : false)
bgcolor(enclosedArea ? (src > emaFast ? color.new(color.green, 90) : color.new(color.red, 90)) : na)
 
// Integration of backtest library
import DinGrogu/backtest/1 as backtest
 
initialCapital = 1000.00
if showBacktest
    [profitAndLoss, openBalance, winrate, entries, exits, wins, losses] = backtest.results(backtestDateStart, backtestDateEnd, buy, sell)
    backtest.table(initialCapital, profitAndLoss, openBalance, winrate, entries, exits, wins, losses)

← 返回列表