Skip to content

Commit

Permalink
Initial commit of LeanReportCreator
Browse files Browse the repository at this point in the history
- Renames `ApiPython` project to `PythonToolbox`
- Moves `LeanReportCreator` from https://github.com/QuantConnect/LeanReportCreator to https://github.com/QuantConnect/Lean/tree/master/PythonToolbox.
- Fixes README.rst
- Add QuantConnect License header
  • Loading branch information
AlexCatarino committed Jun 6, 2018
1 parent f971d59 commit 0be6e8f
Show file tree
Hide file tree
Showing 50 changed files with 1,933 additions and 157 deletions.
31 changes: 0 additions & 31 deletions ApiPython/README.rst

This file was deleted.

Binary file removed AuthorProfile.jpg
Binary file not shown.
Binary file added PythonToolbox/AuthorProfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="quantconnect\api.py" />
<Compile Include="quantconnect\GenerateHTML.py" />
<Compile Include="quantconnect\LeanReportCreator.py" />
<Compile Include="quantconnect\__init__.py" />
<Compile Include="setup.py" />
<Compile Include="tests\test_api.py" />
Expand Down
186 changes: 186 additions & 0 deletions PythonToolbox/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
QuantConnect.com Interaction via API (python edition)
=====================================================

What is it
----------

**quantconnect** is a Python package providing interaction via API with `QuantConnect.com <https://www.quantconnect.com>`_.

Installation Instructions
-------------------------

This package can be installed with pip:

>>> pip install quantconnect -U

Enter Python's interpreter and type the following commands:

>>> from quantconnect.api import Api
>>> api = Api(your-user-id, your-token)
>>> p = api.list_projects()
>>> print(len(p['projects']))

For your user id and token, please visit `your account page <https://www.quantconnect.com/account>`_.

Create the package
------------------

Edit setup.py to set the desired package version. Then, create the distribution and upload it with `twine <https://pypi.python.org/pypi/twine>`_.:

