MACD Format, Triggers, Scan, Label, Watchlist For ThinkOrSwim

@FOTM_8888 Here is the modified version of the default MACD backtesting strategy in ToS. I added the SMA above 200 as a condition as the video instructed.

This is for long trades only.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2020
#

input price = close;
input length = 200;
input displace = 0;
def SMA = Average(price[-displace], length);
def cond = close > SMA;

input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;

def diff = reference MACD(fastLength, slowLength, macdLength, averageType).Diff;

AddOrder(OrderType.BUY_TO_OPEN, diff crosses above 0 and cond, tickColor = GetColor(0), arrowColor = GetColor(0), name = "MACDStratLE");
AddOrder(OrderType.SELL_TO_CLOSE, diff crosses below 0, tickColor = GetColor(1), arrowColor = GetColor(1), name = "MACDStratSE");
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Can anyone create a scan where the slope of MACD is neutral? Scan stocks where the slope is neither rising nor falling.
 
Last edited:
@Miket I don't think I've ever seen MACD flat. But you try doing a scan where MACD()."Value" is equal to MACD()."Value" from 1 bars ago.
 
I am trying to make a STUDY that will ring a bell when the RSI is below 30 and the MACD is above the zero line. Can anyone help me fix my code so it will RING. Its not ringing. A bonus would be if It could somehow plot it also but not necessary.

Here is my code...

Code:
def Bearish = RSI("length" = 14, "over bought" = 70, "over sold" = 30)."RSI" is greater than or equal to 70 and MACD()."Value" is less than 0;
Alert(Bearish, " RSI Possible SIGNAL DOWN ", Alert.BAR, Sound.Ding);

def Bullish =
RSI("length" = 14, "over bought" = 70, "over sold" = 30)."RSI" is less than or equal to 30 and MACD
()."Value" is greater than 0;
Alert(Bullish, " RSI Possible SIGNAL DOWN ", Alert.BAR, Sound.Ding);
 
Last edited by a moderator:
Hi @BenTen

For some reason above doesn't show any indicators on my charts.

Meanwhile, I have a request. Can someone provide the code with following signals?
  • StrongBull: MACD crosses above and SignalLine and crossover is bellow zero line
  • WeakBull: MACD crosses above and SignalLine and crossover is above zero line
  • StrongBear: MACD crosses bellow SignalLine and crossover is above zero line
  • WeakBear: MACD crosses bellow SignalLine and crossover is bellow zero line

Ci8aWsm.png
 
Last edited by a moderator:
Right, I was using it for backtesting. I think I understood the issue, for the interval I have selected MACD crossover happened bellow 200EMA hence it was not showing. Thanks!
 
Hi,

I programmed a MACD scan to identify when the value line is greater than it was in the previous bar. I can also do this with the pre-built scanners in thinkorswim. However, neither seem to work. Is there some sort of limitation in why this wouldn't work?

I know there are stocks that meet the criteria because I can pull up examples manually. Just wondering why the scans wouldn't produce results, and see if it's an error on my end that I can remedy?

The scanner uses code like this:

Code:
MACD()."Value" is greater than MACD()."Value" from -1 bars ago within 2 bars

When I custom programmed it I used this:

Code:
input price = close;
input length = 14;
input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, macdLength);
def diff = reference MACD(fastLength, slowLength, macdLength, averageType).Diff;

def condition =  value > value[-1];

plot scan = condition;
 
i assume this is what are trying to do
Code:
MACD()."Value" is greater than MACD()."Value" from 1 bars ago
 
This code works... Whether the "within 2 bars" is required or not is subjective...

Ruby:
MACD()."Value" is greater than MACD()."Value"[1] within 2 bars
 
Thanks for the code. Any way to color the hourly candles using a daily MACD color trend? That is to say if you can add a MACD timeframe to color the candles with to the code? Thank you!
 
@BenTen can you help to code an scanner and alert based on fast ATR (1.5 and 5 parameter values) and macd retrace (Below Code)

Code:
input fast = 12;
input slow = 26;
input length = 9;
input averageType = AverageType.EXPONENTIAL;

def M0 = MACD(fast, slow, length, averageType).Avg;
def M1 = MACD(fast, slow, length, averageType).Avg[1];
def M2 = MACD(fast, slow, length, averageType).Avg[2];
def M3 = MACD(fast, slow, length, averageType).Avg[3];
def M4 = MACD(fast, slow, length, averageType).Avg[4];
plot signalUp = M4>M3 and M3>M2 and M2>M1 and M0>M1;
plot signalDown = M4<M3 and M3<M2 and M2<M1 and M0<M1;
signalUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
signalDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

V1HTJkd.jpg


Alert has to trigger only if the macd has crossed over and has gone is below (For short) or above (For long) the ATR line
 
Hello guys

Id like to add lines to hear ring always when MACD shows breakout

original's TOS formula for MACD:
Code:
#

# TD Ameritrade IP Company, Inc. (c) 2007-2021

#



declare lower;



input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

input showBreakoutSignals = no;



plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);

plot Avg = MovingAverage(averageType, Value, MACDLength);



plot Diff = Value - Avg;

plot ZeroLine = 0;



plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;

plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;



UpSignal.SetHiding(!showBreakoutSignals);

DownSignal.SetHiding(!showBreakoutSignals);



Value.SetDefaultColor(GetColor(1));

Avg.SetDefaultColor(GetColor(8));

Diff.SetDefaultColor(GetColor(5));

Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

Diff.SetLineWeight(3);

Diff.DefineColor("Positive and Up", Color.GREEN);

Diff.DefineColor("Positive and Down", Color.DARK_GREEN);

Diff.DefineColor("Negative and Down", Color.RED);

Diff.DefineColor("Negative and Up", Color.DARK_RED);

Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));

ZeroLine.SetDefaultColor(GetColor(0));

UpSignal.SetDefaultColor(Color.UPTICK);

UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownSignal.SetDefaultColor(Color.DOWNTICK);

DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


I've tried with some lines but it doesnt work.

Could you help ?
Best regards!
 
Hello, I'm looking for a scanner query to make sure all stocks are currently on the down trend(red). Sorry if this is a noob question.
 
@Kingvt Are you simply looking for downtrend or specifically for downtrend related to the MACD...??? Using a Moving Average or simple comparison can determine downtrend... I use Moving Average... I'm sure we have several Trend indicators here in the forums... Mine triggers off my Moving Average Stack...

Give us more details and we'll go from there...

Until then something as simple as comparing close to close[1] or whatever barcount you want would suffice for scanning...
 
I need one specifically for the MACD since I just need it to rule out some of the MACD scans I already have
 
@Kingvt How about something like the following... It can be tweaked to your needs... The code provided looks back two bars for downtrend...

Ruby:
MACD()."Value" is less than MACD()."Value"[1] and MACD()."Value"[1] is less than MACD()."Value"[2]
 
@raymasa Have you searched the forums...??? There are more than a few variations of MACD indicators here in the forums... The same basic layout can be created by combining several standard TOS indicators... We don't generally just do conversions based on one members request, and especially not for a member with only three posts... There needs to be some greater interest... I quickly pieced something quite similar together by stacking several existing indicators in one lower section... What you see below took me less than 2 minutes to accomplish... Very little extra work would be required to make it exactly the same...

Edited to add: It was actually simpler than the original version... Only 2 indicators instead of the original 4...

oby17a7.png


hxwnv8v.png
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
268 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