Strategy · Pine Script v5 · On the chart

周末布林带策略

by shaoslu· code credited to openbox6

This strategy plots Bollinger bands and a simple moving average on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code71
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago9 May 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

71 lines of Pine Script v5 from strategy/周末震荡策略.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

周末震荡策略.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © openbox6
//@version=5
import openbox6/zhuang/29 as lz
strategy("周末布林带策略",
         overlay=true,
         initial_capital=4000,
         default_qty_type=strategy.percent_of_equity,
         backtest_fill_limits_assumption=1,
         default_qty_value=100,
         process_orders_on_close=true,
         commission_value = 0.04)

// 输入参数
input_leverage = input.float(1.5, title = "杠杆倍数", step = 0.5)
startPeriodTime = input.time(timestamp('1 Jan 2021'), '开始', group='交易日期范围', inline='开始时间')
endPeriodTime = input.time(timestamp('31 Dec 2099'), '结束', group='交易日期范围', inline='结束时间')
isStartPeriodEnabledAndInRange = startPeriodTime <= time
isEndPeriodEnabledAndInRange = endPeriodTime >= time
// 交易时间段
isInRange = isStartPeriodEnabledAndInRange and isEndPeriodEnabledAndInRange
// 初始化上一次出场的bar索引
var lastExitBar = 0
// 止损百分比
st_pt_percent = 0.02
// 止盈百分比
st_p_percent = 0.02

// ##############################################################
// 布林带参数
bullLength = input.int(25, title = '布林带长度')
bullMulti = input.float(2.0, title = '布林带倍数')

// 布林带
[basis, upper, lower] = lz.getBooling(close, bullLength, bullMulti)
plot(basis, title = "中轨")
plot(upper, title = "上轨")
plot(lower, title = "下轨")

maLength = input.int(89, title = '趋势平均线长度')
ma200 = ta.sma(close, maLength)
plot(ma200, title = '趋势平均线', color = color.yellow, linewidth = 2)

if ta.crossover(close, upper) and (dayofweek == 7 or dayofweek == 1) and isInRange and strategy.position_size == 0 and (bar_index - lastExitBar) > 10 and close < ma200
    lastExitBar := bar_index
    strategy.entry('做空', strategy.short, qty = input_leverage * strategy.equity / close[0])
    

if ta.crossunder(close, lower) and (dayofweek == 7 or dayofweek == 1) and isInRange and strategy.position_size == 0 and (bar_index - lastExitBar) > 10 and close > ma200
    lastExitBar := bar_index
    strategy.entry('做多', strategy.long, qty = input_leverage * strategy.equity / close[0])
    



//strategy.exit('做空止损', '做空', stop = close * (1 + st_pt_percent), limit = close * (1 - st_p_percent), comment_profit='做空止盈', comment_loss='做空止损')

//strategy.exit('做多止损', '做多', stop = close * (1 - st_pt_percent), limit = close * (1 + st_p_percent), comment_profit='做多止盈', comment_loss='做多止损')



strategy.exit('做多平仓', '做多', stop = close * (1 - st_pt_percent), trail_points = close * st_p_percent / syminfo.mintick, trail_offset = close * 0.001 / syminfo.mintick)

strategy.exit('做多平仓', '做空', stop = close * (1 + st_pt_percent), trail_points = close * st_p_percent / syminfo.mintick, trail_offset = close * 0.001 / syminfo.mintick)


// if strategy.position_size > 0 and close >= upper
//     strategy.close('做多', comment = '做多平仓')

// if strategy.position_size < 0 and close <= lower
//     strategy.close('做空', comment = '做空平仓')

Overview

周末布林带策略 opens a short when price crosses above the upper band while below the average, and a long when price crosses below the lower band while above the average. Entries are allowed only on Saturday or Sunday, inside a chosen date range, and only when the position is flat for more than ten bars. Size scales with an input leverage and equity. Exits use a two percent stop plus a trailing offset. Fixed take-profit orders in the source are commented out.

Details

Published by

shaoslu on GitHub

Code credit

openbox6, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Moving averages, Mean reversion, Sessions & time

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
杠杆倍数float1.5input_leverage
开始timetimestamp('1 Jan 2021')startPeriodTime
结束timetimestamp('31 Dec 2099')endPeriodTime
布林带长度int25bullLength
布林带倍数float2.0bullMulti
趋势平均线长度int89maLength
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This overlay strategy offers two selectable entry modes: a Bollinger squeeze on typical price and a simple moving-average cross. It filters trades by weekday, session, date range, and an optional news window, and can restrict direction or invert the side. Risk

Strategy v5 Volatility & bandsMoving averages
Updated today · None stated GitHub repository of Boilerplate Configurable Strategy [Yosiet]
218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template
106 stars

This strategy backtests a long Bollinger Band deal on the price chart for 3Commas-style bots. It plots short and long simple moving averages, offset bands, and the average entry price of the open deal. A long starts when price crosses the lower band inside a c

Strategy v4 Volatility & bandsMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 3Commas Bollinger Strategy

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers