Turtle Trade Channels Indicator(TuTCI) for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
0moywTj.png

I added to the below more setting to remove/add lines and signals with option to use the indicator in different timeframe.

Release Note:
Legendary trade system which proved that great traders can be made, not born.
Turtle Trade Experiment made 80% annual return for 4 years and made 150 million $
Turtle Trade trend following system is a complete opposite to the "buy low and sell high" approach.
This trend following system was taught to a group of average and normal individuals, and almost everyone turned into a profitable trader.
They used the basis logic of well known DONCHIAN CHANNELS which developed by Richard Donchian .
The main rule is "Trade an 20-day breakout and take profits when an 10-day high or low is breached ". Examples:
Buy a 20-day breakout and close the trade when price action reaches a 10-day low.
Go short a 20-day breakout and close the trade when price action reaches a 10-day high.
In this indicator,
The red line is the trading line which indicates the trend direction:
Price bars over the trend line indicates uptrend
Price bars under the trend line means downtrend
The dotted blue line is the exit line.


Original system is:
Go long when the price High is equal to or above previous 20 day Highest price.
Go short when the price Low is equal to or below previous 20 day Lowest price.
Exit long positions when the price touches the exit line
Exit short positions when the price touches the exit line
Recommended initial stop-loss is ATR * 2 from the opening price.

Default system parameters were 20,10 and 55,20.

Original Turtle Rules:

To trade exactly like the turtles did, you need to set up two indicators representing the main and the failsafe system.
Set up the main indicator with EntryPeriod = 20 and ExitPeriod = 10 (A.k.a S1) (My comments: Good for Swing - Long timeframe)
Set up the failsafe indicator with EntryPeriod = 55 and ExitPeriod = 20 using a different color. (A.k.a S2) (My comment: Good for intraday - Short timeframe)
The entry strategy using S1 is as follows

Buy 20-day breakouts using S1 only if last signaled trade was a loss.
Sell 20-day breakouts using S1 only if last signaled trade was a loss.
If last signaled trade by S1 was a win, you shouldn't trade -Irregardless of the direction or if you traded last signal it or not-

The entry strategy using S2 is as follows:
Buy 55-day breakouts only if you ignored last S1 signal and the market is rallying without you
Sell 55-day breakouts only if you ignored last S1 signal and the market is pluging without you

You can Highlight the chart with provided trade signals:

Green cloud color when Long
Red cloud color when Short
No cloud color when flat

CSS:
#//@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 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 aggregation   = AggregationPeriod.DAY;

def na = Double.NaN;

#barssince(Condition) =>
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);
}

def up = Highest(TFHigh, EntryLength);
def down = Lowest(TFLow, EntryLength);
def sup = Highest(TFHigh, ExitLength);
def sdown = Lowest(TFLow, ExitLength);

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);
def K3 = If(TFClose > K1, down, na);
def K4 = If(TFClose < K1, up, na);

plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(2);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);

def lower = Lowest(TFLow, EntryLength);
def upper = Highest(TFHigh, EntryLength);
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 148, 255));
l.SetHiding(!ShowLines);

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

plot LongExit = if buyExit and O1 < O3[1] then up else na;
LongExit.SetPaintingStrategy(PaintingStrategy.POINTS);
LongExit.SetDefaultColor(CreateColor(0, 148, 255));
LongExit.SetLineWeight(3);
LongExit.SetHiding(!ShowLines);

plot ShortExit = if sellExit and O2 < O4[1] then down else na;
ShortExit.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortExit.SetDefaultColor(CreateColor(0, 148, 255));
ShortExit.SetLineWeight(3);
ShortExit.SetHiding(!ShowLines);

plot LongEntry = if buySignal and O3 < O1[1] then down else na;
LongEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(3);
LongEntry.SetHiding(!ShowLines);

plot ShortEntry = if sellSignal and O4 < O2[1] then up else na;
ShortEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortEntry.SetDefaultColor(CreateColor(255, 82, 82));
ShortEntry.SetLineWeight(3);
ShortEntry.SetHiding(!ShowLines);

