Vertical Cloud

bp805

Member
I am trying to add a vertical cloud from the day after earnings through the 7th day after earnings that is colored either green or red depending on either a positive or negative return. I have the following code below, which sort of works. It plots either a green or red cloud over the first bar after earnings which correctly corresponds to the either a positive or negative move, however, as you can see the rest of the cloud plots a yellowish color for both a green and red cloud. Im using a calculation (EGap, shown below) to determine either a positive or negative return (which I know is working correctly from another script). Does anyone know why the cloud is not plotting either green or red the whole way through? Thanks!

EGap:
def EGap = fold y = 0 to 1
while DayAfterBN do
if (bn - DayAfterBN) <= 1 then close[-6] - open
else Double.NaN;

Cloud:
AddCloud(if Highest(HasEarnings(), 7) AND EGap >= 0 then Double.POSITIVE_INFINITY else Double.NaN, if Highest(HasEarnings(), 7) AND EGap >= 0 then Double.NEGATIVE_INFINITY else Double.NaN, Color.GREEN);

AddCloud(if Highest(HasEarnings(), 7) AND EGap <= 0 then Double.POSITIVE_INFINITY else Double.NaN, if Highest(HasEarnings(), 7) AND EGap <= 0 then Double.NEGATIVE_INFINITY else Double.NaN, Color.Red);

3qqqBlT.jpg
 
At first glance, this looks to me like fold y = 0 to 1 is specifying the loop should have a single iteration. That may explain why only the first bar colors correctly.

If changing that doesn't fix it, the code you posted is missing the definitions for bn and DayAfterBN. Can you add those definitions?
 
At first glance, this looks to me like fold y = 0 to 1 is specifying the loop should have a single iteration. That may explain why only the first bar colors correctly.

If changing that doesn't fix it, the code you posted is missing the definitions for bn and DayAfterBN. Can you add those definitions?
Here are the rest of the definitions from the script that I am using. I'm using the "Earnings statistics" script that I found on another post on this site and just modified it a bit.

Code:
def bn = BarNumber();
def Month = GetMonth();
def Day = GetDayOfMonth(GetYYYYMMDD());
def isBefore = HasEarnings(EarningTime.BEFORE_MARKET);
def isAfter  = HasEarnings(EarningTime.AFTER_MARKET);
def isToday  = isBefore or isAfter or (HasEarnings() and !isBefore and !isAfter);
def EarningsBN  = if HasEarnings(EarningTime.BEFORE_MARKET) then bn[1]
else if HasEarnings(EarningTime.AFTER_MARKET) then bn
else EarningsBN[1];
def DayAfterBN = if bn - EarningsBN == 1 then bn else Double.NaN;
 
Here are the rest of the definitions from the script that I am using. I'm using the "Earnings statistics" script that I found on another post on this site and just modified it a bit.

I decided to write it from scratch with a different approach. I'm assuming you want the entire cloud colored based on the end result and not switching colors each day price crosses over/under the pre-earnings price. I commented out plots I used while debugging. Those can be deleted if you don't think you'll use them.

ctive-Trader-490509867-Main-thinkorswim-build-1969.png

Ruby:
declare upper;
declare hide_on_intraday;

# TWTR Nov 2020 is an example of where earnings time is unspecificed
# Calling HasEarnings() without any arguments catches those

def beforeEarnings =
  if HasEarnings(EarningTime.BEFORE_MARKET) then close[1]
  else if HasEarnings(EarningTime.AFTER_MARKET) then close
  else if HasEarnings() then close
  else beforeEarnings[1]
;

def bn = BarNumber();
def EarningsBN = if HasEarnings(EarningTime.BEFORE_MARKET) then bn[1]
  else if HasEarnings(EarningTime.AFTER_MARKET) then bn
  else if HasEarnings() then bn
  else EarningsBN[1]
;

def daysSince = bn - EarningsBN;
def inCloudRange = daysSince <= 7 and !isNaN(close);
def afterEarnings = if inCloudRange then close else afterEarnings[1];

# write this the long way to get around ThinkScript requiring constants for indexes
def endedHigher =
  if !isNaN(afterEarnings[-7]) then afterEarnings[-7] > beforeEarnings
  else if !isNaN(afterEarnings[-6]) then afterEarnings[-6] > beforeEarnings
  else if !isNaN(afterEarnings[-5]) then afterEarnings[-5] > beforeEarnings
  else if !isNaN(afterEarnings[-4]) then afterEarnings[-4] > beforeEarnings
  else if !isNaN(afterEarnings[-3]) then afterEarnings[-3] > beforeEarnings
  else if !isNaN(afterEarnings[-2]) then afterEarnings[-2] > beforeEarnings
  else if !isNaN(afterEarnings[-1]) then afterEarnings[-1] > beforeEarnings
  else Double.NaN
;

AddCloud(
  if inCloudRange and endedHigher then Double.POSITIVE_INFINITY else Double.NaN,
  if inCloudRange and endedHigher then Double.NEGATIVE_INFINITY else Double.NaN,
  Color.GREEN
);

