Dual Bollinger Band Mean Reversion

pb-algo · study · 80 行 · 点赞 251 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pb-algo
 
//@version=5
indicator(shorttitle="BBMR", title="Dual Bollinger Band Mean Reversion", overlay=true, max_labels_count = 500)
 
length = input.int(20, minval=1)
length2 = input.int(400, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
basis2 = ta.sma(src, length2)
dev = mult * ta.stdev(src, length)
dev2 = mult * ta.stdev(src, length2)
upper = basis + dev
lower = basis - dev
upper2 = basis2 + dev2
lower2 = basis2 - dev2
 
p1 = plot(upper, "Upper", color=color.new(color.yellow, 80))
p2 = plot(lower, "Lower", color=color.new(color.yellow, 80))
p3 = plot(upper2, "Upper", color=color.new(color.white, 80))
p4 = plot(lower2, "Lower", color=color.new(color.white, 80))
 
cond1 = false
cond2 = false
var cond3 = false
 
var float candleHigh1 = 0
var float candleLow1 = 0
var float candleLevel1 = 0
var float stopLevel1 = 0
var float targetLevel1 = 0
var label orderLabel = na
 
var orderCount = 0
var winCount = 0
var lossCount = 0
 
if barstate.isconfirmed
    if close < lower 
        candleHigh1 := high
        candleLow1 := low
        candleLevel1 := candleLow1 + ((candleHigh1-candleLow1)/2)
        cond1 := true
    if cond1[1] and close > lower 
        cond2:= true
    if close < basis2
        cond2:= false
        
    
    if cond2[1] and close > basis2
        if not cond3
            stopLevel1 := open + (open-upper)
            targetLevel1 := upper[1]
            cond3:= true
        if orderCount == 0
            orderLabel := label.new(bar_index, open, size=size.tiny, style=label.style_label_left, color = color.yellow)
            orderCount := orderCount +1
            // For backtest uncomment these two lines and change "indicator" to "strategy" on line 2.
            // strategy.entry("Long", strategy.long)
            // strategy.exit("Close Long", "Long", stop=stopLevel1, limit=targetLevel1)
 
    if cond3 and low< stopLevel1
        winCount := winCount + orderCount
        label.new(bar_index, high, "L: " + str.tostring(math.round(lossCount/winCount,2)) , color=color.red)
        cond3:=false
        orderCount := 0
    
    if cond3 and high > targetLevel1
        lossCount := lossCount + orderCount
        label.new(bar_index, high, "W: " + str.tostring(math.round(lossCount/winCount,2)) , color=color.green)
 
        cond3:=false
        orderCount := 0
 
plot(stopLevel1, color = color.white, style = plot.style_stepline)
plot(targetLevel1, color = color.white, style = plot.style_stepline)
 
plot(basis2, color = color.yellow, style = plot.style_stepline)

← 返回列表