I know you are expecting an elegant solution to what appears to be a simple request. However in Thinkorswim, we don't have that luxury for something like this. We have a choice between two options. One of which I refer to as brute force methods, which I have employed in my solution below. And the other choice we have are elegant solutions which in this case, are so ridiculously convoluted they will destroy your sanity.
What Thinkorswim is lacking is a looping structure which enables us to handle multiple variables and custom arrays. Those tools and data objects simply don't exist. So while something like this is very easy and simple to do on most other trading platforms, they a royal nightmare to try to implement in Thinkorswim.
And when it comes to free solutions I am willing to post in the Q&A Forum, I am cannot afford the time to go through that nightmare just for the "fun" of it.
So here is the free solution I am willing to give away. this plots four separate lines on the chart. One for each of the four most recent earnings highs.
def targetBar = HasEarnings();
rec highOfEarnings = if HasEarnings() then high else highOfEarnings[1];
rec firstEarningsHigh = if highOfEarnings[1] == 0 and highOfEarnings > 0 then high else firstEarningsHigh[1];
rec secondEarningsHigh = if highOfEarnings <> highOfEarnings[1] and firstEarningsHigh[1] == highOfEarnings[1] and highOfEarnings[1] > 0 then highOfEarnings else secondEarningsHigh[1];
rec thirdEarningsHigh = if highOfEarnings <> highOfEarnings[1] and secondEarningsHigh[1] == highOfEarnings[1] and firstEarningsHigh[1] > 0 then highOfEarnings else thirdEarningsHigh[1];
rec fourthEarningsHigh = if highOfEarnings <> highOfEarnings[1] and thirdEarningsHigh[1] == highOfEarnings[1] and secondEarningsHigh[1] > 0 then highOfEarnings else fourthEarningsHigh[1];
plot earningsHighOne = if firstEarningsHigh > 0 then firstEarningsHigh else Double.NaN;
plot earningsHighTwo = if secondEarningsHigh > 0 then secondEarningsHigh else Double.NaN;
plot earningsHighThree = if thirdEarningsHigh > 0 then thirdEarningsHigh else Double.NaN;
plot earningsHighFour = if fourthEarningsHigh > 0 then fourthEarningsHigh else Double.NaN;