SAR MACD Scan

Carlo592

New member
VIP
Hi All,

I just joined recently, and have been browsing the indicator forums. I found one that I'd like to explore more. The code is in this post. I'd like to get a scanner for it made.
https://usethinkscript.com/threads/sar-macd-for-thinkorswim.15409/
Is this the right forum to ask this question in?
So I'm looking to be able to scan a watch list say S&P500 stocks for stocks where the red colored bar has turned yellow within a certain number of bars, which would be choose able. For time frames, I'm thinking, 1hr, 2hr,4hr,1D. I have drawn arrows on the chart picture below, so show what I'm referring to.
Screenshot 2024-10-30 203802.jpg



Thanks for taking a look at this!
 
Solution
Hi All,

I just joined recently, and have been browsing the indicator forums. I found one that I'd like to explore more. The code is in this post. I'd like to get a scanner for it made.
https://usethinkscript.com/threads/sar-macd-for-thinkorswim.15409/
Is this the right forum to ask this question in?
So I'm looking to be able to scan a watch list say S&P500 stocks for stocks where the red colored bar has turned yellow within a certain number of bars, which would be choose able. For time frames, I'm thinking, 1hr, 2hr,4hr,1D. I have drawn arrows on the chart picture below, so show what I'm referring to.



Thanks for taking a look at this!


here is what i did,

you want something based on the histogram colors, so determine...
Hi All,

I just joined recently, and have been browsing the indicator forums. I found one that I'd like to explore more. The code is in this post. I'd like to get a scanner for it made.
https://usethinkscript.com/threads/sar-macd-for-thinkorswim.15409/
Is this the right forum to ask this question in?
So I'm looking to be able to scan a watch list say S&P500 stocks for stocks where the red colored bar has turned yellow within a certain number of bars, which would be choose able. For time frames, I'm thinking, 1hr, 2hr,4hr,1D. I have drawn arrows on the chart picture below, so show what I'm referring to.



Thanks for taking a look at this!


here is what i did,

you want something based on the histogram colors, so determine what causes them.

this is the histogram and code to change its colors.

macd.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
macd.AssignValueColor(if macdSel then
if bc>0 then CreateColor(1, 185, 7) else
if bc<0 then CreateColor(224, 4, 4) else Color.YELLOW else
if hc>0 then CreateColor(1, 185, 7) else
if hc<0 then CreateColor(224, 4, 4) else Color.YELLOW);

so bc and hc determine the colors. i could stare at the code and figure out the logic, but the if-thens are a little starnge, and it's easier to just draw a bubble with values, to see what is going on.

addchartbubble(1, -0.5,
bc + " bc\n" +
hc + " hc"
, color.yellow, no);

you want a red to yellow transition, so,
when the hisogram is red, bc and hc are -1.
when yellow, they are both 0.
so we need to find when they are = 0 and on the previous bar they were -1.

i could add another bubble, but a verticalline also works. this draws a vertical line just before the first yellow bar, right where you want.

addverticalline(
(bc[1] == -1 and hc[1] == -1 and bc[0] == 0 and hc[0] == 0),
"-",
color.cyan);

the next thing to do is count the bars from that signal and compare the count to some number.
you want a scan study to have 1 plot and for it to be true for the desired situation.
plot z will be true on the signal and for the 5 bars after it.
for a scan or column, you will have to edit the code to change an input number.

def cnt = if (bc[1] == -1 and hc[1] == -1 and bc[0] == 0 and hc[0] == 0) then 1 else cnt[1] + 1;
# scan for signals less than x bars ago
input max_cnt = 6;
plot z = cnt <= max_cnt;

there should be a pulsed line plot on the histogram, a line that goes to 1 on the first yellow bar and stays at 1 for the next 5 bars.
once we get to this point and verify the plot formula, we can remove all the code after it. scans need just 1 plot, no labels, no other plots, no bubbles, no clouds.



watchlist study/scan
sarmacd
http://tos.mx/!HJFiGHME
5 minute time


Code:
# sarmacd
# SAR_MACD_Scan_scan_01

#https://usethinkscript.com/threads/sar-macd-scan.19909/
#SAR MACD Scan

# https://usethinkscript.com/threads/sar-macd-for-thinkorswim.15409/
# to scan a watch list for stocks where the red colored bar has turned yellow within a certain number of bars

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

#https://usethinkscript.com/threads/sar-macd-for-thinkorswim.15409/
#Indicators Custom
#SAR MACD for ThinkOrSwim
#samer800  May 6, 2023

#Buy Condition:
#MA Color - Blue
#Histogram- Above Zero
#Histogram/Candle -Green
#MA Crossover is must
#Sell Condition:
#MA Color - Red
#Histogram- Below Zero
#Histogram/Candle -Red
#MA Cross under is must

#Warning: Must not be used as a standalone indicator. Use for confirmation of your Buy Sell Signals and Entry only.

#// https://www.tradingview.com/v/uXB6f9Y5/
#// © traderharikrishna
#indicator("SARMACD")
# Converted and mod by Sam4Cok@Samer800    - 05/2023

declare lower;

def na = double.nan;
def bn = barnumber();
input ColorBars = yes;
input ShowSignals = no;
input sideways = 5;#,'sideways')
input GraphType = {default MACD, HIST};    # 'Graph Type'
input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;

def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def last = isNaN(close);
def macdSel = GraphType==GraphType.MACD;
def HistSel = GraphType==GraphType.HIST;

def sar = ParabolicSAR();
def cross = crosses(close,sar);
def sa = if cross then close else sa[1];
def dir = if sar < close then 1 else -1;

def m = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def s = MovingAverage(averageType, m, MACDLength);
def h = m - s;

def bc = if dir ==  1 and close > sa and m>0 then  1 else
         if dir == -1 and close < sa and m<0 then -1 else 0;
def hc = if dir ==  1 and close > sa and h>0 then  1 else
         if dir == -1 and close < sa and h<0 then -1 else 0;


def cnt = if (bc[1] == -1 and hc[1] == -1 and bc[0] == 0 and hc[0] == 0) then 1 else cnt[1] + 1;

# scan for signals less than x bars ago
input max_cnt = 6;

plot z = if bn == 1 then 0 else cnt <= max_cnt;

addchartbubble(0, -0.5,
bc + "  bc\n" +
hc + "  hc"
, color.yellow, no);


addverticalline(0 and
 (bc[1] == -1 and hc[1] == -1 and bc[0] == 0 and hc[0] == 0),
 "-",
 color.cyan);
#
 

Attachments

  • img1.JPG
    img1.JPG
    57.6 KB · Views: 40
  • img2.JPG
    img2.JPG
    39.9 KB · Views: 41
Solution
I've has a chance to try this out, looks like it works pretty good, it also works as a scanner too. I think I can make any small tweaks to it from here. Thanks again for scripting this!
 

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