#### 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  = if buyExit and SignalUp and O1 < O3[1] then up else na;
AddChartBubble(ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
def ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
AddChartBubble(ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);

def Long  =  if buySignal and SignalDn and O3 < O1[1] then down else na;
AddChartBubble(Long, SignalDn, "Long Entry", Color.GREEN, no);
def Short =  if sellSignal and SignalUp and O4 < O2[1] then up else na;
AddChartBubble(Short, SignalUp, "Short Entry", Color.RED, yes);

#### Cloud
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;
AddCloud(if color1 then u else na, ExitLine, Color.DARK_GREEN);
AddCloud(if color2 then ExitLine else na, l, Color.DARK_RED);

##### END
 
Last edited by a moderator:
0moywTj.png

I added to the below more setting to remove/add lines and signals with option to use the indicator in different timeframe.

Release Note:
Legendary trade system which proved that great traders can be made, not born.
Turtle Trade Experiment made 80% annual return for 4 years and made 150 million $
Turtle Trade trend following system is a complete opposite to the "buy low and sell high" approach.
This trend following system was taught to a group of average and normal individuals, and almost everyone turned into a profitable trader.
They used the basis logic of well known DONCHIAN CHANNELS which developed by Richard Donchian .
The main rule is "Trade an 20-day breakout and take profits when an 10-day high or low is breached ". Examples:
Buy a 20-day breakout and close the trade when price action reaches a 10-day low.
Go short a 20-day breakout and close the trade when price action reaches a 10-day high.
In this indicator,
The red line is the trading line which indicates the trend direction:
Price bars over the trend line indicates uptrend
Price bars under the trend line means downtrend
The dotted blue line is the exit line.


Original system is:
Go long when the price High is equal to or above previous 20 day Highest price.
Go short when the price Low is equal to or below previous 20 day Lowest price.
Exit long positions when the price touches the exit line
Exit short positions when the price touches the exit line
Recommended initial stop-loss is ATR * 2 from the opening price.

Default system parameters were 20,10 and 55,20.

Original Turtle Rules:

To trade exactly like the turtles did, you need to set up two indicators representing the main and the failsafe system.
Set up the main indicator with EntryPeriod = 20 and ExitPeriod = 10 (A.k.a S1) (My comments: Good for Swing - Long timeframe)
Set up the failsafe indicator with EntryPeriod = 55 and ExitPeriod = 20 using a different color. (A.k.a S2) (My comment: Good for intraday - Short timeframe)
The entry strategy using S1 is as follows

Buy 20-day breakouts using S1 only if last signaled trade was a loss.
Sell 20-day breakouts using S1 only if last signaled trade was a loss.
If last signaled trade by S1 was a win, you shouldn't trade -Irregardless of the direction or if you traded last signal it or not-

The entry strategy using S2 is as follows:
Buy 55-day breakouts only if you ignored last S1 signal and the market is rallying without you
Sell 55-day breakouts only if you ignored last S1 signal and the market is pluging without you

You can Highlight the chart with provided trade signals:

Green cloud color when Long
Red cloud color when Short
No cloud color when flat

CSS:
#//@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 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 aggregation   = AggregationPeriod.DAY;

def na = Double.NaN;

#barssince(Condition) =>
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);
}

def up = Highest(TFHigh, EntryLength);
def down = Lowest(TFLow, EntryLength);
def sup = Highest(TFHigh, ExitLength);
def sdown = Lowest(TFLow, ExitLength);

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);
def K3 = If(TFClose > K1, down, na);
def K4 = If(TFClose < K1, up, na);

plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(2);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);

def lower = Lowest(TFLow, EntryLength);
def upper = Highest(TFHigh, EntryLength);
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 148, 255));
l.SetHiding(!ShowLines);

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

