Donchian Channel Mods For ThinkOrSwim

mrmac

New member
Hi, I have a very simple donchian channel indicator and would like to add a "shift" (I think it is called shift) function to it, is this possible please?

Code:
input length_used_for_calc= 10;
input Use_Other_Time_Frame=yes;
input Time_Frame=aggregationPeriod.DAY;
def h= if use_Other_Time_Frame then high(period=time_Frame) else high;
def l= if use_Other_Time_Frame then low(period=time_Frame) else low;
def highline= highest(high,length_used_for_calc);
def lowline= lowest(low,length_used_for_calc);
Plot High_Line= highline;
high_line.setdefaultColor(color.magENTA);
Plot Low_Line=lowline;
low_Line.setdefaultColor(color.magentA);
 
Last edited by a moderator:

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

Maybe you mean "Displace".
You would need;
input displace = x;
plot High_Line= (highline[displace]);
Plot Low_Line=(lowline[displace]);
 
Hi -- has anyone played around with TOS's Donchian Channel? Donchian is a great concept but I think TOS has sort of muddied it up (I could be wrong). According to everything I've read, Donchian is a clear upper and lower channel with a clear mid line. TOS seems to ignore the mid line -- it just doesn't exist -- and it has all those short/long enter/exit bubbles, which thankfully be can disabled. So I was really hoping to find a version more like tradingview's. Simple upper/lower channel, mid line, adjustable length, etc.

Thanks

uQkEFmH.png


DyybUur.png
 
Here is the version I have which has the midline... I also added a cloud...

Ruby:
#Name:             DonchianChannel
#Programmed By:    Chris Ball ([email protected]) on 10/23/08
#Posted At:        http://chartingwithchris.blogspot.com
#Description:      This is a channel system that is used frequently for trend trading.  Google the term "turtle trader" for more information.
#Modified 2020-06-10 by rad14733 to utilize AddCloud()

input length = 20;

plot upperBand = Highest(high[1], length);
plot lowerBand = Lowest(low[1], length);
plot middleBand = (upperBand + lowerBand) / 2;

upperBand.SetDefaultColor(Color.DARK_GRAY);
lowerBand.SetDefaultColor(Color.DARK_GRAY);
middleBand.SetDefaultColor(Color.Orange);

AddCloud(upperBand, lowerBand, Color.DARK_GRAY, Color.WHITE);
 
Any help will be appreciated. not looking to have someone code this, just some pointers or direction.
I am working with a UST strategy and candles are being painted, what I want to do is count the instances of less than 3 candles between every opening AddOrder and closing AddOrder and have it displayed as a label with % of total AddOrders. yes I can run a report and manually look at it but I want to be able to see at a glance on chart how it is performing. trouble I am running into is not every candle between AddOrder is not an open>close and do I need some sort of counter for AddOrder? I have tried some of the one's here but they dont seem to count the number of AddOrders like the "report" does.
thanks
 
Any help will be appreciated. not looking to have someone code this, just some pointers or direction.
I am working with a UST strategy and candles are being painted, what I want to do is count the instances of less than 3 candles between every opening AddOrder and closing AddOrder and have it displayed as a label with % of total AddOrders. yes I can run a report and manually look at it but I want to be able to see at a glance on chart how it is performing. trouble I am running into is not every candle between AddOrder is not an open>close and do I need some sort of counter for AddOrder? I have tried some of the one's here but they dont seem to count the number of AddOrders like the "report" does.
thanks

This will hopefully help get you started using the Donchian Strategy on GME 1d1m chart on 9/13/2021 data. There are labels explaining some of the statistic code added to the bottom of the following strategy run. I did not work on the 3 bar or less turnaround, but the rest seems to work correctly. There is also a yes/no debug, which you can see as the 1/0 above/below the candles on in the picture below, to assist in coding.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2014-2021
#

