NexusFi: Find Your Edge


Home Menu

 





Scan for stocks above or below simple moving average


Discussion in ThinkOrSwim

Updated
      Top Posters
    1. looks_one apollo23 with 6 posts (1 thanks)
    2. looks_two trepidation with 5 posts (9 thanks)
    3. looks_3 teo2020 with 2 posts (0 thanks)
    4. looks_4 appplejack003 with 1 posts (0 thanks)
    1. trending_up 17,634 views
    2. thumb_up 10 thanks given
    3. group 3 followers
    1. forum 12 posts
    2. attach_file 18 attachments




 
Search this Thread

Scan for stocks above or below simple moving average

  #1 (permalink)
apollo23
Singapore
 
Posts: 12 since Feb 2020
Thanks Given: 4
Thanks Received: 2

Hi

Am new here.

Was told TOS script is very powerful and hope to build a script to scan for stocks that is above or below 20 day simple moving. For bullish scan, the previous day close has to be below the 20 day simple moving average. Vice versa for a bearing scan. Attached is a picture for better understanding.



Can somebody teach me how to build? If such script is too difficult, am willing to pay a small fee for your time.


Thanks in advance.

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
11 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #2 (permalink)
 trepidation 
San Jose, California
 
Experience: Intermediate
Platform: Sierra Chart
Posts: 139 since Apr 2018
Thanks Given: 25
Thanks Received: 167

Thinkscript is actually very easy to learn compared to some languages.

NOTE: The code here is Bullish = close > SMA. If you want to change that, simply reverse the > sign

You can use the built-in MovingAverage_Scan that would allow you to use the interface to customize what % you want to be above the SMA.





Here is the code to create a custom scan:
 
Code
plot signal = if close > SimpleMovingAvg(length=20) then 1 else 0;
You can also create a custom watchlist indicator. In the code below, red means below the 20SMA and green means above the 20SMA

 
Code
plot signal = if close > SimpleMovingAvg(length=20) then 1 else 0;

signal.AssignValueColor(if signal == 1 then Color.Green else Color.Red);
AssignBackgroundCOlor(if signal == 1 then Color.Green else Color.Red);

Reply With Quote
Thanked by:
  #3 (permalink)
apollo23
Singapore
 
Posts: 12 since Feb 2020
Thanks Given: 4
Thanks Received: 2


Hi trepidation

What i want is that the body of the price (open and close) is within the moving average.

How to define it?

Thanks in advance

Reply With Quote
  #4 (permalink)
 trepidation 
San Jose, California
 
Experience: Intermediate
Platform: Sierra Chart
Posts: 139 since Apr 2018
Thanks Given: 25
Thanks Received: 167

Hi, here's your full suite for the indicator.

Scans
Bullish Scan:
 
Code
input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);

plot signal = close > sma && open < sma;
Bearish Scan:
 
Code
input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);

plot signal = open > sma && close < sma;
Watchlist Indicator
 
Code
input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);
def bearish = open > sma && close < sma;
def bullish = close > sma && open < sma;

plot signal = if bullish then 1 else if bearish then 0 else -1;

signal.AssignValueColor(if signal == 1 then Color.Green else if signal == 0 then Color.Red else Color.Black);
AssignBackgroundCOlor(if signal == 1 then Color.Green else if signal == 0 then Color.Red else Color.Black);


SMA + Chart arrows
 
Code
input maLength = 20;
def sma = SimpleMovingAvg(length=maLength);
def bearish = open > sma && close < sma;
def bullish = close > sma && open < sma;


plot ma = sma;
plot bullSignal = if bullish then low - (0.5 + tickSize())else DOuble.NaN;
plot bearSignal = if bearish then high + (0.5 + tickSize()) else Double.NaN;

bullSignal.SetDefaultColor(Color.UPTICK);
bullSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bearSignal.SetDefaultColor(Color.DOWNTICK);
bearSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

Reply With Quote
  #5 (permalink)
apollo23
Singapore
 
