Financial data on tradingview

This is the first code I have created to show financial information of companies on Tradingview.

To start with something basic I have entered just some data: revenue, gross profit, operating income, EBITDA, and free cashflow.

This link contains the financial data you can use on PINE.

What can you check on the selection pane?

On “period”, you can show the data related to:

  • the fiscal quarters
  • or the fiscal years.

You can select a pack of financial data that I have organized in sections:

  • Revenue & earnings
  • EPS & DPS (EPS, EPS estimate, DPS and dividend payout ratio )
  • Debt (total debt, total equity and cash & equivalents)
  • Returns (ROE, ROIC, ROA and R&D revenue to ratio)

. I recommend to just select one of them, in other case the chart is a mess.

Some screenshots

An example of the “Revenue & Earnings” chart (values in millions):

An example of “EPS & DPS” chart, than contains:

  • Earning per share basic
  • Earning estimate (so you can see if the met the target, passed or missed).
  • Dividend per share.
  • Dividend payout ratio.

An example of debt and equities (values in millions):

An example of what I have called “Returns”, that is ROE, ROIC, ROA and R&D revenue to ratio:

The code

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// © @joapen

study("P1 Fundamentals", shorttitle="Fundamentals", precision=6, overlay=false)
MILLION = 1000000

// ---------- Inputs
output = input(defval="Per Share", title='Output type', options=["Per Share", "% of mcap", "Actual"])
// select data by fiscal quarter or by fiscal year
period = input(defval="FQ", title='Period', options=["FQ", "FY"])
// Show labels only last today
showLabel = year(time) == year(timenow) and month(time) == month(timenow) and dayofmonth(time) == dayofmonth(timenow)

// ---------- Variable initializations
// In case you want to add more data you can see all parameters available on the link below

// Revenue and Earnings History and Analysis
rev = financial(syminfo.tickerid, "TOTAL_REVENUE", period) / MILLION
grossProfit = financial(syminfo.tickerid, "GROSS_PROFIT", period) / MILLION
ebitda = financial(syminfo.tickerid, "EBITDA", period) / MILLION
op_expenses = financial(syminfo.tickerid, "OPERATING_EXPENSES", period) / MILLION * -1
fcf = financial(syminfo.tickerid, "FREE_CASH_FLOW", period) / MILLION

// Show it? then plot it //
showR_E = input(true, title = "Show Revenue & Earnings?")
plot(showR_E?rev:na, title="Revenue",, linewidth=3, style=plot.style_area, transp=90)
plot(showR_E?rev:na, title="Revenue", color=color.purple, linewidth=3, style=plot.style_linebr)
plot(showR_E?grossProfit:na, title="Gross Profit",, linewidth=3, style=plot.style_linebr)
plot(showR_E?ebitda:na, title="EBITDA", color=color.aqua, linewidth=3, style=plot.style_linebr)
plot(showR_E?op_expenses:na, title="Operating Expenses",, linewidth=3, style=plot.style_linebr)
plot(showR_E?fcf:na, title="FCF", color=color.lime, linewidth=3, style=plot.style_linebr)

if (showLabel and showR_E)
label revBox =, y=rev, text="Revenue", textalign=text.align_left, textcolor=color.white, color=color.purple, style=label.style_label_left, size=size.small)
label grossProfitBox =, y=grossProfit, text="Gross Profit", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)
label ebitdaBox =, y=ebitda, text="EBITDA", textalign=text.align_left,, color=color.aqua, style=label.style_label_left, size=size.small)
label op_expensesBox =, y=op_expenses, text="Op. Expenses", textalign=text.align_left,,, style=label.style_label_left, size=size.small)
label fcfBox =, y=fcf, text="FCF", textalign=text.align_left,, color=color.lime, style=label.style_label_left, size=size.small)

// EPS & DPS History and Analysis
eps = financial(syminfo.tickerid, "EARNINGS_PER_SHARE_BASIC", period)
epsE = financial(syminfo.tickerid, "EARNINGS_ESTIMATE", period)
dps = financial(syminfo.tickerid, "DPS_COMMON_STOCK_PRIM_ISSUE", period)
dpayout = financial(syminfo.tickerid, "DIVIDEND_PAYOUT_RATIO", period) / 100