input entryLength = 40;
input exitLength = 15;
input atrLength = 20;
input atrFactor = 0.9;
input atrStopFactor = 4.0;
input atrAverageType = AverageType.SIMPLE;

def entryUpper = Highest(high, entryLength)[1];
def entryLower = Lowest(low, entryLength)[1];
def exitUpper = Highest(high, exitLength)[1];
def exitLower = Lowest(low, exitLength)[1];

def atr = reference ATR(length = atrLength, "average type" = atrAverageType);
def volatilityOk = TrueRange(high, low, close)[1] < atrFactor * atr[1];

def buyToOpen = (atrFactor == 0 or volatilityOk) and high > entryUpper;
def sellToOpen = (atrFactor == 0 or volatilityOk) and low < entryLower;
def buyToClose = high > exitUpper;
def sellToClose = low < exitLower;

def position = {default none, long, short};
position = if (buyToOpen or (position[1] == position.long and !sellToClose)) then position.long
    else if (sellToOpen or (position[1] == position.short and !buyToClose)) then position.short
    else position.none;

plot BuyStop;
plot CoverStop;
if (position[1] == position.short) {
    BuyStop = Double.NaN;
    CoverStop = exitUpper;
} else {
    BuyStop = entryUpper;
    CoverStop = Double.NaN;
}

plot ShortStop;
plot SellStop;
if (position[1] == position.long) {
    ShortStop = Double.NaN;
    SellStop = exitLower;
} else {
    ShortStop = entryLower;
    SellStop = Double.NaN;
}

BuyStop.SetDefaultColor(GetColor(3));
CoverStop.SetDefaultColor(GetColor(6));
ShortStop.SetDefaultColor(GetColor(4));
SellStop.SetDefaultColor(GetColor(5));

AddOrder(OrderType.BUY_TO_OPEN, buyToOpen, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "DonchianLE");
AddOrder(OrderType.SELL_TO_OPEN, sellToOpen, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "DonchianSE");
AddOrder(OrderType.SELL_TO_CLOSE, sellToClose, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "DonchianLX");
AddOrder(OrderType.BUY_TO_CLOSE, buyToClose, tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "DonchianSX");

def entryPrice = EntryPrice();

AddOrder(OrderType.SELL_TO_CLOSE, if atrStopFactor != 0 then low < entryPrice - atrStopFactor * atr else no, tickcolor = GetColor(5), arrowcolor = GetColor(7), name = "DonchianATRLX");
AddOrder(OrderType.BUY_TO_CLOSE, if atrStopFactor != 0 then high > entryPrice + atrStopFactor * atr else no, tickcolor = GetColor(6), arrowcolor = GetColor(8), name = "DonchianATRSX");

#Trade Statistics----------------------------------------------
input debug = yes;
plot x = position == position.long;
plot y = position == position.short;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
y.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
x.sethiding(!debug);
y.sethiding(!debug);

def nocount  = if (selltoclose[1] or buytoclose[1]) and  y == 0 and x == 0 then 0 else if x == 1 or y == 1 then 0  else nocount[1] + 1;
Addlabel(1, "Curr NoTrade: " + nocount,color.white);

def buycount = if x[1]==0 and x == 1 then 0 else if sum(x[1]==0,2)==2 then 0 else  buycount[1] + 1;
AddLabel(1, "Curr Long: "  + buycount , Color.WHITE);
def sellcount = if y[1]==0 and y == 1 then 0 else if sum(y[1]==0,2)==2 then 0 else sellcount[1] + 1;
AddLabel(1, "Curr Short: "  + sellcount, Color.WHITE);

def buytrades = if x[1] != 1 and x == 1 then buytrades[1] + 1 else buytrades[1];
AddLabel(1, "#Long Trades: " + buytrades, Color.YELLOW);
def selltrades = if y[1] != 1 and y == 1 then selltrades[1] + 1 else selltrades[1];
AddLabel(1, "#Short Trades: " + selltrades, Color.YELLOW);

