Where Have I Gone Wrong? Sell Bar not Displaying Properly

a1cturner

Well-known member
I cannot figure out for the life of me why one of my sell triggers is not showing properly. I tried to define the crap out of it but it still isn't working.

I am trying to paint a red bar when the RSI crosses below 70 or when the RSI crosses above 30. As of now I am still getting my "hold signal"

I do get a red bar at the end of the day as well as when the EMAs cross but just not when the RSI crosses. It is supposed to be either the EMA cross or RSI cross but always at the end of the day.

Everywhere there is a Red Arrow should be a Red Bar instead.

ZlRn7KF.png


Code:
#JT EMA, MACD, AND RSI INDICATOR

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

def EMAFastCrossDown = EMAFast1 crosses below EMAFast2;
def EMAFastCrossUp = EMAFast1 crosses above EMAFast2;

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];

def EMABull = EMAFastBull and EMASlowBull and EMAPctBull;
def EMABear = EMAFastBear and EMASlowBear and EMAPctBear;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI crosses below RSIOverBought;
def RSICrossUp = RSI crosses above RSIOverSold;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 10;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if
CallBuy == 1 and EndDay == 0 then CallBuy
 else if
CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold
 else if
CallSell == 1 then CallSell
 else if
CallBuy == 1 and EndDay == 1 then EndDay
 else if
CallHold == 1 and EndDay ==1 then EndDay
 else if
CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay
 else double.nan;

def PutSignal = if
PutBuy == 1 and EndDay == 0 then PutBuy
 else if
PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold
 else if
PutSell == 1 and CallHold == 0 then PutSell
 else if
PutBuy ==1 and EndDay == 1 then EndDay
 else if
PutHold ==1 and EndDay == 1 then EndDay
 else if
PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay
 else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy then close else CallOpen[1];
#def CallClose = if CallSell then close else CallClose[1];
#def CallQty = 100;
#def CallProfit = (CallOpen * CallQty) - (CallClose * CallQty);

#addlabel(1, "   ", color.black);
#addlabel(1, "Call Profit " + CallProfit, color.green);
#addlabel(1, "Trades " + CallQty/100, color.yellow);
#addlabel(1, "Avg Call Trade $ " + (CallProfit/CallQty), color.yellow);

#plot CBuy = if CallBuy or CallHold then CallClose else double.nan;
##cbuy.setdefaultcolor(color.green);
################################################################

#COLOR BARS
AssignPriceColor(if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if CallSell then createcolor(255, 0, 0) else if PutSell then createcolor(255, 0, 0) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");
 
I cannot figure out for the life of me why one of my sell triggers is not showing properly. I tried to define the crap out of it but it still isn't working.

I am trying to paint a red bar when the RSI crosses below 70 or when the RSI crosses above 30. As of now I am still getting my "hold signal"

I do get a red bar at the end of the day as well as when the EMAs cross but just not when the RSI crosses. It is supposed to be either the EMA cross or RSI cross but always at the end of the day.

Everywhere there is a Red Arrow should be a Red Bar instead.

ZlRn7KF.png


Code:
#JT EMA, MACD, AND RSI INDICATOR

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

def EMAFastCrossDown = EMAFast1 crosses below EMAFast2;
def EMAFastCrossUp = EMAFast1 crosses above EMAFast2;

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];

def EMABull = EMAFastBull and EMASlowBull and EMAPctBull;
def EMABear = EMAFastBear and EMASlowBear and EMAPctBear;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI crosses below RSIOverBought;
def RSICrossUp = RSI crosses above RSIOverSold;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 10;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if
CallBuy == 1 and EndDay == 0 then CallBuy
 else if
CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold
 else if
CallSell == 1 then CallSell
 else if
CallBuy == 1 and EndDay == 1 then EndDay
 else if
CallHold == 1 and EndDay ==1 then EndDay
 else if
CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay
 else double.nan;

def PutSignal = if
PutBuy == 1 and EndDay == 0 then PutBuy
 else if
PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold
 else if
PutSell == 1 and CallHold == 0 then PutSell
 else if
PutBuy ==1 and EndDay == 1 then EndDay
 else if
PutHold ==1 and EndDay == 1 then EndDay
 else if
PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay
 else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy then close else CallOpen[1];
#def CallClose = if CallSell then close else CallClose[1];
#def CallQty = 100;
#def CallProfit = (CallOpen * CallQty) - (CallClose * CallQty);

#addlabel(1, "   ", color.black);
#addlabel(1, "Call Profit " + CallProfit, color.green);
#addlabel(1, "Trades " + CallQty/100, color.yellow);
#addlabel(1, "Avg Call Trade $ " + (CallProfit/CallQty), color.yellow);

#plot CBuy = if CallBuy or CallHold then CallClose else double.nan;
##cbuy.setdefaultcolor(color.green);
################################################################

