Understanding a company’s earnings is crucial for making informed trading decisions. Pine Script, the programming language used on TradingView, provides a convenient way to access and utilize earnings data in your custom indicators. In this article, we’ll explore how to fetch stock earnings using the request.earnings
function and incorporate them into your indicators with practical code examples.
Accessing Earnings Data with request.earnings
The request.earnings
function in Pine Script allows you to retrieve historical earnings data for a specified stock symbol. This function can fetch various types of earnings information, such as actual earnings, estimates, and can even handle currency conversions.
1. Fetching Basic Earnings Data
To retrieve the actual reported earnings for a stock like Apple Inc. (NASDAQ), you can use the following code:
//@version=5
indicator("AAPL Earnings", overlay=false)
s1 = request.earnings("NASDAQ:AAPL")
plot(s1, color=color.blue, title="Actual Earnings")
This will display AAPL’s actual earnings per share (EPS) over time, allowing you to visualize the company’s progress:
2. Handling Gaps in Earnings Data
Earnings are reported periodically, so there will be gaps in the data. Pine Script fills these gaps by default, which might not be ideal for your analysis. You can control this behavior using the gaps
parameter:
//@version=5
indicator("AAPL Earnings with Gaps", overlay=false)
s2 = request.earnings("NASDAQ:AAPL", gaps=barmerge.gaps_on)
plot(s2, color=color.orange, title="Actual Earnings with Gaps")
gaps=barmerge.gaps_on
: Ensures that the gaps between earnings reports are preserved:
3. Retrieving Earnings Estimates
Analysts often provide earnings estimates before the actual earnings are reported. You can access these estimates using the field
parameter:
//@version=5
indicator("AAPL Earnings Estimates", overlay=false)
s3 = request.earnings("NASDAQ:AAPL", field=earnings.estimate)
plot(s3, color=color.green, title="Earnings Estimates")
4. Converting Earnings to a Different Currency
If you’re interested in viewing the earnings data in a currency other than the company’s reporting currency, you can use the currency
parameter:
//@version=5
indicator("AAPL Earnings in EUR", overlay=false)
s4 = request.earnings("NASDAQ:AAPL", currency='EUR')
plot(s4, color=color.purple, title="Actual Earnings in EUR")
This is useful for international investors who want to assess earnings in their local currency.
Conclusion
Incorporating earnings data into your Pine Script indicators can provide valuable insights into a stock’s performance and help you make better trading decisions. By using the request.earnings
function with its various parameters, you can customize the data to suit your analysis needs.
Feel free to experiment with these code snippets and modify them to create your own unique indicators. Happy coding!
HI , I like your little tutorial. My question refers to section 1. The title on line 5 calls the data “actual earnings” but your description says it pulls EPS. Which is it? I am looking for actual earnings data so i can chart this against estimated earnings and price.
It’s actual EPS, earnings.actual is default field. You can get estimated with a earnings.estimate field.