Vertical Cloud

bp805

New 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
 

Slippage

Active member
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?
 

bp805

New member
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;
 

Slippage

Active member
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.

2021-05-04-15-29-00-TWTR-Active-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:

Slippage

Active member
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.
 

Similar threads

Top