#COLOR BARS
AssignPriceColor(if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if CallSell then createcolor(255, 0, 0) else if PutSell then createcolor(255, 0, 0) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");

If you have 2 or more studies loaded that use AssignPriceColor(), they can interfere with each other. Disable the other studies so there's just 1.

When a long formula doesn't act as expected simplify it.
Copy it , disable the original formula, and make the copy just the one condition to see if that is ever triggered.

When several conditions exist in a formula, I put the stop one first, then the start condition as 2nd. then the others after that.

I would add a bubble to display some variables to see if your condition is ever true.
Code:
addchartbubble(1, low, 
 CallBuy + " cb\n" +
 CallHold + " ch\n" +
 PutBuy + " pb\n" +
 PutHold + " ph\n" +
 CallSell + " cs\n" +
 PutSell + " ps\n" +
 EndDay + " E"
, color.yellow, no);
 
@halcyonguy I removed everything except the def CallSell = RSICrossDown and def PutSell = RSICrossUp but I am still not getting the RSI cross red bar. In doing this I also get the unwanted red bar at the start of the day too.

Code:
def CallSell = RSICrossDown;
def PutSell =  RSICrossUp;

Do I need to somehow edit down to 1 study after AssignPriceColor or limit my def's to one study each?

I am trying to stay away from chart bubbles as it makes the chart too messy, unless you think that creating them and then hiding the bubbles would help me assignpricecolor.

This is the area where I made the change

Code:
#INDICATORS
def CallBuy = TradingDay and EMABull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);
 
Do I need to somehow edit down to 1 study after AssignPriceColor or limit my def's to one study each?

I am trying to stay away from chart bubbles as it makes the chart too messy, unless you think that creating them and then hiding the bubbles would help me assignpricecolor.

only 1 loaded study should have a AssignPriceColor function

i meant to use bubbles just for debugging and then when you're done with them set the time condition to 0. or sometimes I put a # in front of it, to disable it.
 
When several conditions exist in a formula, I put the stop one first, then the start condition as 2nd. then the others after that.

This fixed it. I moved the CallSell in front of the CallBuy and CallHold and the PutSell in front of the PutBuy and PutHold and now I get the trigger.

It looks like this now

Code:
#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

Full Code

Code:
#JT EMA, MACD, AND RSI INDICATOR

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

def EMAFastCrossDown = EMAFast1 crosses below EMAFast2;
def EMAFastCrossUp = EMAFast1 crosses above EMAFast2;

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];

def EMABull = EMAFastBull and EMASlowBull and EMAPctBull;
def EMABear = EMAFastBear and EMASlowBear and EMAPctBear;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI crosses below RSIOverBought;
def RSICrossUp = RSI crosses above RSIOverSold;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 10;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if CallBuy == 1 and EndDay == 0 then CallBuy else if CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold else if CallSell == 1 then CallSell else if CallBuy == 1 and EndDay == 1 then EndDay else if CallHold == 1 and EndDay ==1 then EndDay else if CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay else double.nan;

def PutSignal = if PutBuy == 1 and EndDay == 0 then PutBuy else if PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold else if PutSell == 1 and CallHold == 0 then PutSell else if PutBuy ==1 and EndDay == 1 then EndDay else if PutHold ==1 and EndDay == 1 then EndDay else if PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy then close else CallOpen[1];
#def CallClose = if CallSell then close else CallClose[1];
#def CallQty = 100;
#def CallProfit = (CallOpen * CallQty) - (CallClose * CallQty);

#addlabel(1, "   ", color.black);
#addlabel(1, "Call Profit " + CallProfit, color.green);
#addlabel(1, "Trades " + CallQty/100, color.yellow);
#addlabel(1, "Avg Call Trade $ " + (CallProfit/CallQty), color.yellow);

#plot CBuy = if CallBuy or CallHold then CallClose else double.nan;
##cbuy.setdefaultcolor(color.green);
################################################################

#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");

Already made a quick 400% on BABA calls this morning in 12 minutes. In at 9:01 and out at 9:13
 
So there is only 1 more thing I would like to add in to keep from making a mistake but anytime I try to add it all the bars turn either gray or red.

I would like to add another criteria of EMAPctRound >= 0.1 or vice versa EMAPctRound <= -0.1

Currently I have this

Code:
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];

And I would like this

Code:
def EMAPctBull = (EMAPct >= EMAPct[1]) and (EMAPctRound > 0.1);
def EMAPctBear = (EMAPct <= EMAPct[1]) and (EMAPctRound < -0.1);

but like I said it keeps turning everything to gray or red and removes all other colors.

I have tried defining it by itself and adding it to the def EMAPctBull and def EMAPctBear but that doesn't work either. I have also tried substituting def EMAPctBull = EMAPct >= EMAPct[1]; with def EMAPctBull = EMAPct >= 0.1; and def EMAPctBull = EMAPctRound >= 0.1; but neither of those work either.