plot LongExit = if buyExit and O1 < O3[1] then up else na;
LongExit.SetPaintingStrategy(PaintingStrategy.POINTS);
LongExit.SetDefaultColor(CreateColor(0, 148, 255));
LongExit.SetLineWeight(3);
LongExit.SetHiding(!ShowLines);

plot ShortExit = if sellExit and O2 < O4[1] then down else na;
ShortExit.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortExit.SetDefaultColor(CreateColor(0, 148, 255));
ShortExit.SetLineWeight(3);
ShortExit.SetHiding(!ShowLines);

plot LongEntry = if buySignal and O3 < O1[1] then down else na;
LongEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(3);
LongEntry.SetHiding(!ShowLines);

plot ShortEntry = if sellSignal and O4 < O2[1] then up else na;
ShortEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortEntry.SetDefaultColor(CreateColor(255, 82, 82));
ShortEntry.SetLineWeight(3);
ShortEntry.SetHiding(!ShowLines);

#### 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  = if buyExit and SignalUp and O1 < O3[1] then up else na;
AddChartBubble(ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
def ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
AddChartBubble(ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);

def Long  =  if buySignal and SignalDn and O3 < O1[1] then down else na;
AddChartBubble(Long, SignalDn, "Long Entry", Color.GREEN, no);
def Short =  if sellSignal and SignalUp and O4 < O2[1] then up else na;
AddChartBubble(Short, SignalUp, "Short Entry", Color.RED, yes);

#### Cloud
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;
AddCloud(if color1 then u else na, ExitLine, Color.DARK_GREEN);
AddCloud(if color2 then ExitLine else na, l, Color.DARK_RED);

##### END
Looks good! It is possible to add the Basis (Mid-Level) line in your indicator?
 
I wanted to be able to go through a watchlist and see how this performed on each one, so I saved this as a Strategy by adding the following bit of code at the bottom. In use, I add FloatingPL as a lower Study to the chart.

Python:
#### Strategy Orders
addOrder(OrderType.SELL_TO_CLOSE, ExitLong);
addOrder(OrderType.BUY_TO_CLOSE, ExitShort);
addOrder(OrderType.BUY_TO_OPEN, Long);
addOrder(OrderType.SELL_TO_OPEN, Short);
 
Please explain, You wrote, " I add FloatingPL as a lower Study to the chart." Thanks
When you place a Strategy (not Study) onto a chart in TOS, there will be one or more "addOrder()" instructions in the code, as this is what differentiates Strategies from Studies. So if a Strategy is placing buy and sell orders (which are never really executed, even in your live trading account), it's rather natural to want to know what would happen as a result of those trades. To see the result, add the Study called "FloatingPL" and you'll see a bar chart representing the change in your funds as though the Strategy buy/sell orders had actually been placed. If you set a time period of a year, you can see how much money you would have gained or lost had you used that strategy for a year on the symbol being charted and taken all the trades it identifies.

Thanks for asking a question I can answer. Feel free to ask more questions.

Edit: I should add that using FloatingPL makes it easy to compare the same strategy on multiple symbols in a watch list, but to be able to compare one strategy with another, you need to possibly adjust how many $$$ are placed into the trade, or limit how many shares are in each position. Since I just left it default, it'll be buying and sell 100 shares each trade. I don't really care, as I'm trading options on it, not shares. I don't think they have the ability to trade options in addOrder() when on a stock chart, only if looking at an options chart.
 
Last edited:
I wanted to be able to go through a watchlist and see how this performed on each one, so I saved this as a Strategy by adding the following bit of code at the bottom. In use, I add FloatingPL as a lower Study to the chart.

Python:
#### Strategy Orders
addOrder(OrderType.SELL_TO_CLOSE, ExitLong);
addOrder(OrderType.BUY_TO_CLOSE, ExitShort);
addOrder(OrderType.BUY_TO_OPEN, Long);
addOrder(OrderType.SELL_TO_OPEN, Short);
Hi Cinnamon - Is it possible for you to compare different strategies for the same stock for the same timeframe here in DC7 for today's QQQ and share profit and loss results? You'll need to download and install DC7 code for it.
 
I am liking what you are doing with the Turtle Strategy, samer800. (y)

Since the original rules state for the S1 trade that one should only buy if previous S1 signal was a loss, or if previous S1 was a win then not to trade the current signal.

I am looking through your code and trying to figure out if your code takes the previous signal into account when it gives current buy /sell signals. If it does, which part please? Your coding level is definitely some steps above mine, I'm just a self taught fledgling.

Curious to learn more about coding keeping track of what the previous signal was and how it affects the current signal. Or, if it is more something we need to keep a separate mental note of what the previous signal outcome was before we consider trading the current signal.

Thanks, Rick
 
I am liking what you are doing with the Turtle Strategy, samer800. (y)

Since the original rules state for the S1 trade that one should only buy if previous S1 signal was a loss, or if previous S1 was a win then not to trade the current signal.

I am looking through your code and trying to figure out if your code takes the previous signal into account when it gives current buy /sell signals. If it does, which part please? Your coding level is definitely some steps above mine, I'm just a self taught fledgling.

Curious to learn more about coding keeping track of what the previous signal was and how it affects the current signal. Or, if it is more something we need to keep a separate mental note of what the previous signal outcome was before we consider trading the current signal.

Thanks, Rick
The posted code does not look at the result of the previous signal. I am currently trying to code that in the (little) spare time I have.
 
Thanks for confirming what I was thinking Merch_Man about previous signals being kept track of.

@_Merch_Man_ , @samer800 , @TraderZen , any others
Did some looking around and found that Eugene71 at TradingView has a Turtle System script and worked out the mechanics to track the previous signals to use them as a check with the current signal. He also included pyramiding position adds, the Turtle N (atr) variable for position sizing and a trailing stop as well.
 
Last edited by a moderator:
Thanks for confirming what I was thinking Merch_Man about previous signals being kept track of.

@_Merch_Man_ , @samer800 , @TraderZen , any others
Did some looking around and found that Eugene71 at TradingView has a Turtle System script and worked out the mechanics to track the previous signals to use them as a check with the current signal. He also included pyramiding position adds, the Turtle N (atr) variable for position sizing and a trailing stop as well.

Here is a link if you are interested. I will have a tinker with converting it over to thinkorswim but may need a smarter coder than myself to finish it.
https://www.tradingview.com/script/cOv24513-Turtle-System/
[ POV: Strategy is from the B&W movie era. This was written in 2019. I am getting to know about it in 2022, while I spend significant amount of time reading on such topics online. I don't know what to think! ]

@cinnamon - This code only confirms the ATR based Pyramiding approach that I proposed after inspecting the Donchian Strategy code. I had already thought of adding risk-reward (target and stop-loss) lines. This is the exact implementation of that idea.

@RickAns - Great find and thank you for sharing! Aesthetically I like TuTCI far better. Features wise we have a structure in place in DC7 code. I have learned about 3 more strategies and goal is to provide all of the strategies in one place to choose from. I'd prefer enhancing DC7 as per the original plan. This will help with backtesting them as well (after a proper ToS Strategy is created for DC7).

By all means- if somebody wants to take a lead on converting this separately - totally worth it, IMHO!
 
0moywTj.png

I added to the below more setting to remove/add lines and signals with option to use the indicator in different timeframe.

Release Note:
Legendary trade system which proved that great traders can be made, not born.
Turtle Trade Experiment made 80% annual return for 4 years and made 150 million $
Turtle Trade trend following system is a complete opposite to the "buy low and sell high" approach.
This trend following system was taught to a group of average and normal individuals, and almost everyone turned into a profitable trader.
They used the basis logic of well known DONCHIAN CHANNELS which developed by Richard Donchian .
The main rule is "Trade an 20-day breakout and take profits when an 10-day high or low is breached ". Examples:
Buy a 20-day breakout and close the trade when price action reaches a 10-day low.
Go short a 20-day breakout and close the trade when price action reaches a 10-day high.
In this indicator,
The red line is the trading line which indicates the trend direction:
Price bars over the trend line indicates uptrend
Price bars under the trend line means downtrend
The dotted blue line is the exit line.


Original system is:
Go long when the price High is equal to or above previous 20 day Highest price.
Go short when the price Low is equal to or below previous 20 day Lowest price.
Exit long positions when the price touches the exit line
Exit short positions when the price touches the exit line
Recommended initial stop-loss is ATR * 2 from the opening price.

Default system parameters were 20,10 and 55,20.

Original Turtle Rules:

To trade exactly like the turtles did, you need to set up two indicators representing the main and the failsafe system.
Set up the main indicator with EntryPeriod = 20 and ExitPeriod = 10 (A.k.a S1) (My comments: Good for Swing - Long timeframe)
Set up the failsafe indicator with EntryPeriod = 55 and ExitPeriod = 20 using a different color. (A.k.a S2) (My comment: Good for intraday - Short timeframe)
The entry strategy using S1 is as follows

Buy 20-day breakouts using S1 only if last signaled trade was a loss.
Sell 20-day breakouts using S1 only if last signaled trade was a loss.
If last signaled trade by S1 was a win, you shouldn't trade -Irregardless of the direction or if you traded last signal it or not-

The entry strategy using S2 is as follows:
Buy 55-day breakouts only if you ignored last S1 signal and the market is rallying without you
Sell 55-day breakouts only if you ignored last S1 signal and the market is pluging without you

You can Highlight the chart with provided trade signals:

Green cloud color when Long
Red cloud color when Short
No cloud color when flat

CSS:
#//@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 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 aggregation   = AggregationPeriod.DAY;

def na = Double.NaN;

#barssince(Condition) =>
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);
}

