DC7 - Donchian Channels 7 Strategy Modes Indicator for ThinkOrSwim

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.
1dYgr4B.jpg

Q6RNvib.jpg

XdBT3fP.jpg


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:
I wanted to see if I could convert this from a Study into a Strategy, but I had difficulty. Just leaving it as a Study, I see "Short Entry" when the Strategy_Mode is set to "Long_Pyramid". If the wrong kinds of indications are appearing when as a study, then they'll be there when converted into a strategy too.

Here's the simplistic code I tried adding just above the end to make it into a strategy.
Code:
#### Strategy Orders
addOrder(OrderType.SELL_AUTO, ExitLong);
addOrder(OrderType.BUY_AUTO, ExitShort);
addOrder(OrderType.BUY_AUTO, Long);
addOrder(OrderType.SELL_AUTO, Short);

Since you know your code better, maybe you'll recognize if I'm using the wrong variables here, since you've kept ExitLong and now defined LongExit too.
 
I wanted to see if I could convert this from a Study into a Strategy, but I had difficulty. Just leaving it as a Study, I see "Short Entry" when the Strategy_Mode is set to "Long_Pyramid". If the wrong kinds of indications are appearing when as a study, then they'll be there when converted into a strategy too.

Here's the simplistic code I tried adding just above the end to make it into a strategy.
Code:
#### Strategy Orders
addOrder(OrderType.SELL_AUTO, ExitLong);
addOrder(OrderType.BUY_AUTO, ExitShort);
addOrder(OrderType.BUY_AUTO, Long);
addOrder(OrderType.SELL_AUTO, Short);

Since you know your code better, maybe you'll recognize if I'm using the wrong variables here, since you've kept ExitLong and now defined LongExit too.
Yes you are correct! (y)
The Long Pyramid condition allows shorting but prompts adding new positions only when you are going long.
The Short pyramid does that same when you go short.
Rest of the things are purposefully kept close the stop-&-reverse.

When I was writing the code, I was not planning to create a strategy. Your feedback is valuable. I will make arrangements for this in the next version.

Also I need to make sure to add a code snippet where the orders are place only between 6:30AM and 1:00PM for testing this within the Intraday timeframes!
 
V2 is now published in the first post of the thread.
Change: Bullish and Bearish prompts are much cleaner now.

I will release a strategy for this indicator soon.
@cinnamon - Thank you for your feedback. I have modified the code to keep it pure Long or pure Short trade. This is closer to the strategy.
 
Last edited:
Nice work. I got some errors on the V2. Seems to be a typo / formatting / cut & paste of sorts.

Changed
Code:
 "SMA_&[I]Longer_Entry_Combo" , "SMA[/I]&_Longer_Exit_Combo"
to
Code:
  "SMA_&_Longer_Entry_Combo" , "SMA_&_Longer_Exit_Combo"

in a few places and works fine now.
The
Code:
  [I]   and [/I]
instead of _ underscore.
 
Last edited by a moderator:
Nice work. I got some errors on the V2. Seems to be a typo / formatting / cut & paste of sorts.

Changed
Code:
 "SMA_&[I]Longer_Entry_Combo" , "SMA[/I]&_Longer_Exit_Combo"
to
Code:
  "SMA_&_Longer_Entry_Combo" , "SMA_&_Longer_Exit_Combo"

in a few places and works fine now.
The
Code:
  [I]   and [/I]
instead of _ underscore.
Thank you - I have changed the identifier name and I have replaced "&" with "and". I hope this problem should go away. Please test.
 
Version 2.1 loads up fine for me. No issues in copying from the forum and pasting into TOS.

I think the Original Turtles had a limit on how many times they would add to their position (the pyramiding). I guess I'm curious to know how true you're wanting to stay to the original strategy. From most of what I read on The Internet, the original strategy isn't quite so great in this more volatile market, and that adjustments are needed. I believe that the limit on pyramiding was to keep their risk in check.
 
Version 2.1 loads up fine for me. No issues in copying from the forum and pasting into TOS.