// Show it? then plot it //
showE_D = input(false, title = "Show EPS & DPS?")
plot(showE_D?eps:na, title="EPS",, linewidth=3, style=plot.style_linebr)
plot(showE_D?epsE:na, title="EPSE", color=color.teal, linewidth=1, style=plot.style_linebr)
plot(showE_D?dps:na, title="DPS",, linewidth=3, style=plot.style_linebr)
plot(showE_D?dpayout:na, title="Dividend payout ratio",, linewidth=2, style=plot.style_linebr)

if (showLabel and showE_D)
label epsBox =, y=eps, text="EPS", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)
label epsEBox =, y=epsE, text="Estimated EPS", textalign=text.align_left, textcolor=color.white, color=color.teal, style=label.style_label_left, size=size.small)
label dpsBox =, y=dps, text="DPS", textalign=text.align_left,,, style=label.style_label_left, size=size.small)
label dpayoutBox =, y=dpayout, text="Div. Payout", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)

// Debt to Equity History and Analysis
totalDebt = financial(syminfo.tickerid, "TOTAL_DEBT", period) / MILLION
totalEquity = financial(syminfo.tickerid, "TOTAL_EQUITY", period) / MILLION
cash_and_equivalents = financial(syminfo.tickerid, "CASH_N_SHORT_TERM_INVEST", period) / MILLION
// Show it? then plot it //
showDebt = input(false, title = "Show Debt?")
plot(showDebt?totalDebt:na, title="Total Debt",, linewidth=3, style=plot.style_linebr)
plot(showDebt?totalEquity:na, title="Total Equity",, linewidth=3, style=plot.style_linebr)
plot(showDebt?cash_and_equivalents:na, title="cash & equivalents", color=color.lime, linewidth=3, style=plot.style_linebr)

if (showLabel and showDebt)
label totalDebtBox =, y=totalDebt, text="Total Debt", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)
label totalEquityBox =, y=totalEquity, text="Total Equity", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)
label cash_and_equivalentsBox =, y=cash_and_equivalents, text="Cash & Equivalents", textalign=text.align_left, textcolor=color.white, color=color.lime, style=label.style_label_left, size=size.small)

// Returns History and Analysis
roe = financial(syminfo.tickerid, "RETURN_ON_EQUITY", period) / 100
roic = financial(syminfo.tickerid, "RETURN_ON_INVESTED_CAPITAL", period) / 100
roa = financial(syminfo.tickerid, "RETURN_ON_ASSETS", period) / 100
RandDtoRevenueRatio = financial(syminfo.tickerid, "RESEARCH_AND_DEVELOP_TO_REVENUE", period) / 100

// Show it? then plot it //
showReturn = input(false, title = "Show Returns?")
plot(showReturn?roe:na, title="ROE",, linewidth=3, style=plot.style_linebr)
plot(showReturn?roic:na, title="ROIC", color=color.teal, linewidth=3, style=plot.style_linebr)
plot(showReturn?roa:na, title="ROA", color=color.purple, linewidth=3, style=plot.style_linebr)
plot(showReturn?RandDtoRevenueRatio:na, title="R&D revenue to ratio",, linewidth=3, style=plot.style_linebr)
hline(showReturn?0.2:na,, linewidth=2)

if (showLabel and showReturn)
label roeBox =, y=roe, text="ROE", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)
label roicBox =, y=roic, text="ROIC", textalign=text.align_left, textcolor=color.white, color=color.teal, style=label.style_label_left, size=size.small)
label roaBox =, y=roa, text="ROA", textalign=text.align_left, textcolor=color.white, color=color.purple, style=label.style_label_left, size=size.small)
label RandDtoRevenueRatioBox =, y=RandDtoRevenueRatio, text="R&D to Revenue Ratio", textalign=text.align_left, textcolor=color.white,, style=label.style_label_left, size=size.small)


Any suggestion of improvement is welcome.

Trading learning year 3

After a good 2019 learning about how to trade, this 2020 was fine in so many ways, that I would like to review.

The math result? I did a 36%,

2020 started with $1.420,94
and ended with   $1.934,41
so the P&L was $513,47   or a 36,1%

Total moves 188
wins 157     84%
loses   31    16%

This is the screen with the main data of win/loss events:

I have ordered by date of long move and the main major losses come from issues related to the crash happened in March.

Some data

Best values I traded:

TicketGross Margin

Worst values I traded:

TicketGross Margin

The number of moves by month were:

Month# moves

Portfolio data month by month

MonthStartedEndedDif ($)Dif (%)
Oct 14011353-48-3%