def up = Highest(TFHigh, EntryLength);
def down = Lowest(TFLow, EntryLength);
def sup = Highest(TFHigh, ExitLength);
def sdown = Lowest(TFLow, ExitLength);

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);
def K3 = If(TFClose > K1, down, na);
def K4 = If(TFClose < K1, up, na);

plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(2);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);

def lower = Lowest(TFLow, EntryLength);
def upper = Highest(TFHigh, EntryLength);
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 148, 255));
l.SetHiding(!ShowLines);

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

plot LongExit = if buyExit and O1 < O3[1] then up else na;
LongExit.SetPaintingStrategy(PaintingStrategy.POINTS);
LongExit.SetDefaultColor(CreateColor(0, 148, 255));
LongExit.SetLineWeight(3);
LongExit.SetHiding(!ShowLines);

plot ShortExit = if sellExit and O2 < O4[1] then down else na;
ShortExit.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortExit.SetDefaultColor(CreateColor(0, 148, 255));
ShortExit.SetLineWeight(3);
ShortExit.SetHiding(!ShowLines);

plot LongEntry = if buySignal and O3 < O1[1] then down else na;
LongEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(3);
LongEntry.SetHiding(!ShowLines);

plot ShortEntry = if sellSignal and O4 < O2[1] then up else na;
ShortEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortEntry.SetDefaultColor(CreateColor(255, 82, 82));
ShortEntry.SetLineWeight(3);
ShortEntry.SetHiding(!ShowLines);