I think the Original Turtles had a limit on how many times they would add to their position (the pyramiding). I guess I'm curious to know how true you're wanting to stay to the original strategy. From most of what I read on The Internet, the original strategy isn't quite so great in this more volatile market, and that adjustments are needed. I believe that the limit on pyramiding was to keep their risk in check.
Are you reading my mind? :)

There are two ways to do it -
Aggressive and easy to code (should I call it quick and dirty?): frequently prompt to add positions when a new buySignal is generated and leave the decision to the user's discretion.
Rational and proper: Involve ATR in the calculations and prompt responsibly.

Cinnamon, I believe you are more familiar with ToS Strategies than I am. There is a Strategy in TOS called "Donchian". If you look at its code, it appears to be close to the Turtle method of risk management. See more here. Let me know if this complies with what you have read!

We can give an option to choose between BuySignal or ATR for Pyramid "+" prompts. ATR should be the default. So if a real Strategy is created then the calculations are already dealt with the default.
 
In my quick look at about 100 stocks, I never noticed the Donchian strategy adding to a position before exiting the position. It was 100% entry followed by exit; sometimes both short, and sometimes both long. No pyramiding. Not authentic to the Original Turtles.
 
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.
1dYgr4B.jpg

Q6RNvib.jpg

XdBT3fP.jpg


Code:
# V2.1 - 8/17/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_&_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_&_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_&_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
Hi, is anyone else getting the following errors?

This is in the original code:
Invalid statement: ] at 345:1

When I remove it I get this:

No such enum value: Enum "SMA_&_Longer_Entry_Combo" has no such case Strategy_mode at 79:9
No such enum value: Enum "SMA_&_Longer_Exit_Combo" has no such case Strategy_mode at 79:9
All cases should be described for Strategy_mode at 137:1
No such enum value: Enum "SMA_&_Longer_Entry_Combo" has no such case Strategy_mode at 215:9
No such enum value: Enum "SMA_&_Longer_Exit_Combo" has no such case Strategy_mode at 215:9
All cases should be described for Strategy_mode at 285:1
Expected double
Value never assigned to BearMode at 135:1
Value never assigned to BullMode at 134:1
Value never assigned to ShowSMA at 133:1
Value never assigned to TheEntryLength at 131:1
Value never assigned to TheExitLength at 132:1
Value never assigned to BearMode at 106:1
Value never assigned to BullMode at 105:1
Value never assigned to ShowSMA at 104:1
Value never assigned to TheEntryLength at 102:1
Value never assigned to TheExitLength at 103:1
Value never assigned to BearMode at 99:1
Value never assigned to BullMode at 98:1
Value never assigned to ShowSMA at 97:1
 
Hi, is anyone else getting the following errors?

This is in the original code:
Invalid statement: ] at 345:1

When I remove it I get this:

No such enum value: Enum "SMA_&_Longer_Entry_Combo" has no such case Strategy_mode at 79:9
No such enum value: Enum "SMA_&_Longer_Exit_Combo" has no such case Strategy_mode at 79:9
All cases should be described for Strategy_mode at 137:1
No such enum value: Enum "SMA_&_Longer_Entry_Combo" has no such case Strategy_mode at 215:9
No such enum value: Enum "SMA_&_Longer_Exit_Combo" has no such case Strategy_mode at 215:9
All cases should be described for Strategy_mode at 285:1
Expected double
Value never assigned to BearMode at 135:1
Value never assigned to BullMode at 134:1
Value never assigned to ShowSMA at 133:1
Value never assigned to TheEntryLength at 131:1
Value never assigned to TheExitLength at 132:1
Value never assigned to BearMode at 106:1
Value never assigned to BullMode at 105:1
Value never assigned to ShowSMA at 104:1
Value never assigned to TheEntryLength at 102:1
Value never assigned to TheExitLength at 103:1
Value never assigned to BearMode at 99:1
Value never assigned to BullMode at 98:1
Value never assigned to ShowSMA at 97:1
I re-uploaded the code in the top post of the thread - it was tested earlier too. Try it again.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
316 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top