Peri Bollinger Mean Reversion V2

thechadyogi · strategy · 58 行 · 点赞 694 · TradingView 原页

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

Pine Script

//@version=6
strategy("Peri Bollinger Mean Reversion V2", overlay = true)
 
//──────────────────────
// Inputs
//──────────────────────
source         = input.source(close,  "Source")
length         = input.int(20,        "BB Length",          minval = 1)
mult           = input.float(2.0,     "BB Multiplier",      minval = 0.001, maxval = 50.0)
rsiOversold    = input.int(30,        "RSI Oversold Level", minval = 1,     maxval = 50)
rsiOverbought  = input.int(70,        "RSI Overbought Level", minval = 50,  maxval = 99)
 
//──────────────────────
// Bollinger Bands
//──────────────────────
basis = ta.sma(source, length)
dev   = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
 
//──────────────────────
// RSI
//──────────────────────
strength = ta.rsi(close, 14)
 
//──────────────────────
// Plots (default colors)
//──────────────────────
pbasis = plot(basis, "Middle BB")          // default color
p1     = plot(upper, "Upper BB")           // default color
p2     = plot(lower, "Lower BB")           // default color
fill(p1, p2)                               // default fill color
 
//──────────────────────
// Position sizing
//──────────────────────
amount = 0.1 * (strategy.initial_capital + strategy.netprofit)
units  = math.floor(amount / close)
 
//──────────────────────
// Entry / Exit Logic
//──────────────────────
 
// LONG: price below lower band AND RSI in oversold zone
if (close < lower and strength < rsiOversold and strategy.position_size < units * 5)
    strategy.order("Long", strategy.long, units)
 
// CLOSE LONG: price above upper band AND RSI recovered above 50
if (close > upper and strength > 50 and strategy.position_size > 0)
    strategy.close("Long", comment = "Close Long")
 
// SHORT: price above upper band AND RSI in overbought zone
if (close > upper and strength > rsiOverbought and strategy.position_size > -units * 5)
    strategy.order("Short", strategy.short, units)
 
// CLOSE SHORT: when price touches or goes below the middle SMA (basis)
if (strategy.position_size < 0 and close <= basis)
    strategy.close("Short", comment = "Close Short")

← 返回列表