Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Collaborating in an Open Source Trading Project – Part I

Collaborating in an Open Source Trading Project – Part I

Posted March 10, 2023 at 8:55 am
Mario Pisa
QuantInsti

A must-have tool for any individual programmer or development team is version control software. In this post, we introduce you to Git/Github to modify an open source project such as the Pyfolio library.

Topics covered:

  • Scope
  • An introduction to Pyfolio
  • An introduction to Git and GitHub
  • Getting the pyfolio source code
  • Improving the pyfolio library
  • Publishing the new functionality

Scope

One of the basic principles of software engineering is not to reinvent the wheel. Well developed pieces of software can be used over and over again in multiple projects.

Python and its ecosystem of open source libraries allow us to build software using the work of countless development teams and individual programmers.

Every Python project uses libraries in one way or another. These are nothing more than pieces of software that package classes and functions with specific functionalities such as:

These libraries are generic and flexible enough to adapt to any software we are building and save us a lot of time by not having to implement from scratch the functionality that these libraries offer.

In our trading environment, a recurring need for all projects is a performance analysis of our strategies. It would not make sense to code our statistics over and over again, so having a performance analysis library is a necessity.

We can either build our own library that calculates:

  • Returns
  • Standard deviation
  • Sharpe ratio
  • Sortino ratio
  • Skew
  • Performance graphs

and a myriad of performance parameters. Or we can use an existing library and improve it if possible, with our own performance indicators.

In this post, we are going to modify an open-source and free software library pyfolio to adapt it to our needs and maybe our changes will be useful for the community. We will release our modification so that other developers can benefit from it. In the process,  we will also learn how to use the Git/GitHub tools to control changes and software versions.

Pyfolio is distributed under Apache 2.0 license, which allows us to modify the code, distribute it and even commercialize it. However, it is worth reading the license agreement, at least one time in your life.

Here we will not go into the many licenses under which open and/or free source is distributed nor into the philosophy.


An introduction to Pyfolio

Pyfolio is a performance and risk analysis python library for financial portfolios developed by Quantopian Inc.

Note: This company has stopped its operations although their libraries are still alive on the Internet.

Pyfolio is a comprehensive library that generates performance reports that cover the basic needs of any analyst quite well.

Outstanding features of Pyfolio:

  • Simple tear sheet:
    • Summary performance statistics table:
      • Annual return
      • Cumulative returns
      • Annual volatility
      • Sharpe ratio
      • Maximum drawdown
      • Skew
      • Kurtosis
      • And many more key performance indicators.
    • Plots:
      • Cumulative returns
      • Rolling beta
      • Rolling Sharpe
      • Underwater
      • And more
  • Returns tear sheet
    • Summary performance statistics table
    • Plots:
      • Rolling returns
      • Rolling beta
      • Rolling Sharpe
      • Rolling Fama-French risk factors
      • Drawdowns
      • Underwater plot
      • Monthly and annual return plots
      • Daily similarity plots and 
      • Return quantile box plot
  • Full tear sheet
    • Summary performance statistics table
    • Returns tear sheet
    • Transactions tear sheet
    • Round trip tear sheet
    • Interesting times tear sheet
    • Capacity tear sheet
    • Performance attribute tear sheet

All these features make the Pyfolio library something to consider. The library is flexible enough to effectively cover a performance analysis of a strategy or portfolio.


An introduction to Git and GitHub

Git is a distributed version and changes control software developed by Linus Torvalds and currently owned by Microsoft. It is a tool that allows controlling changes and versions in the software over time, with ease to recover the code in any of its previous states.

It allows distributed development, where different teams of programmers can make changes and generate their own versions or contribute their work to the main version.

Basically, Git is a code repository associated with each individual project that we update through commits.

Outstanding features of Git:

  • Branches are used to work on and modify the code without affecting the main version. They also allow us to initiate alternative developments, where the branch becomes a new library that, due to the scope of the changes and new functionality, requires a new independent version.
  • Commit to save the code in a time point, a commit saves the work and adds an identification with a message in order to be able to recover this development state in the future.
  • Merge to fuse different branches
  • Rollbacks to revert changes at any point time saved through the commits.

This functionality alone is enough to merit the attention of any individual developer and even more so for development teams working on the same project.

The GitHub tool is nothing more than a Git server in the cloud that allows us to publish our Git repositories on the Internet so that any other developer can use the code or contribute with her own development.

It also has a very interesting tool such as the ticket manager to manage bugs or improvements in the software and other tools to help in collaborative development.


Stay tuned for part II to learn about getting the pyfolio source code.

Originally posted on QuantInsti Blog.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.