Creating and Visualizing a Portfolio with STOCHAST.ICU and TradingView

Aaron Janeiro Stone
4 min readOct 27, 2020


Currently, TradingView rather easily allows you to plot series for different equities across many different markets. While useful, investors of course are likely to be dealing with more than a single equity at a time (not to mention other instruments). Having an understanding of how to tackle such a problem from the point of view of a researcher, I figured that this presented an opportunity to broaden my horizons in terms of creating an application with a front-end. This resulted in STOCHAST.ICU, a web-app created with the simple goal of being able to take in tickers and put out something to be used with TradingView!

Something? Well, unfortunately, it is currently not possible to have the series representing the portfolio automatically be inserted into TradingView. Rather, Pine Script code is generated which can be pasted into the Pine Editor of TradingView. After doing so, the something becomes a time series representing the log-returns of the portfolio.

R(t), being the total weighted returns at time t, time being positive integers.

So how do we choose the best weights for each of our i in {1, …, N} assets? The most common way is to find the efficient frontier of the portfolio.

Let us examine a collection of candidate portfolios with different weights. On the graph below, each is represented as a dot. In order to simplify matters, let us consider from hereon out that:


meaning, in other words, that we are looking at yearly expected returns and variance.

The sideways “bullet” represents a sort of bound which will exist for our portfolios plotted in this regard. They exist because there are bounds on our weights ([0,1] per weight). Portfolios on the top half of the “bullet” are highlighted in red and are said to be efficient because for each of such portfolios, they offer the best expected returns for their level of variance. In cases without a risk-free asset, these are said to be “on the efficient frontier” and the top half of the bullet is the efficient frontier.

In cases with a risk free asset with some rate of return, rf, let us plot it in terms of the expected value (thus on the vertical axis).

Here, there is exactly one line we can draw which touches the top of the bullet without touching any other point, the tangency portfolio being the intersection of such a line with the bullet. In this case, the efficient frontier is this drawn line, representing a portfolio which is a combination of risk free and risky (in the bullet) assets.

Expected Return= 0.09429448127450267, Volatility= 0.04612413333354159, Sharpe= 1.6107507264635263. Data fit over 6988 days.

For now, STOCHAST.ICU will use exponentially-weighted moving averages for adjusting our statistics for expectation and variance to not merely be the sample mean and variance, but these weighted versions which grants stronger influence to more recent values. For a portfolio made of assets SPY and IEF (with IEF being an ETF for 7–10 Year Treasury Bonds), we are given a weighting favouring IEF.

Below the chart, we are given some code:

Copying the code and clicking the link to TradingView Charts, we can enter the Pine Editor. Clicking it and pasting in our code, we are now at:

Finally, we click “Add to Chart” and are met with a visualization of our series, as below:

Now we have a series on its own panel which can be treated as an input for other indicators!



Aaron Janeiro Stone

Statistician, MMath Statistics Student at the University of Waterloo