MACD Buy & Sell Signal

Panzer12

New member
Hey guys. I'm trying to learn ThinkScript, so I'm creating different systems to learn various tricks. I've been experimenting with making a strategy using indicators. In this case, it's with MACD.

The following code is a collection of different codes that I've taken from various forums.

My main goal is to code a strategy where a buy or sell signal is generated when the MACD changes color and confirms after two consecutive bars of the same color. Additionally, I want to include an input for a stop-loss percentage in the same system.

This is what I have so far. Now, I don't quite understand if it's correct or how to modify it so that the confirmation signal is generated when there are consecutive bars of the same color.

Hope I explained myself clearly

Thanks fot your help!!!

Code:
# macd_histo_colors_arrows_strat

#https://usethinkscript.com/threads/accessing-items-in-macd-diff.17075/
#Accessing items in MACD.Diff
#Peppermshrimp 11/2

#Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.

def na = Double.NaN;
def bn = BarNumber();

def ref1 = if bn == 1 then close else ref1[1];

# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;

# create # 's for diff colors , -2 to 2
# all color transistitions
#def Diff_color_num =
# if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;


# color changes, lowest and highest
def Diff_color_num =
if Diff > 0 and Diff < Diff[1] then -1
else if Diff < 0 and Diff > Diff[1] then 1
else 0;


# require x colors in a row to be considered a new color
input consecutive_bars = 2;
def qty = consecutive_bars;
def numsum = if Sum(Diff_color_num, qty) == (Diff_color_num * qty) then 1 else 0;

def new_color = if (numsum and GetValue(Diff_color_num, qty) != Diff_color_num) then 1 else 0;
#def new_color = if (numsum and getvalue(numsum, qty) and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;


# create a trade signal , from the 1st of consequetive signals
def trade = if bn == 1 then 0
else if Diff_color_num == -1 and new_color then -1
else if Diff_color_num == 1 and new_color then 1
else trade[1];

#//================================================================//#

input offsetType = {default percent, value, tick};
input stop = 0.75;

def entryPrice = entryPrice();
def mult;
switch (offsetType) {
case percent:
mult = entryPrice / 100;
case value:
mult = 1;
case tick:
mult = tickSize();
}
def stopPrice = entryPrice - stop * mult;


#//================================================================//#

def buy = (trade[0] == 1 and trade[1] == -1);
def sell = (trade[0] == -1 and trade[1] == 1);
input order_size = 1;
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], order_size, Color.RED, Color.RED, name = "SELL");

addOrder(OrderType.SELL_TO_CLOSE, low <= stopPrice, tickColor = GetColor(5), arrowColor = GetColor(5), name = "Stop");

It gives me the buy signal on the third (input) consecutive lower bar. But it is still the same color.

1731853029190.png




Edit:
Don´t understand the objective of this code.
Code:
# create # 's for diff colors , -2 to 2
# all color transistitions
#def Diff_color_num =
# if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;
 
Don´t understand the objective of this code.
Code:
# create # 's for diff colors , -2 to 2
# all color transistitions
#def Diff_color_num =
# if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;

The Diff variable is your MACD Histogram. This compares the current MACD Histogram value against 0 and the previous (left bar's) value. The logic is:

  • MACD H > 0 and > previous value => 2
  • MACD H < 0 and < previous value => -2
  • MACD H > 0 and < previous value => -1
  • MACD H < 0 and > previous value => 1
  • Fallback to 0.

If MACD H is going up, this gives us a positive number. Being above the zero line is stronger.

If MACD H is going down, this gives us a negative number. Being below the zero line is stronger.

Did you intend to use Value (MACD line)?
 
Last edited:

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