eHfwiBV.png
 
Last edited:
This fixed it. I moved the CallSell in front of the CallBuy and CallHold and the PutSell in front of the PutBuy and PutHold and now I get the trigger.

It looks like this now

Code:
#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

Full Code

Code:
#JT EMA, MACD, AND RSI INDICATOR

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

def EMAFastCrossDown = EMAFast1 crosses below EMAFast2;
def EMAFastCrossUp = EMAFast1 crosses above EMAFast2;

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];

def EMABull = EMAFastBull and EMASlowBull and EMAPctBull;
def EMABear = EMAFastBear and EMASlowBear and EMAPctBear;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI crosses below RSIOverBought;
def RSICrossUp = RSI crosses above RSIOverSold;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 10;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if CallBuy == 1 and EndDay == 0 then CallBuy else if CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold else if CallSell == 1 then CallSell else if CallBuy == 1 and EndDay == 1 then EndDay else if CallHold == 1 and EndDay ==1 then EndDay else if CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay else double.nan;

def PutSignal = if PutBuy == 1 and EndDay == 0 then PutBuy else if PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold else if PutSell == 1 and CallHold == 0 then PutSell else if PutBuy ==1 and EndDay == 1 then EndDay else if PutHold ==1 and EndDay == 1 then EndDay else if PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy then close else CallOpen[1];
#def CallClose = if CallSell then close else CallClose[1];
#def CallQty = 100;
#def CallProfit = (CallOpen * CallQty) - (CallClose * CallQty);

#addlabel(1, "   ", color.black);
#addlabel(1, "Call Profit " + CallProfit, color.green);
#addlabel(1, "Trades " + CallQty/100, color.yellow);
#addlabel(1, "Avg Call Trade $ " + (CallProfit/CallQty), color.yellow);

#plot CBuy = if CallBuy or CallHold then CallClose else double.nan;
##cbuy.setdefaultcolor(color.green);
################################################################

#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(153, 255, 153) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(153, 153, 255) else if  EndDay then createcolor(255, 0, 0) else color.dark_gray);

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");

Already made a quick 400% on BABA calls this morning in 12 minutes. In at 9:01 and out at 9:13

did you intend to compare the ema lengths ? and convert the length ratio to a percent ?
ema1 is a length, not a ema value

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];


maybe these should be changed to be less confusing
input EMA1_len = 5;
input EMA2_len = 12;
input EMA3_len = 34;
input EMA4_len = 50;
 
did you intend to compare the ema lengths ? and convert the length ratio to a percent ?
ema1 is a length, not a ema value

#EMA PERCENT
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPct >= EMAPct[1];
def EMAPctBear = EMAPct <= EMAPct[1];


maybe these should be changed to be less confusing
input EMA1_len = 5;
input EMA2_len = 12;
input EMA3_len = 34;
input EMA4_len = 50;
I am actually trying to compare the distance between the two. The closer they get the weaker the trend.

I know the math works because I have a label that tracks the distance between the 5EMA and 12EMA on the 1 min and 10 minute charts. I am just trying to be able to have the distance between them considered as a factor for a buy signal.
 
As you can see in this screen shot I have a label in the upper left box that says MARA 0.19 which is the distance between the 5 and 12 EMA on the 1 minute chart and MARA 0.31 which is the distance between the 5 and 12 EMA on the 10 minute chart. This is just another confirmation of trend for me. I want them both to be green for Calls or both blue for Puts.

Not important but my 1 min criteria is >0.1 or <-0.1 and for the 10 min chart it is >0.8 or <-0.8

DOUcOVM.jpg
 
I am actually trying to compare the distance between the two. The closer they get the weaker the trend.

I know the math works because I have a label that tracks the distance between the 5EMA and 12EMA on the 1 min and 10 minute charts. I am just trying to be able to have the distance between them considered as a factor for a buy signal.
the distance between 2 what?
lengths or averages?

read #8 again


This is comparing 2 constants 5 and 12
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
 
Last edited:
the distance between 2 what?
lengths or averages?

read #8 again


This is comparing 2 constants 5 and 12
def EMAPct = ((EMA1 / EMA2) * 100) - 100;
Well I guess the price point of the 5EMA at the time and the price point of the 12 EMA at the time.

The wider the cloud the higher the number or vice versa if the cloud is bearish than the wider the cloud the more negative the number.

Hope that makes sense.

If I went long on a stock where the 5EMA is basically right on top of the 12EMA than I don’t have much momentum to work with. The wider the better. Or in this case the higher number the better. Atleast for my style of trading.
 
I think I see what you’re saying now. Are you thinking I should use EMAFast1 / EMAFast2 vs EMA1 / EMA2. That would be a difference between the lengths vs the difference between the averages.

