Script Trigger as Soon as MACD Diff Crosses to Negative

Tomahawk6117

Member
Plus
Hi everyone,

The script below triggers when the stock price goes below the market open (9:30 a.m.) price for the current day and when the MACD Diff is negative. The script below triggers after the candle closes. Is there a way to adjust the script so that the script triggers as soon as the MACD Diff crosses below zero instead of waiting until after the candle closes? Thank you very much for your time and your help.

Code:
input sell_percent_gain = 0.0;
input price1 = close;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(price1 - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);
def d = MACDHistogram().diff;

input trigger_on_all_prices_below_stop = yes;
def sell1 = if (daystart or trigger_on_all_prices_below_stop) and pergain <= sell_percent_gain and d < 0 then 1 else if d < 0 and pergain crosses below sell_percent_gain then 1 else 0;

plot z1 = sell1;

huKNuLL.jpeg


zWC2azd.jpeg
 
Last edited by a moderator:
the problem with the logic you have is that it shows after the close because you dont know if the bar will close green or red. it can BRIEFLY be red only to end up closing green. if you have it as soon as it crosses but yet it ends up closing green, you are going to end up with many "unwanted alerts".
 
the problem with the logic you have is that it shows after the close because you dont know if the bar will close green or red. it can BRIEFLY be red only to end up closing green. if you have it as soon as it crosses but yet it ends up closing green, you are going to end up with many "unwanted alerts".
Thank you very much for your response @XeoNoX. So there is no way to formulate the script so that it triggers if the MACD Diff crosses below zero before the candle closes? I am not interested in how the candle closes, I am interested in when the MACD Diff crosses below zero, even if it does not stay there until the candle closes. Thank you again for all of your time and your help.
 
@Tomahawk6117 Just remember that until a bar closes, close == last so if you are parsing the current candle you will end up with false positives... This is why @XeoNoX hinted to wait for candle close which will yield an instantaneous yet more accurate result... If we don't wait for close then the price may drop below zero at the open of the candle, or anytime before close, and then switch directions and that will case a false positive and potential reversal/loss...
 
@Tomahawk6117 Just remember that until a bar closes, close == last so if you are parsing the current candle you will end up with false positives... This is why @XeoNoX hinted to wait for candle close which will yield an instantaneous yet more accurate result... If we don't wait for close then the price may drop below zero at the open of the candle, or anytime before close, and then switch directions and that will case a false positive and potential reversal/loss...
Thank you very much @rad14733, I really appreciate it. I am primarily using the script for the purpose of a conditional order where I would like the script to trigger as soon as the MACD Diff crosses below zero even if it does not result in being below zero at the end of the candle. I completely understand what you are saying though, and I really appreciate all of your time and your insights.
 

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