Some lessons from data:

  1. I have to stop loss better than I did, this happened to me in 2019 too.
  2. Portfolio management was better done in second half of the year and it gave me more consistent small wins.
  3. To pay attention to ex-date gave me a nice 47$ of dividends.
  4. The average of earnings have been 1,99$ in comparison with 2019 is worst (2,32%).
  5. 10% of the worst moves made me lose 705$ which is a lot.
  6. I have to improve on the way I’m trading, I still have some negative skew.
  7. The rally I did during the last 3 months enabled me to recuperate of a disaster. I have to focus myself on what I did well during Q4 because it worked.
  8. I have learned to read better the market timing indicators and this valuation has enabled me to exit and enter in values with better results.


  • My 2019 trades were quite better than 2020 in so many ways: benefit, number of errors and bias.
  • I have to concentrate more on the process and avoid the noise.

What to expect for 2021?

  • I have increase the funds with 2000$, so we will start the year with 3934$.
  • I will use the same percentage when opening positions: 1%, 2%, 3%.


Algorithmic Trading Wardley Map

I have been drawing this map as part of a conversation on slack with other user (Tom) where I was trying to figure out what map and needs are required.

So I started with the user: the algorithmic trader. This person wants to implement an algorithm for executing trades automatically without human intervention. So, the design of a system, the back-test of the algorithm and the execution of the final tested code are its main needs.

Then, you could decide if to do it by yourself or using one of the existing platforms. I know Quantopian a little bit, so I have used this one as reference for the map (you can use others).

I have drawn the lines in different colors to differentiate the selection of a user that wants to take one route or other: user that builds its own environment (green) or that uses a platform (orange).

I have doubts with the practice, in the few knowledge I have, practices depends more on the models and technique that an author writes about.

The Wardley Map

The typical steps that an algorithmic trader do are:

  1. Design the system starting defining the trading rules that wants to put in place. Once done it’s moved into an algorithm.
  2. The algorithm is tested on a data set, and results are evaluated through a set of indicators and standard values that traders use to work with.
  3. Once a tested algorithm is considered as valid, the code is executed in production with real money.

Key aspects of the map:

  • Real time data is key for the execution of the code, specially if the algorithm works well in small spaces of time. There is a big space of work here and the competition with high frequency traders is tough.
  • The connection with the broker is key, a platform as Quantopian that used to have it, removed it. The brokers uses to enable APIs for connections from external computers. Here again the time response is key.
  • One of the advantages of Quantopian is that they offer a prepared datasets that in the past had high quality. This makes you save a lot of time because the preparation and maintenance of data is something that is very time consuming.
  • I have not represented concepts as risk management, as this is something conceptual that the algorithm trader has to implement in the code following the principles, rules and systems that is more valid for him/her.

Quantopian Business model

This Bostonian company has two main lines of business are:

  1. Developer members, who develop and test algorithms for free, focusing on specific factors that, in case they are winning algorithms, can be added to Quantopian’s offerings to institutional investors. In this case they can have some royalties or commissions.
  2. Institutional investors, have their investments managed by the winning algorithms.

There were a moment where they took a decision

In 2017, Quantopian decided not support the connection with live trading brokers

I have read this note, where it’s announced that Quantopian no longer supports live trading through Interactive Brokers (or Robinhood). Here, the users are moving to other environments, some of the ones mentioned are:

Reading the comments of the forums, it seems that the solutions are not perfect, but the users are surviving using other environments. The users having their own environment must be laughing.

The point is, Quantopian wanted to have a free environment to promote winning scripts and then use them with their institutional investors. To have real winning algorithms you need real competition that forces people to find higher profits, not just a mediocre profit that an individual could use to earn some dollars. They probably identified that real competition was invaded by average brokers that wanted to take advantage of a free environment, and they cut the umbilical cord for these average brokers.

Announcement on October 29th

Quantopian’s Community Services are Closing, this is the title of the note published to announce the closing of the Research and Backtesting are no longer available.


We can identify two sets of gameplays used by Quantopian:

  • Education: they have had a clear approach to educate people and the amount of educational content is really extense and high quality.
  • Open approaches: the use of python as programing language enabled them to attract a lot of researches that are used to work with it, and to take the advantage of libraries as Zipline.
  • Differentiation: they were offering the institutional investors a set of algorithms that were tested and they had real winning percentage and profits. The market is very extense, and to have an “army of researchers” looking for weird correlations that leverage profits for “free” is something definitely different.
  • Creating constraints : the removal of access to live brokers was an invitation to some set of users to leave the environment. I can imagine that they took the number of users connected to brokers, and they easily calculated the amount of resources that they were consuming.