AddCloud(
  if inCloudRange and !endedHigher then Double.POSITIVE_INFINITY else Double.NaN,
  if inCloudRange and !endedHigher then Double.NEGATIVE_INFINITY else Double.NaN,
  Color.RED
);
 
Last edited:
Also, if you want the clouds to be less bold you can use darker colors. If DARK_GREEN and DARK_RED are not bold enough, you can use CreateColor() to find something in between.
 
Correct me if I am wrong, but does this color code based upon the close of the day prior to announcement?
Any way to also code 10 or 14 or 21 days prior to close before announcement based upon a run up or run down prior to earnings?
Green if up ten day prior, red if down?
 
Correct me if I am wrong, but does this color code based upon the close of the day prior to announcement?
Any way to also code 10 or 14 or 21 days prior to close before announcement based upon a run up or run down prior to earnings?
Green if up ten day prior, red if down?

Here is the Pre-Earnings Range Cloud, colored by green/red as an increasing/decreasing range. Change the input range length to indicate how far back you want to look.

Capture.jpg

Ruby:
declare upper;
declare hide_on_intraday;

# TWTR Nov 2020 is an example of where earnings time is unspecificed
# Calling HasEarnings() without any arguments catches those

def beforeEarnings =
  if HasEarnings(EarningTime.BEFORE_MARKET) then close[1]
  else if HasEarnings(EarningTime.AFTER_MARKET) then close
  else if HasEarnings() then close
  else beforeEarnings[1]
;

def bn = BarNumber();
def EarningsBN = if HasEarnings(EarningTime.BEFORE_MARKET) then bn[1]
  else if HasEarnings(EarningTime.AFTER_MARKET) then bn
  else if HasEarnings() then bn
  else EarningsBN[1]
;

def daysSince = bn - EarningsBN;
def inCloudRange = daysSince <= 7 and !IsNaN(close);
def afterEarnings = if inCloudRange then close else afterEarnings[1];

# write this the long way to get around ThinkScript requiring constants for indexes
def endedHigher =
  if !IsNaN(afterEarnings[-7]) then afterEarnings[-7] > beforeEarnings
  else if !IsNaN(afterEarnings[-6]) then afterEarnings[-6] > beforeEarnings
  else if !IsNaN(afterEarnings[-5]) then afterEarnings[-5] > beforeEarnings
  else if !IsNaN(afterEarnings[-4]) then afterEarnings[-4] > beforeEarnings
  else if !IsNaN(afterEarnings[-3]) then afterEarnings[-3] > beforeEarnings
  else if !IsNaN(afterEarnings[-2]) then afterEarnings[-2] > beforeEarnings
  else if !IsNaN(afterEarnings[-1]) then afterEarnings[-1] > beforeEarnings
  else Double.NaN
;

AddCloud(
  if inCloudRange and endedHigher then Double.POSITIVE_INFINITY else Double.NaN,
  if inCloudRange and endedHigher then Double.NEGATIVE_INFINITY else Double.NaN,
  Color.GREEN
);

AddCloud(
  if inCloudRange and !endedHigher then Double.POSITIVE_INFINITY else Double.NaN,
  if inCloudRange and !endedHigher then Double.NEGATIVE_INFINITY else Double.NaN,
  Color.RED
);

#Pre-Earnings Range Clouds
input range = 20;
def cond       = if HasEarnings() then 1 else cond[1] + 1;
def condrange  = if Between(cond, 1, range + 1) then condrange[1] + 1 else 0;
def condition  = if condrange[-(range + 1)] then 1 else 0;

def cbegin = if condition and condrange[-(range + 1)] == 1 then close[-range+1] else cbegin[1];
def rbegin = if condition and condrange[-(range + 1)] == 1 then close else rbegin[1];

def  xcond = if condrange[-(range + 1)]  then cbegin-rbegin else Double.NaN;

AddCloud(
  if condition and (xcond) < 0 then Double.POSITIVE_INFINITY else Double.NaN,
  Double.NEGATIVE_INFINITY,
  Color.RED);
AddCloud(
  if condition and (xcond) > 0 then Double.POSITIVE_INFINITY else Double.NaN,
  Double.NEGATIVE_INFINITY,
  Color.GREEN);
 
Here is the Pre-Earnings Range Cloud, colored by green/red as an increasing/decreasing range. Change the input range length to indicate how far back you want to look.
here is a page on robert's site that has an amazing and simple code for showing clouds before and after earnings.

https://funwiththinkscript.com/highlighting-the-period-around-earnings/

here is a post with the code
https://usethinkscript.com/threads/days-until-earnings-watchlist-for-thinkorswim.1020/#post-11592
 
Last edited:
Correct me if I am wrong, but does this color code based upon the close of the day prior to announcement?
Any way to also code 10 or 14 or 21 days prior to close before announcement based upon a run up or run down prior to earnings?
Green if up ten day prior, red if down?

If TOS knows whether earnings was before market or after market it will use the final closing price before the earnings call. For after market that would be the same day. If TOS knows the day of earnings but not the time then it assumes it was after and uses that day's close as the pre-earnings price.

You could make it cover more days by changing the 7 in the definition of InCloudRange and adding more lines to the if/else block.
 

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