#JT Newest Strategy Based on EMAs, TSI, Premarket Highs/Lows, and MACD
#VERSION 10 - 05/19/2022
#Notes
Declare Upper;
##################################################################
# OPTIONS #
##################################################################
input ShowTestBubbles = no;
input ShowFlexGridBackgroundColor = no;
input ShowColorDefLabels = yes;
input AlertOn = yes;
input Show200EMA = no;
input ShowEMACloud = no;
input ShowPreMarketCloud = no;
input ShowPreMarketLabel = yes;
input ShowPrevHighLowLabel = no;
input ShowBuySellBubbles = no;
input ShowOverExtCloud = no;
input ShowTkPftBubble = no;
input ShowOrders = no;
input ShowTkPftOrders = no;
##################################################################
# COLORS #
##################################################################
DefineGlobalColor("200EMA", createcolor (255, 255, 255));
DefineGlobalColor("CloudUp", createcolor (102, 255, 102));
DefineGlobalColor("CloudDown", createcolor (255, 102, 102));
DefineGlobalColor("OpenLongPosition", createcolor (0, 255, 0));
DefineGlobalColor("OpenShortPosition", createcolor (255, 0, 0));
DefineGlobalColor("HoldLongPosition", createcolor (153, 255, 153));
DefineGlobalColor("HoldShortPosition", createcolor (255, 153, 153));
DefineGlobalColor("SellPosition", createcolor (255, 0, 0));
DefineGlobalColor("TakeProfit", createcolor (255, 0, 255));
DefineGlobalColor("CriteriaNotMet", createcolor (102, 102, 102)); #(255, 255, 0)
##################################################################
# TIMES #
##################################################################
#START AND END TIMES
input TimeFrame = {default "5-Min", "1-Min", "10-Min", "15-Min", "30-Min", "1-Hour"};
input PreMarketStart = 0700; #EST
input PreMarketEnd = 0929; #EST
input StartTime = 0930; #EST
input BuySignalDelay = 0;
input EndTime = 1600; #EST
input BuySignalStop = 60; #Minutes Before End of Day
def LastCandleStop; #Last Candle is Sell Candle
switch (TimeFrame) {
case "1-min": LastCandleStop = 1;
case "10-Min": LastCandleStop = 10;
case "15-Min": LastCandleStop = 15;
case "30-Min": LastCandleStop = 30;
case "1-Hour": LastCandleStop = 60;
default: LastCandleStop = 5;
}
def TradingDayStart= SecondsFromTime(StartTime);
def BuySignalDelaySeconds = BuySignalDelay * 60;
def SignalStart = TradingDayStart > BuySignalDelaySeconds;
def TradingDayEnd = SecondsTillTime(EndTime);
def EndSignalDelaySeconds = BuySignalStop * 60;
def SignalEnd = TradingDayEnd > EndSignalDelaySeconds;
def TradingDay = SignalStart and SignalEnd; #10:00EST - 1500EST
def LastStopDelaySeconds = LastCandleStop * 60;
def EndDay = TradingDayEnd == LastStopDelaySeconds; #1550EST
def TradingDayExt = TradingDayEnd > LastStopDelaySeconds;
##################################################################
# INDICATORS #
##################################################################
#200 EMA
plot EMA200 = ExpAverage(HL2, 200);
EMA200.SetDefaultColor(globalcolor("200EMA"));
EMA200.HideBubble();
EMA200.sethiding(!Show200EMA);
#EMAS
input Ema1Length = 5;
input Ema2Length = 12;
plot EMA1 = ExpAverage(close, Ema1Length);
EMA1.SetStyle(Curve.Short_Dash);
EMA1.SetDefaultColor(globalcolor("CloudUp"));
EMA1.HideBubble();
plot EMA2 = ExpAverage(close, Ema2Length);
EMA2.SetDefaultColor(globalcolor("CloudDown"));
EMA2.HideBubble();
EMA2.sethiding(!ShowEMACloud);
AddCloud(if ShowEMACloud then EMA1 else double.nan, if ShowEMACloud then EMA2 else double.nan, globalcolor("CloudUp"), globalcolor("CloudDown"));
#EMAS BULLISH OR BEARISH
def EMABullish = EMA1 > EMA2;
def EMABearish = EMA1 < EMA2;
#EMA PERCENT SEPERATION
def EMAPctBull = ((EMA1 / EMA2) * 100) - 100; #Distance Between EMA1 and EMA2
def EMAPctBullRound = Round(EMAPctBull, 2);
def EMAPctBear = ((EMA2 / EMA1) * 100) - 100; #Distance Between EMA2 and EMA1
def EMAPctBearRound = Round(EMAPctBear, 2);
#FAST EMA PERCENT SEPERATION BUY SIGNAL
def EMASepThrHld;
switch (TimeFrame) {
case "1-min": EMASepThrHld = 0.1;
case "10-Min": EMASepThrHld = 0.25;
case "15-Min": EMASepThrHld = 0.25;
case "30-Min": EMASepThrHld = 0.3;
case "1-Hour": EMASepThrHld = 0.3;
default: EMASepThrHld = 0.2;
}
def EMAPctBullish = EMAPctBullRound > EMASepThrHld and EMAPctBullRound >= EMAPctBullRound[1];
def EMAPctBearish = EMAPctBearRound > EMASepThrHld and EMAPctBearRound >= EMAPctBearRound[1];
#TESTING
addchartbubble(ShowTestBubbles and EMAPctBullish, High * 1.005, EMAPctBullRound, color.dark_green, yes);
addchartbubble(ShowTestBubbles and EMAPctBearish, Low * 0.995, EMAPctBearRound, color.dark_red, no);
#EMA SELL SIGNALS
def EMACrossDown = EMA1 crosses below EMA2;
def EMACrossUp = EMA1 crosses above EMA2;
#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;
def MACDValue = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def MACDAverage = ExpAverage(MACDValue, MACDLength);
def MACDDiff = MACDValue - MACDAverage;
def ZeroLine = 0;
#MACD BUY SIGNAL
def MACDBull = (MACDAverage > MACDAverage[1]) within 3 bars;
def MACDBear = (MACDAverage < MACDAverage[1]) within 3 bars;
#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;
def TSIDiff = close - close[1];
def DoubleSmoothedAbsDiff = ExpAverage(ExpAverage(AbsValue(TSIDiff), TSILongLength), TSIShortLength);
def TSIRound = Round((100 * (ExpAverage(ExpAverage(TSIDiff, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiff), 2);
#TSI BUY SIGNAL
def TSIBull = (TSIRound > TSIRound[2]); #(TSIRound > 10)
def TSIBear = (TSIRound < TSIRound[2]); #(TSIRound < -10)
#TSI SELL SIGNAL
def TSICrossDown = TSIRound < (TSIRound[1] * 0.91);
def TSICrossUp = TSIRound > (TSIRound[1] * 0.91);
##################################################################
# PREMARKET #
##################################################################
#PREMARKET HIGHS AND LOWS
def PreMarketTimeRange = secondsFromTime(PreMarketStart) >= 0 and secondsTillTime(PreMarketEnd) >= 0;
def PreMarket = PreMarketTimeRange and !PremarketTimeRange[1];
def Pre_Market_High = compoundValue(1, if((high > Pre_Market_High[1] and PremarketTimeRange) or PreMarket, high, Pre_Market_High[1]), high);
def Pre_Market_Low = compoundValue(1, if((low < Pre_Market_Low[1] and PremarketTImeRange) or PreMarket, low, Pre_Market_Low[1]), low);
plot PreMarketHigh = Pre_Market_High;
PreMarketHigh.SetStyle(curve.short_dash);
PreMarketHigh.SetDefaultColor(color.light_gray);
PreMarketHigh.Sethiding(!ShowPreMarketCloud);
plot PreMarketLow = Pre_Market_Low;
PreMarketLow.SetStyle(curve.short_dash);
PreMarketLow.SetDefaultColor(color.light_gray);
PreMarketLow.Sethiding(!ShowPreMarketCloud);
AddCloud(if ShowPreMarketCloud then PreMarketHigh else double.nan, if ShowPreMarketCloud then PreMarketLow else double.nan, color.light_gray, color.light_gray);
AddLabel(if ShowPreMarketLabel then yes else no, " ", color.black);
AddLabel(if ShowPreMarketLabel then yes else no, " PM High - $" + PreMarketHigh + " ", color.gray);
AddLabel(if ShowPreMArketLabel then yes else no, " PM Low - $" + PreMarketLow + " ", color.gray);
#PREMARKET HIGH/LOW BUY SIGNAL
def PreMarketBull = close > Pre_Market_High;
def PreMarketBear = close < Pre_Market_Low;
#PREMARKET HIGH/LOW SELL SIGNAL
def PreMarketBullSell = PreMarketBull[1] and low < Pre_Market_High;
def PreMarketBearSell = PreMarketBear[1] and high > Pre_Market_Low;
##################################################################
# YESTERDAY HIGH/LOW #
##################################################################
def PrevHigh = high(period = "day")[1];
def PrevLow = low(period = "day")[1];
AddLabel(if ShowPrevHighLowLabel then 1 else 0, " ", color.black);
AddLabel(if ShowPrevHighLowLabel then 1 else 0, "Prev High " + "$ " + PrevHigh + " ", color.gray);
AddLabel(if ShowPrevHighLowLabel then 1 else 0, "Prev Low " + "$ " + PrevLow + " ", color.gray);
##################################################################
# SIGNALS #
##################################################################
#INDICATOR BUY SIGNALS
def LongBuyInd = TradingDay and EMABullish and EMAPctBullish and MACDBull and TSIBull and PreMarketBull;
def ShortBuyInd = TradingDay and EMABearish and EMAPctBearish and MACDBear and TSIBear and PreMarketBear;
#INDICATOR ENTRY SIGNALS
def LongEntryInd = (LongBuyInd[1] or LongBuyInd[2]) and LongBuyInd and low <= (EMA1 * 1.0025);
def ShortEntryInd = (ShortBuyInd[1] or ShortBuyInd[2]) and ShortBuyInd and high >= (EMA1 * 0.9975);
#INDICATOR HOLD SIGNALS
def LongHoldInd = TradingDayExt and (LongBuyInd[1] or LongHoldInd[1]);
def ShortHoldInd = TradingDayExt and (ShortBuyInd[1] or ShortHoldInd[1]);
##################################################################
# ADDED FAST EMA PCT HERE BASED OFF BUY SIGNAL #
##################################################################
def EMAPctAtLongBuy = if LongEntryInd then EMAPctBullRound else EMAPctAtLongBuy[1];
def EMAPctAtShortBuy = if ShortEntryInd then EMAPctBearRound else EMAPctAtShortBuy[1];
#FAST EMA CLOUD SELL TRIGGER BASED OFF BUY INDICATOR
input EMAPctDecFromBuy = 0.6;
def LongEMAPctSell = EMAPctBullRound < (EMAPctAtLongBuy * EMAPctDecFromBuy);
def ShortEMAPctSell = EMAPctBearRound < (EMAPctAtShortBuy * EMAPctDecFromBuy);
##################################################################
# SIGNALS CONT. #
##################################################################
#SELL SIGNALS
def LongPriceActSell = low < low[4];
def ShortPriceActSell = high > high[4];
def LongSellInd = TradingDayExt and (EMACrossDown or TSICrossDown or LongEMAPctSell or LongPriceActSell);
def ShortSellInd = TradingDayExt and (EMACrossUp or TSICrossUp or ShortEMAPctSell or ShortPriceActSell);
#TESTING
addchartbubble(ShowTestBubbles and TSICrossDown, High * 1.005, "T-Dn", color.dark_green, yes);
addchartbubble(ShowTestBubbles and TSICrossUp, Low * 0.995, "T-Up", color.dark_red, no);
addchartbubble(ShowTestBubbles and EMACrossDown, High * 1.005, "E-X-Dn", color.dark_green, yes);
addchartbubble(ShowTestBubbles and EMACrossUp, Low * 0.995, "E-X-Up", color.dark_red, no);
addchartbubble(ShowTestBubbles and LongEMAPctSell, High * 1.005, "E-Pct-Dn", color.dark_green, yes);
addchartbubble(ShowTestBubbles and SHortEMAPctSell, Low * 0.995, "E-Pct-Up", color.dark_red, no);
#HOLD SIGNALS
def LongHoldInd2 = LongHoldInd and !LongSellInd;
def ShortHoldInd2 = ShortHoldInd and !ShortSellInd;
##################################################################
# BARS #
##################################################################
AssignPriceColor(if LongBuyInd then GlobalColor("OpenLongPosition") else if ShortBuyInd then GlobalColor("OpenShortPosition") else if LongHoldInd2 then GlobalColor("HoldLongPosition") else if ShortHoldInd2 then GlobalColor("HoldShortPosition") else GlobalColor("CriteriaNotMet"));
##################################################################
# EMA TAKE PROFIT CLOUD (IDENTIFY IF PRICE IS EXTENDED FROM EMAS)#
##################################################################
def OvrExtUp1 = if ShowOverExtCloud and SignalStart and TradingDayExt and (LongEntryInd or LongHoldInd2) then EMA1 * 1.01 else Double.NaN;
def OvrExtUp2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (LongEntryInd or LongHoldInd2) then EMA1 * 1.02 else Double.NaN;
AddCloud(OvrExtUp1, OvrExtUp2, color.light_green, color.light_green);
def OvrExtDn1 = if ShowOverExtCloud and SignalStart and TradingDayExt and (ShortEntryInd or ShortHoldInd2) then EMA1 * 0.99 else Double.NaN;
def OvrExtDn2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (ShortEntryInd or ShortHoldInd2) then EMA1 * 0.98 else Double.NaN;
AddCloud(OvrExtDn1, OvrExtDn2, color.light_green, color.light_green);
##################################################################
# LABELS #
##################################################################
#LABELS
AddLabel(yes, " ", color.black);
AddLabel(yes, "PREMKT", if PreMarketBull then GlobalColor("OpenLongPosition") else if PreMarketBear then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "CLOUD", if EMABullish then GlobalColor("OpenLongPosition") else if EMABearish then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "EMA SEP", if EMAPctBullish then GlobalColor("OpenLongPosition") else if EMAPctBearish then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "TSI", if TSIBull then GlobalColor("OpenLongPosition") else if TSIBear then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "MACD", if MACDBull then GlobalColor("OpenLongPosition") else if MACDBear then GlobalColor("OpenShortPosition") else color.gray);
##################################################################
# CHART BUBBLES/POINTS FOR BUY/SELL #
##################################################################
#BUY
def LongBuyBub = LongEntryInd;
addchartbubble(ShowBuySellBubbles and LongBuyBub, High * 1.005, "Long", color.green, yes);
def ShortBuyBub = ShortEntryInd;
addchartbubble(ShowBuySellBubbles and ShortBuyBub, Low * 0.995, "Short", color.red, no);
plot LongBuyPoint = if LongBuyBub then EMA1 else double.nan;
LongBuyPoint.SetDefaultColor(color.white);
LongBuyPoint.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
LongBuyPoint.SetLineWeight(5);
LongBuyPoint.HideBubble();
plot ShortBuyPoint = if ShortBuyBub then EMA1 else double.nan;
ShortBuyPoint.SetDefaultColor(color.white);
ShortBuyPoint.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
ShortBuyPoint.SetLineWeight(5);
ShortBuyPoint.HideBubble();
#SELL
def LongSellBub = (LongEntryInd[1] or LongHoldInd2[1]) and (LongEntryInd[2] or LongHoldInd2[2]) and LongSellInd;
addchartbubble(ShowBuySellBubbles and LongSellBub, High * 1.005, "Sell", color.red, yes);
def ShortSellBub = (ShortEntryInd[1] or ShortHoldINd2[1]) and (ShortEntryInd[2] or ShortHoldINd2[2]) and ShortSellInd;
addchartbubble(ShowBuySellBubbles and ShortSellBub, Low * 0.995, "Sell", color.green, no);
plot LongSellPoint = if LongSellBub then Close else double.nan;
LongSellPoint.SetDefaultColor(color.red);
LongSellPoint.SetPaintingStrategy(PaintingStrategy.Squares);
LongSellPoint.SetLineWeight(5);
LongSellPoint.HideBubble();
plot ShortSellPoint = if ShortSellBub then Close else double.nan;
ShortSellPoint.SetDefaultColor(color.green);
ShortSellPoint.SetPaintingStrategy(PaintingStrategy.Squares);
ShortSellPoint.SetLineWeight(5);
ShortSellPoint.HideBubble();
#TAKE PROFIT
def LongTkPftBub = TradingDayExt and (LongEntryInd or LongHoldInd2) and high > EMA1 * 1.01;
addchartbubble(ShowTkPftBubble and LongTkPftBub, high * 1.001, "Tk Pft", GlobalColor("TakeProfit"), yes);
def ShortTkPftBub = TradingDayExt and (ShortEntryInd or ShortHoldInd2) and low < EMA1 * 0.99;
addchartbubble(ShowTkPftBubble and ShortTkPftBub, low * 0.999, "Tk Pft", GlobalColor("TakeProfit"), no);
plot LongTkPftPoint = if LongTkPftBub then High else double.nan;
LongTkPftPoint.SetDefaultColor(color.green);
LongTkPftPoint.SetPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
LongTkPftPoint.SetLineWeight(5);
LongTkPftPoint.HideBubble();
plot ShortTkPftPoint = if ShortTkPftBub then Low else double.nan;
ShortTkPftPoint.SetDefaultColor(color.red);
ShortTkPftPoint.SetPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
ShortTkPftPoint.SetLineWeight(5);
ShortTkPftPoint.HideBubble();
##################################################################
# STOP LOSS #
##################################################################
def longstop = if (open < close) then open else close;
def shortstop = if (open > close) then open else close;
addlabel(if (LongEntryInd or LongHoldInd2) then yes else no, "STOP $ " + longstop[4] + " ", color.white);
addlabel(if (ShortEntryInd or ShortHoldInd2) then yes else no, "STOP $ " + shortstop[4] + " ", color.white);
##################################################################
# ORDERS #
##################################################################
#BUY ORDERS
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and LongEntryInd, EMA1 * 1.0025, 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and ShortEntryInd, EMA1 * 0.9975, 100, Color.GREEN, Color.LIGHT_GREEN);
#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and ShowTkPftOrders and LongTkPftBub, high * 0.999, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and (LongSellInd or EndDay[-1]), open[-1], 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and ShowTkPftOrders and ShortTkPftBub, low * 1.001, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and (ShortSellInd or EndDay[-1]), open[-1], 100, Color.RED, Color.LIGHT_RED);
##################################################################
# ALERTS #
##################################################################
Alert(AlertOn and (LongSellBub or ShortSellBub or LongTkPftBub or ShortTkPftBub), "SELL" + GetSymbol(), Alert.BAR, Sound.Bell);
Alert(AlertOn and (LongBuyInd or ShortBuyInd), "BUY" + GetSymbol(), Alert.BAR, Sound.Chimes);
##################################################################
# CANDLE COLOR LABEL #
##################################################################
AddLabel(if ShowColorDefLabels == 1 then yes else no, " ", color.black);
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Long Ind", GlobalColor("OpenLongPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Long Hld", GlobalColor("HoldLongPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Short Ind", GlobalColor("OpenShortPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Short Hld", GlobalColor("HoldShortPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "No Criteria", GlobalColor("CriteriaNotMet"));
##################################################################
# BACKGROUND COLOR FOR FLEX GRID #
##################################################################
AssignBackgroundColor(if ShowFlexGridBackgroundColor and LongBuyInd then GlobalColor("OpenLongPosition") else if ShowFlexGridBackgroundColor and ShortBuyInd then GlobalColor("OpenShortPosition") else if ShowFlexGridBackgroundColor and (LongTkPftBub or ShortTkPftBub) then GlobalColor("TakeProfit") else color.current);