>>> python setup.py sdist
>>> twine upload dist/*

Lean-Report-Creator
-------------------
Create beautiful HTML/PDF reports for sharing your LEAN backtest results in Python.

Instruction on installing and running the program
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Dear users, please refer to the following instructions to generate your strategy report!

1. Install Python3
""""""""""""""""""
You are strongly recommended to use Anaconda (or Miniconda) to install Python3 and its dependencies.

After downloading Anaconda (or Miniconda), open Anaconda Prompt and run "conda install python=3.6". Use the same method to install all its dependencies.

2. Prepare input files
""""""""""""""""""""""
(1) The first input file is the .json file which you can download once you finish your backtesting. You could put this file into a convenient directory, such as ./json/sample.json.

(2) Then please replace the file "AuthorProfile.jpg" with your own profile image, but do not change the file name.

3. Generate report
""""""""""""""""""""""
Execute the following command to generate your strategy report:

lrc = LeanReportCreator("./json/sample.json")
lrc.genearte_report()

4. Get the outputs
""""""""""""""""""
(1) Report.html

(2) all the individual images in the directory "./outputs"

Explanation on the meaning of the charts
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here I am going to give you a detailed explanation on the meaning of each chart.

1. Cumulative Return
""""""""""""""""""""
.. image:: outputs//cumulative-return.png
This chart shows the cumulative returns for both your strategy (in orange) and the benchmark (in gray).

The backtest version of this chart is calculated based on daily data. If the original price series in json file is not daily, we will first convert them into daily data.

The live version of this chart is calculated based on miniute data. Icons on the chart will show when the live trading started, stopped, or had runtime errors.

2. Daily Return
"""""""""""""""
.. image:: outputs//daily-returns.png
This chart shows the daily returns for your strategy.

When the return is positive, a orange bar will show above the horizontal line; when the return is negative, a gray bar will show below the horizontal line.

3. Top 5 Drawdown Periods
"""""""""""""""""""""""""
.. image:: outputs//drawdowns.png
This chart shows the drawdown of each day.

A certain day's drawdown is defined as the percentage of loss compared to the maximum value prior to this day. The drawdowns are calculated based on daily data.

By this definition, we can infer that when certain day's value is the maximum so far, its drawdown is 0.

The top 5 drawdown periods are marked in the chart with different colors.

4. Monthly Returns
""""""""""""""""""
.. image:: outputs//monthly-returns.png
This chart shows the return of each month.

We convert original price series into monthly series, and calculate the returns of each month.

The green color indicates positive return, the red color indicates negative return, and the greater the loss is, the darker the color is; the yellow color means the gain or loss is rather small; the white color means the month is not included in the backtest period.

The values in the cells are in percentage.

5. Annual Returns
"""""""""""""""""
.. image:: outputs//annual-returns.png
This chart shows the return of each year.

We calculate the total return within each year, shown by the blue bars. The red dotted line represents the average of the annual returns.

One thing needs mentioning: if the backtest covers less than 12 month of a certain year, then the value in the chart is the actual return which is not annualized.

6. Distribution of Monthly Returns
""""""""""""""""""""""""""""""""""
.. image:: outputs//distribution-of-monthly-returns.png
This chart shows the distribution of monthly returns.

The x-axis represents the value of return. The y-axis is the number of months which have a certain return. The red dotted line represents mean value of monthly returns.

7. Crisis Events
""""""""""""""""
9/11
.. image:: outputs//crisis-9-11.png
Lehman Brothers
.. image:: outputs//crisis-lehman-brothers.png
Us Downgrade/European Debt Crisis
.. image:: outputs//crisis-us-downgrade-european-debt-crisis.png
This group of charts shows the behaviors of both your strategy and the benchmark during a certain historical period.

We set the value of your strategy the same as the benchmark at the beginning of each crisis event, and the lines represent the cumulative returns of your strategy and benchmark from the beginning of this crisis event.

We won't draw the crisis event charts whose time periods are not covered by your strategy.

8. Rolling Portfolio Beta to Equity
"""""""""""""""""""""""""""""""""""
.. image:: outputs//rolling-portfolio-beta-to-equity.png
This chart shows the rolling portfolio beta to the benchmark.

This chart is drawn based on daily data. Every day, we calculate the beta of your portfolio to the benchmark over the past 6 months (gray line) or 12 months (blue line).

A beta close to 1 means the strategy has a risk exposure similar to the benchmark; a beta higher than 1 means the strategy is riskier than the benchmark; a beta close to 0 means the strategy is "market neutral", which isn't much affected by market situation. Beta could also be negative, under which the strategy has opposite risk exposure to the benchmark.

We won't draw this chart when your backtest period is less than 12 months.

9. Rolling Sharpe Ratio
"""""""""""""""""""""""
.. image:: outputs//rolling-sharpe-ratio(6-month).png
This chart shows the rolling sharpe ratio of your strategy.

The rolling sharpe ratio is calculated on daily data, and annualized. Every day, we calculate the sharpe ratio of your portfolio over the past 6 months, and connect the sharpe ratios into a line. The red dotted line represents the mean value of the total sharpe ratios.

We won't draw this chart when your backtest period is less than 6 months.

10. Net Holdings
""""""""""""""""
.. image:: outputs//net-holdings.png
This chart shows the net holdings of your portfolio.

The net holding is the aggregated weight of risky assets in your portfolio. It could be either positive (when your total position is long), negative (when your total position is short) or 0 (when you only hold cash). The net holding changes only if new order is fired.

The chart is drawn based on minute data, which means we aggregate all the risky positions in every minute together.

11. Leverage
""""""""""""
.. image:: outputs//leverage.png
This chart shows the leverage of your portfolio.

The value of the leverage is always non-negative. When you only hold cash, the leverage is 0; a leverage smaller than 1 means you either long assets with money less than your portfolio value or short assets with total value less than your portfolio value; a leverage larger than 1 means you either borrow money to buy assets or short assets whose value is larger than your portfolio value. The leverage changes only if new order is fired.

The chart is drawn based on minute data, which means we aggregate all the risky positions in every minute together.

12. Asset Allocations
"""""""""""""""""""""
.. image:: outputs//asset-allocation-all.png
.. image:: outputs//asset-allocation-equity.png
This group of charts show your asset allocations.

It is a time-weighted average of each class of asset to your portfolio.

The first chart shows the percentages of all the assets together. The sum of the percentages is 100%. When a certain asset has very small percentage and is too small to be shown in the pie chart, it will be incorporated into "others" class. The value of the percentage could be either positive or negative.

The rest of the pie charts shows the percentages of some more specific asset classes, for example, stocks, foreign exchanges, etc. We won't draw the chart if your portfolio doesn't include any asset within this class.
1 change: 1 addition & 0 deletions PythonToolbox/json/sample.json

Large diffs are not rendered by default.

741 changes: 741 additions & 0 deletions PythonToolbox/outputs/Report.html

Large diffs are not rendered by default.

Binary file added PythonToolbox/outputs/annual-returns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/asset-allocation-all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/asset-allocation-equity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-2009q1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-2009q2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-9-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-aug07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-dotcom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-flash-crash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-gfc-crash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-lehman-brothers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-mar08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-recovery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/crisis-sept08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/cumulative-return.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/daily-returns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/drawdowns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/leverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PythonToolbox/outputs/monthly-returns.png
Binary file added PythonToolbox/outputs/net-holdings.png
1 change: 1 addition & 0 deletions PythonToolbox/outputs/strategy-statistics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Key Characteristics": [["Significant Period", 1], ["Significant Trading", 1], ["Diversified", 0], ["Risk Control", 1], ["Markets", ["Equity"]]], "Key Statistics": [["CAGR", "24.06%"], ["Drawdown", "12.7%"], ["Sharpe Ratio", 1.783], ["Information Ratio", 0.701], ["Trades Per Day", 2.367396]]}
17 changes: 15 additions & 2 deletions GenerateHTML.py → PythonToolbox/quantconnect/GenerateHTML.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import os
import os.path
Expand Down Expand Up @@ -175,8 +188,8 @@ def MethodGetAssetAllocationPageHTML(outdir):
rightHeaderText = "Basic Template Algorithm"
footerId = ""
strategyDescription = "Put your strategy description here:"
authorProfile = "D:\fakepath\LeanReportCreatorInPython\AuthorProfile.jpg"
authorName = "Xiang Li"
authorProfile = "AuthorProfile.png"
authorName = "QuantConnect User"
authorBio = "Put your biography here."

def GenerateHTMLReport(outdir):
Expand Down
Loading

0 comments on commit 0be6e8f

Please sign in to comment.