
* Added multiple channels to select from - test it and use it at your risk.
Author Message:
he "BBTrend w SuperTrend decision - Strategy " is a trading strategy designed to identify market trends using Bollinger Bands and SuperTrend indicators. What sets this strategy apart is its use of two Bollinger Bands with different lengths to capture both short-term and long-term market trends, providing a more comprehensive view of market dynamics. Additionally, the strategy includes customizable take profit (TP) and stop loss (SL) settings, allowing traders to tailor their risk management according to their preferences.
CODE:
CSS:
#// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © PresentTrading
#// "BBTrend w SuperTrend decision - Strategy [presentTrading]" employs Bollinger Bands to identify market trends and utilizes SuperTrend indicators for trade signals
#strategy("BBTrend w SuperTrend decision - Strategy [presentTrading]",
# Converted by Sam4Cok@Samer800 - 06/2024
declare lower;
input labelOptions = {Default "Summary", "Detailed", "Don't Show"};
input barColor = no;
input signalOptions = {Default "Enter Long/Short", "Exit Long/Short", "Both Entry/Exit", "Don't Show Signals"};
input atrLength = 10; #, "Length",
input SupertrendFactor = 7.0; #, "Factor",
input channelType = {Default "Bollinger", "Keltner", "Donchian", "Squeeze"};
input useTrueRange = yes;
input tradeDirection = {default "Both", "Long", "Short"}; # "Trading Direction"
input channelAverageType = AverageType.SIMPLE;
input channelSource = close;
input shortLength = 20; #, "Short BB Length",
input longLength = 50; #, "Long BB Length"
input stdDevMultiplier = 2.0; #, "StdDev"
input TPSLCondition = {"TP", "SL", "Both", default "None"}; #("None", "TPSL C
input lotSize = 15;
input takeProfitPercentage = 3.0; #, title="Take Profit (%)")
input stopLossPercentage = 1.5; #, title="Stop Loss (%)")
def na = Double.NaN;
def last = IsNaN(close);
def full = labelOptions==labelOptions."Detailed";
def summ = labelOptions==labelOptions."Summary" or full;
def allSig = signalOptions==signalOptions."Both Entry/Exit";
def enter = signalOptions==signalOptions."Enter Long/Short" or allSig;
def exit = signalOptions==signalOptions."Exit Long/Short" or allSig;
def long = tradeDirection == tradeDirection."Long" or tradeDirection == tradeDirection."Both";
def short = tradeDirection == tradeDirection."Short" or tradeDirection == tradeDirection."Both";
def TpF = TPSLCondition == TPSLCondition."TP" or TPSLCondition == TPSLCondition."Both";
def SlF = TPSLCondition == TPSLCondition."SL" or TPSLCondition == TPSLCondition."Both";
#-- color
DefineGlobalColor("sUP", CreateColor(10, 190, 160));
DefineGlobalColor("wUP", CreateColor(5, 97, 82));
DefineGlobalColor("sDn", CreateColor(242, 54, 69));
DefineGlobalColor("wDn", CreateColor(131, 8, 18));
DefineGlobalColor("stUP", Color.GREEN); #CreateColor(0, 140, 255));
DefineGlobalColor("stDn", Color.RED); #Color.MAGENTA); #CreateColor(255, 72, 0));
#// the same on pine
script f_bb {
input avgType = AverageType.SIMPLE;
input src = close;
input length = 20;
input mult = 2;
def basis = MovingAverage(avgType, src, length);
def stdv = StDev(src, length);
def dev = mult * stdv;
plot base = basis;
plot up = basis + dev;
plot lo = basis - dev;
}
#//KELTNERCHANNEL
script f_kc { #f_kc {
input avgType = AverageType.SIMPLE;
input src = close;
input length = 20;
input mult = 2;
input useTrueRange = yes;
def basis = MovingAverage(avgType, src, length);
def tr = TrueRange(high, close, low);
def range = if useTrueRange then tr else high - low;
def rangema = MovingAverage(avgType, range, length);
plot base = basis;
plot up = basis + rangema * mult;
plot lo = basis - rangema * mult;
}
script f_dc {
input src = close;
input length = 20;
def hh = Highest(src, length);
def ll = Lowest(src, length);
def basis = (hh + ll ) / 2;
plot base = basis;
plot up = hh;
plot lo = ll;
}
script st {
input src = hl2;
input c = close;
input atr = close;
input factor = 6;
def up1 = src + factor * atr;
def up = if !up[1] then up1 else
if (up1 < up[1]) or (c[1] > up[1]) then up1 else up[1];
def dn1 = src - factor * atr;
def dn = if !dn[1] then dn1 else
if (dn1 > dn[1]) or (c[1] < dn[1]) then dn1 else dn[1];
def st; # = na
def dir = if IsNaN(atr[1]) then 1 else
if st[1] == up[1] then (if c > up then -1 else +1) else (if c < dn then +1 else -1);
st = if dir == -1 then dn else up;
plot s = if st then st else Double.NaN;
plot d = if dir then dir else Double.NaN;
}
def shortMiddle; # = f_bb(BollingerBandsAverageType, BollingerBandsSource, shortLengthInput, stdDevMultInput).base;
def shortUpper; # = f_bb(BollingerBandsAverageType, BollingerBandsSource, shortLengthInput, stdDevMultInput).up;
def longUpper; # = f_bb(BollingerBandsAverageType, BollingerBandsSource, longLengthInput, stdDevMultInput).up;
def shortLower; # = f_bb(BollingerBandsAverageType, BollingerBandsSource, shortLengthInput, stdDevMultInput).dn;
def longLower; # = f_bb(BollingerBandsAverageType, BollingerBandsSource, longLengthInput, stdDevMultInput).dn;
#input channelType = {Default "Bollinger", "Keltner", "Donchian"};
Switch (channelType) {
Case "Keltner" :
shortMiddle = f_kc(channelAverageType, channelSource, shortLength, stdDevMultiplier, useTrueRange).base;
shortUpper = f_kc(channelAverageType, channelSource, shortLength, stdDevMultiplier, useTrueRange).up;
longUpper = f_kc(channelAverageType, channelSource, longLength, stdDevMultiplier, useTrueRange).up;
shortLower = f_kc(channelAverageType, channelSource, shortLength, stdDevMultiplier, useTrueRange).lo;
longLower = f_kc(channelAverageType, channelSource, longLength, stdDevMultiplier, useTrueRange).lo;
Case "Donchian" :
shortMiddle = f_dc(channelSource, shortLength).base;
shortUpper = f_dc(channelSource, shortLength).up;
longUpper = f_dc(channelSource, longLength).up;
shortLower = f_dc(channelSource, shortLength).lo;
longLower = f_dc(channelSource, longLength).lo;
Case "Squeeze" :
shortMiddle = f_bb(channelAverageType, channelSource, shortLength, stdDevMultiplier).base;
shortUpper = f_kc(channelAverageType, channelSource, shortLength, stdDevMultiplier, useTrueRange).up;
longUpper = f_bb(channelAverageType, channelSource, longLength, stdDevMultiplier).up;
shortLower = f_kc(channelAverageType, channelSource, shortLength, stdDevMultiplier, useTrueRange).lo;
longLower = f_bb(channelAverageType, channelSource, longLength, stdDevMultiplier).lo;
Default :
shortMiddle = f_bb(channelAverageType, channelSource, shortLength, stdDevMultiplier).base;
shortUpper = f_bb(channelAverageType, channelSource, shortLength, stdDevMultiplier).up;
longUpper = f_bb(channelAverageType, channelSource, longLength, stdDevMultiplier).up;
shortLower = f_bb(channelAverageType, channelSource, shortLength, stdDevMultiplier).lo;
longLower = f_bb(channelAverageType, channelSource, longLength, stdDevMultiplier).lo;
}
def absLo = AbsValue(shortLower - longLower);
def absUp = AbsValue(shortUpper - longUpper);
def BBTrend = (absLo - absUp) / shortMiddle * 100;
def BBTrend1 = if isNaN(BBTrend[1]) then 0 else BBTrend[1];
def histColor =
if BBTrend > 0 and BBTrend >= BBTrend1 then 2 else
if BBTrend > 0 and BBTrend < BBTrend1 then 1 else
if BBTrend < 0 and BBTrend > BBTrend1 then -1 else
if BBTrend < 0 and BBTrend <= BBTrend1 then -2 else 1;
#def src = BBTrend;
def o = BBTrend1;
def h = Max(BBTrend1, BBTrend);
def l = Min(BBTrend1, BBTrend);
def c = BBTrend;
def hl = (h + l) / 2;
def oc = (o + c) / 2;
def tr = if useTrueRange then TrueRange(h, c, l) else h - l;
def atr = if atrLength <= 1 then tr else WildersAverage(tr, atrLength);
def st = st(hl, BBTrend, atr, SupertrendFactor).s;
def dir = st(hl, BBTrend, atr, SupertrendFactor).d;
plot BearST = if dir > 0 then st else na; #, 'Bear ST', cst, 1, plot.style_linebr)
plot BullST = if dir < 0 then st else na; #, 'Bull ST', cst, 1, plot.style_linebr)
BearST.SetDefaultColor(GlobalColor("stDN"));
BullST.SetDefaultColor(GlobalColor("stUP"));
plot bbTrendHisto = BBTrend; #, "BBTrend"
plot zero = if last then na else 0; #, "Zero line")
bbTrendHisto.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bbTrendHisto.AssignValueColor(if histColor == 2 then GlobalColor("sUP") else
if histColor == 1 then GlobalColor("wUP") else
if histColor == -1 then GlobalColor("wDN") else
if histColor == -2 then GlobalColor("sDN") else Color.DARK_GREEN);
zero.SetDefaultColor(Color.DARK_GRAY);
AddCloud(BearST, oc, Color.DARK_RED);
AddCloud(oc, BullST, Color.DARK_GREEN);
#-- ber Color
AssignPriceColor(if !barColor then Color.CURRENT else
if histColor == 2 then GlobalColor("sUP") else
if histColor == 1 then GlobalColor("wUP") else
if histColor == -1 then GlobalColor("wDN") else
if histColor == -2 then GlobalColor("sDN") else Color.DARK_GREEN);
#---
def scon = if Sign(dir - dir[1]) == 1 then yes else no;
def bcon = if Sign(dir - dir[1]) == -1 then yes else no;
plot ptDn = if scon then st else na;
plot ptUp = if bcon then st else na;
ptDn.SetLineWeight(2);
ptUp.SetLineWeight(2);
ptDn.SetPaintingStrategy(PaintingStrategy.POINTS);
ptUp.SetPaintingStrategy(PaintingStrategy.POINTS);
ptDn.SetDefaultColor(GlobalColor("stDN"));
ptUp.SetDefaultColor(GlobalColor("stUP"));
#/ Entry/Exit conditions
def LongEntry = dir < 0;
def LongExit = dir > 0;
def ShortEntry = dir > 0;
def ShortExit = dir < 0;
#// Strategy logic
def inTrades;
def entryLong;
def closeLong;
def entryShort;
def closeShort;
def longCond = long and LongEntry and !inTrades[1];
def shotCond = short and ShortEntry and !inTrades[1];
def LcloseCond = LongExit and inTrades[1] > 0;
def ScloseCond = ShortExit and inTrades[1] < 0;
#/ Apply Take Profit and Stop Loss conditions
def tpLvlLong = close * (1 + takeProfitPercentage / 100);
def slLvlLong = close * (1 - stopLossPercentage / 100);
def tpLvlShort = close * (1 - takeProfitPercentage / 100);
def slLvlShort = close * (1 + stopLossPercentage / 100);
def exitLong = (if(TpF, entryLong[1] >= tpLvlLong, no) or if(SlF, entryLong[1] < slLvlLong, no)) and inTrades[1] > 0;
def exitShort = (if(TpF, entryShort[1] <= tpLvlShort, no) or if(SlF, entryShort[1] > slLvlShort, no)) and inTrades[1] < 0;
if longCond {
inTrades = 1;
entryLong = open[-1];
entryShort = 0;
closeLong = 0;
closeShort = 0;
} else if (LcloseCond or exitLong) {
inTrades = 0;
entryLong = entryLong[1];
entryShort = entryShort[1];
closeLong = open[-1] - entryLong;
closeShort = 0;
} else if shotCond {
inTrades = -1;
entryLong = 0;
entryShort = open[-1];
closeLong = 0;
closeShort = 0;
} else if (ScloseCond or exitShort) {
inTrades = 0;
entryLong = entryLong[1];
entryShort = entryShort[1];
closeLong = 0;
closeShort = entryShort - open[-1];
} else {
inTrades = inTrades[1];
entryLong = entryLong[1];
entryShort= entryShort[1];
closeLong = 0;
closeShort= 0;
}
#def tot1 = inTrades;
def longSig = inTrades == 1 and inTrades[1] != 1;
def shortSig = inTrades ==-1 and inTrades[1] !=-1;
def exitL = inTrades == 0 and inTrades[1] == 1;
def exitS = inTrades == 0 and inTrades[1] ==-1;
def totLong = TotalSum(if exitL then 1 else 0);
def totShort = TotalSum(if exitS then 1 else 0);
def tottrd = totLong + totShort;
def tradeL = Sign(closeLong);
def tradeS = Sign(closeShort);
def WinL = TotalSum(if tradeL > 0 then 1 else 0);
def WinS = TotalSum(if tradeS > 0 then 1 else 0);
def totWin = (WinL + WinS);
def winRt = Round(totWin / totTrd *100, 1);
def winRtL = Round(WinL / totLong *100, 1);
def winRtS = Round(WinS / totShort *100, 1);
def amtL = TotalSum(if exitL then closeLong else 0) * lotSize;
def amtS = TotalSum(if exitS then closeShort else 0) * lotSize;
def pl = RoundDown(amtL + amtS, 2);
def plL = RoundDown(amtL, 2);
def plS = RoundDown(amtS, 2);
AddChartBubble(enter and shortSig, st + tr / 3, "S", Color.RED);
AddChartBubble(enter and longSig, st - tr / 3, "L", Color.GREEN, no);
AddVerticalLine(exit and exitL, "Exit L", Color.CYAN);
AddVerticalLine(exit and exitS, "Exit S", Color.MAGENTA);
AddLabel(summ, "WinRate(" + winRt + "%, " + tottrd + ")", if winRt >= 50 then Color.GREEN else Color.RED);
AddLabel(full, "Long(" + winRtL + "%, " + totLong + ")", if winRtL >= 50 then Color.GREEN else Color.RED);
AddLabel(full, "Short(" + winRtS + "%, " + totShort + ")", if winRtS >= 50 then Color.GREEN else Color.RED);
AddLabel(full, "AmtLong($" + plL + ")", if amtL > 0 then Color.GREEN else Color.RED);
AddLabel(full, "AmtShort($" + plS + ")", if amtS > 0 then Color.GREEN else Color.RED);
AddLabel(summ, "P/L($" + pl + ")", if pl > 0 then Color.GREEN else if pl < 0 then Color.RED else Color.GRAY);
#-- END of CODe