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