Files
quanxiel/course/动手练习/01_pandas_热身.py
T

27 lines
974 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""01 | pandas 热身:收益率、波动、净值曲线
目标:搞懂量化里最基础的三个东西——收益、风险、净值。
"""
import pandas as pd, numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from lib_quant import get_daily, add_returns, net_value, stats
df = get_daily("600519", "20200101", "20241231") # 贵州茅台
df = add_returns(df)
nv = net_value(df["ret"])
print("==", "600519 贵州茅台", "==")
print(df[["日期", "收盘", "ret"]].tail(3).to_string(index=False))
print("绩效:", stats(df["ret"]))
plt.figure(figsize=(9, 4))
plt.plot(df["日期"], nv)
plt.title("Net value (buy & hold)")
plt.xlabel("date"); plt.ylabel("nav")
plt.tight_layout(); plt.savefig("../data/01_net_value.png", dpi=110)
print("图已存 data/01_net_value.png")
# 思考题:
# 1) 为什么不看"价格涨了多少",而要看"收益率"和"净值"?
# 2) 日收益率为 0 的日子占多少?(df['ret'].eq(0).mean())