Professional Documents
Culture Documents
Several Key PerformanceAnalytics Functions From R Now in Python (Special Thanks To Vijay Vaidyanathan) - QuantStrat TradeR
Several Key PerformanceAnalytics Functions From R Now in Python (Special Thanks To Vijay Vaidyanathan) - QuantStrat TradeR
QuantStrat TradeR
Posted on June 21, 2023 by Ilya Kipnis • Posted in AI, Asset Allocation, Basics, Data Analysis, Portfolio
Management, Python, R, Replication, Reporting, Trading • Tagged AI, Python, R • Leave a comment
So, thanks to my former boss, and head of direct indexing at BNY Mellon, Vijay Vaidyanathan, and his
Coursera course, along with the usual assistance from chatGPT (I officially see it as a pseudo programming
language), I have some more software for the Python community now released to my github. As
wordpress now makes it very difficult to paste formatted code, I’ll be linking more often to github files.
In today’s post, the code in question is a python file containing quite a few functions from R’s
PerformanceAnalytics library outside of Return.portfolio (or Return_portfolio
(https://github.com/IlyaKipnis/PythonBacktesting/blob/main/Return_portfolio.py), as it may be in
Python, because the . operator is reserved in Python…ugh).
So, here is the “library” of new functions, for now called edhec_perfa.py, as a nod to Vijay Vaidyanathan’s
Coursera course(s). (https://www.coursera.org/learn/introduction-portfolio-construction-python)
Now, while a lot of the functions should be pretty straightforward (annualized return, VaR, CVaR, tracking
error/annualized tracking error, etc.), two functions that I think are particularly interesting that I ported
over from R with the help of chatGPT (seriously, when prompted correctly, an LLM is quite helpful in
translating from one programming language to another, provided the programmer has enough
background in both languages to do various debugging) are charts_PerformanceSummary, which does
exactly what you might think it does from seeing me use the R function here on the blog, and
table_Drawdowns, which gives the highest N drawdowns by depth.
Here’s a quick little Python demo (once you import my file from the github):
https://quantstrattrader.com/2023/06/21/several-key-performanceanalytics-functions-from-r-now-in-python-special-thanks-to-vijay-vaidyanathan/ 1/4
6/29/23, 2:57 PM Several Key PerformanceAnalytics Functions From R Now In Python (special thanks to Vijay Vaidyanathan) | QuantStrat TradeR
import yfinance as yf
import pandas as pd
import numpy as np
symbol = "SPY"
start_date = "1990-01-01"
end_date = pd.Timestamp.today().strftime("%Y-%m-%d")
df_list = []
data.name = symbol
data = pd.DataFrame(data)
returns = Return_calculate(data)
charts_PerformanceSummary(returns)
table_Drawdowns(returns)
https://quantstrattrader.com/2023/06/21/several-key-performanceanalytics-functions-from-r-now-in-python-special-thanks-to-vijay-vaidyanathan/ 2/4
6/29/23, 2:57 PM Several Key PerformanceAnalytics Functions From R Now In Python (special thanks to Vijay Vaidyanathan) | QuantStrat TradeR
Share
https://quantstrattrader.com/2023/06/21/several-key-performanceanalytics-functions-from-r-now-in-python-special-thanks-to-vijay-vaidyanathan/ 3/4
6/29/23, 2:57 PM Several Key PerformanceAnalytics Functions From R Now In Python (special thanks to Vijay Vaidyanathan) | QuantStrat TradeR
Share
The NaT in the “To” column means that the drawdown is current, as does the NaN in the Recovery–
they’re basically Python’s equivalent of NAs, though NaT means an NA on *time*, while an NaN means
an NA on a numerical value. So that’s interesting in that Python gives you a little more information
regarding your column data types, which is kind of interesting.
Another function included in this library, as a utility function is the infer_trading_periods function. Simply,
plug in a time series, and it will tell you the periodicity of the data. Pandas was supposed to have a function
that did this, but due to differences in frequency of daily data caused by weekends and holidays, this
actually doesn’t work on financial data, so I worked with chatGPT to write a new function that works more
generally. R’s checkData function is also in this library (translated over to Python), but I’m not quite sure
where it was intended to be used, but left it in for those that want it.
So, for those interested, just feel free to read through some of the functions, and use whichever ones you
find applicable to your workflow. That said, if you *do* wind up using it, please let me know, since there’s
more value I can add.
Lastly, I’m currently in the job market *and* have a volatility trading signal subscription
(https://patreon.com/quantstrattrader) available to offer for those interested. You can email me at
ilya.kipnis@gmail.com or find me on my LinkedIn profile here. (https://www.linkedin.com/in/ilyakipnis/)
Blog at WordPress.com.
https://quantstrattrader.com/2023/06/21/several-key-performanceanalytics-functions-from-r-now-in-python-special-thanks-to-vijay-vaidyanathan/ 4/4