pip install ffn 是一个专门为从事量化金融工作的人们提供金融数据分析功能的py包
以每日基金净值数据为样本 fund_std.py
# coding: utf-8
import os, sys
import pandas as pd
import ffn
if len(sys.argv) ==2:
code = sys.argv[1]
else:
print('usage: python fund_std.py fundcode ')
sys.exit(1)
if len(code) !=6:
print('stock code length: 6')
sys.exit(2)
f1 = code +'.csv'
df = pd.read_csv(f1, index_col='date')
df.index = pd.to_datetime(df.index)
# 18.1.3 ffn 计算简单收益率 p252
return1 = ffn.to_returns(df.jz).dropna()
print(return1.tail())
print(" min = {0} , max = {1}".format(return1.min(), return1.max()))
# 18.5 方差 p266
print(" std = {0}".format(return1.std()))
# 18.5.2 下行风险 DownSide Deviation
print(" DownSide Deviation = {0}".format(return1.quantile(0.05)))
# 协方差矩阵法 p270
from scipy.stats import norm
pp = norm.ppf(0.05, return1.mean(), return1.std())
print(" norm.ppf = {0}".format(pp))
运行 python fund_std.py 006671
Name: jz, dtype: float64
min = -0.06453904908520336 , max = 0.05495658811182125
std = 0.014247097909503239
DownSide Deviation = -0.01904123902653393
norm.ppf = -0.021504358883276994
参考书:[ 量化投资以Python为工具 ]