Combine EMA cross and ATR Trailing Stop

Ajordan930

New member
Hello,

I am trying to learn how to write/manipulate code to combine a signal from existing code to generate if another criteria is met such as stacked ema's. I have an indicator that gives a buy and sell signal that I use with confluence of stacked ema's (8 and 21). The code I wrote shows an up arrow when 8 crosses above 21 and a down arrow when 5 crosses below the 21.

I dabbled in writing some code for the ema crossover that I was able to get to work so I was happy about that with it being my first time but I know I have a ton to learn to get this to work how I would like and eventually build the strategy.

# EMA Crossover Buy Signal and Exit signal
# This script generates buy signals when the 8-period EMA crosses above the 21-period EMA and generates sell signals when then 5 crosses below the 21-period EMA

input fastLength = 5;
input medianLength = 8;
input slowLength = 21;

def EMAfast = ExpAverage(close, fastLength);
def EMAmedian = EXpAverage (close, medianLength);
def EMAslow = ExpAverage(close, slowLength);

plot BuySignal = if EMAmedian > EMAslow and EMAmedian[1] <= EMAslow[1] then 1 else 0;
BuySignal.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);

plot SellSignal = if EMAfast < EMAslow and EMAfast[1] >= EMAslow[1] then 1 else 0;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);


Now I am trying to figure out how to use this existing code to plot only when there are stacked ema's but honestly I have no idea where to start. Any guidance is appreciated.


input Key_Value = 1.0;
def a = key_value;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;


script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);


#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;


def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "Buy", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "Sell", globalColor("Red"), yes);


input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
input mobile_arrows_on = yes;
plot mobile_arrows_buy = if buy and mobile_arrows_on then 1 else 0;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);

plot mobile_arrows_sell = if sell and mobile_arrows_on then 1 else 0;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);

Thanks in advance
Jason
 

Attachments

  • EMA Crossover Buy Signal and Exit.txt
    818 bytes · Views: 137
  • UT Auto bot.txt
    2.7 KB · Views: 100
Hello,

I am trying to learn how to write/manipulate code to combine a signal from existing code to generate if another criteria is met such as stacked ema's. I have an indicator that gives a buy and sell signal that I use with confluence of stacked ema's (8 and 21). The code I wrote shows an up arrow when 8 crosses above 21 and a down arrow when 5 crosses below the 21.

I dabbled in writing some code for the ema crossover that I was able to get to work so I was happy about that with it being my first time but I know I have a ton to learn to get this to work how I would like and eventually build the strategy.

# EMA Crossover Buy Signal and Exit signal
# This script generates buy signals when the 8-period EMA crosses above the 21-period EMA and generates sell signals when then 5 crosses below the 21-period EMA

input fastLength = 5;
input medianLength = 8;
input slowLength = 21;

def EMAfast = ExpAverage(close, fastLength);
def EMAmedian = EXpAverage (close, medianLength);
def EMAslow = ExpAverage(close, slowLength);

plot BuySignal = if EMAmedian > EMAslow and EMAmedian[1] <= EMAslow[1] then 1 else 0;
BuySignal.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);

plot SellSignal = if EMAfast < EMAslow and EMAfast[1] >= EMAslow[1] then 1 else 0;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);


Now I am trying to figure out how to use this existing code to plot only when there are stacked ema's but honestly I have no idea where to start. Any guidance is appreciated.


input Key_Value = 1.0;
def a = key_value;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;


script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);


#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;


def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "Buy", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "Sell", globalColor("Red"), yes);


input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
input mobile_arrows_on = yes;
plot mobile_arrows_buy = if buy and mobile_arrows_on then 1 else 0;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);

plot mobile_arrows_sell = if sell and mobile_arrows_on then 1 else 0;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);

Thanks in advance
Jason

here is my iterpretation to merging the 2 studies. can choose to use stacked emas or not

Code:
#ema_cross_atr_stop

#https://usethinkscript.com/threads/combine-ema-cross-and-atr-trailing-stop.16737/

#Questions UnAnswered Graveyard 
#Combine EMA cross and ATR Trailing Stop

script nz{
 input x = close;
 input y = 0;
 plot result = if isnan(x) then y else x;
}

def na = double.nan;
def bn = barnumber();

input fastLength = 5;
input medianLength = 8;
input slowLength = 21;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);

