#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#
def open_time = 0930;
def closing_time = 1600;
def price = close;
def inside = high < high[1] and low > low[1];
def outside = high > high[1] and low < low[1];
AssignPriceColor(if inside
then Color.BLUE
else Color.CURRENT);
AssignPriceColor(if outside
then Color.white
else Color.CURRENT);
def double_inside = high[1] < high[2] && Low[1] > Low[2] && high[2] < high[3] && Low[2] >Low[3];
#OUTS_-----------------------
def nan2 = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
(afterEnd[-2] == 1 and afterEnd == 0) or
(isRollover[-2] and firstBarOfDay[-2])
then 1
else 0;
plot data = close;
data.Hide();
def breakout = high[1] < high[2] && Low[1] > Low[2] && high crosses above high[1] ;
def outsidebreakout = high[1] > high[2] && Low[1] < Low[2] && high crosses above high[1] ;
def Breakdown = high[1] < high[2] && Low[1] > Low[2] && Low crosses below Low[1];
def outsidereakdown = high[1] > high[2] && Low[1] < Low[2] && Low crosses below Low[1] ;
def BullBreak = double_inside && High crosses above high[3];
def BearBreak = double_inside && Low crosses below Low[3];
AddOrder(OrderType.BUY_TO_OPEN, Breakout[-1] && SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800,name = "Long Entry", tickcolor = GetColor(8), arrowcolor = GetColor(8));
AddOrder(OrderType.BUY_TO_OPEN,BullBreak[-1] && SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800,name = "Long Entry", tickcolor = GetColor(8), arrowcolor = GetColor(8));
AddOrder(OrderType.BUY_TO_OPEN, outsidebreakout[-1] && SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800,name = "Long Entry", tickcolor = GetColor(8), arrowcolor = GetColor(8));
#AddOrder(OrderType.BUY_TO_OPEN, outside and close crosses above open, tickcolor = GetColor(8), arrowcolor = GetColor(8));
#addOrder(OrderType.BUY_TO_OPEN, close crosses above close[1] and high[1] < high[2] and low[1] > low[2] and close[2] > close[3], tickColor = GetColor(9), arrowColor = GetColor(9));
AddOrder(OrderType.SELL_TO_CLOSE, High crosses above high[3] or Low crosses below Low[2] , name = "Sell", tickcolor = GetColor(9), arrowcolor = GetColor(9));
AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay, tickcolor = Color.PLUM, arrowcolor = Color.PLUM, name = "EOD");
AddOrder(OrderType.SELL_TO_OPEN, Breakdown[-1] && SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800, name = "Short", tickcolor = GetColor(9), arrowcolor = GetColor(9));
AddOrder(OrderType.SELL_TO_OPEN, BearBreak[-1]&& SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800, name = "Short", tickcolor = GetColor(9), arrowcolor = GetColor(9));
AddOrder(OrderType.SELL_TO_OPEN, outsidereakdown[-1] && SecondsFromTime(open_time) > 900 && SecondsTillTime(closing_time) > 1800, name = "Short", tickcolor = GetColor(9), arrowcolor = GetColor(9));
#AddOrder(OrderType.SELL_TO_OPEN, outside and close crosses below open, tickcolor = GetColor(9), arrowcolor = GetColor(9));
#addOrder(OrderType.SELL_TO_OPEN, close crosses below close[1] and high[1] < high[2] and close[2] < close[3], tickColor = GetColor(9), arrowColor = GetColor(9));
AddOrder(OrderType.BUY_TO_CLOSE, Low < Low[3] or high crosses above high[2], name = "Cover", tickcolor = GetColor(9), arrowcolor = GetColor(9));
AddOrder(OrderType.BUY_TO_CLOSE, lastBarOfDay, tickcolor = Color.PLUM, arrowcolor = Color.PLUM, name = "EOD");
###Targets
#addOrder(OrderType.SELL_TO_CLOSE, entryprice + range, name = "Target",tickColor = GetColor(6), arrowColor = GetColor(6));
#addOrder(OrderType.SELL_TO_CLOSE, high >= targetPrice, tickColor = GetColor(6), arrowColor = GetColor(6));
#addOrder(OrderType.BUY_TO_CLOSE, entryprice - range, name = "Target",tickColor = GetColor(6), arrowColor = GetColor(6));
#addOrder(OrderType.BUY_TO_CLOSE, low <= targetPrice, tickColor = GetColor(6), arrowColor = GetColor(6));
##Stop Loss
#addOrder(OrderType.BUY_TO_CLOSE, high >= stopPrice,name = "Stop Out", tickColor = GetColor(5), arrowColor = GetColor(5));
#addOrder(OrderType.SELL_TO_CLOSE, low <= stopPrice, name = "Stop Out", tickColor = GetColor(5), arrowColor = GetColor(5));
# SCENARIOS
# -----------------------------------
def day = AggregationPeriod.DAY;
def Vday = GetAggregationPeriod() <= AggregationPeriod.DAY;
def dayO;
def dayC;
def dayH;
def dayL;
if Vday {
dayO = open(period = day);
dayC = close(period = day);
dayH = high(period = day);
dayL = low(period = day);
} else {
dayO = Double.NaN;
dayC = Double.NaN;
dayH = Double.NaN;
dayL = Double.NaN;
}
def diff = high[5] - low[5];
#Globals
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
# SHARECALC
def qty = Round(2500 / dayO, 0);
def risk = qty * diff;
#AddLabel(yes, Round(qty, 0), color.white);
#AddLabel(yes, Round(risk, 1), if risk > 50 then color.red else if risk < 50 then color.green else color.white);
#Inputs
input fplTargetWinLoss = .50;
#hint fplTargetWinLoss: sets the target winlossRatio (in percent) which determines display colors of the W/L label.
input fplTargetWinRate = 1;
#hint fplTargetWinRate: sets the target winRate (float) which determines display colors of the WinRate label;
input fplHidePrice = no;
#hint fplHidePrice: hide's the underlying price graph. \nDefault is no.
input fplHideFPL = yes;
#hint fplHideFPL: hide's the underlying P&L graph.\nDefault is yes.
#temp input var references
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def hidePrice = fplHidePrice;
def hideFPL = fplHideFPL;
#hide chart candles
HidePricePlot(hidePrice);
#Plot default Floating P&L
plot FPL = FPL();
FPL.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
FPL.DefineColor("Positive and Up", Color.GREEN);
FPL.DefineColor("Positive and Down", Color.DARK_GREEN);
FPL.DefineColor("Negative and Down", Color.RED);
FPL.DefineColor("Negative and Up", Color.DARK_RED);
FPL.AssignValueColor(if FPL >= 0
then if FPL > FPL[1]
then FPL.Color("Positive and Up")
else FPL.Color("Positive and Down")
else if FPL < FPL[1]
then FPL.Color("Negative and Down")
else FPL.Color("Negative and Up"));
FPL.SetHiding(hideFPL);
plot ZeroLine = if IsNaN(close)
then nan
else 0;
ZeroLine.SetDefaultColor(Color.GRAY);
ZeroLine.SetHiding(hideFPL);
#Global Scripts
script incrementValue {
input condition = yes;
input increment = 1;
input startingValue = 0;
def _value = CompoundValue(1,
if condition
then _value[1] + increment
else _value[1], startingValue);
plot incrementValue = _value;
}
;
# Entry Calculations. Note: Only parses on a Strategy Chart
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry)
then entry
else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
#is active trade
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def fplreturn = (FPL - FPL[1]) / FPL[1];
def cumsum = Sum(fplreturn);
def highBarNumber = CompoundValue(1, if FPL == highFPL
then bn
else highBarNumber[1], 0);
def lowBarNumber = CompoundValue(1, if FPL == lowFPL
then bn
else lowBarNumber[1], 0);
#Win/Loss ratios
def entryBarsTemp = if hasEntry
then bn
else nan;
def entryBarNum = if hasEntry and isNewEntry
then bn
else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar
then FPL
else entryBarPL[1];
def exitBarsTemp = if !hasEntry
and bn > entryBarsTemp[1]
then bn
else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1])
then bn
else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar
then FPL
else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
#Counts
def entryCount = incrementValue(entryFPLAll);
def winCount = incrementValue(isWin);
def lossCount = incrementValue(isLoss);
def highestReturn = if entryReturnWin[1] > highestReturn[1]
then entryReturnWin[1]
else highestReturn[1];
def lowestReturn = if entryReturnLoss[1] < lowestReturn[1]
then entryReturnLoss[1]
else lowestReturn[1];
def winRate = winCount / lossCount;
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;
#Labels
AddLabel(yes,
text = "Total Trades: " + entryCount + " ",
color = Color.CYAN
);
AddLabel(yes,
text = "WinCount: " + winCount +
" | LossCount: " + lossCount +
" | WinRate: " + Round(winRate, 2) + " ",
color = if winRate >= targetWinRate
then Color.GREEN
else Color.MAGENTA
);
AddLabel(yes,
text = "W/L: " + AsPercent(winLossRatio) + " ",
color = if winLossRatio > targetWinLoss
then Color.GREEN
else Color.MAGENTA
);
AddLabel(yes,
text = "AvgReturn: " + AsDollars(avgReturn) +
" | AvgWin: " + AsDollars(avgWin) +
" | AvgLoss: " + AsDollars(avgLoss) + " ",
color = if avgReturn >= 0
then Color.DARK_ORANGE
else Color.MAGENTA
);
AddLabel(yes,
text = "Total Profit: " + AsDollars(FPL) + " ",
color = if FPL > 0
then Color.GREEN
else Color.MAGENTA
);