TOS-and-Thinkscript-Snippet-Collection
                                
                                 TOS-and-Thinkscript-Snippet-Collection copied to clipboard
                                
                                    TOS-and-Thinkscript-Snippet-Collection copied to clipboard
                            
                            
                            
                        Average Range in Earnings for 52 weeks
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 :)
#==================================================== 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); #====================================================