def EMAfast = ExpAverage(close, fastLength);
def EMAmedian = EXpAverage (close, medianLength);
def EMAslow = ExpAverage(close, slowLength);

def stackup = (EMAfast > EMAmedian and EMAmedian > EMAslow);
def stackdwn = (EMAfast < EMAmedian and EMAmedian < EMAslow);

# move the conditions to be DEFs, so can be used in other formulas
# use input controlled plots to display
def buysignal = if EMAmedian > EMAslow and EMAmedian[1] <= EMAslow[1] then 1 else 0;
def SellSignal = if EMAfast < EMAslow and EMAfast[1] >= EMAslow[1] then 1 else 0;

input ema_cross_arrows = no;
plot zBuySignal = if ema_cross_arrows and buysignal then 1 else 0;
zBuySignal.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
zBuySignal.SetDefaultColor(Color.GREEN);

plot zSellSignal = if ema_cross_arrows and sellsignal then 1 else 0;
zSellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zSellSignal.SetDefaultColor(Color.RED);

input show_avg_lines = no;
plot z1 = if show_avg_lines then EMAfast else na;
plot z2 = if show_avg_lines then EMAmedian else na;
plot z3 = if show_avg_lines then EMAslow else na;

#addverticalline(BuySignal , "-", color.green);
#addverticalline(sellSignal , "-", color.red);


# Key Vaule. This changes the sensitivity
input Key_Value = 1.0;
input atr_period = 10;
input Signals_from_Heikin_Ashi_Candles = no;
def ha_sigs = signals_from_Heikin_Ashi_Candles;

input show_labels = yes;
def xATR = atr(atr_period);
def nLoss = Key_Value * xATR;

def ha_close = (open + high + low + close)/4;
def src = if ha_sigs then ha_close else close;


#xATRTrailingStop = 0.0
def xATRTrailingStop = 
 compoundValue(1,
   if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0),
     max(nz(xATRTrailingStop[1]), src - nLoss),
     if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0),
       min(nz(xATRTrailingStop[1]), src + nLoss),
       if(src > nz(xATRTrailingStop[1], 0),
         src - nLoss,
         src + nLoss
       )
     )
   )
,0);


input show_trail_stop = yes;
plot ztrail = if show_trail_stop then xATRTrailingStop else na;
ztrail.SetDefaultColor(Color.yellow);


#pos = 0
def pos = compoundValue(1, 
  if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
    if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1,
      nz(pos[1], 0))),
  0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);
def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;
def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

input use_stacked_avgs = yes;
input arrows_on = yes;
def mobile_arrows_buy2 = if !buy then 0
  else if use_stacked_avgs and stackup and arrows_on then 1 
  else if !use_stacked_avgs and arrows_on then 1  
  else 0;
def mobile_arrows_sell2 = if !sell then 0
  else if use_stacked_avgs and stackdwn and arrows_on then 1 
  else if !use_stacked_avgs and arrows_on then 1  
  else 0;

def y1 = 0.005;
#addchartbubble(show_labels and buy, low*0.996, "Buy", globalColor("Green"), no);
#addchartbubble(show_labels and sell, high*1.004, "Sell", globalColor("Red"), yes);
addchartbubble(show_labels and mobile_arrows_buy2, (low*(1-y1)), "Buy", globalColor("Green"), no);
addchartbubble(show_labels and mobile_arrows_sell2, (high*(1+y1)), "Sell", globalColor("Red"), yes);


input price_color_on = yes;
#assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
#assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);
#    better to use only one instance of assignPriceColor()
assignPriceColor(if price_color_on and barbuy then globalColor("Green")  else if price_color_on and barsell then globalColor("Red") else color.currENT);


input alert_sound_on = yes;
#alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
#alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
#alert(alert_sound_on and buy, "UT Long", alert.bar, sound.ring);
#alert(alert_sound_on and sell, "UT Short", alert.bar, sound.bell);
alert(alert_sound_on and mobile_arrows_buy2, "UT Long", alert.bar, sound.ring);
alert(alert_sound_on and mobile_arrows_sell2, "UT Short", alert.bar, sound.bell);


plot mobile_arrows_buy = mobile_arrows_buy2;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);

plot mobile_arrows_sell = mobile_arrows_sell2;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);
#
 

Attachments

  • img2.JPG
    img2.JPG
    64.2 KB · Views: 50

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
358 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