pythonpandasfinance

Financial performance and risk analysis statistics from sample DataFrame


How do I output detailed financial performance and risk analysis statistics from this sample pandas DataFrame?

Can anyone show how this could be done with Quantstats, Pyfolio or another similar approach?

Code

start_amount = 100000

np.random.seed(8)
win_loss_df = pd.DataFrame(
    np.random.choice([1000, -1000], 543),
    index=pd.date_range("2020-01-01", "2022-01-30", freq="B"),
    columns=["win_loss_amount"]
)
win_loss_df["total_profit"] = win_loss_df.win_loss_amount.cumsum() + start_amount

Sample DataFrame

win_loss_df.head(10)

win_loss_amount total_profit
2020-01-01  -1000   99000
2020-01-02  1000    100000
2020-01-03  -1000   99000
2020-01-06  -1000   98000
2020-01-07  -1000   97000
2020-01-08  1000    98000
2020-01-09  1000    99000
2020-01-10  -1000   98000
2020-01-13  1000    99000
2020-01-14  -1000   98000

Desired output

I would like to see output including:

I was hoping to use a library for this which would simplify the process and return data similar to a tear sheet.


Solution

  • We will use the profit column and use quantstats to generate reports.

    Code

    import quantstats as qs
    import numpy as np
    import pandas as pd
    
    
    start_amount = 100000
    
    np.random.seed(8)
    win_loss_df = pd.DataFrame(
        np.random.choice([1000, -1000], 543),
        index=pd.date_range("2020-01-01", "2022-01-30", freq="B"),
        columns=["win_loss_amount"]
    )
    win_loss_df["total_profit"] = win_loss_df.win_loss_amount.cumsum() + start_amount
    
    profit = win_loss_df.total_profit
    
    # Save to image file, this image can also be seen in full report.
    qs.plots.yearly_returns(profit, savefig='yearly_return.png')
    
    print(f'montly returns:\n{qs.stats.monthly_returns(profit)}')
    print(f'sharpe ratio: {qs.stats.sharpe(profit)}')
    print(f'max markdown: {qs.stats.max_drawdown(profit)}')
    
    # Print full report in html.
    qs.reports.html(profit, title='ABC', output='', download_filename='profit.html')
    

    Output

    Yearly return

    enter image description here

    Montly returns, Sharpe and markdown
    montly returns:
               JAN       FEB       MAR           APR       MAY       JUN       JUL       AUG       SEP           OCT       NOV       DEC       EOY
    2020 -0.060606  0.000000 -0.064516  4.597701e-02  0.032967  0.000000 -0.010638 -0.010753  0.086957 -1.110223e-16  0.030000  0.048544  0.101444
    2021  0.046296 -0.035398  0.045872 -4.440892e-16 -0.026316  0.018018  0.017699 -0.069565  0.018692 -4.587156e-02 -0.057692 -0.030612 -0.117146
    2022 -0.042105  0.000000  0.000000  0.000000e+00  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000e+00  0.000000  0.000000 -0.041881
    sharpe ratio: -0.16968348978006012
    max markdown: -0.23529411764705888
    
    Full report

    profit.html

    enter image description here

    enter image description here

    enter image description here

    enter image description here