#### 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  = if buyExit and SignalUp and O1 < O3[1] then up else na;
AddChartBubble(ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
def ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
AddChartBubble(ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);

def Long  =  if buySignal and SignalDn and O3 < O1[1] then down else na;
AddChartBubble(Long, SignalDn, "Long Entry", Color.GREEN, no);
def Short =  if sellSignal and SignalUp and O4 < O2[1] then up else na;
AddChartBubble(Short, SignalUp, "Short Entry", Color.RED, yes);

#### Cloud
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;
AddCloud(if color1 then u else na, ExitLine, Color.DARK_GREEN);
AddCloud(if color2 then ExitLine else na, l, Color.DARK_RED);

##### END
What chart is Good for intraday?
 
0moywTj.png

I added to the below more setting to remove/add lines and signals with option to use the indicator in different timeframe.

Release Note:
Legendary trade system which proved that great traders can be made, not born.
Turtle Trade Experiment made 80% annual return for 4 years and made 150 million $
Turtle Trade trend following system is a complete opposite to the "buy low and sell high" approach.
This trend following system was taught to a group of average and normal individuals, and almost everyone turned into a profitable trader.
They used the basis logic of well known DONCHIAN CHANNELS which developed by Richard Donchian .
The main rule is "Trade an 20-day breakout and take profits when an 10-day high or low is breached ". Examples:
Buy a 20-day breakout and close the trade when price action reaches a 10-day low.
Go short a 20-day breakout and close the trade when price action reaches a 10-day high.
In this indicator,
The red line is the trading line which indicates the trend direction:
Price bars over the trend line indicates uptrend
Price bars under the trend line means downtrend
The dotted blue line is the exit line.


Original system is:
Go long when the price High is equal to or above previous 20 day Highest price.
Go short when the price Low is equal to or below previous 20 day Lowest price.
Exit long positions when the price touches the exit line
Exit short positions when the price touches the exit line
Recommended initial stop-loss is ATR * 2 from the opening price.

Default system parameters were 20,10 and 55,20.

Original Turtle Rules:

To trade exactly like the turtles did, you need to set up two indicators representing the main and the failsafe system.
Set up the main indicator with EntryPeriod = 20 and ExitPeriod = 10 (A.k.a S1) (My comments: Good for Swing - Long timeframe)
Set up the failsafe indicator with EntryPeriod = 55 and ExitPeriod = 20 using a different color. (A.k.a S2) (My comment: Good for intraday - Short timeframe)
The entry strategy using S1 is as follows

Buy 20-day breakouts using S1 only if last signaled trade was a loss.
Sell 20-day breakouts using S1 only if last signaled trade was a loss.
If last signaled trade by S1 was a win, you shouldn't trade -Irregardless of the direction or if you traded last signal it or not-

The entry strategy using S2 is as follows:
Buy 55-day breakouts only if you ignored last S1 signal and the market is rallying without you
Sell 55-day breakouts only if you ignored last S1 signal and the market is pluging without you

You can Highlight the chart with provided trade signals:

Green cloud color when Long
Red cloud color when Short
No cloud color when flat

CSS:
#//@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 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 aggregation   = AggregationPeriod.DAY;

def na = Double.NaN;

#barssince(Condition) =>
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);
}

