EMA Help

adefrenza

New member
Hi, and wondering if someone can help with this script. The script below will generate a chart label if one EMA crosses above another, It can be used in any chart, but I would like to modify it so the user can see the lable using two time frames. For example, if using a 1 minute chart, I would like the script to to display an EMA cross on a 5 minute chart as well. Of course I can load a second chart onto TOS with the second time frame and watch both of them, but that takes space. I would like to see both labels on whatever chart I have loaded. Can anyone help?


input AvgType = AverageType.EXPONENTIAL;
input PaintBars = yes;
input ShowBubbles = yes;
input ShowLabels = yes;
input UseEmaCross = yes;
input EMA1 = 10;
input EMA2 = 20;

def EMA1Val = MovAvgExponential(close, EMA1);
def EMA2Val = MovAvgExponential(close, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;

AddLabel(ShowLabels and UseEmaCross, "EMA: " + (if EMADirection == 1 then "Bullish" else if EMADirection == -1 then "Bearish" else "Neutral"),
if EMADirection == 1 then Color.GREEN else if EMADirection == -1 then Color.RED else Color.GRAY);
 
Solution
Hi, and wondering if someone can help with this script. The script below will generate a chart label if one EMA crosses above another, It can be used in any chart, but I would like to modify it so the user can see the lable using two time frames. For example, if using a 1 minute chart, I would like the script to to display an EMA cross on a 5 minute chart as well. Of course I can load a second chart onto TOS with the second time frame and watch both of them, but that takes space. I would like to see both labels on whatever chart I have loaded. Can anyone help?


show labels for 2 sets of averages, using the same type and lengths,
..1 set uses chart time,
..1 set a 2nd aggregation time.

the labels show the minutes used...
Hi, and wondering if someone can help with this script. The script below will generate a chart label if one EMA crosses above another, It can be used in any chart, but I would like to modify it so the user can see the lable using two time frames. For example, if using a 1 minute chart, I would like the script to to display an EMA cross on a 5 minute chart as well. Of course I can load a second chart onto TOS with the second time frame and watch both of them, but that takes space. I would like to see both labels on whatever chart I have loaded. Can anyone help?


show labels for 2 sets of averages, using the same type and lengths,
..1 set uses chart time,
..1 set a 2nd aggregation time.

the labels show the minutes used by the averages.
can show all 4 average lines.

when a crossing happens with the 2nd aggregation lines, the label will show crossed for several bars.
(qty of bars = 2nd agg minutes / chart minutes)

you didn't have any formulas for testing if the averages crossed, so your input UseEmaCross = yes; didn't check for it.

to calculate prices based on another time frame, add a period parameter to a price function,
input agg = aggregationPeriod.five_MIN;
def x = close(period = agg);


Code:
# ema_cross_mtf_0

#https://usethinkscript.com/threads/ema-help.12665/
#  display a label for an EMA cross on 2 time frames.

def na = double.nan;

input AvgType = AverageType.EXPONENTIAL;
input ma1_len = 10;
input ma2_len = 20;

input ShowLabels = yes;
input UseEmaCross = yes;
#input PaintBars = yes;
#input ShowBubbles = yes;
input show_average_lines = yes;

def ma1 = MovingAverage(AvgType, close, ma1_len);
def ma2 = MovingAverage(AvgType, close, ma2_len);
def ma1xup = ma1 crosses above ma2;
def ma1xdwn = ma1 crosses below ma2;
def ma_dir = if ma1 > ma2 then 1 else if ma1 < ma2 then -1 else 0;

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 60000);

addlabel(1, " " , color.black);
AddLabel(ShowLabels, "MA, " + chartmin + " min: " + (if ma_dir == 1 then "Bullish" else if ma_dir == -1 then "Bearish" else "Neutral"),
(if ma_dir == 1 then Color.GREEN else if ma_dir == -1 then Color.RED else Color.GRAY));

AddLabel(ShowLabels, (if ma1xup then "Crossed above" else if ma1xdwn then "Crossed below" else "--"),
(if ma1xup then Color.GREEN else if ma1xdwn then Color.RED else Color.GRAY));

#----------------------------

input agg = aggregationPeriod.five_MIN;
#input agg = aggregationPeriod.fifteen_MIN;
#input agg = aggregationPeriod.thirty_MIN;

def agg_min = agg/60000;

def ma1_agg = MovingAverage(AvgType, close(period = agg), ma1_len);
def ma2_agg = MovingAverage(AvgType, close(period = agg), ma2_len);
def ma1aggxup = ma1_agg crosses above ma2_agg;
def ma1aggxdwn = ma1_agg crosses below ma2_agg;

def ma_agg_dir = if ma1_agg > ma2_agg then 1 else if ma1_agg < ma2_agg then -1 else 0;

addlabel(1, " " , color.black);
AddLabel(ShowLabels, "MA, " + agg_min + " min: " + (if ma_agg_dir == 1 then "Bullish" else if ma_agg_dir == -1 then "Bearish" else "Neutral"),
(if ma_agg_dir == 1 then Color.GREEN else if ma_agg_dir == -1 then Color.RED else Color.GRAY));

AddLabel(ShowLabels, (if ma1aggxup then "Crossed above" else if ma1aggxdwn then "Crossed below" else "--"),
(if ma1aggxup then Color.GREEN else if ma1aggxdwn then Color.RED else Color.GRAY));


input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.setdefaultcolor(getcolor(1));
z1.hidebubble();
plot z2 = if show_lines then ma2 else na;
z2.setdefaultcolor(getcolor(2));
z2.hidebubble();

plot z3 = if show_lines then ma1_agg else na;
z3.setdefaultcolor(getcolor(3));
z3.hidebubble();
plot z4 = if show_lines then ma2_agg else na;
z4.setdefaultcolor(getcolor(4));
z4.hidebubble();
#

.
X3PhS1C.jpg

.
G7EjC7k.jpg
 
Solution

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