0

我有这个代码:

from empyrical import max_drawdown
from pandas_datareader import data as pdr

myStartDate = "2017-01-01"
myEndDate = "2017-04-30"
myTicker = "SPY"
myData = pdr.get_data_yahoo(myTicker, myStartDate, myEndDate)
daily_close = myData[['Adj Close']]
returns = daily_close.pct_change()
mdd = max_drawdown(returns)
print("My max drawdown:", mdd)

输出是:

My max drawdown: 0   -0.026101
dtype: float64

我希望输出显示为小数点后 2 位的百分比。例如:

My max drawdown: -2.61%

我该怎么做呢?

4

2 回答 2

0

这有效:

myVal = mdd.iloc[0]
print(f"My max drawdown: {round(myVal*100, 2)}%")
于 2020-11-16T16:07:57.550 回答
0

您需要将变量转换为常规浮点数,例如:

print(f"My max drawdown: {round(float(mdd) * 100, 2)}%")

或者:

print("My max drawdown: {0:.2f}%".format(float(mdd) * 100))
于 2020-11-16T16:01:09.427 回答