Which other gameplays do you identify?

Which other platforms exist?

I found this picture in  that is very useful to understand some of them:


Monthly POMO evolution

I was digging into the Permanent open market operations (POMO) this August, trying to understand the impact of the FED in the stock market.

Now that some months after the turmoil have passed, I have looked into the numbers in a monthly basis.

The amount injected by the Fed in the market during these 9 first months of 2020 is:

To see the evolution of the last 4 months in a better way, I have removed March and April. This is how data looks like:

What about the 2.3T$ promised by the FED?

In March the FED announced a stimulus program of 2.3T$ for the market through POMO. Right now they have spent 1.84T$ which is around 80% of the program.

What’s coming during next 6 weeks?

Well the main events are:

  • US President election.
  • Q3 results of the majority of the main companies of the indexes.



tradingview pine limit

I am trying to write a script on Pine (related to a Tradingview strategy) where I want to use the series and take the previous value or past values to make a set of operations.

Something as:

a = if(Alerta1[-1] == 5, 10,0)

the pine script is saved without error, but during the execution, an error is shown:

Index can’t be a negative value (-1)

Is there anybody that could let me know how to solve it?

I would like to use “[-1]” and other “minus” such “[-20]” for a script.

many thanks in advance

The answer

The answer is as simple that I’m embarrassed of not being able to see it:

Value Line Geometric Index

The american market (I focus on S&P 500) has recuperated 40% of the value since March where it touch the minimum value.

It’s an impressive “come back” while the macro economy data is showing terrible numbers about unemployment, consumption, industrial production….

The economy and the market are driving themselves in the opposite direction. So, so many questions come to my mind:

  • Is the market crazy?
  • is the market completely disconnected from the economy?
  • are we looking at the right numbers?

Well, Mr. Market does what he wants, and we cannot do anything about it. Disconnected from the economy? I do not think so, maybe there is a bubble, but sooner or later it will adjust. Well, my answers are poor, and it’s basically because I have not a concrete answer to these questions.

The last question: “are we looking at the right numbers?” makes me to go to Value Line Geometric Index

Value Line Geometric Index

This index includes all the american market. For more information the Wikipedia.

“All companies in the Value Line Composite Index are publicly listed on one of the major exchanges listed below. The number of companies in the Value Line Composite Index fluctuates based on factors including: the addition or delisting of the companies on the exchanges themselves, mergers, acquisitions, bankruptcies, and the coverage decisions made by Value Line for the Value Line Composite Index. Value Line’s decisions as to which companies to include are undertaken with the intention to create a broad representation of the North American equity market.

Exchanges in The Value Line Composite Index are:

  • American Stock Exchange
  • New York Stock Exchange
  • Toronto Stock Exchange

Well, comparing the VALUG (blue) with SPX (red), you can see that Value Line Geometric Index is more closed to the reality that I had in mind that is that we still have not recuperated.

If we look the comparison of the indexes from February 12th, the result is that draw-down is bigger for VALUG, and that the recuperation is below than S&P:

Update on 07/August/2020

  1. The Value line has crossed over the 200 days SMA for the second time. This is an interesting point of intersection.
  2. This event happened in June and the market reacted for few days. These days the FED did not use big amount of money on POMO.

To do: I will add this index to my reviews, I have first to learn from it.

Spanish market issue: script dividend as extended policy

One of the big issues of the stock market in Spain is the one represented in this picture.

Dividends? No, “papers”

Some of the popular investment methods is based on dividends, some others at least recognize that dividends is an incentive to invest on a company.

The main companies in the IBEX-35 market, so many times, instead of cutting the dividend or ask for money to the market, they have created a mechanism to do two things at the same time: script dividend.

They define a process with different steps that you have to follow. Mainly there are 2 options:

  • you grab the dividends and run.
  • you grab a set of new stocks (a new amount of stocks published to inject extra money into the company).

The process to do it

Once that is declared the “script dividend” you have to learn how to go through the process of claiming the new stocks or to take the dividend itself. This process is not straight forward, it’s a very complex process that requires you time to understand it, and then you have to pay attention the key days where you have to make a decision.

In a nutshell, so many small investors fall in the trap and they capture more stocks that they really do not want, diluting the value of the company.

My opinion

is that an incentive turns into an obligation to the investor, making him to decide on something that is not initially their responsibility. The result in the long term is not working well for the companies that are following this policy.