I’ll look into it in a few. I think you may have found my error.
 
That was it. Didn't even notice it until you pointed it out....multiple times, lol

I also added an input for the variable ThrHld so it could be changed according to time frame. (0.1 for 1 min chart and 0.8 for 10 minute chart)

I changed the RSI sell parameter from RSI crosses below 70 to RSI < RSI[1] -10 and from RSI crosses above 30 to RSI > RSI[1] +10 to limit the sell signals.

Finally, I also changed the EMA crossover to EMAPctRound crosses below StopThrHld and added an input to change between time frames. This gives a little earlier stop for the EMAs that are flattening out.

Updated Code:

Code:
#JT EMA, MACD, AND RSI INDICATOR

#If you want your last candle to be a stop than you need to change "Last Stop" to then time frame you're on. i.e. 1 for 1 minute, 10 for 10 minute, 60 for 1 hour etc.
#Change ThrHld to match the timeframe you are on. I use 0.1 for 1 minute and 0.8 for 10 minute.
#Change StopThrHld to match the timeframe you are on. I use 0.03 for 1 min and 0.1 for 10 minute.

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;
input EMA5 = 8;
input EMA6 = 9;
input ThrHld = 0.1;
input StopThrHld = 0.03;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
AddCloud(EMAFast1, EMAFast2, Color.light_green, Color.pink);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);
AddCloud(EMASlow1, EMASlow2, Color.dark_green, Color.dark_red);
def EMAFast3 = ExpAverage(close, EMA5);
def EMAFast4 = ExpAverage(close, EMA6);
AddCloud(EMAFast3, EMAFast4, Color.light_green, Color.red);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

