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));