Donchian Channel and Bollinger Bands

Bladeof300

New member
Has anyone had success shadowing Bollinger Bands with Donchian Channels? I tried today to write put a script together and while it works it isn't particularly clean. Can anyone code These two together?
Code:
#Bladeof300


input entryLength = 40;
input exitLength = 15;
input atrLength = 20;
input atrFactor = 0.9;
input atrStopFactor = 4.0;
input atrAverageType = AverageType.SIMPLE;
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;


def entryUpper = Highest(high, entryLength)[1];
def entryLower = Lowest(low, entryLength)[1];
def exitUpper = Highest(high, exitLength)[1];
def exitLower = Lowest(low, exitLength)[1];
def sDev = stdev(data = price[-displace], length = length);


def atr = reference ATR(length = atrLength, "average type" = atrAverageType);
def volatilityOk = TrueRange(high, low, close)[1] < atrFactor * atr[1];

def buyToOpen = (atrFactor == 0 or volatilityOk) and high > entryUpper;
def sellToOpen = (atrFactor == 0 or volatilityOk) and low < entryLower;
def buyToClose = high > exitUpper;
def sellToClose = low < exitLower;

def position = {default none, long, short};
position = if (buyToOpen or (position[1] == position.long and !sellToClose)) then position.long
    else if (sellToOpen or (position[1] == position.short and !buyToClose)) then position.short
    else position.none;

plot BuyStop;
plot CoverStop;
if (position[1] == position.short) {
    BuyStop = Double.NaN;
    CoverStop = exitUpper;
} else {
    BuyStop = entryUpper;
    CoverStop = Double.NaN;
}

plot ShortStop;
plot SellStop;
if (position[1] == position.long) {
    ShortStop = Double.NaN;
    SellStop = exitLower;
} else {
    ShortStop = entryLower;
    SellStop = Double.NaN;
}

BuyStop.SetDefaultColor(GetColor(3));
CoverStop.SetDefaultColor(GetColor(6));
ShortStop.SetDefaultColor(GetColor(4));
SellStop.SetDefaultColor(GetColor(5));


plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
AddCloud(UpperBand, LowerBand, Color.LIGHT_GRAY, Color.LIGHT_GRAY);

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
 

Bladeof300

New member
I did it with the help of rad14733. This shows fully customizable Donchian Channels, Bollinger Bands, and Keltner Channels. It also shows squeeze as a blue cloud when the Bollinger Band moves into the Keltner Channel

Code:
# Working example of how TTM_Squeeze works
# Coded by rad14733 for usethinkscript.com
# Edited by Bladeof300- added Donchian Channels, and made it more customizable.
# Original code created 2020-12-23


# Use reference calls to TOS BB's and plot midline and bands

# Working example of how TTM_Squeeze works
# Coded by rad14733 for usethinkscript.com
# Original code created 2020-12-23


# Use reference calls to TOS BB's and plot midline and bands

input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
#Donchian Inputs
input length_used_for_calc = 15;
input Use_Other_Time_Frame = yes;
input Time_Frame = AggregationPeriod.DAY;

#Shift is the same as displace, but shift is for the Donchian Channels
input shift = 1;

#Donchian Code

def h = if Use_Other_Time_Frame then high(period = Time_Frame) else high;
def l = if Use_Other_Time_Frame then low(period = Time_Frame) else low;
def highline = Highest(high, length_used_for_calc);
def lowline = Lowest(low, length_used_for_calc);

plot High_Line = (highline[shift]);
High_Line.SetDefaultColor(Color.YELLOW);
Plot Low_Line=(lowline[shift]);
Low_Line.SetDefaultColor(Color.YELLOW);

#BollingerCode

plot bb_midline = BollingerBands().MidLine;
bb_midline.SetDefaultColor(Color.GRAY);

# Tried using gradient colors to help indicate when a squeeze may occur(I recommend using OBV paired w/ a short exp moving average to predict this instead if they trend the same way a breakout should follow)
# AssignNormGradientColor(length, Color.BLACK, Color.GRAY);
# AssignNormGradientColor(length, Color.GRAY, Color.BLACK);

# for selectable color use this SetDefaultColor(Color.GRAY);

plot bb_upperband = BollingerBands().UpperBand;
bb_upperBand.SetDefaultColor(Color.GRAY);


plot bb_lowerband = BollingerBands().LowerBand;
bb_lowerBand.SetDefaultColor(Color.GRAY);


# Use reference calls to TOS KC's and plot midline and bands

plot kc_midline = KeltnerChannels().Avg;
kc_midline.SetDefaultColor(Color.YELLOW);
kc_midline.Hide(); #Both midlines are the same and we only need one

plot kc_upperband = KeltnerChannels().Upper_Band;
kc_upperband.SetDefaultColor(Color.GREEN);

plot kc_lowerband = KeltnerChannels().Lower_Band;
kc_lowerband.SetDefaultColor(Color.GREEN);


# When the BB upper and lower bands cross inside the KC's we are in a Squeeze

DefineGlobalColor("Squeeze", Color.BLUE);
DefineGlobalColor("UnSqueezed", Color.CURRENT);

AddCloud(kc_upperband, bb_upperband, GlobalColor("Squeeze"), GlobalColor("UnSqueezed"));
AddCloud(bb_lowerband, kc_lowerband, GlobalColor("Squeeze"), GlobalColor("UnSqueezed"));


# Add vertical lines to further demarcate start and end of Squeeze

AddVerticalLine(bb_upperband crosses below kc_upperband and bb_lowerband crosses above kc_lowerband, "Squeeze On", Color.RED);
AddVerticalLine(bb_upperband crosses above kc_upperband and bb_lowerband crosses below kc_lowerband, "Squeeze Off", Color.GREEN);
 

Similar threads

Top