• LIMITED TIME ONLY: Take $40 off VIP with code: BTD40.

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
Lifetime
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);

Alert(both_cond, "between levels", alert.once, Sound.ding);
#


JWSo9qQ.jpg



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

alert info
https://readtheprospectus.wordpress...ng-alerts-using-thinkscript-in-think-or-swim/
 
Solution

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);

Alert(both_cond, "between levels", alert.once, Sound.ding);
#


JWSo9qQ.jpg



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

alert info
https://readtheprospectus.wordpress...ng-alerts-using-thinkscript-in-think-or-swim/
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?
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
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?
Commonly Asked Questions about Alerts:
There are Three Types of Alerts:
There are alerts written into studies. They only alert when the chart w/ the study is open on your screen
They cannot be sent to phone/email. They cannot have custom sounds.
There are alerts created on a chart for one specific stock that you want to alert on. The alert will only fire once. At that point, the condition has been met, the alert expires. No, this can't be changed.
There are scanned watchlist alerts which alert whenever the results of the scan changes. These alerts can be 3-7min delayed.
Alerts blinking by too fast? To see all alerts: click on the message center
Want to see a list of alerts all the time? detach the message center
TeXjiwC.png
 

Similar threads

Top