def up = Highest(TFHigh, EntryLength);
def down = Lowest(TFLow, EntryLength);
def sup = Highest(TFHigh, ExitLength);
def sdown = Lowest(TFLow, ExitLength);

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);
def K3 = If(TFClose > K1, down, na);
def K4 = If(TFClose < K1, up, na);

plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(2);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);

def lower = Lowest(TFLow, EntryLength);
def upper = Highest(TFHigh, EntryLength);
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 148, 255));
l.SetHiding(!ShowLines);

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

plot LongExit = if buyExit and O1 < O3[1] then up else na;
LongExit.SetPaintingStrategy(PaintingStrategy.POINTS);
LongExit.SetDefaultColor(CreateColor(0, 148, 255));
LongExit.SetLineWeight(3);
LongExit.SetHiding(!ShowLines);

plot ShortExit = if sellExit and O2 < O4[1] then down else na;
ShortExit.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortExit.SetDefaultColor(CreateColor(0, 148, 255));
ShortExit.SetLineWeight(3);
ShortExit.SetHiding(!ShowLines);

plot LongEntry = if buySignal and O3 < O1[1] then down else na;
LongEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(3);
LongEntry.SetHiding(!ShowLines);

plot ShortEntry = if sellSignal and O4 < O2[1] then up else na;
ShortEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortEntry.SetDefaultColor(CreateColor(255, 82, 82));
ShortEntry.SetLineWeight(3);
ShortEntry.SetHiding(!ShowLines);

