来自示例 DataFrame 的财务绩效和风险分析统计数据

Financial performance and risk analysis statistics from sample DataFrame

如何从此样本 pandas DataFrame 输出详细的财务绩效和风险分析统计数据?

任何人都可以展示如何使用 Quantstats、Pyfolio 或其他类似方法来完成这项工作吗?

代码

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

示例数据帧

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

期望输出

我希望看到输出包括:

我希望为此使用一个库来简化流程和 return 类似于撕裂的数据 sheet。

使用量子统计。它有很好的指标库。

我们将使用利润栏并使用quantstats生成报告。

代码

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')

输出

每年return

每月 returns,Sharpe 和 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
完整报告

profit.html