Strategy · Pine Script v4 · Own pane

Bollinger Bands and RSI Mix with DCA

by hasnocool· code credited to lagobrian23

This strategy combines Stochastic RSI and a normalized Bollinger Bands calculation into a composite oscillator named MIX, plotted in a separate pane.

View on GitHub

Repaints

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code80
Settings21inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 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

80 lines of Pine Script v4 from Bollinger Bands and RSI mix with DCA.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Bollinger Bands and RSI mix with DCA.pine
// © lagobrian23
//@version=4
strategy(title = 'Bollinger Bands and RSI mix with DCA', shorttitle = 'BB/RSI with DCA',pyramiding = 20, calc_on_every_tick = true, process_orders_on_close=true,overlay = false )
source=close
smoothK = input(3, "K", minval=1)
smoothD = input(3, "D", minval=1)
lengthRSI = input(14, "RSI Length", minval=1)
lengthStoch = input(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
// Bollinger Band
length = input(20,title = 'BB lookback length', minval=1)
mult = input(2.0, minval=0.001, maxval=50, title="StdDev")
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
BBval = (src - basis)/dev*30+50
offset = input(0, title = "Offset", type = input.integer, minval = -500, maxval = 500)
mix=(d + BBval)/2
//plot
//plot(k, "K", color=#606060)
plot(BBval, "BBval", color=#872323, offset = offset)
plot(d, "D", color=#FF6A00)
h0 = hline(80, "Upper Band", color=#606060)
h1 = hline(20, "Lower Band", color=#606060)
plot(mix, "MIX", color=#888888, linewidth=3)
//background MIX
bgcolor(mix < 20 ? color.green : color.white, transp=50)
bgcolor(mix > 80 ? color.red : color.white, transp=50)
// Choosing the date range
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 1970)
toMonth = input(defval = 10,    title = "To Month",      type = input.integer, minval = 1, maxval = 12)
toDay   = input(defval = 1,    title = "To Day",        type = input.integer, minval = 1, maxval = 31)
toYear  = input(defval = 2020, title = "To Year",       type = input.integer, minval = 1970)
start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(toYear, toMonth, toDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false
// Initializing the strategy paraeters
P = input(defval = 1, title = 'Amount (P)' , type = input.integer, minval = 1, maxval = 100)
X = input(defval = 2, title = '% Price drop for consecutive entries(X)', type = input.float, minval = 1, maxval = 100)
B_tp = input(defval = 3, title = '% Level for Take Profit (B)', type = input.float , minval = 1, maxval = 100)
D_sl = input(defval = 4, title = '% Level for Stop Loss (D)', type = input.float, minval = 1, maxval = 100)
A = input(defval = 4, title = 'Max consecutive entries (A)', type = input.integer, minval = 2, maxval = 20)
Z = input(defval = 1, title = 'Z', type = input.float , minval = 0, maxval = 10)
P2 = input(defval = 0, title = 'Amount (P2)' , type = input.float, minval = 0, maxval = 100)
// Declaring key DCA variables
entry_price = 0.0
entry_price := na(entry_price[1]) ? na : entry_price[1]
new_entry = 0.0
// Implementing the strategy
longEntry = crossover(mix,20)
exitLongs = crossunder(mix,80)
if(longEntry)
    entry_price := close
    strategy.entry('main_LE', strategy.long, P, when = window() and longEntry)
// close all if exit conditions are met
// Consecutive entries upto A times
strategy.risk.max_intraday_filled_orders(A)
main_stop = strategy.position_avg_price - strategy.position_avg_price*(D_sl/100)
main_takeprofit = strategy.position_avg_price + strategy.position_avg_price*(B_tp/100)
slCondition = close <= main_stop
tpCondition = close >= main_takeprofit
exitConditions = exitLongs or slCondition or tpCondition
//Dollar-Cost-Averaging
// Enter long whenever price goes down X%: amount set to (P+Y)*Z
newAmount = ceil(Z*(P + P2))
// calcualte DCA price 
dcaCond = strategy.opentrades < A and strategy.opentrades > 0
if(dcaCond  and not exitConditions)
    new_entry := entry_price - ((X/100)*entry_price)
    strategy.entry('consec_LE', strategy.long, newAmount, limit = new_entry, when = window())
    entry_price := new_entry
// The main trade is closed only when the  main exit conditions are satisfied
strategy.close_all(exitConditions)
Expand (93 lines)

Overview

Long positions are entered when the MIX line crosses above the oversold threshold of 20. The strategy utilizes a dollar-cost averaging approach, placing additional entries when price drops by a specified percentage up to a maximum entry count. Key settings allow customization of oscillator lengths, band standard deviations, DCA spacing, and profit targets. The strategy repaints on live bars due to tick-level calculations.

Details

Published by

hasnocool on GitHub

Code credit

lagobrian23, as written in the script's header

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Volatility & bands, Mean reversion

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
Kint3smoothK
Dint3smoothD
RSI Lengthint14lengthRSI
Stochastic Lengthint14lengthStoch
RSI Sourcesourceclosesrc
BB lookback lengthint20length
StdDevfloat2.0mult
Offsetint0offset
From Monthint1fromMonth
From Dayint1fromDay
From Yearint2020fromYear
To Monthint10toMonth
Show all 21 settings
SettingTypeDefaultVariable
To Dayint1toDay
To Yearint2020toYear
Amount (P)int1P
% Price drop for consecutive entries(X)float2X
% Level for Take Profit (B)float3B_tp
% Level for Stop Loss (D)float4D_sl
Max consecutive entries (A)int4A
Zfloat1Z
Amount (P2)float0P2
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 strategy combines Larry Williams' Vix Fix synthetic volatility indicator with Bollinger Bands. It calculates standard and inverted Williams Vix Fix metrics, displaying them as histogram columns with standard deviation bands in a separate pane. Long entrie

Strategy v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Williams Vix Fix Strategy [Alorse]

Greeffer BB Strategy

by SammyEnigma
18 stars

This strategy fades Bollinger Band extremes using the close. It enters short when price crosses above the upper band and the 14-period RSI is above 70, and enters long when price crosses below the lower band and RSI is below 30. It closes half of an open short

Strategy v4 Volatility & bandsOscillators & momentum
Updated 6 years ago · None stated GitHub repository of Greeffer BB Strategy
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy
13 stars

This strategy overlays the price chart to trade DAX futures during European market hours. It calculates exponential and simple moving averages, RSI, MACD, Bollinger Bands, ATR, and realized volatility to simulate VIX levels. Trades execute when session time fi

Strategy v5 Sessions & timeOscillators & momentum
Updated 8 months ago · Custom licence GitHub repository of ClaudeHedge - DAX Session Strategy

Calculates a combination of the Relative Strength Index, MACD histogram, and Bollinger Bands contraction to identify long trade entries. The strategy signals a long position on a bullish bar when the MACD histogram turns upward while negative, RSI remains belo

Strategy v4 Oscillators & momentumVolatility & bands
Updated 3 years ago · Apache-2.0 GitHub repository of RSI + MACD + BB Long Strategy (Not Good)

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