Average Range of Earnings in 52 weeks

Whitepineapple

New member
Hi, I'm trying to write a code that calculates the average range in earnings of 52 weeks. However, I encounter issues and I can't seem to write it out due to my lack of knowledge in Thinkscript.
I get stuck on finding the range of the previous earnings, I think it has got to do with defining the date of the earnings. Below is the code that's as far as I get.
Kindly appreciate if anyone is able to help out thanks for reading
Cheers :)

Code:
#====================================================
def ear= HasEarnings(EarningTime.BEFORE_MARKET)[1];
def countdown = if ear then getyyYYMMDD() - getyYYYMMDD() else double.nan;
def a = if ear then getyyyymmdd() else double.nan;
def hod = if a then high(period = aggregationperiod.day) else double.nan;
def lod = if a then low(period = aggregationperiod.day) else double.nan;
def range = if a then hod-lod else double.nan;
AddLabel(yes, "range" + range, Color.white);
#====================================================
 

markos

Well-known member
VIP
Welcome @Whitepineapple Please go to Universe of ThinkScript located in Tutorials. It is very deep, have a look to find what you need. Please be a little more definitive... the lowest and highest earnings eg 0.53 cents to 0.98 cents over the last 4 earns releases? People with more smarts than I have done it, I believe. The answer would be in the Tutorial.
 

Whitepineapple

New member
This the link of the original code that is used as a reference. Twitch the code to get an average range of 4 previous earnings.

Code:
# Data1 to be used to back up to prior days close and ImpVolatility if earnings before market

def afterstart = GetTime() > RegularTradingStart(GetYYYYMMDD());

def Data1 = if (HasEarnings() and HasEarnings(type = EarningTime.AFTER_MARKET))
     then 1
     else if (HasEarnings() and HasEarnings(type = EarningTime.BEFORE_MARKET))
     then 2
     else 0;
 
# Data1 set = 2 if earnings before open, only need to back up 1 day so add 1 to make a new field TheBarNumber

def TheBarNumber = BarNumber() - Data1 + 2;

# Calculate barnumber to get closing price for each earnings period

def Data  = if Data1 != 0 and !IsNaN(HIGH) then TheBarNumber else Data[1];

def data2 = if Data[1]  != Data  and Data1 != 0 then Data[1]  else data2[1];
def data3 = if data2[1] != data2 and Data1 != 0 then data2[1] else data3[1];
def data4 = if data3[1] != data3 and Data1 != 0 then data3[1] else data4[1];
def data5 = if data4[1] != data4 and Data1 != 0 then data4[1] else data5[1];


# Calculate highest price earnings
def h1 = if BarNumber() == HighestAll(Data)  then HIGH else h1[1];
def h2 = if BarNumber() == HighestAll(data2) then HIGH else h2[1];
def h3 = if BarNumber() == HighestAll(data3) then HIGH else h3[1];
def h4 = if BarNumber() == HighestAll(data4) then HIGH else h4[1]; 
def h5 = if BarNumber() == HighestAll(data5) then HIGH else h5[1]; 


# Calculate lowest price earnings
def l1 = if BarNumber() == HighestAll(Data)  then low else l1[1];
def l2 = if BarNumber() == HighestAll(data2) then low else l2[1];
def l3 = if BarNumber() == HighestAll(data3) then low else l3[1];
def l4 = if BarNumber() == HighestAll(data4) then low else l4[1];
def l5 = if BarNumber() == HighestAll(data5) then low else l5[1];

#Calculate Range
def r1 = h1-l1;
def r2 = h2-l2;
def r3 = h3-l3;
def r4 = h4-l4;
def r5 = h5-l5;

#avg range
def avg = (r1 + r2 + r3 + r4 )/4;
def avg2 = (r2+r3+r4+r5)/4;


def if_tdy_ear = if afterstart == 1 and hasearnings(EarningTime.ANY) then avg2 else avg;

addlabel(yes, " Avg E.R " + round(if_tdy_ear,2),color.yellow);
#end code
 

markos

Well-known member
VIP
Hi @Whitepineapple , out of respect for other coders and keeping best practices, please leave the original header in and add your name to the bottom as shown. Thanks, Markos ☺

PS One reason for this is that there is a coder named Paris who lives in Japan and he has over 7000 thinkscripts on his server, all cataloged, so if I'm misssing a piece from a file, and I ask real nice, he'll often look up the original! @BenTen

Code:
#zzz_ExampleUsingPriorEarnings_v9.2
#Example Using Prior Earnings
#
#Paris Chat Request 4.22.2015
#zzz           - 04/22/2015 - original coding
#AlphaInvestor - 04/22/2015 - adjusted to account for Pre-Market Earnings
#AlphaInvestor - 04/22/2015 - added % gain and average % gain post-earnings
#AlphaInvestor - 04/24/2015 - after testing, turned off Pre-Market Earnings adjustment - not working correctly
#AlphaInvestor - 04/24/2015 - added % gain and average % gain pre-earnings
#zzz           - 04/25/2015 - changed earningsdisplayed to an input
#zzz           - 04/26/2015 - added condition for "No Earnings" (ex: RUT)
#zzz           - 04/26/2015 - added fix to exclude future earnings
#Dilbert       - 04/27/2015 - Chg'ed the data1/data/data2-9 logic to make before market work
#Dilbert       - 05/03/2015 - Add IV data , Remove earnclose/preEarnClose/lback/lookforward logic because replaced by data1 calculation.  Clean up code, and comments.
#Dilbert       - 05/03/2015 - Change colors of the IV lables because after earnings there is going to always be a colapse.  Color them green if IV goes up before earnings, and red if IV goes down before earnings.  Fix a couple of fields in the IV labels that were pointing at price fields.
#Dilbert  V9.2 - 05/03/2015 - Comments etc'
#Lookback 10 bars is set to find prior close to earnings and lookforward captures the close 5 bars later.
# Whitepineapple - whatever date and any tweaks you've made, if any.
 
Last edited by a moderator:

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.
Top