Posts: 12 since Feb 2020
Thanks Given: 4
Thanks Received: 2

Hi trepidation

Thanks for your time and help but I can't achieve what you have shown.

Using your Watchlist Indicator:

input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);
def bearish = open > sma && close < sma;
def bullish = close > sma && open < sma;

plot signal = if bullish then 1 else if bearish then 0 else -1;

signal.AssignValueColor(if signal == 1 then Color.Green else if signal == 0 then Color.Red else Color.Black);
AssignBackgroundCOlor(if signal == 1 then Color.Green else if signal == 0 then Color.Red else Color.Black);

Below is my scan result:



OMF is not touching the SMA 20:



XPO also not touching the sma 20:



Its a bullish scan, GFF is below sma 20:



Finally when i use the last code you share, i can't save the script.



Did i do anything wrong? Really appreciate your time and help.

Reply With Quote
  #6 (permalink)
 trepidation 
San Jose, California
 
Experience: Intermediate
Platform: Sierra Chart
Posts: 139 since Apr 2018
Thanks Given: 25
Thanks Received: 167

Are you using the right script in the right location? The watchlist and scan only allow for one plot. It looks like you used the watchlist scanner for the scan, the chart study for the watchlist, and didn't use the scan code. The way I set up scan is you can only search for ONLY bearish or ONLY bullish trades.



Here are examples of of each code in action:

BULLISH






[B] BEARISH [/B}






Reply With Quote
Thanked by:
  #7 (permalink)
 trepidation 
San Jose, California
 
Experience: Intermediate
Platform: Sierra Chart
Posts: 139 since Apr 2018
Thanks Given: 25
Thanks Received: 167

Did you figure it out?


apollo23 View Post
Hi trepidation

Thanks for your time and help but I can't achieve what you have shown.


Reply With Quote
  #8 (permalink)
apollo23
Singapore
 
Posts: 12 since Feb 2020
Thanks Given: 4
Thanks Received: 2


trepidation View Post
Did you figure it out?

Hi trepidation

Sad to say, still not 100% to what you have shown. If i use the 1st code:

input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);

plot signal = open > sma && close < sma;

The price bar will be above the SMA.

However if i use the last code:

input maLength = 20;
def sma = SimpleMovingAvg(length=maLength);
def bearish = open > sma && close < sma;
def bullish = close > sma && open < sma;


plot ma = sma;
plot bullSignal = if bullish then low - (0.5 + tickSize())else DOuble.NaN;
plot bearSignal = if bearish then high + (0.5 + tickSize()) else Double.NaN;

bullSignal.SetDefaultColor(Color.UPTICK);
bullSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bearSignal.SetDefaultColor(Color.DOWNTICK);
bearSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);



I can't save the code. It showed "Exactly one plot expected" at the bottom of page. Why is this?

Thanks again.

Reply With Quote
  #9 (permalink)
 trepidation 
San Jose, California
 
Experience: Intermediate
Platform: Sierra Chart
Posts: 139 since Apr 2018
Thanks Given: 25
Thanks Received: 167


apollo23 View Post
Hi trepidation

Sad to say, still not 100% to what you have shown. If i use the 1st code:

I realize I was too ambitious and tried to cover too much ground. Here is your code for bullish scans.

 
Code
input ma_length = 20;
def sma = SimpleMovingAvg(length=ma_length);

plot signal = close > sma && open < sma;

Reply With Quote
Thanked by:
  #10 (permalink)
apollo23
Singapore
 
Posts: 12 since Feb 2020
Thanks Given: 4
Thanks Received: 2


Hi trepidation

Hope you are well and making tons of money.

The stock market crashed shortly after and didnt have much opportunity to test further the script.

Can you add another condition for me? I want the scan to show stocks that is making a new high as compared to the last price bar. That is to say, i want stocks that are above their 20 sma and when scan today, current price bar has made a higher high as compared to previous day.

Thanks in advance.

Reply With Quote




Last Updated on March 9, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts