Python Library To Run Quantopian Algorithm In Live

Quantopian — The Online Algo Trading Platform

Quantopian is one of the most popular online algo trading platforms and communities today. It provides the great backtesting environment where you can experiment with your idea, build algorithms and even participate in the contest, as well as share the idea and discuss it with smart people there.

Photo by  Rodion Kutsaev  on  Unsplash

One of the things many people have asked Alpaca during the beta program is how to run the algorithms that they built in Quantopian platform for their own purpose, not just for the contest. While Quantopian has built so much in the platform, they are so great to share the internal framework as open source zipline.

The Newest Open Source Libraries for Quantopian Users

Today, I wanted to share our newest open source libraries for Quantopian users; pylivetrader and pipeline-live.

alpacahq/pylivetrader
Python live trade execution library with zipline interface. - alpacahq/pylivetradergithub.com

alpacahq/pipeline-live
Pipeline Extension for Live Trading. Contribute to alpacahq/pipeline-live development by creating an account on GitHub.github.com

pylivetrader is a zipline API compatible trading framework in python which again focuses on live trading, with much less overhead and dependency problems. It is written from the ground up for live trading use cases, so it removes a lot of heavy lifting that zipline had to do such as price adjustment etc.

This means, you don’t need to build your data bundle to kick off your algorithm in live, but instead you can just start your live trading from the Quantopian algorithm source right away.

At the moment, the supported backend is only Alpaca, but we are happy to connect to IB etc. if someone contributes the code.

Pipeline API — the Core Piece of Quantopian Framework

Pipeline API is the core piece of Quantopian algorithm framework that allows easy stock selection based on the different metrics, much in a pythonic way, and this differentiates the platform from others. I found Pipeline is providing a tremendous value when it comes to trading wide range of universe. Unfortunately, it is not so easy for most people to use this great feature outside of the Quantopian platform.

pipeline-live is a python tool that allows you to do something similar anywhere so that you can do your research somewhere else as well as use it with existing python trading framework such as zipline-live or backtrader, including pylivetrader which I am introducing below. pipeline-live primarily uses IEX public API for pricing and basic fundamental information.

As you know, IEX provides market-wide volume data for daily OHLCV which makes it a perfect choice for pipeline usage. Since pipeline-live focuses on live trading use cases, it does not provide historical view unlike inside Quantopian, but the upside is it is fairly independent and easy to use. It is also very extensible so you can hook up with other paid data sources if you would find useful.

How to Convert Your Quantopian Algorithms to Run in Live Trading

We also put some practices together about how you could convert your Quantopian algorithms to run in live trading. You may want to take a look at these documents if you are interested in.

https://github.com/alpacahq/pipeline-live/blob/master/migration.md
https://github.com/alpacahq/pylivetrader/blob/master/migration.md

I also posted in Quantopian forum with the real example, and you may take a look at it, too.

Long-only non-day trading algorithm for live
This is a modified version of the algorithm presented in…www.quantopian.com

Feel free to give me any feedback/questions/criticism. Happy to help you get started with live trading with these tools too.

And here is the example code migrated from the post above.


/

Algo Trading News Headlines 8/27/2018

Trading Places — Lines Blurred Between Traders And Programmers

(seekingalpha.com)

The recent WSJ article focused upon details from Adam Korn, a 16-year veteran at Goldman. He stated that success today depends less on trusting one’s gut, rather much of a trader’s job is embedded in the computer code or algorithms, which do much of the work now.

What is the real story though, what has all this computerized algorithmic trading truly done, how much value has it truly created? One question I would like to ask, is there a correlation between the explosion of our debt levels and this newly digitized financial age?

Photo by  Phil Botha  on  Unsplash

Photo by Phil Botha on Unsplash

Major Russian Airline Tests Blockchain in Bid to Track Fuel Payments

(www.coindesk.com)

According to S7, the application shares data about fuel demand on a shared ledger, a copy of which is managed by each of the three parties. Further, payments for the fuel can be conducted on the network, with digital invoices created via smart contract during each transaction.

Python Notebook Research to Replicate ETF Using Free Data

(alpaca.markets)

ETF is one of the great investment products in the last decade, and it has allowed so many people to gain the exposure to the wide range of assets easily at low cost. It is easy to buy a share of ETF without knowing what’s in there, but as a tech-savvy guy yourself, you may wonder how it works. By reconstructing the fund yourself, you may even come up with something better.

Trading Lesson: Don’t Touch That Dial. More to Come, Hedge Your Bets

(www.moneyshow.com)

In my 30 years as a trader, I’ve never seen a market like this. If the bots remain faithful to their programs, we are still likely to see higher stock prices over the next few weeks to months.

STEROID Launches New Automated Cryptocurrency Trading Algorithm

(www.bitcoinexchangeguide.com)

Algorithms run our online world, for the most part, a majority of everything done online is associated with an algorithm in one way or another. It only makes sense therefore that they would be used in the financial world as well. That is why STEROID has been developed, to create a functioning opportunity for traders on crypto exchanges.

Here’s how artificial intelligence can be used to beat the market

(www.cnbc.com)

CNBC’s Bob Pisani is joined by Sam Masucci, ETF Managers Group CEO, to discuss how he’s using an AI program to pick stocks.

/

Python Notebook Research to Replicate ETF Using Free Data

ETF is one of the great investment products in the last decade, and it has allowed so many people to gain the exposure to the wide range of assets easily at low cost. It is easy to buy a share of ETF without knowing what’s in there, but as a tech-savvy guy yourself, you may wonder how it works. By reconstructing the fund yourself, you may even come up with something better.

In this article, we present some basis for you to start your research easily in python to science the ETF world. You can find the complete notebook in GitHub.

Photo by  Kevin Ku  on  Unsplash

Photo by Kevin Ku on Unsplash

What is ETF by the way?

ETF stands for Exchange-Traded Fund. Unlike other types of funds, its shares are traded in exchanges like individual company’s common stocks. The fund is managed by an ETF company and manages portfolio based on the strategy, often diversifying the exposure spread across the market.

One of the most popular ETF is SPY, that tracks S&P 500 index performance. Because of its convenience to manage the risks, not only has it been used by individual investors, but also robo advisors construct their portfolio using ETFs. The convenience doesn’t come for free, of course, and there is an associated cost called expense ratio, that varies an ETF to another.

An ETF’s return comes from the returns of underlying assets it holds. ETFs can hold not just individual stocks but also options and swaps, but in the case of market index ETF like SPY, it constructs a simple long position portfolio.

If the constituents are simply long only stocks, is it easy to run some simulation even in python? If it’s possible to build your own ETF-like portfolio, you don’t even need to pay ETF cost? The answer is YES.

Recreating ETF

Various services provide ETF constituent data either through their website or API, with paid and unpaid style. Some provide even historical data. We recommend to find your best services by yourself, but here we automate the process by Selenium to save your time copying and pasting the list of underlying stocks of particular ETF.

get_etf_holdings() will return the list of constituents in pandas DataFrame format, and the columns include weight in the portfolio and an actual number of shares holding as of today. 

Note this does not come with the price data, but you can pull the historical price data from IEX API for free.

get_closes() will take the constituent data from get_etf_holdings() and return the daily closing price history for the last month from IEX API.

Simulate SPY performance

Before doing something unique, let’s just check if our assumption is correct. The task here is to calculate the historical performance of reconstructed portfolio and compare that with the actual ETF.

Remember the constituent list we have is the one as of today. The fund may have rebalanced, but we assume that’s not the case and we build our portfolio a month ago. Putting altogether, we get something like this.

1_OZI7PVciSwZS3MuNxxAudA.png

Even though we took the constituent data as of today, and applied it to simulate the last month, the result isn’t too different. This means this ETF hasn’t changed the holding shares significantly.

So, I don’t need to buy ETF but just buy these stocks?

It’s a natural question whether you can replicate ETF portfolio by buying only underlying stocks.

Yes, you can, only if you have more than $260,000,000,000 ($260BN) which is SPY’s market cap today. But no, you don’t have it, so let’s see how it changes if you do so with $10K. The resulted portfolio we get after some calculation is as below.

The actual total market value of this portfolio is about $2K. The reason why it diverges from the original target is because you don’t buy fractional shares. All fractions are truncated, resulting to much smaller. On the flip side, we found that we can build something similar to SPY with smaller amount of dollar. Running the same historical plotting, we get this.

1_wneCpDX4nv__MddVlztFIw.png

The divergence is much bigger compared to the first one, and the volatility increased, but in terms of the return, it is not too bad. As a study, it is great to see the actual example like this that more diversified portfolio has less volatility, as the modern portfolio theory teaches.

Summary, and now what?

We presented some python research with actual notebook to study how ETF works, and did some simple experiments. You can look at the complete notebook here.

You can try it in your environment! We recommend to clone the notebook and extend the study for your purpose from here. Potential questions you may ask are:

  • what if the cash size is bigger, or smaller?
  • how about other index ETF such as QQQ?
  • how much dollar do you need to have at least one share for each?
  • can you replicate the return more precisely by rebalancing frequently?
  • can you build something similar by using other set of stocks too?

Research is always fun, and you should continue asking these questions. It is a great moment that this kind of research can be done in a day with only your laptop.

We leave it to the readers to what to do from here, but please let us know what you find if you do something in the comment, or to our Twitter @AlpacaHQ! We hope you will leverage the technology to automate your investments.

/