Declare Upper;
######################################################################
# Options #
######################################################################
input TradeSize = 10;
input StopLossPct = -5; #Represents Percent of price loss per share
def StopLossPctFm = (StopLossPct / 100) + .9;
input HideTkPftBubble = No;
input HideOvrExtBubble = No;
input ShowOrders = No;
input ShowSquares = No;
input InverseChart = {default "No", "2x", "3x"};
########################################################################
# 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 (120, 255, 120));
DefineGlobalColor("HoldShortPosition", CreateColor (255, 120, 120));
DefineGlobalColor("SellPosition", CreateColor (255, 0, 0));
DefineGlobalColor("TakeProfit", CreateColor (255, 0, 255));
DefineGlobalColor("CriteriaNotMet", CreateColor (102, 102, 102)); #(255, 255, 0)
DefineGlobalColor("EMA", CreateColor (0, 0, 0));
DefineGlobalColor("Up", CreateColor (153, 255, 153));
DefineGlobalColor("Dn", CreateColor (255, 153, 153));
########################################################################
# Times #
########################################################################
#START AND END TIMES
input PreMarketStart = 0700; #EST
input PreMarketEnd = 0929; #EST
input RegularDayStart = 0930; # EST
input RegularDayEnd = 1600; # EST
input AfterHoursStarts = 1601; #Est
input AfterHoursEnd = 1800; #EST
input StartDelay = 0;
def TradingDay = RegularDayStart and RegularDayEnd; #0930EST - 1600EST
def TradingDayExt = AfterHoursStarts and AfterHoursEnd; #1601EST - 1800EST
def SignalStart = PreMarketStart;
def EndDay = AfterHoursEnd;
def Day = GetAggregationPeriod() >=AggregationPeriod.DAY;
def Interday = GetAggregationPeriod() < AggregationPeriod.DAY;
###########################################################################
# Indicaors #
###########################################################################
#SMA's
input HideSma5 = Yes;
input HideSma5Label = Yes;
input HideSma10 = Yes;
input HideSma20 = No;
input ShowSmaCloud = Yes;
#SMA 5
plot SMA5 = SimpleMovingAvg(HL2, 5);
SMA5.SetPaintingStrategy(PaintingStrategy.LINE);
SMA5.SetDefaultColor(GlobalColor("CloudUp"));
SMA5.SetHiding(HideSma5);
AddLabel(!HideSma5Label, if SMA5 then " 5 SMA" else " ", Color.GREEN);
#SMA10
plot SMA10 = SimpleMovingAvg(HL2, 10);
SMA10.SetPaintingStrategy(PaintingStrategy.LINE);
SMA10.SetDefaultColor(GlobalColor("CloudDown"));
SMA10.SetHiding(HideSma10);
AddCloud(if ShowSmaCloud or Interday then SMA5 else Double.NaN, if ShowSmaCloud then SMA10 else Double.NaN, GlobalColor("CloudUp"), GlobalColor("CloudDown"), yes);
#EMA's
#EMA 5
input SellCrossBelowPct = -0.00; #Should be negetive number
input BuyCrossAbovePct = 0.00;
input HideEma5 = Yes;
input HideEma10 = Yes;
input HideEmaCloud = Yes;
def SellCrossBelowPctFm = (SellCrossBelowPct / 100);
def BuyCrossAbovePctFm = (BuyCrossAbovePct / 100);
input EMA5Label = {default "Yes", "No"};
plot EMA5 = ExpAverage(close, 5);
EMA5.SetPaintingStrategy(PaintingStrategy.LINE);
EMA5.SetDefaultColor(Color.GRAY);
EMA5.SetHiding(HideEma5);
def EMA5Low = ExpAverage(low, 5);
def EMA5High = ExpAverage(high, 5);
# EMA 10
plot EMA10 = ExpAverage(close, 10);
EMA10.SetPaintingStrategy(PaintingStrategy.LINE);
EMA10.AssignValueColor(if IsAscending(close, 10) then Color.LIGHT_GREEN else Color.LIGHT_RED);
EMA10.SetLineWeight(1);
EMA10.SetHiding(HideEma10);
def EMA10Low = ExpAverage(low, 10);
def EMA10High = ExpAverage(high, 10);
# EMA 5&10 Color Gradient
def EMA1UpColor = EMA5 > EMA10;
def EMA1DnColor = EMA5 < EMA10;
AddCloud(if HideEmaCloud or Day then Double.NaN else EMA5, if HideEmaCloud or Day then Double.NaN else EMA10 , Color.LIGHT_GREEN, Color.LIGHT_RED, yes);
# EMA's Bullish or Bearish
def EMABullish = EMA5 > EMA10;
def EMABearish = EMA5 < EMA10;
#EMA Percent Seperation
def EMAPctBull = ((EMA5 / EMA10) * 100) - 100; # Distance Between EMA5 and EMA10
def EMAPctBullRound = Round(EMAPctBull, 2);
def EMAPctBear = ((EMA10 / EMA5) * 100) - 100; # Distance Between EMA10 and EMA5
def EMAPctBearRound = Round(EMAPctBear, 2);
#Fast EMA Percent Seperation Buy Signals
input TimeFrame = {default "5-MIN", "1-MIN", "10-MIN", "15-MIN", "30-MIN", "HOUR"};
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 "HOUR":
EMASepThrHld = 0.3;
default:
EMASepThrHld = 0.2;
}
def EMAPctBullish = EMAPctBullRound > EMASepThrHld and EMAPctBullRound >= EMAPctBullRound[1];
def EMAPctBearish = EMAPctBearRound > EMASepThrHld and EMAPctBearRound >= EMAPctBearRound[1];
# EMA Buy / Sell Signals
def EMACrossDown = EMA5 crosses below EMA10;
def EMACrossUP = EMA5 crosses above EMA10;
def ProfitPct;
switch (InverseChart){
case "2x":
ProfitPct = 1.00625;
case "3x":
ProfitPct = 1.0125;
default:
ProfitPct = 1.00417;
}
def OverExtPct;
switch (InverseChart) {
case "2x":
OverExtPct = .9835;
case "3x":
OverExtPct = .9725;
default:
OverExtPct = .995;
}
def TakeProfit = close > (EMA5 * ProfitPct);
def OverExtended = close < (EMA5 * OverExtPct);
AddChartBubble(if HideOvrExtBubble or Day then Double.NaN else OverExtended, close, "Over Ext", GlobalColor("OpenShortPosition"));
AddChartBubble(if HideTkPftBubble or Day then Double.NaN else TakeProfit, close, "Smart Sell", GlobalColor("OpenLongPosition"), yes);
# MACD
input MacdUpPct = 0.5;
input MacdDownPct = 0.5;
input ShowMacdBubble = No;
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageType = AverageType.EXPONENTIAL;
def value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, value, MACDLength);
def Diff = value - Avg;
def ZeroLine = 0;
def MacdBullish = IsAscending(close, 5) and (value * 1.009) > Avg;
def MacdBearish = IsDescending(close, 5) and value < (Avg * 0.0875);
def HalfMacdValueHigh = HighestAll(value) / 2;
def HalfMacdAvgHigh = HighestAll (Avg) / 2;
def HalfMacdValueLow = LowestAll (value) / 2;
def HalfMacdAvgLow = LowestAll (Avg) / 2;
def MacdBullishStrong = IsAscending(value) >= (HalfMacdValueLow) and (Avg) >= (HalfMacdAvgLow) and (value) > (Avg + 0.03);
def MacdBearishStrong = IsDescending(close, 9) and (value) <= (HalfMacdValueHigh) and (Avg) <= (HalfMacdAvgHigh) and (value) < (Avg - 0.03);
#AddChartBubble(MacdBullish, close, "MacdBullish", Color.LIGHT_GREEN);
#AddChartBubble(MacdBullishStrong, close, "MacdBullishStrong", Color.GREEN, yes);
#AddChartBubble(MacdBearish, close, "MacdBearish", Color.LIGHT_RED);
#AddChartBubble(MacdBearishStrong, close, "MacdBearishStrong", Color.RED);
##################################################################
# PREMARKET #
##################################################################
#PREMARKET HIGHS AND LOWS
input ShowPreMarketLabel = Yes;
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.Hide();
plot PreMarketLow = Pre_Market_Low;
PreMarketLow.SetStyle(Curve.SHORT_DASH);
PreMarketLow.SetDefaultColor(Color.LIGHT_GRAY);
PreMarketLow.Hide();
;
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);
##############################################################################
# Order & Bar Conditions #
##############################################################################
def SoftBuyEmaCross = ((close) > EMA5 ) or (EMA5 > EMA10) ;
def SoftBuyMacd = MacdBullish;
def StrongBuy = (SoftBuyEmaCross) and (MacdBullishStrong) and (MacdBullish);
plot LongBuy = if ShowSquares or day and StrongBuy[1] or ShowSquares or day and StrongBuy[2] then Double.NaN else ShowSquares or day and StrongBuy;
LongBuy.SetDefaultColor(Color.GREEN);
LongBuy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
LongBuy.SetLineWeight(3);
LongBuy.HideBubble();
#LongBuy.Hide();
plot SmartSell = if ShowSquares or day and TakeProfit then close else Double.NaN;
SmartSell.SetDefaultColor(GlobalColor("TakeProfit"));
SmartSell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SmartSell.SetLineWeight(3);
SmartSell.HideBubble();
#SmartSell.Hide();
def SoftSell = Close < EMA5Low;
plot SellSoft = if ShowSquares or Day and (SoftSell[2]) or ShowSquares or Day and (SoftSell[1]) then Double.NaN else ShowSquares or Day and (SoftSell) ;
SellSoft.SetDefaultColor(Color.LIGHT_RED);
SellSoft.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SellSoft.SetLineWeight(3);
SellSoft.HideBubble();
#SellSoft.Hide();
plot SellStrong = If ShowSquares and (close[1] < EMA10Low[1]) or ShowSquares and (Close[2] < EMA10Low[2])then Double.NaN else ShowSquares and (Close < EMA10Low) ;
SellStrong.SetDefaultColor(Color.RED);
SellStrong.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SellStrong.SetLineWeight(5);
SellStrong.HideBubble();
#SellStrong.Hide();
def StopLoss = (EntryPrice()) * StopLossPctFm;
def SoftShortMacd = MacdBearish;
def SoftShortEma = ((close) < EMA10);
def StrongShort = (SoftShortEma) and (SoftShortMacd) and (MacdBearishStrong) and (close * .99) < EMA10Low;
#plot SoftShortEntry = SoftShortEma and SoftShortMacd;
#SoftShortEntry.SetDefaultColor(Color.LIGHT_GREEN);
#SoftShortEntry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#SoftShortEntry.SetLineWeight(5);
#SoftShortEntry.HideBubble();
#SoftShortEntry.Hide();
#AddChartBubble(StopLoss,Open,"Stop",Color.DARK_RED);
##############################################################################
# Bars #
##############################################################################
AssignPriceColor (if StrongBuy then GlobalColor("OpenLongPosition") else if StrongShort then GlobalColor("OpenShortPosition") else if (SoftBuyEmaCross) and (MacdBullish) or (SoftBuyEmaCross) and (MacdBullishStrong) and (close > EMA5) then GlobalColor("HoldLongPosition") else if SoftShortMacd or SoftShortEma then GlobalColor("HoldShortPosition") else GlobalColor("CriteriaNotMet"));
##############################################################################
# Orders #
##############################################################################
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and SoftBuyEmaCross and SoftBuyMacd, open[-1], (TradeSize / 2), Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and SmartSell, open[-1], TradeSize, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and SellStrong, open[-1], TradeSize, Color.RED, Color.RED);