#EMA PERCENT
def EMAPct = ((EMAFast1 / EMAFast2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPctRound >= EMAPctRound[1] and EMAPctRound > ThrHld;
def EMAPctBear = EMAPctRound <= EMAPctRound[1] and EMAPctRound < -ThrHld;

def EMABull = EMAFastBull and EMASlowBull;
def EMABear = EMAFastBear and EMASlowBear;

def EMAFastCrossDown = EMAPctRound crosses below StopThrHld;
def EMAFastCrossUp = EMAPctRound crosses above -StopThrHld;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI < RSI[1] - 10;
def RSICrossUp = RSI > RSI[1] + 10;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 1;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and EMAPctBull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and EMAPctBear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if CallBuy == 1 and EndDay == 0 then CallBuy else if CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold else if CallSell == 1 then CallSell else if CallBuy == 1 and EndDay == 1 then EndDay else if CallHold == 1 and EndDay ==1 then EndDay else if CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay else double.nan;

def PutSignal = if PutBuy == 1 and EndDay == 0 then PutBuy else if PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold else if PutSell == 1 and CallHold == 0 then PutSell else if PutBuy ==1 and EndDay == 1 then EndDay else if PutHold ==1 and EndDay == 1 then EndDay else if PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
def CallOpen = if CallBuy and !CallBuy[1] then 1 else 0;
def PutOpen = if PutBuy and !PutBuy[1] then 1 else 0;
def CallClose = if CallSell then 1 else 0;
def PutClose = if PutSell then 1 else 0;
def LastClose = if EndDay then 1 else 0;
################################################################

#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(204, 255, 204) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(102, 255, 255) else if  EndDay then createcolor(255, 0, 0) else createcolor(40, 40, 40));

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy[-1], close, 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy[-1], close, 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell[-1] or EndDay[-1], close, 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell[-1] or EndDay[-1], close, 100, Color.RED, Color.LIGHT_RED, "SELL PUT");
 
Last edited:
That was it. Didn't even notice it until you pointed it out....multiple times, lol

I also added an input for the variable ThrHld so it could be changed according to time frame. (0.1 for 1 min chart and 0.8 for 10 minute chart)

I changed the RSI sell parameter from RSI crosses below 70 to RSI < RSI[1] -10 and from RSI crosses above 30 to RSI > RSI[1] +10 to limit the sell signals.

Finally, I also changed the EMA crossover to EMAPctRound crosses below StopThrHld and added an input to change between time frames. This gives a little earlier stop for the EMAs that are flattening out.

Updated Code:

Code:
#JT EMA, MACD, AND RSI INDICATOR

#If you want your last candle to be a stop than you need to change "Last Stop" to then time frame you're on. i.e. 1 for 1 minute, 10 for 10 minute, 60 for 1 hour etc.
#Change ThrHld to match the timeframe you are on. I use 0.1 for 1 minute and 0.8 for 10 minute.
#Change StopThrHld to match the timeframe you are on. I use 0.03 for 1 min and 0.1 for 10 minute.

declare upper;

#EMAs
input EMA1 = 5;
input EMA2 = 12;
input EMA3 = 34;
input EMA4 = 50;
input EMA5 = 8;
input EMA6 = 9;
input ThrHld = 0.1;
input StopThrHld = 0.03;

def EMAFast1 = ExpAverage(close, EMA1);
def EMAFast2 = ExpAverage(close, EMA2);
AddCloud(EMAFast1, EMAFast2, Color.light_green, Color.pink);
def EMASlow1 = ExpAverage(close, EMA3);
def EMASlow2 = ExpAverage(close, EMA4);
AddCloud(EMASlow1, EMASlow2, Color.dark_green, Color.dark_red);
def EMAFast3 = ExpAverage(close, EMA5);
def EMAFast4 = ExpAverage(close, EMA6);
AddCloud(EMAFast3, EMAFast4, Color.light_green, Color.red);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

#EMA PERCENT
def EMAPct = ((EMAFast1 / EMAFast2) * 100) - 100;
def EMAPctRound = Round(EMAPct, 2);
def EMAPctBull = EMAPctRound >= EMAPctRound[1] and EMAPctRound > ThrHld;
def EMAPctBear = EMAPctRound <= EMAPctRound[1] and EMAPctRound < -ThrHld;

def EMABull = EMAFastBull and EMASlowBull;
def EMABear = EMAFastBear and EMASlowBear;

def EMAFastCrossDown = EMAPctRound crosses below StopThrHld;
def EMAFastCrossUp = EMAFast1 crosses above -StopThrHld;

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#RSI
input RSILength = 14;
input RSIOverBought = 70;
input RSIOverSold = 30;

def NetChgAvg = WildersAverage(close - close[1], RSILength);
def TotChgAvg = WildersAverage(AbsValue(close - close[1]), RSILength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def RSIBull = RSI > 40 and RSI >= RSI[1];
def RSIBear = RSI < 60 and RSI <= RSI[1];
def RSICrossDown = RSI < RSI[1] - 10;
def RSICrossUp = RSI > RSI[1] + 10;

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 30;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 1;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and EMAPctBull and MACDBull and RSIBull;
def CallHold = TradingDay and EMABull;
def CallPrev = CallBuy[1] or CallHold[1];
def PutBuy = TradingDay and EMABear and EMAPctBear and MACDBear and RSIBear;
def PutHold = TradingDay and EMABear;
def PutPrev = PutBuy[1] or PutHold[1];
def CallSell =  TradingDay and (EMAFastCrossDown or RSICrossDown);
def PutSell =  TradingDay and (EMAFastCrossUp or RSICrossUp);

def CallSignal = if CallBuy == 1 and EndDay == 0 then CallBuy else if CallHold == 1 and CallBuy == 0 and EndDay == 0 then CallHold else if CallSell == 1 then CallSell else if CallBuy == 1 and EndDay == 1 then EndDay else if CallHold == 1 and EndDay ==1 then EndDay else if CallBuy == 0 and CallHold == 0 and EndDay ==1 then EndDay else double.nan;

def PutSignal = if PutBuy == 1 and EndDay == 0 then PutBuy else if PutHold == 1 and PutBuy == 0 and EndDay == 0 then PutHold else if PutSell == 1 and CallHold == 0 then PutSell else if PutBuy ==1 and EndDay == 1 then EndDay else if PutHold ==1 and EndDay == 1 then EndDay else if PutBuy == 0 and PutHold == 0 and EndDay ==1 then EndDay else double.nan;

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy then close else CallOpen[1];
#def CallClose = if CallSell then close else CallClose[1];
#def CallQty = 100;
#def CallProfit = (CallOpen * CallQty) - (CallClose * CallQty);

#addlabel(1, "   ", color.black);
#addlabel(1, "Call Profit " + CallProfit, color.green);
#addlabel(1, "Trades " + CallQty/100, color.yellow);
#addlabel(1, "Avg Call Trade $ " + (CallProfit/CallQty), color.yellow);

#plot CBuy = if CallBuy or CallHold then CallClose else double.nan;
##cbuy.setdefaultcolor(color.green);
################################################################

#COLOR BARS
AssignPriceColor(if CallSell then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(204, 255, 204) else if PutSell then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(102, 255, 255) else if  EndDay then createcolor(255, 0, 0) else createcolor(40, 40, 40));

#ORDERS
input ShowOrders = no;
##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay, open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");
people get immersed in something and operate on what they think they did, instead of pulling back and looking things over. happens to everyone.
 
Haha. I’m just not super detail oriented and tend to scan rather than dive into every detail! Thank goodness for guys like you with a good eye.

If you feel like it, I would like to get that profit/loss label and plot working. I tried to use your prior help to get it to work but I couldn’t figure it out.

It’s down towards the bottom with ##### before it.

The strategy isn’t working out that well the way I have it coded but it would all be the same with the way you coded one of my other studies.

In all honestly, I haven’t spend a lot of time on it. Wanted to get the base down first.
 
This is the most up to date code. THIS IS A STRATEGY and not a study! I am still testing, adding, and adjusting. I have made multiple trades and only one was a loser. I am still super picky and this is not the end all be all. Made over 500% on Friday trading BABA with this. I was using the 1 minute version when trading BABA which is what this defaults to but I recommend trading it on a 10 minute chart from what I am seeing so far. Refer to the notes at the top of the strategy to see what needs to be changed for the 10 minute chart. I actually have both the 1 minute and 10 minute up at home but only look at the 10 minute when I am not on my desktop. Backtesting is lacking and the results aren't that great. Still a work in progress and I think diving deep into the EMA cloud separation will be the key to reliable signals. Any findings would be appreciated.

I don't use a scanner. I use watchlist labels instead but I don't want to post those just yet until I get the code perfected.

Code:
#JT EMA, MACD, AND TSI INDICATOR

#If you want your last candle to be a stop than you need to change "Last Stop" to the time frame you're on. i.e. 1 for 1 minute, 10 for 10 minute, 60 for 1 hour etc.
#Change ThrHldFast to match the timeframe you are on. I use 0.1 for 1 minute and 0.7 for 10 minute.
#Change ThrHldFast to match the timeframe you are on. I use 1.0 for 1 minute and 1.0 for 10 minute.
#Change StopThrHld to match the timeframe you are on. I use 0.03 for 1 min and 0.1 for 10 minute.

declare upper;

#EMAs
input Ema_1 = 5;
input Ema_2 = 12;
input Ema_3 = 34;
input Ema_4 = 50;
input Ema_5 = 8;
input Ema_6 = 9;
input ThrHldFast = 0.1;
input ThrHldSlow = 1.0;
input StopThrHld = 0.03;

plot EMAFast1 = ExpAverage(close, EMA_1);
EMAFast1.setdefaultcolor(color.blue);
plot EMAFast2 = ExpAverage(close, EMA_2);
EMAFast2.setdefaultcolor(color.yellow);
AddCloud(EMAFast1, EMAFast2, Color.green, Color.red);
plot EMASlow1 = ExpAverage(close, EMA_3);
EMASlow1.setdefaultcolor(color.blue);
plot EMASlow2 = ExpAverage(close, EMA_4);
EMASlow2.setdefaultcolor(color.yellow);
AddCloud(EMASlow1, EMASlow2, Color.dark_green, Color.dark_red);
def EMAFast3 = ExpAverage(close, EMA_5);
def EMAFast4 = ExpAverage(close, EMA_6);
#AddCloud(EMAFast3, EMAFast4, Color.light_green, Color.red);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

#EMA PERCENT
def EMAPctFast = ((EMAFast1 / EMAFast2) * 100) - 100;
def EMAPctRoundFast = Round(EMAPctFast, 2);
def EMAPctBullFast = EMAPctRoundFast >= EMAPctRoundFast[1] or EMAPctRoundFast >= ThrHldFast;
def EMAPctBearFast = EMAPctRoundFast <= EMAPctRoundFast[1] or EMAPctRoundFast <= -ThrHldFast;

def EMAPctSlow = ((EMASlow1 / EMASlow2) * 100) - 100;
def EMAPctRoundSlow = Round(EMAPctSlow, 2);
def EMAPctBullSlow = (EMAPctRoundSlow >= EMAPctRoundSlow[1]) or (EMAPctRoundSlow >= ThrHldSlow);
def EMAPctBearSlow = (EMAPctRoundSlow <= EMAPctRoundSlow[1]) or (EMAPctRoundSlow <= -ThrHldSlow);

def EMABull = EMAFastBull and EMASlowBull;
def EMABear = EMAFastBear and EMASlowBear;

def EMAFastCrossDown = (EMAPctRoundFast crosses below StopThrHld) or (EMAFast1 crosses below EMAFast2);
def EMAFastCrossUp = (EMAPctRoundFast crosses above -StopThrHld) or (EMAFast1 crosses above EMAFast2);

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;

#TRUE STENGTH INDEX(TSI)
def DiffShort = close - close[1];
def DoubleSmoothedAbsDiffShort = ExpAverage(ExpAverage(AbsValue(diffShort), TSILongLength), TSIShortLength);

#TSI
def TSI = if DoubleSmoothedAbsDiffShort == 0 then 0 else round((100 * (ExpAverage(ExpAverage(DiffShort, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiffShort), 2);
def ZeroLine2 = 0;

def TSIBull = TSI > 10 and TSI > TSI[1];
def TSIBear = TSI < -10 and TSI < TSI[1];
def TSICrossDown = TSI < (TSI[1] * 0.9);
def TSICrossUp = TSI > (TSI[1] * 0.9);

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 19;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 1;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and EMAPctBullFast and EMAPctBullSlow and MACDBull and TSIBull;
def CallHold = TradingDay and EMABull and (CallBuy[1] or CallHold[1]);
def CallSell = TradingDay and (CallBuy[1] or CallHold[1]) and (EMAFastCrossDown or TSICrossDown);
def CallSell2 = CallSell and !CallSell[1];
def PutBuy = TradingDay and EMABear and EMAPctBearFast and EMAPctBearSlow and MACDBear and TSIBear;
def PutHold = TradingDay and EMABear and (PutBuy[1] or PutHold[1]);
def PutSell =  TradingDay and (PutBuy[1] or PutHold[1]) and (EMAFastCrossUp or TSICrossUp);
def PutSell2 = PutSell and !PutSell[1];

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy and !CallBuy[1] then 1 else 0;
#def PutOpen = if PutBuy and !PutBuy[1] then 1 else 0;
#def CallClose = if CallSell then 1 else 0;
#def PutClose = if PutSell then 1 else 0;
#def LastClose = if EndDay then 1 else 0;

#COLOR BARS
AssignPriceColor(if CallSell2 then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(204, 255, 204) else if PutSell2 then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(102, 255, 255) else if EndDay then createcolor(255, 0, 0) else createcolor(40, 40, 40));

#ORDERS
input ShowOrders = no;

##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay[-1], open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay[-1], open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");

#ALERTS
Alert(CallSell2 or PutSell2, "SELL", Alert.BAR, Sound.Bell);
 
This is the most up to date code. THIS IS A STRATEGY and not a study! I am still testing, adding, and adjusting. I have made multiple trades and only one was a loser. I am still super picky and this is not the end all be all. Made over 500% on Friday trading BABA with this. I was using the 1 minute version when trading BABA which is what this defaults to but I recommend trading it on a 10 minute chart from what I am seeing so far. Refer to the notes at the top of the strategy to see what needs to be changed for the 10 minute chart. I actually have both the 1 minute and 10 minute up at home but only look at the 10 minute when I am not on my desktop. Backtesting is lacking and the results aren't that great. Still a work in progress and I think diving deep into the EMA cloud separation will be the key to reliable signals. Any findings would be appreciated.

I don't use a scanner. I use watchlist labels instead but I don't want to post those just yet until I get the code perfected.

Code:
#JT EMA, MACD, AND TSI INDICATOR

#If you want your last candle to be a stop than you need to change "Last Stop" to the time frame you're on. i.e. 1 for 1 minute, 10 for 10 minute, 60 for 1 hour etc.
#Change ThrHldFast to match the timeframe you are on. I use 0.1 for 1 minute and 0.7 for 10 minute.
#Change ThrHldFast to match the timeframe you are on. I use 1.0 for 1 minute and 1.0 for 10 minute.
#Change StopThrHld to match the timeframe you are on. I use 0.03 for 1 min and 0.1 for 10 minute.

declare upper;

#EMAs
input Ema_1 = 5;
input Ema_2 = 12;
input Ema_3 = 34;
input Ema_4 = 50;
input Ema_5 = 8;
input Ema_6 = 9;
input ThrHldFast = 0.1;
input ThrHldSlow = 1.0;
input StopThrHld = 0.03;

plot EMAFast1 = ExpAverage(close, EMA_1);
EMAFast1.setdefaultcolor(color.blue);
plot EMAFast2 = ExpAverage(close, EMA_2);
EMAFast2.setdefaultcolor(color.yellow);
AddCloud(EMAFast1, EMAFast2, Color.green, Color.red);
plot EMASlow1 = ExpAverage(close, EMA_3);
EMASlow1.setdefaultcolor(color.blue);
plot EMASlow2 = ExpAverage(close, EMA_4);
EMASlow2.setdefaultcolor(color.yellow);
AddCloud(EMASlow1, EMASlow2, Color.dark_green, Color.dark_red);
def EMAFast3 = ExpAverage(close, EMA_5);
def EMAFast4 = ExpAverage(close, EMA_6);
#AddCloud(EMAFast3, EMAFast4, Color.light_green, Color.red);

def EMAFastBull = EMAFast1 > EMAFast2;
def EMASlowBull = EMASlow1 > EMASlow2;
def EMAFastBear = EMAFast1 < EMAFast2;
def EMASlowBear = EMASlow1 < EMASlow2;

#EMA PERCENT
def EMAPctFast = ((EMAFast1 / EMAFast2) * 100) - 100;
def EMAPctRoundFast = Round(EMAPctFast, 2);
def EMAPctBullFast = EMAPctRoundFast >= EMAPctRoundFast[1] or EMAPctRoundFast >= ThrHldFast;
def EMAPctBearFast = EMAPctRoundFast <= EMAPctRoundFast[1] or EMAPctRoundFast <= -ThrHldFast;

def EMAPctSlow = ((EMASlow1 / EMASlow2) * 100) - 100;
def EMAPctRoundSlow = Round(EMAPctSlow, 2);
def EMAPctBullSlow = (EMAPctRoundSlow >= EMAPctRoundSlow[1]) or (EMAPctRoundSlow >= ThrHldSlow);
def EMAPctBearSlow = (EMAPctRoundSlow <= EMAPctRoundSlow[1]) or (EMAPctRoundSlow <= -ThrHldSlow);

def EMABull = EMAFastBull and EMASlowBull;
def EMABear = EMAFastBear and EMASlowBear;

def EMAFastCrossDown = (EMAPctRoundFast crosses below StopThrHld) or (EMAFast1 crosses below EMAFast2);
def EMAFastCrossUp = (EMAPctRoundFast crosses above -StopThrHld) or (EMAFast1 crosses above EMAFast2);

#MACD
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;

def Value = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def Average = ExpAverage(Value, MACDLength);
def MACDDiff = Value - Average;
def ZeroLine = 0;

def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];

#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;

#TRUE STENGTH INDEX(TSI)
def DiffShort = close - close[1];
def DoubleSmoothedAbsDiffShort = ExpAverage(ExpAverage(AbsValue(diffShort), TSILongLength), TSIShortLength);

#TSI
def TSI = if DoubleSmoothedAbsDiffShort == 0 then 0 else round((100 * (ExpAverage(ExpAverage(DiffShort, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiffShort), 2);
def ZeroLine2 = 0;

def TSIBull = TSI > 10 and TSI > TSI[1];
def TSIBear = TSI < -10 and TSI < TSI[1];
def TSICrossDown = TSI < (TSI[1] * 0.9);
def TSICrossUp = TSI > (TSI[1] * 0.9);

#START AND END TIME
input StartTime = 0930;
input StartTimeDelay = 19;
input EndTime = 1600;
input EndTimeDelay = 10;
input LastStop = 1;

def Start = SecondsFromTime(StartTime);
def StartSignalDelay = StartTimeDelay * 60;
def StartSignal = Start > StartSignalDelay;
def End = SecondsTillTime(EndTime);
def EndSignalDelay = EndTimeDelay * 60;
def EndSignal = End > EndSignalDelay;
def LastStopDelay = LastStop*60;
def TradingDay = StartSignal and EndSignal;
def EndDay = End == LastStopDelay;

#INDICATORS
def CallBuy = TradingDay and EMABull and EMAPctBullFast and EMAPctBullSlow and MACDBull and TSIBull;
def CallHold = TradingDay and EMABull and (CallBuy[1] or CallHold[1]);
def CallSell = TradingDay and (CallBuy[1] or CallHold[1]) and (EMAFastCrossDown or TSICrossDown);
def CallSell2 = CallSell and !CallSell[1];
def PutBuy = TradingDay and EMABear and EMAPctBearFast and EMAPctBearSlow and MACDBear and TSIBear;
def PutHold = TradingDay and EMABear and (PutBuy[1] or PutHold[1]);
def PutSell =  TradingDay and (PutBuy[1] or PutHold[1]) and (EMAFastCrossUp or TSICrossUp);
def PutSell2 = PutSell and !PutSell[1];

############TRYING TO ADD LINES AND PROFIT LABELS###############
#def CallOpen = if CallBuy and !CallBuy[1] then 1 else 0;
#def PutOpen = if PutBuy and !PutBuy[1] then 1 else 0;
#def CallClose = if CallSell then 1 else 0;
#def PutClose = if PutSell then 1 else 0;
#def LastClose = if EndDay then 1 else 0;

#COLOR BARS
AssignPriceColor(if CallSell2 then createcolor(255, 0, 0) else if CallBuy then createcolor(0, 255, 0) else if CallHold then createcolor(204, 255, 204) else if PutSell2 then createcolor(255, 0, 0) else if PutBuy then createcolor(0, 0, 255) else if PutHold then createcolor(102, 255, 255) else if EndDay then createcolor(255, 0, 0) else createcolor(40, 40, 40));

#ORDERS
input ShowOrders = no;

##BUY
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and CallBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY CALL");
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PutBuy, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN, "BUY PUT");

##SELL
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSell or EndDay[-1], open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL CALL");
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSell or EndDay[-1], open[-1], 100, Color.RED, Color.LIGHT_RED, "SELL PUT");

#ALERTS
Alert(CallSell2 or PutSell2, "SELL", Alert.BAR, Sound.Bell);
@a1cturner wow amazing job, i just love it. a quick question, how can i changes the gray candles color for white? because i cant see the candles.. thanks and keep up the good job
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
446 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top