def ttrades = if position != position[1] then ttrades[1] + 1 else ttrades[1];
AddLabel(1, "Total Trades: " + ttrades);

def torders = Floor(ttrades / 2);
AddLabel(1, "Total Orders: " + torders);
 
#DonchianChannels
#mpardesi 05/04/22
# modified thinkorswim and added cloud colors indicating uptrend/downtrend
input DCentryLength = 20;# was 40
input DCexitLength = 20; # was 15
input DCd_atrLength = 20; # was 20
input DCatrFactor = 1.8; # was 4
input DCatrAverageType = AverageType.SIMPLE;

def DCentryUpper = Highest(high, DCentryLength)[1];
def DCentryLower = Lowest(low, DCentryLength)[1];
def DCexitUpper = Highest(high, DCexitLength)[1];
def DCexitLower = Lowest(low, DCexitLength)[1];
def DCWidth = DCentryUpper - DCentryLower;
def DCatr = reference ATR(length =DCd_atrLength, "average type" = DCatrAverageType);
def DCvolatilityOk = TrueRange(high, low, close)[1] < DCatrFactor * DCatr[1];

def DCbuyToOpen = (DCatrFactor == 0 or DCvolatilityOk) and high > DCentryUpper ;
def DCsellToOpen = (DCatrFactor == 0 or DCvolatilityOk) and low < DCentryLower ;
def DCbuyToClose = high > DCexitUpper;
def DCsellToClose = low < DCexitLower;
def DCbuyToOpenStopLoss = low < DCentryLower;
def DCsellToOpenStopLoss = high > DCentryUpper;

plot DCBuyStop;
plot DCCoverStop;
plot DCShortStop;
plot DCSellStop;

DCBuyStop.SetDefaultColor(color.red);
DCBuyStop.SetStyle(Curve.LONG_DASH);
DCBuyStop.SetLineWeight(1);

DCCoverStop.SetDefaultColor(color.yellow);
DCCoverStop.SetStyle(Curve.LONG_DASH);
DCCoverStop.SetLineWeight(1);

DCShortStop.SetDefaultColor(color.green);
DCShortStop.SetStyle(Curve.LONG_DASH);
DCShortStop.SetLineWeight(1);

DCSellStop.SetDefaultColor(GetColor(5));
DCSellStop.SetStyle(Curve.LONG_DASH);
DCSellStop.SetLineWeight(1);

def DCHpositionLong= if (DCbuyToOpen or (DCHpositionLong[1] and !DCsellToClose)) then 1 else 0;
def DCHpositionShort= if (DCsellToOpen or (DCHpositionShort[1] and !DCbuyToClose)) then 1 else 0;

if (DCHpositionShort[1]) {
DCBuyStop = Double.NaN;
DCCoverStop = DCexitUpper;
} else {
DCBuyStop = DCentryUpper;
DCCoverStop = Double.NaN;
}

if (DCHpositionLong[1]) {
DCShortStop = Double.NaN;
DCSellStop = DCexitLower;
} else {
DCShortStop = DCentryLower;
DCSellStop = Double.NaN;
}

plot DCentryBasicline = (DCentryUpper + DCentryLower ) / 2;
plot DCexitBasicline = (DCexitUpper + DCexitLower ) / 2;
DCexitBasicline.SetDefaultColor(color.WHITE);
DCexitBasicline.SetStyle(Curve.LONG_DASH);
DCexitBasicline.SetLineWeight(2);


DefineGlobalColor("TRENDA", color.RED);
DefineGlobalColor("TRENDC", CreateColor(139,34,82));
DefineGlobalColor("TRENDB", Color.LIGHT_GREEN);

AddCloud(DCBuyStop, DCSellStop,GlobalColor("TRENDB"),GlobalColor("TRENDC"));
AddCloud( DCCoverStop,DCShortStop,GlobalColor("TRENDA"),GlobalColor("TRENDC"));
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
417 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