```
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);
```