Making a 13 over 30 Crossover Scan

leonrossi

New member
Hi All,
I'm new to thinkscript. I would like to create a scan to detect EMA 13 over 30 crossover on the hourly time frame in TOS. I would also like to incorporate the parameter of a stock in an uptrend or downtrend for about 2 weeks before they cross over. I have tried to do this in TOS but i do not get good results. The part that I struggle with is how to capture the stock either in an uptrend or downtrend for about 2 weeks. The idea behind this scan is to spot a momentum stock when it's reversing. Does anyone know how to do this? any help would be greatly appreciated.
 

BenTen

Administrative
Staff
VIP
You can use the ToS scanner and build one. Here is an example:

nZHwdmH.png
 

leonrossi

New member
hi Ben,
That's what I did but I'm not getting good results. The part that I need help with is how do I program to identify stocks that made a strong uptrend or downtrend for about 2 weeks and that's when the momentum is shifting. I wish I could send you an example but I can't upload the image.
 

leonrossi

New member
these are strong momentum stocks and the plan is to decipher when that momentum is changing and then short it (Put options for a few days) on the pull back and then do the opposite when the cross over happens again.
 

leonrossi

New member
yes, it is to find crossovers but only after the stock has been trending for a while (~about 2 weeks). For example with MTCH above and SPY below, the stock has been on a uptrend for about 2 wks before the cross over happens.
qfwAPr9.png


Thanks for fast response. I really appreciate your feedback.

Leon
 

leonrossi

New member
also, what i find interesting is that momentum stocks tends to find support at the 200 hr moving average (SMA). they tend to bounce off of it and then continue higher.
 

Pensar

Active member
VIP
@WOLVES Looks like you've been wanting something like this for a bit. Try this scan code; it should find 13/30 crossovers after two weeks of trend, please read the notes in the code.
Code:
# Hourly Scan for 13/30 Crossover After 2 Weeks
# Scans for crossovers in both directions
# Choose hourly aggregation - Do NOT include extended hours

# 70 is the number of hourly bars that total two weeks - adjust as desired
input bars_since_cross = 70;

# allows a bit of room to scan for the cross - 7 is within the current day
input max_bars_since_cross = 7;

def ma1 = average(close,13);
def ma2 = average(close,30);

def cross_up = ma1 crosses above ma2;
def cross_dn = ma1 crosses below ma2;

def count_after_cross_up = if cross_up and !cross_up[1] then 1
                           else count_after_cross_up[1]+1;
def count_after_cross_dn = if cross_dn and !cross_dn[1] then 1
                           else count_after_cross_dn[1]+1;

def long_entry = count_after_cross_up > bars_since_cross
                 and cross_dn within max_bars_since_cross bars;
def short_entry = count_after_cross_dn > bars_since_cross
                  and cross_up within max_bars_since_cross bars;

plot scan = long_entry or short_entry;

# end scan code
 

WOLVES

New member
@Pensar Thanks for responding. The scanner gives an error:
def long_entry = count_after_cross_up > bars_since_cross
and cross_dn within max_bars_since_cross bars;
def short_entry = count_after_cross_dn > bars_since_cross
and cross_up within max_bars_since_cross bars;

the complier says: no such variable bars_since_cross at 17:42

thanks
 

omar__naeem

New member
@Pensar - this is helpful thanks! I was looking for something similar but for Simple Moving Average. I can update the code to reflect this.
The part I am not clear about is the following:

How are you defining 70 as a two week period? Are weekends and pre/post market included in the 70 hour count? Or will this only include the last 10 TRADING DAYS over a 2 week period (excluding pre/post market).

***********************************
"# 70 is the number of hourly bars that total two weeks - adjust as desired
input bars_since_cross = 70;"
 

Pensar

Active member
VIP
@omar__naeem The 13/30 crossover scan code already uses the Simple Moving Average (written as "average"), no need to change it.

How are you defining 70 as a two week period? Are weekends and pre/post market included in the 70 hour count?

There are 7 hourly bars each day (when not including extended hours), so I multiplied that number by 10 (the number of trading days in two weeks) to get 70. You can always adjust the code to scan for a different number of bars. No weekends (the stock market is closed). To only scan regular trading hours, you must make certain to de-select the "EXT" box when creating your scan. Hope that helps! :)
 

Pensar

Active member
VIP
@Rayanmjr Try creating a scan with this code in ThinkorSwim, then populate the results to a watchlist column. It should update every 3-5 mimutes. I have no idea if custom scan/indicator codes can even be used in StockCharts.
 

omar__naeem

New member
Thanks @Pensar. Confirming if it is working correctly - I set the value of input bars_since_cross = 49. However, as you can see in the image below, the cross over is still happening prior to 49 bars...in this case it is 47 and 46 bars and I am seeing quite a few cases that are showing this. Do you know why this would be the case?

Ek4RKZY.png

7fcTTiH.png
 

Pensar

Active member
VIP
@omar__naeem The code finds crossovers within a certain amount of bars. So where it says . . .
Code:
# allows a bit of room to scan for the cross - 7 is within the current day
input max_bars_since_cross = 7;
. . . try changing it to 1 instead of 7, let me know if that fixes it.
 

omar__naeem

New member
Isn't max_bars_since_cross only for the right side of the cross-over? I.e. Bars since the 2 MAs crossed <= 7? I love that the code accommodates for this. Reducing it below 7 will impact the results but more to do with the right side of the cross-over vs the left which is what I am unable to solve for. Not sure if that makes sense?
 

Similar threads

Top