#### 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  = if buyExit and SignalUp and O1 < O3[1] then up else na;
AddChartBubble(ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
def ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
AddChartBubble(ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);

def Long  =  if buySignal and SignalDn and O3 < O1[1] then down else na;
AddChartBubble(Long, SignalDn, "Long Entry", Color.GREEN, no);
def Short =  if sellSignal and SignalUp and O4 < O2[1] then up else na;
AddChartBubble(Short, SignalUp, "Short Entry", Color.RED, yes);

#### Cloud
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;
AddCloud(if color1 then u else na, ExitLine, Color.DARK_GREEN);
AddCloud(if color2 then ExitLine else na, l, Color.DARK_RED);

##### END
Is it possible to build a scanner for this, or add a collum to the watchlist?
 
How can I make a watch list that would signal when there is a long or short with this strategy?


//@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 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 aggregation = AggregationPeriod.DAY;

def na = Double.NaN;

#barssince(Condition) =>
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);
}

def up = Highest(TFHigh, EntryLength);
def down = Lowest(TFLow, EntryLength);
def sup = Highest(TFHigh, ExitLength);
def sdown = Lowest(TFLow, ExitLength);

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);
def K3 = If(TFClose > K1, down, na);
def K4 = If(TFClose < K1, up, na);

plot TrendLine = K1;
TrendLine.SetDefaultColor(CreateColor(255, 82, 82));
TrendLine.SetLineWeight(2);
TrendLine.SetHiding(!ShowLines);
plot ExitLine = K2;
ExitLine.SetStyle(Curve.POINTS);
ExitLine.SetDefaultColor(CreateColor(0, 148, 255));
ExitLine.SetHiding(!ShowLines);

def lower = Lowest(TFLow, EntryLength);
def upper = Highest(TFHigh, EntryLength);
plot u = upper;
plot l = lower;
u.SetDefaultColor(CreateColor(0, 148, 255));
u.SetHiding(!ShowLines);
l.SetDefaultColor(CreateColor(0, 148, 255));
l.SetHiding(!ShowLines);

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

plot LongExit = if buyExit and O1 < O3[1] then up else na;
LongExit.SetPaintingStrategy(PaintingStrategy.POINTS);
LongExit.SetDefaultColor(CreateColor(0, 148, 255));
LongExit.SetLineWeight(3);
LongExit.SetHiding(!ShowLines);

plot ShortExit = if sellExit and O2 < O4[1] then down else na;
ShortExit.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortExit.SetDefaultColor(CreateColor(0, 148, 255));
ShortExit.SetLineWeight(3);
ShortExit.SetHiding(!ShowLines);

plot LongEntry = if buySignal and O3 < O1[1] then down else na;
LongEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(3);
LongEntry.SetHiding(!ShowLines);

plot ShortEntry = if sellSignal and O4 < O2[1] then up else na;
ShortEntry.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortEntry.SetDefaultColor(CreateColor(255, 82, 82));
ShortEntry.SetLineWeight(3);
ShortEntry.SetHiding(!ShowLines);

#### 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 = if buyExit and SignalUp and O1 < O3[1] then up else na;
AddChartBubble(ExitLong, SignalUp, "Exit Long", CreateColor(0, 148, 255), yes);
def ExitShort = if sellExit and SignalDn and O2 < O4[1] then down else na;
AddChartBubble(ExitShort, SignalDn, "Exit Short", CreateColor(0, 148, 255), no);

def Long = if buySignal and SignalDn and O3 < O1[1] then down else na;
AddChartBubble(Long, SignalDn, "Long Entry", Color.GREEN, no);
def Short = if sellSignal and SignalUp and O4 < O2[1] then up else na;
AddChartBubble(Short, SignalUp, "Short Entry", Color.RED, yes);

#### Cloud
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;
AddCloud(if color1 then u else na, ExitLine, Color.DARK_GREEN);
AddCloud(if color2 then ExitLine else na, l, Color.DARK_RED);

##### END
 

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
451 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