5% Gain from when MACD Diff Goes Above Zero

Tomahawk6117

Member
Plus
Hi everyone,

I would please like to create a script that will execute when the price increases 5% from when the MACD Diff goes above zero for the first time during regular trading hours. In the coding section below I have inserted the code for when MACD Diff goes above zero, I am just not sure how to specify when it occurs for the first time after the market open (9:30 a.m.) and how to identify the price for the open of this specific candle.

Code:
def d = MACDHistogram().diff;
def goodMACD = d > 0;

An example of what I am looking to achieve would be, if the price was $1.00 when the MACD Diff went above zero then I would want to sell when the stock price was $1.05. Would someone be able to help me with this please? I have included an image here to describe what I am looking to achieve. Thank you very much for your time and help.

nKUz2lQ.jpeg
 
Last edited:
Solution
Hi everyone,

I would please like to create a script that will execute when the price increases 5% from when the MACD Diff goes above zero for the first time during regular trading hours. In the coding section below I have inserted the code for when MACD Diff goes above zero, I am just not sure how to specify when it occurs for the first time after the market open (9:30 a.m.) and how to identify the price for the open of this specific candle.

Code:
def d = MACDHistogram().diff;
def goodMACD = d > 0;

An example of what I am looking to achieve would be, if the price was $1.00 when the MACD Diff went above zero then I would want to sell when the stock price was $1.05. Would someone be able to help me with this please? I have included...
Hi everyone,

I would please like to create a script that will execute when the price increases 5% from when the MACD Diff goes above zero for the first time during regular trading hours. In the coding section below I have inserted the code for when MACD Diff goes above zero, I am just not sure how to specify when it occurs for the first time after the market open (9:30 a.m.) and how to identify the price for the open of this specific candle.

Code:
def d = MACDHistogram().diff;
def goodMACD = d > 0;

An example of what I am looking to achieve would be, if the price was $1.00 when the MACD Diff went above zero then I would want to sell when the stock price was $1.05. Would someone be able to help me with this please? I have included an image here to describe what I am looking to achieve. Thank you very much for your time and help.

here is something to try
i changed the percent to be 0.5% , because for most stocks, a 5% move will not be attained
draws 2 horizontal lines, at buy level and at a sell level, at x% higher.
draw a white dot at target price


Code:
#macd_cross_buy

#https://usethinkscript.com/threads/5-gain-from-when-macd-diff-goes-above-zero.18872/
#5% Gain from when MACD Diff Goes Above Zero

def na = double.nan;
def bn = barnumber();

def NewDay = GetDay() != GetDay()[1];
def start = 930;
def end = 1600;
def daytime = if ( SecondsFromTime(start) >= 0 and Secondstilltime(end) > 0 ) then 1 else 0;

# macd-------------------
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 = Value - Avg;
#-----------------------

def goodMACD = diff crosses above 0;

def open1 = if bn == 1 then 0
else if newday then 0
else if open1[1] > 0 then open1[1]
else if daytime and goodMACD then open
else open1[1];

input percent_gain = .50;

def target1 = if bn == 1 then 0
else if newday then 0
else if open1 > 0 then (open1 * (1 + (percent_gain /100)))
else target1[1];

plot zopen = if open1 > 0 then open1 else na;
zopen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot ztarget1 = if target1 > 0 then target1 else na;
ztarget1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def sell = if target1 > 0 and close crosses above target1 then 1 else 0;
#def sell = if target1 > 0 and high > target1 and low < target1 then 1 else 0;

Alert(sell, "sell",  Alert.BAR, sound.ding);

plot zselldot = if sell then target1 else na;
zselldot.setPaintingStrategy(PaintingStrategy.POINTS);
#zselldot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zselldot.SetDefaultColor(Color.white);
zselldot.setlineweight(3);
zselldot.hidebubble();
#
 

Attachments

  • img1.JPG
    img1.JPG
    49 KB · Views: 111
Solution

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

here is something to try
i changed the percent to be 0.5% , because for most stocks, a 5% move will not be attained
draws 2 horizontal lines, at buy level and at a sell level, at x% higher.
draw a white dot at target price


Code:
#macd_cross_buy

#https://usethinkscript.com/threads/5-gain-from-when-macd-diff-goes-above-zero.18872/
#5% Gain from when MACD Diff Goes Above Zero

def na = double.nan;
def bn = barnumber();

def NewDay = GetDay() != GetDay()[1];
def start = 930;
def end = 1600;
def daytime = if ( SecondsFromTime(start) >= 0 and Secondstilltime(end) > 0 ) then 1 else 0;

# macd-------------------
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 = Value - Avg;
#-----------------------

def goodMACD = diff crosses above 0;

def open1 = if bn == 1 then 0
else if newday then 0
else if open1[1] > 0 then open1[1]
else if daytime and goodMACD then open
else open1[1];

input percent_gain = .50;

def target1 = if bn == 1 then 0
else if newday then 0
else if open1 > 0 then (open1 * (1 + (percent_gain /100)))
else target1[1];

plot zopen = if open1 > 0 then open1 else na;
zopen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot ztarget1 = if target1 > 0 then target1 else na;
ztarget1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def sell = if target1 > 0 and close crosses above target1 then 1 else 0;
#def sell = if target1 > 0 and high > target1 and low < target1 then 1 else 0;

Alert(sell, "sell",  Alert.BAR, sound.ding);

plot zselldot = if sell then target1 else na;
zselldot.setPaintingStrategy(PaintingStrategy.POINTS);
#zselldot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zselldot.SetDefaultColor(Color.white);
zselldot.setlineweight(3);
zselldot.hidebubble();
#
Hi @halcyonguy this is awesome, thank you so much for taking the time to work on this, I really appreciate it.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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