What riskfree rate should we use now for the Sharpe Ratio Calculation?

I would like to understand how to put the riskfree rate into the API on quantrocket. If the current 3M US treasury is 5.3%.

What if you are running a strategy across different risk free rate? e.g. the past 4 years from Jan 2020 until April 2024.

If you're using Moonchart and want to use a fixed risk-free rate, you can pass it as a parameter to Tearsheet.from_moonshot_csv:

from moonchart import Tearsheet
Tearsheet.from_moonshot_csv(
    "moonshot_results.csv", 
    riskfree=0.053 / 252
)

The number you pass will be subtracted from the returns for the purpose of calculating the Sharpe ratio (returns - riskfree) as seen here, so you want to pass it as a daily amount, that's the reason for dividing it by 252.

To model a changing risk-free rate, you can pass a pd.Series instead of a constant, because returns - riskfree will work whether riskfree is a constant or a Series. Assuming you have a CSV file of treasury yields, you would need to align it to the dates of your returns and then pass it to Moonchart. Maybe something like this:

from quantrocket.moonshot import read_moonshot_csv
results = read_moonshot_csv("moonshot_results.csv")
returns = results.loc["Return"]

riskfree = pd.read_csv("treasury_rates.csv")
riskfree = riskfree / 252
riskfree = riskfree.reindex(returns.index)

Tearsheet.from_moonshot_csv(
    "moonshot_results.csv", 
    riskfree=riskfree
)

Pyfolio is a little trickier. Pyfolio uses empyirical to calculate the Sharpe ratio and empyrical supports a risk_free parameter (see here), but currently pyfolio doesn't expose it.