TraderZen
Member
Current Version is V2.1 as of 8/17/2022.
V2 replaced V1 code.
First things first: I was in the process of writing a custom Donchian Channels(“DC”) Indicator when @samer800 published his TOS adaptation of Turtle Trade Channels Indicator (TuTCI); which happens to be based on DC. It is a true adaptation of the original TradingView Indicator with some much sought-after TOS specific enhancements – like MTF support.
DC is versatile. The original code in TradingView does not include The Basis Line and it provides only one (the default) Strategy Mode. Other than that, the code is solid. In fact, the method used to count bars is better than the one I had written.
Code Credit: I decided to use the TuTCI as a foundation for my study. DC7 is aimed at doing much more.
I decided to start a new thread for this Indicator, as I plan to keep adding newer features and keep track of them.
The DC7 indicator:
I am getting to know that DCs can provide a versatile trading toolset when combined with Moving Averages (and other indicators).
So, let’s see how we did –
DC7 provides Top Seven Strategy Modes of DC signals-
1) Stop and Reverse is the default Donchian Strategy.
2) UpSignal is a Bullish Signal only mode.
3) DownSignal is a Brearish Signal only mode.
4) Long_Pyramid is a Bullish Strategy, and it prompts "(+)" to add more Long positions when a new buySignal is generated.
5) Short_Pyramid is a Bearish Strategy, and it prompts "(+)" to add more Short Positions.
6) SMA_&_Longer_Entry_Combo is a smart Intraday Strategy: It toggles between Bull and Bear Mode.
Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
In Bull mode Exit Condition is met when Close Crosses below SMA_Period level.
In Bear Mode Exit Condition is met when Close Crosses above SMA_Period level.
User defined Longer_Entry_Legth is used for calculations instead of EntryLength.
User defined SMA is used, and the line is shown.
Entry and Exit, Bull/Bear toggle cues are provided; with cues to add Long/Short Positions depending on the Bull/Bear Mode respectively.
7) SMA_&_Longer_Exit_Combo is a Smart Swing Strategy – It toggles between Bull and Bear Mode.
Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
In Bull mode Exit Condition is met when Close Crosses below SMA_Period level.
In Bear Mode Exit Condition is met when Close Crosses above SMA_Period level.
User defined Longer_Exit_Legth is used for calculations instead of ExitLength.
User defined SMA is used, and the line is shown.
Entry and Exit, Bull/Bear toggle cues are provided; with cues to add Long/Short Positions depending on the Bull/Bear Mode respectively.
8) All Signals - All signals can simultaneous be turned on, except those for Mode(6) and Mode(7) above are display.
V2 replaced V1 code.
First things first: I was in the process of writing a custom Donchian Channels(“DC”) Indicator when @samer800 published his TOS adaptation of Turtle Trade Channels Indicator (TuTCI); which happens to be based on DC. It is a true adaptation of the original TradingView Indicator with some much sought-after TOS specific enhancements – like MTF support.
DC is versatile. The original code in TradingView does not include The Basis Line and it provides only one (the default) Strategy Mode. Other than that, the code is solid. In fact, the method used to count bars is better than the one I had written.
Code Credit: I decided to use the TuTCI as a foundation for my study. DC7 is aimed at doing much more.
I decided to start a new thread for this Indicator, as I plan to keep adding newer features and keep track of them.
The DC7 indicator:
I am getting to know that DCs can provide a versatile trading toolset when combined with Moving Averages (and other indicators).
So, let’s see how we did –
DC7 provides Top Seven Strategy Modes of DC signals-
1) Stop and Reverse is the default Donchian Strategy.
2) UpSignal is a Bullish Signal only mode.
3) DownSignal is a Brearish Signal only mode.
4) Long_Pyramid is a Bullish Strategy, and it prompts "(+)" to add more Long positions when a new buySignal is generated.
5) Short_Pyramid is a Bearish Strategy, and it prompts "(+)" to add more Short Positions.
6) SMA_&_Longer_Entry_Combo is a smart Intraday Strategy: It toggles between Bull and Bear Mode.
Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
In Bull mode Exit Condition is met when Close Crosses below SMA_Period level.
In Bear Mode Exit Condition is met when Close Crosses above SMA_Period level.
User defined Longer_Entry_Legth is used for calculations instead of EntryLength.
User defined SMA is used, and the line is shown.
Entry and Exit, Bull/Bear toggle cues are provided; with cues to add Long/Short Positions depending on the Bull/Bear Mode respectively.
7) SMA_&_Longer_Exit_Combo is a Smart Swing Strategy – It toggles between Bull and Bear Mode.
Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
In Bull mode Exit Condition is met when Close Crosses below SMA_Period level.
In Bear Mode Exit Condition is met when Close Crosses above SMA_Period level.
User defined Longer_Exit_Legth is used for calculations instead of ExitLength.
User defined SMA is used, and the line is shown.
Entry and Exit, Bull/Bear toggle cues are provided; with cues to add Long/Short Positions depending on the Bull/Bear Mode respectively.
8) All Signals - All signals can simultaneous be turned on, except those for Mode(6) and Mode(7) above are display.
Code:
# V 2.1 -- re-uploaded 8/23/2022
# Top Seven Strategy Modes of Donchian Channels
# Notes:
# 1) Stop and Reverse is the default Donchian Strategy.
# 2) UpSignal is a Bullish Signal only mode.
# 3) DownSignal is a Brearish Signal only mode.
# 4) Long_Pyramid is a Bullish Strategy and it prompts "(+)" to add more Long positions.
# 5) Short_Pyramid is a Bearish Strategy and it prompts "(+)" to add more Short Positions.
# 6) SMA_&_Longer_Entry_Combo is a smart Intraday Strategy:
# *** -- Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
# *** -- In Bull mode Exit Condition is met when Close Crosses below SMA_Period level.
# *** -- In Bear Mode Exit Conditino is met when Close Crosses above SMA_Period level.
# *** -- Longer_Entry_Legth is used for calculations instead of EntryLength.
# 7) SMA_&_Longer_Exit_Combo is a Smart Swing Stragegy
# *** -- Bull mode is when Close Level is greater than the SMA_Period OTHERWISE Bear mode.
# *** -- In Bull mode Exit Condition is met when Close Crosses below SMA_Period level; or Donchain Channel Crosses Above SMA_Period_Level .
# *** -- In Bear Mode Exit Conditino is met when Close Crosses above SMA_Period level; or DonChain Channel Crosses Above SMA_Period Level.
# *** -- Longer_Exit_Legth is used for calculations instead of ExitLength.
# 8) All Signals - All signals except those for Mode(6) and Mode(7) above are display.
#
# --------------- Turtle Trade Channels Indicator(TuTCI)
#//@version=4
#//author: @kivancozbilgic
#https://www.tradingview.com/script/pB5nv16J/
#study(title="Turtle Trade Channels Indicator", shorttitle="TuTCI"
# Converted and modifiedTrueRange by Sam4COK@Samer800 - 08/2022
input EntryLength = 20; # "Entry Length"
input ExitLength = 10; # "Exit Length"
input showBasis = yes; # "Show Basis Line?"
input ShowLines = yes; # "Show Lines?"
input showSignals = yes; # "Show Entry/Exit Signals ?"
input SignalOnPrice = no; # "Show Entry/Exit Signals on Price?"
input ShowCloud = yes; # "Highlighter On/Off ?"
input UseChartTime = yes; # "Use Chart timeframe or Aggregation?"
input BG_Color_fill = {Default "Mid_Line", "Full"}; # "Backgroud Color fill option"
input aggregation = AggregationPeriod.DAY;
input Strategy_Mode = {Default "Stop_and_Reverse", "UpSignal" , "DownSignal", "Long_Pyramid", "Short_Pyramid",
"SMA_and_Longer_Entry_Combo" , "SMA_and_Longer_Exit_Combo" , "All Signals" };
input SMA_Period = 50;
input Longer_EntryLength = 55; #Longer Entry Length to be used with SMA_&_Longer_Entry_Combo"
input Longer_ExitLength = 55; #Longer Exit Length to be used with SMA_&_Longer_Exit_Combo"
def na = double.nan;
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
#MTF
def TFLow;
def TFHigh;
def TFClose;
if UseChartTime
then {
TFLow = low;
TFHigh = high;
TFClose = close;
} else {
TFLow = low(period = aggregation);
TFHigh = high(period = aggregation);
TFClose = close(period = aggregation);
}
#//Strategy_Mode settings for Donchian Lenths
### Length Specific variables
Def TheEntryLength;
Def TheExitLength;
Def ShowSMA;
Def BullMode;
Def BearMode;
Switch (Strategy_mode) {
Case "Stop_and_Reverse":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
Case "UpSignal":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
Case "DownSignal":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
Case "Long_Pyramid":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
Case "Short_Pyramid":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
Case "SMA_and_Longer_Entry_Combo":
TheEntryLength = Longer_EntryLength;
TheExitLength = ExitLength;
ShowSMA = yes;
BullMode = TFClose > SimpleMovingAvg(TFClose,SMA_Period);
BearMode = TFClose < SimpleMovingAvg(TFClose,SMA_Period);
Case "SMA_and_Longer_Exit_Combo":
TheEntryLength = EntryLength;
TheExitLength = Longer_ExitLength;
ShowSMA = yes;
BullMode = TFClose > SimpleMovingAvg(TFClose,SMA_Period);
BearMode = TFClose < SimpleMovingAvg(TFClose,SMA_Period);
Case "All Signals":
TheEntryLength = EntryLength;
TheExitLength = ExitLength;
ShowSMA = no;
BullMode = na;
BearMode =na;
}
def up = Highest(TFHigh, TheEntryLength);
def down = Lowest(TFLow, TheEntryLength);
def sup = Highest(TFHigh, TheExitLength);
def sdown = Lowest(TFLow, TheExitLength);
plot HH= Max(highest(TFHigh,TheEntryLength),TFHigh);
HH.SetDefaultColor(CreateColor(100,200,100));
plot LL= Min(Lowest(TFLow,TheExitLength),TFLow);
LL.SetDefaultColor(CreateColor(200,100,100));
#def lower = Lowest(TFLow, EntryLength);
#def upper = Highest(TFHigh, EntryLength);
def Lower = LL;
def upper = HH;
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 255, 148));
l.SetHiding(!ShowLines);
#u.hide();
#l.hide();
def K1 = if barssince(TFHigh >= up[1]) <= barssince(TFLow <= down[1]) then down else up;
def K2 = If(barssince(TFHigh >= up[1]) <= barssince(TFLow <= down[1]), sdown, sup);
plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(1);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);
#RB Code
def mid = (HH+LL)/2;
plot TrendBasis = mid;
TrendBasis.SetDefaultColor(CreateColor(55,55,55));
TrendBasis.setHiding(yes);
def buySignal = TFHigh == upper[1] or TFHigh crosses above upper[1];
def sellSignal = TFLow == lower[1] or lower[1] crosses above TFLow;
def buyExit = TFLow == sdown[1] or sdown[1] crosses above TFLow;
def sellExit = TFHigh == sup[1] or TFHigh crosses above sup[1];
def O1 = barssince(buySignal);
def O2 = barssince(sellSignal);
def O3 = barssince(buyExit);
def O4 = barssince(sellExit);
def E1 = barssince(buySignal[1]);
def E2 = barssince(sellSignal[1]);
def E3 = barssince(buyExit[1]);
def E4 = barssince(sellExit[1]);
#### Bubbles
def SignalUp = if showSignals then if SignalOnPrice then high else up else na;
def SignalDn = if showSignals then if SignalOnPrice then low else down else na;
def ExitLong;
def ExitShort;
def Long;
def Short;
def color1 = if ShowCloud and (Min(Min(O1, O2), O3) == O1) then 1 else na;
def color2 = if ShowCloud and (Min(Min(O1, O2), O4) == O2) then 1 else na;
### Strategy_Mode settings for Bubbles
### Mode Specific variables
Def Add_Order_up;
Def Add_Order_Dn;
Switch (Strategy_mode) {
Case "Stop_and_Reverse":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_Order_Up = na;
Add_Order_Dn = na;
Case "UpSignal":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = na;
Add_Order_up = na;
Add_Order_Dn = na;
Case "DownSignal":
ExitLong = na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_Order_Up = na;
Add_Order_Dn = na;
Case "Long_Pyramid":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = na;
Add_Order_up = if isNan(Add_order_Up[1]) && buySignal then yes else na;
Add_Order_Dn = na;
Case "Short_Pyramid":
ExitLong = na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_Order_up = na;
Add_Order_Dn = if isNan(Add_Order_Dn[1]) && Sellsignal then yes else na;
Case "SMA_and_Longer_Entry_Combo":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_order_Up = if BullMode && isNan(Add_order_Up[1]) && buySignal then yes else na;
Add_Order_Dn = if BearMode && isNan(Add_Order_Dn[1]) && Sellsignal then yes else na; ;
Case "SMA_and_Longer_Exit_Combo":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_order_Up = if BullMode && isNan(Add_order_Up[1]) && buySignal then yes else na;
Add_Order_Dn = if BearMode && isNan(Add_Order_Dn[1]) && Sellsignal then yes else na; ;
Case "All Signals":
ExitLong = if buyExit and SignalUp and O1 < O3[1] then up else na;
ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
Add_Order_up = if isNan(Add_Order_up[1]) && buySignal then yes else na;
Add_Order_Dn = if isNan(Add_Order_Dn[1]) && Sellsignal then yes else na;
}
AddChartBubble( (isNan(BullMode) or isNan(BearMode)) && ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
AddChartBubble( (isNan(BullMode) or isNan(BearMode)) && ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);
AddChartBubble( (isNan(BullMode) or isNan(BearMode)) && Long, SignalDn, "Long Entry", Color.GREEN, no);
AddChartBubble( (isNan(BullMode) or isNan(BearMode)) && Short, SignalUp, "Short Entry", Color.RED, yes);
AddChartBubble(Add_Order_Up, SignalUp, "+", Color.Green,yes);
AddChartBubble(Add_Order_Dn, SignalDn, "+", Color.REd,No);
AddCloud(if color1 then u else na, mid, Color.DARK_GREEN,Color.Dark_red);
AddCloud(if BG_Color_fill == BG_Color_fill."Full" && color1 then mid else na, l, Color.DARK_GREEN,Color.Dark_red);
AddCloud(if color2 then mid else na, l, Color.Dark_red,color.Dark_green);
AddCloud(if BG_Color_fill == BG_Color_fill."Full" && color2 then mid else na, u, Color.Dark_green,color.Dark_red);
#Combo_Cases
plot Strategy_SMA = if ShowSMA then SimpleMovingAvg(TFClose,SMA_Period) else na;
Strategy_SMA.AssignValueColor(if BullMode then Color.Light_green else if BearMode then color.light_red else createColor(0,148,255));
Strategy_Sma.setlineWeight(2);
plot LongExit = if (BullMode or BearMode) and TFClose Crosses Below Strategy_SMA then Strategy_sma else na;
Plot ShortExit = if (BullMode or BearMode) and TFClose Crosses Above Strategy_SMA then Strategy_sma else na;
Plot LongEntry = if (BullMode or BearMode) and TFClose Crosses Above Strategy_SMA then Strategy_sma else na;
plot ShortEntry = if (BullMode or BearMode) and TFClose Crosses Below Strategy_SMA then STrategy_SMA else na;
LongExit.SetPaintingStrategy(PaintingStrategy.Points);
LongExit.AssignValueColor(createColor(0,148,255));
LongExit.SetLineWeight(2);
LongEntry.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
LongEntry.AssignValueColor(color.green);
LongEntry.SetLineWeight(5);
ShortExit.SetPaintingStrategy(PaintingStrategy.Points);
ShortExit.AssignValueColor(createColor(0,148,255));
ShortExit.SetLineWeight(2);
ShortEntry.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ShortEntry.AssignValueColor(color.red);
ShortEntry.SetLineWeight(5);
AddLabel(Yes, "Mode: " + Strategy_Mode, color.white);
AddLabel(yes, "EntryLength: " + TheEntryLength + " ExitLength: " + TheExitLength, Color.white);
AddLabel((Strategy_Mode == Strategy_Mode."SMA_and_Longer_Entry_Combo" or Strategy_Mode == Strategy_Mode."SMA_and_Longer_Exit_Combo") , "SMA: " + SMA_Period, Color.White);
AddLabel((Strategy_Mode == Strategy_Mode."SMA_and_Longer_Entry_Combo" or Strategy_Mode == Strategy_Mode."SMA_and_Longer_Exit_Combo") &&
BullMode == yes , "Bullish", Color.green);
AddLabel((Strategy_Mode == Strategy_Mode."SMA_and_Longer_Entry_Combo" or Strategy_Mode == Strategy_Mode."SMA_and_Longer_Exit_Combo") &&
BearMode == yes , "Bearish", Color.red);
#AddChartBubble((BullMode or BearMode) and TFClose Crosses Below Strategy_SMA, STrategy_SMA, "×$", CreateColor(0, 148, 255), yes);
#AddChartBubble((BullMode or BearMode) and TFClose Crosses Above Strategy_SMA, STrategy_SMA, "×$", CreateColor(0, 148, 255), no);
#AddChartBubble((BullMode or BearMode) and TFClose Crosses Above Strategy_SMA, STrategy_SMA, "$", Color.Green, No);
#AddChartBubble((BullMode or BearMode) and TFClose Crosses Below Strategy_SMA, STrategy_SMA, "$", Color.Red, Yes);
##### END
### End - 8/23/2022 Re-upload.
Last edited: