Price above 150 ema in weekly but below in daily chart?

new_spy123

New member
Hi All,

I want to create a study alert which alerts me when price is above 150 ema on weekly chart but price below 150 ema on daily chart. How do I do this?

Here is what I have and this alerts me only when price is below 150 ema on daily chart. How do I add weekly chart condition to this?:
Code:
close is less than MovAvgExponential("length"=150)."AvgExp" True

Please note that I do programming for living, so if you can point me to any similar example or give some pointers that would be great.

Solution
I hope this will help to get you started.
It draws 2 lines on a chart and draws purple shading between them when close is between them.
It does have an alert function, but i can't verify it works after hours.

I used a chart setting of 2Y 1D, and used these stocks for testing. their last bar is between the lines.
SBUX , EMR , PM

you can define the data to be generated from a different time frame than what is on the chart. this is called 2nd aggregation. you can have a chart of hour bars and do calculations on data from a larger time, a 2nd aggregation.
you can define only one 2nd agg time in a formula, so create a formula for each data that will use 2nd agg. then combine those variables in another formula.

this uses 2nd agg for...

halcyonguy

Well-known member
VIP
I hope this will help to get you started.
It draws 2 lines on a chart and draws purple shading between them when close is between them.
It does have an alert function, but i can't verify it works after hours.

I used a chart setting of 2Y 1D, and used these stocks for testing. their last bar is between the lines.
SBUX , EMR , PM

you can define the data to be generated from a different time frame than what is on the chart. this is called 2nd aggregation. you can have a chart of hour bars and do calculations on data from a larger time, a 2nd aggregation.
you can define only one 2nd agg time in a formula, so create a formula for each data that will use 2nd agg. then combine those variables in another formula.

this uses 2nd agg for both emas, by defining the 2nd agg time as a parameter of the price function, close.
this study can be used on a chart of 1 minute up to a day chart, assuming there is 150 weeks of data to be found.

Ruby:
#    close <  daily ema150 AND close >  weekly ema150

#----------------------------
input aggd = AggregationPeriod.DAY;
input lengthday = 150;
def ExpAvgday = ExpAverage( close( period = aggd), lengthday );

input aggw = AggregationPeriod.week;
input lengthweek = 150;
def ExpAvgweek = ExpAverage( close( period = aggw), lengthweek );

#----------------------------
# true when close is between both lines
def both_cond = (close < ExpAvgday) and (close > ExpAvgweek);

#----------------------------
# display a label when both conditions are true
addlabel(both_cond, "close < daily ema150  AND  close > weekly ema150", color.yellow);

# draw a cloud between the 2 emas when condition is true.
addcloud( (if both_cond then ExpAvgday else double.nan), ExpAvgweek, color.magenta, color.magenta);

input show_ema_lines = yes;
plot zd = if show_ema_lines then ExpAvgday else double.nan;
zd.setdefaultcolor(color.cyan);
plot zw = if show_ema_lines then ExpAvgweek else double.nan;
zw.setdefaultcolor(color.yellow);

# study , output is line(s) , from numbers.
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MovAvgExponential

function , output is 1 number
can specify what type of average
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/MovingAverage

function
just exponential average
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/ExpAverage

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals
https://tlc.thinkorswim.com/center/...tants/AggregationPeriod/AggregationPeriod-DAY

• SleepyZ

new_spy123

New member
I hope this will help to get you started.
It draws 2 lines on a chart and draws purple shading between them when close is between them.
It does have an alert function, but i can't verify it works after hours.

I used a chart setting of 2Y 1D, and used these stocks for testing. their last bar is between the lines.
SBUX , EMR , PM

you can define the data to be generated from a different time frame than what is on the chart. this is called 2nd aggregation. you can have a chart of hour bars and do calculations on data from a larger time, a 2nd aggregation.
you can define only one 2nd agg time in a formula, so create a formula for each data that will use 2nd agg. then combine those variables in another formula.

this uses 2nd agg for both emas, by defining the 2nd agg time as a parameter of the price function, close.
this study can be used on a chart of 1 minute up to a day chart, assuming there is 150 weeks of data to be found.

Ruby:
#    close <  daily ema150 AND close >  weekly ema150

#----------------------------
input aggd = AggregationPeriod.DAY;
input lengthday = 150;
def ExpAvgday = ExpAverage( close( period = aggd), lengthday );

input aggw = AggregationPeriod.week;
input lengthweek = 150;
def ExpAvgweek = ExpAverage( close( period = aggw), lengthweek );

#----------------------------
# true when close is between both lines
def both_cond = (close < ExpAvgday) and (close > ExpAvgweek);

#----------------------------
# display a label when both conditions are true
addlabel(both_cond, "close < daily ema150  AND  close > weekly ema150", color.yellow);

# draw a cloud between the 2 emas when condition is true.
addcloud( (if both_cond then ExpAvgday else double.nan), ExpAvgweek, color.magenta, color.magenta);

input show_ema_lines = yes;
plot zd = if show_ema_lines then ExpAvgday else double.nan;
zd.setdefaultcolor(color.cyan);
plot zw = if show_ema_lines then ExpAvgweek else double.nan;
zw.setdefaultcolor(color.yellow);

# study , output is line(s) , from numbers.
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MovAvgExponential

function , output is 1 number
can specify what type of average
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/MovingAverage

function
just exponential average
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/ExpAverage

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals
https://tlc.thinkorswim.com/center/...tants/AggregationPeriod/AggregationPeriod-DAY

This is awesome. Learnt some new stuff from your code.

Only one question I have is on the Alert : Can I get this alert on my mobile? how is this alert different from the study alerts that show up in the Marketwatch?

• halcyonguy

MerryDay

Staff member
Staff
VIP
This is awesome. Learnt some new stuff from your code.

Only one question I have is on the Alert : Can I get this alert on my mobile? how is this alert different from the study alerts that show up in the Marketwatch? • 