Stock Market Programming in TradingView

Share: is a website that allows users to view and work with stock charts online or via mobile app.

It also comes with the ability to code your own charts, create indicators and strategies, and back test those strategies. While thinkorswim from TD Ameritrade also provides these capabilities, Tradingviews are a bit more advanced and work more smoothly, in my experience.

Here is a “normal” chart from Tradingview:

Now, the fun begins!

What are the variables people want to see when analyzing chart?

Personally, I like seeing what the current and average draw-down of the stock is. A draw-down is the percent that the price has decreased since the highest price point of the stock. Here is how I made this:

As you can see, this is simple but effective. The code calculates the current draw-down as well as the average draw-down over the previous 500 days. Further, it colors the line gray when the current draw-down is less than the 500-day average draw-down, and green when the current draw-down is more than the 500-day draw-down.

But this is still just price action, which doesn’t give us much information about the fundamentals of the company. How is revenue? How are projections? To add more information, I want to add price to earnings ratios and forward price to earnings ratios.

Price to earnings ratios divide the market cap of the stock by the actual earnings per share of the company. Forward earnings are the earnings that Wall Street analysts expect the company to achieve one year in the future (in this example). The purpose of price to earnings is to have a metric that reflects both the price of the security and some underlying data on how the company is fairing. Therefore, price to earnings ratios tell you more about the fundamentals of a company than price alone can.

So, I wanted to make an indicator channel with 1) price to earnings, 2) price to forward earnings, 3) Avg and Maximum price to earnings (historically), and 4) the Forward Price to Earnings Draw-down (see bottom channel in this chart).

From the regular chart, we can see that NVIDIA is currently down a whopping 47 percent from its high price. This tells us that people are selling the stock, but nothing more.

From looking at the new, bottom PE indicator channel, we can see that from a forward price to earnings basis, the stock is actually down over 70 percent. That is, the price that people are willing to pay per expected dollar earned (per share) has fallen drastically. I would argue that this indicator gives more data than the previous, making it more useful.

Here is the code for this:

As you can see, there is code provided that involves grabbing fundamental data from Tradingview for whichever stock you are looking at. You can chart balance sheet and income statement data, as well as others. Further, you can program algorithms to analyze this data, and grab data from multiple stocks.

In this last example, I wanted to take a group of software stocks to see how forward valuations (i.e., forward P/E) is performing on as a group. This is what I found:

This version is a bit messy, as I should probably get rid of the excess data that I do not want to see, but from this we can see that this group of stocks is collectively down 42 percent by forward P/E metrics. Meanwhile, you can see that total forward earnings per share is still increasing while the price falls (see top orange line titled ‘Total FWD EPS’).

Here is the code used to create this:

These are just examples of the possibilities with Tradingview and it’s programming language of choice, pinescript.

For anyone interested in stocks and finance as well as programming, I recommend digging into either this system or the ThinkorSwim system, both of which allow you to program most of the same things. I chose Tradingview in the end because it comes with 1-year forward earning per share data, while Thinkorswim does not (that I know of). However, if you are looking into options, ThinkOrSwim has far superior data, but I will get into that in another post.

Thanks for reading, and have fun!