#JT TEMP Strategy Based on TSI, EMAs, MACD, and Premarket Highs/Lows
#VERSION 1 - 05/16/2022
#NOTES MACD Above 0?
Declare Upper;
##################################################################
# OPTIONS #
##################################################################
input ShowTestBubbles = no;
input ShowFlexGridBackgroundColor = no;
input ShowColorDefLabels = yes;
input AlertOn = yes;
input Show200EMA = yes;
input ShowEMAClouds = yes;
input ShowPreMarketCloud = yes;
input ShowPreMarketLabel = yes;
input ShowPrevHighLowLabel = no;
input ShowBuySellBubbles = Yes;
input ShowOverExtCloud = yes;
input ShowTkPftBubble = Yes;
input ShowOrders = no;
input ShowTkPftOrders = no;
##################################################################
# COLORS #
##################################################################
DefineGlobalColor("200EMA", createcolor (255, 255, 255));
DefineGlobalColor("FastCloudUp", createcolor (102, 255, 102));
DefineGlobalColor("FastCloudDown", createcolor (255, 102, 102));
DefineGlobalColor("SlowCloudUp", createcolor (102, 255, 102));
DefineGlobalColor("SlowCloudDown", 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 (255, 255, 0));
##################################################################
# TIMES #
##################################################################
#START AND END TIMES
input TimeFrame = {default "10-Min", "1-Min", "5-Min", "15-Min", "30-Min", "1-Hour"};
input PreMarketStart = 0700; #EST
input PreMarketEnd = 0929; #EST
input StartTime = 0930; #EST
input BuySignalDelay = 29;
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 "5-Min": LastCandleStop = 5;
case "15-Min": LastCandleStop = 15;
case "30-Min": LastCandleStop = 30;
case "1-Hour": LastCandleStop = 60;
default: LastCandleStop = 10;
}
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 = 34;
input Ema2Length = 50;
input Ema3Length = 5;
input Ema4Length = 12;
plot EMA3 = ExpAverage(close, Ema3Length);
EMA3.SetDefaultColor(globalcolor("FastCloudUp"));
EMA3.HideBubble();
plot EMA4 = ExpAverage(close, Ema4Length);
EMA4.SetDefaultColor(globalcolor("FastCloudDown"));
EMA4.HideBubble();
EMA4.sethiding(!ShowEMAClouds);
AddCloud(if ShowEMAClouds then EMA3 else double.nan, if ShowEMAClouds then EMA4 else double.nan, globalcolor("FastCloudUp"), globalcolor("FastCloudDown"));
plot EMA1 = ExpAverage(close, Ema1Length);
EMA1.SetDefaultColor(globalcolor("SlowCloudUp"));
EMA1.HideBubble();
EMA1.sethiding(!ShowEMAClouds);
plot EMA2 = ExpAverage(close, Ema2Length);
EMA2.SetDefaultColor(globalcolor("SlowCloudDown"));
EMA2.HideBubble();
EMA2.sethiding(!ShowEMAClouds);
AddCloud(if ShowEMAClouds then EMA1 else double.nan, if ShowEMAClouds then EMA2 else double.nan, globalcolor("SlowCloudUp"), globalcolor("SlowCloudDown"));
#EMAS BULLISH OR BEARISH
def FastEMABullish = EMA3 > EMA4;
def SlowEMABullish = EMA1 > EMA2;
def FastEMABearish = EMA3 < EMA4;
def SlowEMABearish = EMA1 < EMA2;
#EMA BUY SIGNAL
def BothEMASBullish = FastEMABullish and SlowEMABullish;
def BothEMASBearish = FastEMABearish and SlowEMABearish;
#EMA PERCENT SEPERATION
def FastEMAPctBull = ((EMA3 / EMA4) * 100) - 100; #Distance Between EMA3 and EMA4
def FastEMAPctBullRound = Round(FastEMAPctBull, 2);
def FastEMAPctBear = ((EMA4 / EMA3) * 100) - 100; #Distance Between EMA4 and EMA3
def FastEMAPctBearRound = Round(FastEMAPctBear, 2);
#FAST EMA PERCENT SEPERATION BUY SIGNAL
def EMASepThrHld;
switch (TimeFrame) {
case "1-min": EMASepThrHld = 0.1;
case "5-Min": EMASepThrHld = 0.2;
case "15-Min": EMASepThrHld = 0.25;
case "30-Min": EMASepThrHld = 0.3;
case "1-Hour": EMASepThrHld = 0.3;
default: EMASepThrHld = 0.25;
}
def FastEMAPctBullish = BothEMASBullish and FastEMAPctBullRound > EMASepThrHld and FastEMAPctBullRound >= FastEMAPctBullRound[1];
def FastEMAPctBearish = BothEMASBearish and FastEMAPctBearRound > EMASepThrHld and FastEMAPctBearRound >= FastEMAPctBearRound[1];
#TESTING
addchartbubble(ShowTestBubbles and FastEMAPctBullish, High * 1.005, FastEMAPctBullRound, color.dark_green, yes);
addchartbubble(ShowTestBubbles and FastEMAPctBearish, Low * 0.995, FastEMAPctBearRound, color.dark_green, no);
def SlowEMAPctBull = ((EMA1 / EMA2) * 100) - 100; #Distance Between EMA1 and EMA2
def SlowEMAPctBullRound = Round(SlowEMAPctBull, 2);
def SlowEMAPctBear = ((EMA1 / EMA2) * 100) - 100; #Distance Between EMA1 and EMA2
def SlowEMAPctBearRound = Round(SlowEMAPctBear, 2);
#SLOW EMA PERCENT SEPERATION BULLISH
def SlowEMAPctBullish = BothEMASBullish and SlowEMAPctBullRound >= SlowEMAPctBullRound[1];
def SlowEMAPctBearish = BothEMASBearish and SlowEMAPctBearRound <= SlowEMAPctBearRound[1];
#EMA SELL SIGNALS
def FastEMACrossDown = EMA3 crosses below EMA4;
def FastEMACrossUp = EMA3 crosses above EMA4;
#def FastEMAPctBullDecrease = BothEMASBullish and FastEMAPctBullRound < (FastEMAPctBullRound[1] * 0.75) and !FastEMAPctBullDecrease[1];
#def FastEMAPctBearDecrease = BothEMASBearish and FastEMAPctBearRound < (FastEMAPctBearRound[1] * 0.75) and !FastEMAPctBearDecrease[1];
#TESTING
#addchartbubble(ShowTestBubbles and FastEMAPctBullDecrease, High * 1.005, FastEMAPctBullRound, color.dark_red, yes);
#addchartbubble(ShowTestBubbles and FastEMAPctBearDecrease, Low * 0.995, FastEMAPctBearRound, color.dark_red, no);
#SLOWEMA2 SLOPE (NOT USING AT THIS TIME)
#def Height = EMA2 - EMA2[11];
#def SlopeDeg = Round((ATan(Height / 10) * 180 / Double.Pi), 0);
#SLOWEMA2 SLOPE SELL SIGNAL
#def BullSlopeSell = BothEMASBullish and SlopeDeg < SlopeDeg[1];
#def BearSlopeSell = BothEMASBearish and SlopeDeg > SlopeDeg[1];
#TESTING
#addchartbubble(if ShowTestBubbles and BullSlopeSell == 1 then 1 else 0, SlowEMA2*0.995, "Slope", color.dark_red, no);
#addchartbubble(if ShowTestBubbles and BearSlopeSell == 1 then 1 else 0, SlowEMA2*1.005, "Slope", color.dark_red, yes);
def EMABull = EMA3 > EMA200 and (open and close) > EMA3;
def EMABear = EMA3 < EMA200 and (open and close) < EMA3;
#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 = MACDDiff > MACDDiff[1];
def MACDBear = MACDDiff < MACDDiff[1];
#MACD SELL SIGNAL
#No Sell SIgnals At This Time
#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 > 10) and (TSIRound > TSIRound[1]);
def TSIBear = (TSIRound < -10) and (TSIRound < TSIRound[1]);
#TSI SELL SIGNAL
def TSICrossDown = TSIRound < (TSIRound[1] * 0.91);
def TSICrossUp = TSIRound > (TSIRound[1] * 0.91);
#TSI TAKE PROFIT
#No TSI Take Profit At This Time
##################################################################
# 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; #was low
def PreMarketBear = close < Pre_Market_Low; #was high
#TESTING
#addchartbubble(ShowTestBubbles and PreMarketBull, EMA2*0.995, "Pre", color.dark_green, no);
#addchartbubble(ShowTestBubbles and PreMarketBear, EMA2*1.005, "Pre", color.dark_green, yes);
#PREMARKET HIGH/LOW SELL SIGNAL
def PreMarketBullSell = PreMarketBull[1] and low < Pre_Market_High;
def PreMarketBearSell = PreMarketBear[1] and high > Pre_Market_Low;
#TESTING
#addchartbubble(ShowTestBubbles and PreMarketBullSell, EMA2*0.995, "Pre", color.dark_red, no);
#addchartbubble(ShowTestBubbles and PreMarketBearSell, EMA2*1.005, "Pre", color.dark_red, yes);
##################################################################
# 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 BothEMASBullish and FastEMAPctBullish and MACDBull and TSIBull and PreMarketBull; # EMABull; #PreMarketBull;
def ShortBuyInd = TradingDay and BothEMASBearish and FastEMAPctBearish and MACDBear and TSIBear and PreMarketBear; #EMABear; #PreMarketBear;
#INDICATOR HOLD SIGNALS
def LongHoldInd = TradingDayExt and (LongBuyInd[1] or LongHoldInd[1]);
def ShortHoldInd = TradingDayExt and (ShortBuyInd[1] or ShortHoldInd[1]);
#INDICATOR ENTRY SIGNALS
def LongEntryInd = LongBuyInd[1] and LongBuyInd and low <= (EMA3 * 1.0025);
def ShortEntryInd = ShortBuyInd[1] and ShortBuyInd and high >= (EMA3 * 0.9975);
##################################################################
# ADDED FAST EMA PCT HERE BASED OFF BUY SIGNAL #
##################################################################
def EMAPctAtLongBuy = if LongEntryInd then FastEMAPctBullRound else EMAPctAtLongBuy[1]; #Used to Calculate Sell Trigger
def EMAPctAtShortBuy = if ShortEntryInd then FastEMAPctBearRound else EMAPctAtShortBuy[1]; #Used to Calculate Sell Trigger
#FAST EMA CLOUD SELL TRIGGER BASED OFF BUY INDICATOR
input EMAPctDecFromBuy = 0.6;
def LongEMAPctSell = FastEMAPctBullRound < (EMAPctAtLongBuy * EMAPctDecFromBuy);
def ShortEMAPctSell = FastEMAPctBearRound < (EMAPctAtShortBuy * EMAPctDecFromBuy);
##################################################################
# SIGNALS CONT. #
##################################################################
#SELL SIGNALS
def LongSellInd = TradingDayExt and (FastEMACrossDown or TSICrossDown or LongEMAPctSell);
def ShortSellInd = TradingDayExt and (FastEMACrossUp or TSICrossUp or ShortEMAPctSell);
#HOLD SIGNALS
def CallHoldInd2 = LongHoldInd and !LongSellInd;
def PutHoldInd2 = ShortHoldInd and !ShortSellInd;
##################################################################
# BARS #
##################################################################
AssignPriceColor(if LongBuyInd then GlobalColor("OpenLongPosition") else if ShortBuyInd then GlobalColor("OpenShortPosition") else if CallHoldInd2 then GlobalColor("HoldLongPosition") else if PutHoldInd2 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 (LongBuyInd or LongHoldInd) then EMA3 * 1.01 else Double.NaN;
def OvrExtUp2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (LongBuyInd or LongHoldInd) then EMA3 * 1.02 else Double.NaN;
AddCloud(OvrExtUp1, OvrExtUp2, color.light_green, color.light_green);
def OvrExtDn1 = if ShowOverExtCloud and SignalStart and TradingDayExt and (ShortBuyInd or ShortHoldInd) then EMA3 * 0.99 else Double.NaN;
def OvrExtDn2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (ShortBuyInd or ShortHoldInd) then EMA3 * 0.98 else Double.NaN;
AddCloud(OvrExtDn1, OvrExtDn2, color.light_green, color.light_green);
#*****************************************************************************#
#*****************STILL EDITING AND CHECKING EVERYTHING BELOW*****************#
#*****************************************************************************#
##################################################################
# 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, "CLOUDS", if BothEMASBullish then GlobalColor("OpenLongPosition") else if BothEMASBearish then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "EMA SEP", if FastEMAPctBullish then GlobalColor("OpenLongPosition") else if FastEMAPctBearish 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 FOR BUY/SELL #
##################################################################
#BUY
addchartbubble(LongEntryInd, High * 1.005, "Long", color.green, yes);
addchartbubble(ShortEntryInd, Low * 0.995, "Short", color.red, no);
#SELL
def CallSellBub = LongSellInd; #and (CallBuy[1] or CallHold[1])
#addchartbubble(ShowBuySellBubbles and CallSellBub, low * 0.999, "Sell", GlobalColor("SellPosition"), no);
def PutSellBub = ShortSellInd; #and (PutBuy[1] or PutHold[1])
#addchartbubble(ShowBuySellBubbles and PutSellBub, high * 1.001, "Sell", GlobalColor("SellPosition"), yes);
#TAKE PROFIT
def TkPftBubCall = TradingDayExt and (LongBuyInd or LongHoldInd) and high > EMA3 * 1.01;
addchartbubble(ShowTkPftBubble and TkPftBubCall, high * 1.001, "Tk Pft", GlobalColor("TakeProfit"), yes);
def TkPftBubPut = TradingDayExt and (ShortBuyInd or ShortHoldInd) and low < EMA3 * 0.99;
addchartbubble(ShowTkPftBubble and TkPftBubPut, low * 0.999, "Tk Pft", GlobalColor("TakeProfit"), no);
##################################################################
# STOP LOSS #
##################################################################
def longstop = if (open < close) then open else close;
def shortstop = if (open > close) then open else close;
addlabel(if (LongBuyInd or LongEntryInd) then yes else no, "STOP $ " + longstop[2] + " ", color.white);
addlabel(if (ShortBuyInd or ShortEntryInd) then yes else no, "STOP $ " + shortstop[2] + " ", color.white);
##################################################################
# ORDERS #
##################################################################
#BUY ORDERS
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and LongEntryInd, if LongEntryInd then (EMA3 * 1.0025) else double.nan, 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and ShortEntryInd, if ShortEntryInd then (EMA3 * 0.9975) else double.nan, 100, Color.GREEN, Color.LIGHT_GREEN);
#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and ShowTkPftOrders and TkPftBubCall, 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 TkPftBubPut, 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 (CallSellBub or PutSellBub or TkPftBubCall or TkPftBubPut), "SELL", Alert.BAR, Sound.Bell);
Alert(AlertOn and (LongBuyInd or ShortBuyInd), "BUY", 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 (TkPftBubCall or TkPftBubPut) then GlobalColor("TakeProfit") else color.current);